path.cpp 5.6 KB

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