path.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. #include "path.h"
  24. namespace crown
  25. {
  26. namespace path
  27. {
  28. //-----------------------------------------------------------------------------
  29. bool is_valid_segment(const char* segment)
  30. {
  31. CE_ASSERT(segment != NULL, "Segment must be != NULL");
  32. size_t segment_len = string::strlen(segment);
  33. // Empty segment is not valid
  34. if (segment_len == 0)
  35. {
  36. return false;
  37. }
  38. // Segments containing only '.' are non valid
  39. if (segment_len == 1 && segment[0] == '.')
  40. {
  41. return false;
  42. }
  43. // Segments containing only ".." are not valid
  44. if (segment_len == 2 && segment[0] == '.' && segment[1] == '.')
  45. {
  46. return false;
  47. }
  48. // The segment does not have to contain any forward slashes ('/')
  49. // nor back slashes ('\'), nor colon signs (':')
  50. for (size_t i = 0; i < segment_len; i++)
  51. {
  52. if (segment[i] == '/' ||
  53. segment[i] == '\\' ||
  54. segment[i] == ':')
  55. {
  56. return false;
  57. }
  58. }
  59. return true;
  60. }
  61. /// Returns whether the path is valid.
  62. /// @note
  63. /// The rules for valid paths are as follows:
  64. /// a) The empty string is not valid.
  65. /// b) If the path is absolute, it mustn't contain any leading character.
  66. bool is_valid_path(const char* path)
  67. {
  68. (void)path;
  69. // size_t path_len = string::strlen(path);
  70. // if (pathLen == 0)
  71. // {
  72. // return false;
  73. // }
  74. // if (is_root_path(path))
  75. // {
  76. // return true;
  77. // }
  78. // Array<Str> segmentList;
  79. // if (!get_segments(Str(path), segmentList))
  80. // {
  81. // return false;
  82. // }
  83. // size_t i = 0;
  84. // if (IsAbsolutePath(path) && path[0] != '/')
  85. // {
  86. // i = 1;
  87. // }
  88. // for (; i < segmentList.GetSize(); i++)
  89. // {
  90. // if (!IsValidSegment(segmentList[i].c_str()))
  91. // {
  92. // return false;
  93. // }
  94. // }
  95. return true;
  96. }
  97. /// Returns the pathname of the path.
  98. /// @note
  99. /// e.g. "/home/project/texture.tga" -> "/home/project"
  100. /// e.g. "/home/project" -> "/home"
  101. /// e.g. "/home" -> "/"
  102. /// e.g. "home" -> ""
  103. ///
  104. /// The @a path must be valid.
  105. void pathname(const char* path, char* str, size_t len)
  106. {
  107. CE_ASSERT(path != NULL, "Path must be != NULL");
  108. CE_ASSERT(str != NULL, "Str must be != NULL");
  109. const char* last_separator = string::find_last(path, '/');
  110. if (last_separator == string::end(path))
  111. {
  112. string::strncpy(str, "", len);
  113. }
  114. else
  115. {
  116. string::substring(string::begin(path), last_separator, str, len);
  117. }
  118. }
  119. /// Returns the filename of the path.
  120. /// @note
  121. /// e.g. "/home/project/texture.tga" -> "texture.tga"
  122. /// e.g. "/home/project/texture" -> "texture"
  123. /// e.g. "/home -> "home"
  124. /// e.g. "/" -> ""
  125. ///
  126. /// The @a path must be valid.
  127. void filename(const char* path, char* str, size_t len)
  128. {
  129. CE_ASSERT(path != NULL, "Path must be != NULL");
  130. CE_ASSERT(str != NULL, "Str must be != NULL");
  131. const char* last_separator = string::find_last(path, '/');
  132. if (last_separator == string::end(path))
  133. {
  134. string::strncpy(str, "", len);
  135. }
  136. else
  137. {
  138. string::substring(last_separator + 1, string::end(path), str, len);
  139. }
  140. }
  141. /// Returns the basename of the path.
  142. /// @note
  143. /// e.g. "/home/project/texture.tga" -> "texture"
  144. /// e.g. "/home/project" -> "project"
  145. /// e.g. "/" -> ""
  146. ///
  147. /// The @a path must be valid.
  148. void basename(const char* path, char* str, size_t len)
  149. {
  150. CE_ASSERT(path != NULL, "Path must be != NULL");
  151. CE_ASSERT(str != NULL, "Str must be != NULL");
  152. const char* last_separator = string::find_last(path, '/');
  153. const char* last_dot = string::find_last(path, '.');
  154. if (last_separator == string::end(path) && last_dot != string::end(path))
  155. {
  156. string::substring(string::begin(path), last_dot, str, len);
  157. }
  158. else if (last_separator != string::end(path) && last_dot == string::end(path))
  159. {
  160. string::substring(last_separator + 1, string::end(path), str, len);
  161. }
  162. else if (last_separator == string::end(path) && last_dot == string::end(path))
  163. {
  164. string::strncpy(str, path, len);
  165. }
  166. else
  167. {
  168. string::substring(last_separator + 1, last_dot, str, len);
  169. }
  170. }
  171. /// Returns the extension of the path.
  172. /// @note
  173. /// e.g. "/home/project/texture.tga" -> "tga"
  174. /// e.g. "/home/project.x/texture" -> ""
  175. ///
  176. /// The @a path must be valid.
  177. void extension(const char* path, char* str, size_t len)
  178. {
  179. CE_ASSERT(path != NULL, "Path must be != NULL");
  180. CE_ASSERT(str != NULL, "Str must be != NULL");
  181. const char* last_dot = string::find_last(path, '.');
  182. if (last_dot == string::end(path))
  183. {
  184. string::strncpy(str, "", len);
  185. }
  186. else
  187. {
  188. string::substring(last_dot + 1, string::end(path), str, len);
  189. }
  190. }
  191. /// Returns the filename without the extension.
  192. /// @note
  193. /// e.g. "/home/project/texture.tga" -> "/home/project/texture"
  194. /// e.g. "/home/project/texture" -> "/home/project/texture"
  195. ///
  196. /// The @a path must be valid.
  197. void filename_without_extension(const char* path, char* str, size_t len)
  198. {
  199. CE_ASSERT(path != NULL, "Path must be != NULL");
  200. CE_ASSERT(str != NULL, "Str must be != NULL");
  201. const char* last_dot = string::find_last(path, '.');
  202. string::substring(string::begin(path), last_dot, str, len);
  203. }
  204. /// Returns the segments contained in path.
  205. //bool segments(const char* path, Array<Str>& ret)
  206. //{
  207. // path.Split(os::PATH_SEPARATOR, ret);
  208. // if (ret.GetSize() > 0)
  209. // {
  210. // return true;
  211. // }
  212. // return false;
  213. //}
  214. /// Fills 'ret' with the same path but without the trailing directory separator.
  215. /// @note
  216. /// e.g. "/home/project/texture.tga/" -> "/home/project/texture.tga"
  217. /// e.g. "/home/project/texture.tga" -> "/home/project/texture.tga"
  218. ///
  219. /// The @a path must be valid.
  220. void strip_trailing_separator(const char* path, char* str, size_t len)
  221. {
  222. CE_ASSERT(path != NULL, "Path must be != NULL");
  223. CE_ASSERT(str != NULL, "Str must be != NULL");
  224. size_t path_len = string::strlen(path);
  225. if (path[path_len - 1] == '/')
  226. {
  227. string::substring(string::begin(path), string::end(path) - 2, str, len);
  228. }
  229. else
  230. {
  231. string::substring(string::begin(path), string::end(path), str, len);
  232. }
  233. }
  234. } // namespace path
  235. } // namespace crown