Path.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "StringUtils.h"
  25. namespace crown
  26. {
  27. namespace path
  28. {
  29. bool is_valid_segment(const char* segment);
  30. bool is_valid_path(const char* path);
  31. bool is_absolute_path(const char* path);
  32. void pathname(const char* path, char* str, size_t len);
  33. void filename(const char* path, char* str, size_t len);
  34. void basename(const char* path, char* str, size_t len);
  35. void extension(const char* path, char* str, size_t len);
  36. void filename_without_extension(const char* path, char* str, size_t len);
  37. //bool segments(const char* path, List<Str>& ret);
  38. inline void strip_trailing_separator(const char* path, char* ret, size_t len);
  39. /// Returns whether the segment is valid.
  40. /// @note
  41. /// The rules for valid segments are as follows:
  42. /// a) The empty string is not valid.
  43. /// b) Any string containing the slash character ('/') is not valid.
  44. /// c) Common notations for current ('.') and parent ('..') directory are forbidden.
  45. /// d) Any string containing segment or device separator characters on the local file system,
  46. /// such as the backslash ('\') and colon (':') on some file systems.
  47. /// (Thanks org.eclipse.core.runtime for the documentation ;D).
  48. inline bool is_valid_segment(const char* segment)
  49. {
  50. CE_ASSERT(segment != NULL, "Segment must be != NULL");
  51. size_t segment_len = string::strlen(segment);
  52. // Empty segment is not valid
  53. if (segment_len == 0)
  54. {
  55. return false;
  56. }
  57. // Segments containing only '.' are non valid
  58. if (segment_len == 1 && segment[0] == '.')
  59. {
  60. return false;
  61. }
  62. // Segments containing only ".." are not valid
  63. if (segment_len == 2 && segment[0] == '.' && segment[1] == '.')
  64. {
  65. return false;
  66. }
  67. // The segment does not have to contain any forward slashes ('/')
  68. // nor back slashes ('\'), nor colon signs (':')
  69. for (size_t i = 0; i < segment_len; i++)
  70. {
  71. if (segment[i] == '/' ||
  72. segment[i] == '\\' ||
  73. segment[i] == ':')
  74. {
  75. return false;
  76. }
  77. }
  78. return true;
  79. }
  80. /// Returns whether the path is valid.
  81. /// @note
  82. /// The rules for valid paths are as follows:
  83. /// a) The empty string is not valid.
  84. /// b) If the path is absolute, it mustn't contain any leading character.
  85. inline bool is_valid_path(const char* path)
  86. {
  87. (void)path;
  88. // size_t path_len = string::strlen(path);
  89. // if (pathLen == 0)
  90. // {
  91. // return false;
  92. // }
  93. // if (is_root_path(path))
  94. // {
  95. // return true;
  96. // }
  97. // List<Str> segmentList;
  98. // if (!get_segments(Str(path), segmentList))
  99. // {
  100. // return false;
  101. // }
  102. // size_t i = 0;
  103. // if (IsAbsolutePath(path) && path[0] != '/')
  104. // {
  105. // i = 1;
  106. // }
  107. // for (; i < segmentList.GetSize(); i++)
  108. // {
  109. // if (!IsValidSegment(segmentList[i].c_str()))
  110. // {
  111. // return false;
  112. // }
  113. // }
  114. return true;
  115. }
  116. /// Returns the pathname of the path.
  117. /// @note
  118. /// e.g. "/home/project/texture.tga" -> "/home/project"
  119. /// e.g. "/home/project" -> "/home"
  120. /// e.g. "/home" -> "/"
  121. /// e.g. "home" -> ""
  122. ///
  123. /// The @a path must be valid.
  124. inline void pathname(const char* path, char* str, size_t len)
  125. {
  126. CE_ASSERT(path != NULL, "Path must be != NULL");
  127. CE_ASSERT(str != NULL, "Str must be != NULL");
  128. const char* last_separator = string::find_last(path, '/');
  129. if (last_separator == string::end(path))
  130. {
  131. string::strncpy(str, "", len);
  132. }
  133. else
  134. {
  135. string::substring(string::begin(path), last_separator, str, len);
  136. }
  137. }
  138. /// Returns the filename of the path.
  139. /// @note
  140. /// e.g. "/home/project/texture.tga" -> "texture.tga"
  141. /// e.g. "/home/project/texture" -> "texture"
  142. /// e.g. "/home -> "home"
  143. /// e.g. "/" -> ""
  144. ///
  145. /// The @a path must be valid.
  146. inline void filename(const char* path, char* str, size_t len)
  147. {
  148. CE_ASSERT(path != NULL, "Path must be != NULL");
  149. CE_ASSERT(str != NULL, "Str must be != NULL");
  150. const char* last_separator = string::find_last(path, '/');
  151. if (last_separator == string::end(path))
  152. {
  153. string::strncpy(str, "", len);
  154. }
  155. else
  156. {
  157. string::substring(last_separator + 1, string::end(path), str, len);
  158. }
  159. }
  160. /// Returns the basename of the path.
  161. /// @note
  162. /// e.g. "/home/project/texture.tga" -> "texture"
  163. /// e.g. "/home/project" -> "project"
  164. /// e.g. "/" -> ""
  165. ///
  166. /// The @a path must be valid.
  167. inline void basename(const char* path, char* str, size_t len)
  168. {
  169. CE_ASSERT(path != NULL, "Path must be != NULL");
  170. CE_ASSERT(str != NULL, "Str must be != NULL");
  171. const char* last_separator = string::find_last(path, '/');
  172. const char* last_dot = string::find_last(path, '.');
  173. if (last_separator == string::end(path) && last_dot != string::end(path))
  174. {
  175. string::substring(string::begin(path), last_dot, str, len);
  176. }
  177. else if (last_separator != string::end(path) && last_dot == string::end(path))
  178. {
  179. string::substring(last_separator + 1, string::end(path), str, len);
  180. }
  181. else if (last_separator == string::end(path) && last_dot == string::end(path))
  182. {
  183. string::strncpy(str, path, len);
  184. }
  185. else
  186. {
  187. string::substring(last_separator + 1, last_dot, str, len);
  188. }
  189. }
  190. /// Returns the extension of the path.
  191. /// @note
  192. /// e.g. "/home/project/texture.tga" -> "tga"
  193. /// e.g. "/home/project.x/texture" -> ""
  194. ///
  195. /// The @a path must be valid.
  196. inline void extension(const char* path, char* str, size_t len)
  197. {
  198. CE_ASSERT(path != NULL, "Path must be != NULL");
  199. CE_ASSERT(str != NULL, "Str must be != NULL");
  200. const char* last_dot = string::find_last(path, '.');
  201. if (last_dot == string::end(path))
  202. {
  203. string::strncpy(str, "", len);
  204. }
  205. else
  206. {
  207. string::substring(last_dot + 1, string::end(path), str, len);
  208. }
  209. }
  210. /// Returns the filename without the extension.
  211. /// @note
  212. /// e.g. "/home/project/texture.tga" -> "/home/project/texture"
  213. /// e.g. "/home/project/texture" -> "/home/project/texture"
  214. ///
  215. /// The @a path must be valid.
  216. inline void filename_without_extension(const char* path, char* str, size_t len)
  217. {
  218. CE_ASSERT(path != NULL, "Path must be != NULL");
  219. CE_ASSERT(str != NULL, "Str must be != NULL");
  220. const char* last_dot = string::find_last(path, '.');
  221. string::substring(string::begin(path), last_dot, str, len);
  222. }
  223. /// Returns the segments contained in path.
  224. //bool segments(const char* path, List<Str>& ret)
  225. //{
  226. // path.Split(os::PATH_SEPARATOR, ret);
  227. // if (ret.GetSize() > 0)
  228. // {
  229. // return true;
  230. // }
  231. // return false;
  232. //}
  233. /// Fills 'ret' with the same path but without the trailing directory separator.
  234. /// @note
  235. /// e.g. "/home/project/texture.tga/" -> "/home/project/texture.tga"
  236. /// e.g. "/home/project/texture.tga" -> "/home/project/texture.tga"
  237. ///
  238. /// The @a path must be valid.
  239. inline void strip_trailing_separator(const char* path, char* str, size_t len)
  240. {
  241. CE_ASSERT(path != NULL, "Path must be != NULL");
  242. CE_ASSERT(str != NULL, "Str must be != NULL");
  243. size_t path_len = string::strlen(path);
  244. if (path[path_len - 1] == '/')
  245. {
  246. string::substring(string::begin(path), string::end(path) - 2, str, len);
  247. }
  248. else
  249. {
  250. string::substring(string::begin(path), string::end(path), str, len);
  251. }
  252. }
  253. } // namespace path
  254. } // namespace crown