foobar2000 SDK  2015-01-14
Typedefs | Functions | Variables
pfc::io::path Namespace Reference

Typedefs

typedef string::comparatorCaseInsensitive comparator
 

Functions

string combine (string basePath, string fileName)
 
template<typename t1 , typename t2 >
int compare (t1 const &p1, t2 const &p2)
 
template<typename t1 , typename t2 >
bool equals (const t1 &v1, const t2 &v2)
 
static const string g_illegalNameChars (g_pathSeparators#ifdef _WINDOWS+":<>*?\""#else+"*?"#endif)
 
static const string g_illegalNameChars_noWC (g_pathSeparators#ifdef _WINDOWS+":<>?\""#endif)
 
static const string g_pathSeparators ("/")
 
char getDefaultSeparator ()
 
string getDirectory (string filePath)
 
string getFileExtension (string path)
 
string getFileName (string path)
 
string getFileNameWithoutExtension (string path)
 
string getIllegalNameChars (bool allowWC)
 
string getParent (string filePath)
 
string getSeparators ()
 
bool isDirectoryRoot (string path)
 
static bool isIllegalTrailingChar (char c)
 
bool isInsideDirectory (pfc::string directory, pfc::string inside)
 
bool isSeparator (char c)
 
static string replaceIllegalChar (char c)
 
string replaceIllegalNameChars (string fn, bool allowWC)
 
string replaceIllegalPathChars (string fn)
 
string validateFileName (string name, bool allowWC)
 

Variables

static const string g_pathSeparators ("\\/|")
 

Typedef Documentation

Definition at line 5 of file pathUtils.h.

Function Documentation

string pfc::io::path::combine ( string  basePath,
string  fileName 
)

Definition at line 41 of file pathUtils.cpp.

41  {
42  if (basePath.length() > 0) {
43  if (!isSeparator(basePath.lastChar())) {
44  basePath += getDefaultSeparator();
45  }
46  return basePath + fileName;
47  } else {
48  //todo?
49  return fileName;
50  }
51 }
bool isSeparator(char c)
Definition: pathUtils.cpp:53
char getDefaultSeparator()
Definition: pathUtils.cpp:124
template<typename t1 , typename t2 >
int pfc::io::path::compare ( t1 const &  p1,
t2 const &  p2 
)
inline

Definition at line 29 of file pathUtils.h.

29 {return comparator::compare(p1, p2); }
int compare(t1 const &p1, t2 const &p2)
Definition: pathUtils.h:29
template<typename t1 , typename t2 >
bool pfc::io::path::equals ( const t1 &  v1,
const t2 &  v2 
)
inline

Definition at line 27 of file pathUtils.h.

27 {return comparator::compare(v1,v2) == 0;}
int compare(t1 const &p1, t2 const &p2)
Definition: pathUtils.h:29
static const string pfc::io::path::g_illegalNameChars ( g_pathSeparators#ifdef _WINDOWS+":<>*?\""#else+"*?"#  endif)
static
static const string pfc::io::path::g_illegalNameChars_noWC ( g_pathSeparators#ifdef _WINDOWS+":<>?\""#  endif)
static
static const string pfc::io::path::g_pathSeparators ( "/"  )
static
char pfc::io::path::getDefaultSeparator ( )

Definition at line 124 of file pathUtils.cpp.

124  {
125 #ifdef _WINDOWS
126  return '\\';
127 #else
128  return '/';
129 #endif
130 }
string pfc::io::path::getDirectory ( string  filePath)

Definition at line 28 of file pathUtils.cpp.

28 {return getParent(filePath);}
string getParent(string filePath)
Definition: pathUtils.cpp:30
string pfc::io::path::getFileExtension ( string  path)

Definition at line 22 of file pathUtils.cpp.

22  {
23  string fn = getFileName(path);
24  t_size split = fn.lastIndexOf('.');
25  if (split == ~0) return "";
26  else return fn.subString(split);
27 }
string getFileName(string path)
Definition: pathUtils.cpp:11
size_t t_size
Definition: int_types.h:48
string pfc::io::path::getFileName ( string  path)

Definition at line 11 of file pathUtils.cpp.

11  {
12  t_size split = path.lastIndexOfAnyChar(g_pathSeparators);
13  if (split == ~0) return path;
14  else return path.subString(split+1);
15 }
size_t t_size
Definition: int_types.h:48
static const string g_pathSeparators("/")
string pfc::io::path::getFileNameWithoutExtension ( string  path)

Definition at line 16 of file pathUtils.cpp.

16  {
17  string fn = getFileName(path);
18  t_size split = fn.lastIndexOf('.');
19  if (split == ~0) return fn;
20  else return fn.subString(0,split);
21 }
string getFileName(string path)
Definition: pathUtils.cpp:11
size_t t_size
Definition: int_types.h:48
string pfc::io::path::getIllegalNameChars ( bool  allowWC)

Definition at line 145 of file pathUtils.cpp.

145  {
146  return allowWC ? g_illegalNameChars_noWC : g_illegalNameChars;
147 }
static const string g_illegalNameChars(g_pathSeparators#ifdef _WINDOWS+":<>*?\""#else+"*?"#endif)
static const string g_illegalNameChars_noWC(g_pathSeparators#ifdef _WINDOWS+":<>?\""#endif)
string pfc::io::path::getParent ( string  filePath)

Definition at line 30 of file pathUtils.cpp.

30  {
31  t_size split = filePath.lastIndexOfAnyChar(g_pathSeparators);
32  if (split == ~0) return "";
33 #ifdef _WINDOWS
34  if (split > 0 && getIllegalNameChars().contains(filePath[split-1])) {
35  if (split + 1 < filePath.length()) return filePath.subString(0,split+1);
36  else return "";
37  }
38 #endif
39  return filePath.subString(0,split);
40 }
size_t t_size
Definition: int_types.h:48
string getIllegalNameChars(bool allowWC)
Definition: pathUtils.cpp:145
static const string g_pathSeparators("/")
string pfc::io::path::getSeparators ( )

Definition at line 56 of file pathUtils.cpp.

56  {
57  return g_pathSeparators;
58 }
static const string g_pathSeparators("/")
bool pfc::io::path::isDirectoryRoot ( string  path)

Definition at line 118 of file pathUtils.cpp.

118  {
119  return getParent(path).isEmpty();
120 }
string getParent(string filePath)
Definition: pathUtils.cpp:30
bool isEmpty() const
Definition: stringNew.h:139
static bool pfc::io::path::isIllegalTrailingChar ( char  c)
static

Definition at line 150 of file pathUtils.cpp.

150  {
151  return c == ' ' || c == '.';
152 }
bool pfc::io::path::isInsideDirectory ( pfc::string  directory,
pfc::string  inside 
)

Definition at line 109 of file pathUtils.cpp.

109  {
110  //not very efficient
111  string walk = inside;
112  for(;;) {
113  walk = getParent(walk);
114  if (walk == "") return false;
115  if (equals(directory,walk)) return true;
116  }
117 }
string getParent(string filePath)
Definition: pathUtils.cpp:30
bool equals(const t1 &v1, const t2 &v2)
Definition: pathUtils.h:27
bool pfc::io::path::isSeparator ( char  c)

Definition at line 53 of file pathUtils.cpp.

53  {
54  return g_pathSeparators.indexOf(c) != ~0;
55 }
static const string g_pathSeparators("/")
t_size indexOf(char c, t_size base=0) const
Definition: stringNew.cpp:5
static string pfc::io::path::replaceIllegalChar ( char  c)
static

Definition at line 60 of file pathUtils.cpp.

60  {
61  switch(c) {
62  case '*':
63  return "x";
64  case '\"':
65  return "\'\'";
66  case ':':
67  case '/':
68  case '\\':
69  return "-";
70  default:
71  return "_";
72  }
73 }
string pfc::io::path::replaceIllegalNameChars ( string  fn,
bool  allowWC 
)

Definition at line 93 of file pathUtils.cpp.

93  {
94  const string illegal = getIllegalNameChars(allowWC);
96  for(t_size walk = 0; walk < fn.length(); ++walk) {
97  const char c = fn[walk];
98  if (string::isNonTextChar(c) || illegal.contains(c)) {
99  string replacement = replaceIllegalChar(c);
100  if (replacement.containsAnyChar(illegal)) /*per-OS weirdness security*/ replacement = "_";
101  output << replacement.ptr();
102  } else {
103  output.add_byte(c);
104  }
105  }
106  return output.toString();
107 }
static string replaceIllegalChar(char c)
Definition: pathUtils.cpp:60
size_t t_size
Definition: int_types.h:48
string8_fastalloc string_formatter
Definition: string_base.h:614
string getIllegalNameChars(bool allowWC)
Definition: pathUtils.cpp:145
Definition: output.h:95
string pfc::io::path::replaceIllegalPathChars ( string  fn)

Definition at line 74 of file pathUtils.cpp.

74  {
75  string illegal = getIllegalNameChars();
76  string separators = getSeparators();
78  for(t_size walk = 0; walk < fn.length(); ++walk) {
79  const char c = fn[walk];
80  if (separators.contains(c)) {
81  output.add_byte(getDefaultSeparator());
82  } else if (string::isNonTextChar(c) || illegal.contains(c)) {
83  string replacement = replaceIllegalChar(c);
84  if (replacement.containsAnyChar(illegal)) /*per-OS weirdness security*/ replacement = "_";
85  output << replacement.ptr();
86  } else {
87  output.add_byte(c);
88  }
89  }
90  return output.toString();
91 }
string getSeparators()
Definition: pathUtils.cpp:56
static string replaceIllegalChar(char c)
Definition: pathUtils.cpp:60
size_t t_size
Definition: int_types.h:48
string8_fastalloc string_formatter
Definition: string_base.h:614
string getIllegalNameChars(bool allowWC)
Definition: pathUtils.cpp:145
char getDefaultSeparator()
Definition: pathUtils.cpp:124
Definition: output.h:95
string pfc::io::path::validateFileName ( string  name,
bool  allowWC 
)

Definition at line 155 of file pathUtils.cpp.

155  {
156  for(t_size walk = 0; name[walk];) {
157  if (name[walk] == '?') {
158  t_size end = walk;
159  do { ++end; } while(name[end] == '?');
160  name = name.subString(0, walk) + name.subString(end);
161  } else {
162  ++walk;
163  }
164  }
165 #ifdef _WINDOWS
166  name = replaceIllegalNameChars(name, allowWC);
167  if (name.length() > 0) {
168  t_size end = name.length();
169  while(end > 0) {
170  if (!isIllegalTrailingChar(name[end-1])) break;
171  --end;
172  }
173  t_size begin = 0;
174  while(begin < end) {
175  if (!isIllegalTrailingChar(name[begin])) break;
176  ++begin;
177  }
178  if (end < name.length() || begin > 0) name = name.subString(begin,end - begin);
179  }
180  if (name.isEmpty()) name = "_";
181  return name;
182 #else
183  return replaceIllegalNameChars(name);
184 #endif
185 }
static bool isIllegalTrailingChar(char c)
Definition: pathUtils.cpp:150
size_t t_size
Definition: int_types.h:48
string replaceIllegalNameChars(string fn, bool allowWC)
Definition: pathUtils.cpp:93

Variable Documentation

const string pfc::io::path::g_pathSeparators("/") ( "\\/|"  )
static