aboutsummaryrefslogtreecommitdiff
path: root/libs/base/file_name_utils.cpp
blob: 650677d7bf64deaab5e652b9f39aecfc73bbfb21 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "base/file_name_utils.hpp"

namespace base
{
void GetNameWithoutExt(std::string & name)
{
  std::string::size_type const i = name.rfind('.');
  if (i != std::string::npos)
    name.erase(i);
}

std::string FilenameWithoutExt(std::string name)
{
  GetNameWithoutExt(name);
  return name;
}

std::string GetFileExtension(std::string const & name)
{
  size_t const pos = name.find_last_of("./\\");
  return ((pos != std::string::npos && name[pos] == '.') ? name.substr(pos) : std::string());
}

void GetNameFromFullPath(std::string & name)
{
  std::string::size_type const i = name.find_last_of("/\\");
  if (i != std::string::npos)
    name = name.substr(i + 1);
}

std::string FileNameFromFullPath(std::string path)
{
  GetNameFromFullPath(path);
  return path;
}

std::string GetNameFromFullPathWithoutExt(std::string path)
{
  GetNameFromFullPath(path);
  GetNameWithoutExt(path);
  return path;
}

std::string GetDirectory(std::string const & name)
{
  auto const sep = GetNativeSeparator();
  size_t const sepSize = sizeof(sep);

  std::string::size_type i = name.rfind(sep);
  if (i == std::string::npos)
    return ".";
  while (i > sepSize && (name[i - sepSize] == sep))
    i -= sepSize;
  return name.substr(0, i ? i : sepSize);
}

std::string::value_type GetNativeSeparator()
{
#ifdef OMIM_OS_WINDOWS
  return '\\';
#else
  return '/';
#endif
}

std::string AddSlashIfNeeded(std::string const & path)
{
  auto const sep = GetNativeSeparator();
  std::string::size_type const pos = path.rfind(sep);
  if (pos != std::string::npos && pos + sizeof(sep) == path.size())
    return path;
  return path + sep;
}
}  // namespace base