path.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. // Copyright (c) 2014-2021 Thomas Fussell
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE
  20. //
  21. // @license: http://www.opensource.org/licenses/mit-license.php
  22. // @author: see AUTHORS file
  23. #include <algorithm>
  24. #include <fstream>
  25. #include <iterator>
  26. #include <sstream>
  27. #include <sys/stat.h>
  28. #ifdef __APPLE__
  29. #include <mach-o/dyld.h>
  30. #elif defined(__linux)
  31. #include <linux/limits.h>
  32. #include <sys/types.h>
  33. #include <unistd.h>
  34. #elif defined(_MSC_VER)
  35. #include <codecvt>
  36. #endif
  37. #include <xlnt/utils/path.hpp>
  38. #include <detail/external/include_windows.hpp>
  39. namespace {
  40. #ifdef WIN32
  41. char system_separator()
  42. {
  43. return '\\';
  44. }
  45. bool is_drive_letter(char letter)
  46. {
  47. return letter >= 'A' && letter <= 'Z';
  48. }
  49. bool is_root(const std::string &part)
  50. {
  51. if (part.size() == 1 && part[0] == '/') return true;
  52. if (part.size() != 3) return false;
  53. return is_drive_letter(part[0]) && part[1] == ':' && (part[2] == '\\' || part[2] == '/');
  54. }
  55. bool is_absolute(const std::string &part)
  56. {
  57. if (!part.empty() && part[0] == '/') return true;
  58. if (part.size() < 3) return false;
  59. return is_root(part.substr(0, 3));
  60. }
  61. #else
  62. char system_separator()
  63. {
  64. return '/';
  65. }
  66. bool is_root(const std::string &part)
  67. {
  68. return part == "/";
  69. }
  70. bool is_absolute(const std::string &part)
  71. {
  72. return !part.empty() && part[0] == '/';
  73. }
  74. #endif
  75. std::vector<std::string> split_path(const std::string &path, char delim)
  76. {
  77. std::vector<std::string> split;
  78. std::string::size_type previous_index = 0;
  79. auto separator_index = path.find(delim);
  80. while (separator_index != std::string::npos)
  81. {
  82. auto part = path.substr(previous_index, separator_index - previous_index);
  83. split.push_back(part);
  84. previous_index = separator_index + 1;
  85. separator_index = path.find(delim, previous_index);
  86. }
  87. // Don't add trailing slash
  88. if (previous_index < path.size())
  89. {
  90. split.push_back(path.substr(previous_index));
  91. }
  92. return split;
  93. }
  94. bool file_exists(const std::string &path)
  95. {
  96. struct stat info;
  97. return stat(path.c_str(), &info) == 0 && (info.st_mode & S_IFREG);
  98. }
  99. bool directory_exists(const std::string path)
  100. {
  101. struct stat info;
  102. return stat(path.c_str(), &info) == 0 && (info.st_mode & S_IFDIR);
  103. }
  104. } // namespace
  105. namespace xlnt {
  106. char path::system_separator()
  107. {
  108. return ::system_separator();
  109. }
  110. path::path()
  111. {
  112. }
  113. path::path(const std::string &path_string)
  114. {
  115. std::remove_copy(path_string.begin(), path_string.end(), std::back_inserter(internal_), '\"');
  116. }
  117. path::path(const std::string &path_string, char sep)
  118. : internal_(path_string)
  119. {
  120. char curr_sep = guess_separator();
  121. if (curr_sep != sep)
  122. {
  123. for (char &c : internal_) // simple find and replace
  124. {
  125. if (c == curr_sep)
  126. {
  127. c = sep;
  128. }
  129. }
  130. }
  131. }
  132. // general attributes
  133. bool path::is_relative() const
  134. {
  135. return !is_absolute();
  136. }
  137. bool path::is_absolute() const
  138. {
  139. return ::is_absolute(internal_);
  140. }
  141. bool path::is_root() const
  142. {
  143. return ::is_root(internal_);
  144. }
  145. path path::parent() const
  146. {
  147. if (is_root()) return *this;
  148. auto split_path = split();
  149. split_path.pop_back();
  150. if (split_path.empty())
  151. {
  152. return path("");
  153. }
  154. path result;
  155. for (const auto &component : split_path)
  156. {
  157. result = result.append(component);
  158. }
  159. return result;
  160. }
  161. std::string path::filename() const
  162. {
  163. auto split_path = split();
  164. return split_path.empty() ? "" : split_path.back();
  165. }
  166. std::string path::extension() const
  167. {
  168. auto base = filename();
  169. auto last_dot = base.find_last_of('.');
  170. return last_dot == std::string::npos ? "" : base.substr(last_dot + 1);
  171. }
  172. std::pair<std::string, std::string> path::split_extension() const
  173. {
  174. auto base = filename();
  175. auto last_dot = base.find_last_of('.');
  176. return {base.substr(0, last_dot), base.substr(last_dot + 1)};
  177. }
  178. // conversion
  179. const std::string &path::string() const
  180. {
  181. return internal_;
  182. }
  183. #ifdef _MSC_VER
  184. std::wstring path::wstring() const
  185. {
  186. std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> convert;
  187. return convert.from_bytes(string());
  188. }
  189. #endif
  190. std::vector<std::string> path::split() const
  191. {
  192. return split_path(internal_, guess_separator());
  193. }
  194. path path::resolve(const path &base_path) const
  195. {
  196. if (is_absolute())
  197. {
  198. return *this;
  199. }
  200. path copy(base_path.internal_);
  201. for (const auto &part : split())
  202. {
  203. if (part == "..")
  204. {
  205. copy = copy.parent();
  206. continue;
  207. }
  208. copy = copy.append(part);
  209. }
  210. return copy;
  211. }
  212. // filesystem attributes
  213. bool path::exists() const
  214. {
  215. return is_file() || is_directory();
  216. }
  217. bool path::is_directory() const
  218. {
  219. return directory_exists(string());
  220. }
  221. bool path::is_file() const
  222. {
  223. return file_exists(string());
  224. }
  225. // filesystem
  226. std::string path::read_contents() const
  227. {
  228. std::ifstream f(string());
  229. std::ostringstream ss;
  230. ss << f.rdbuf();
  231. return ss.str();
  232. }
  233. // append
  234. path path::append(const std::string &to_append) const
  235. {
  236. path copy(internal_);
  237. if (!internal_.empty() && internal_.back() != guess_separator())
  238. {
  239. copy.internal_.push_back(guess_separator());
  240. }
  241. copy.internal_.append(to_append);
  242. return copy;
  243. }
  244. path path::append(const path &to_append) const
  245. {
  246. path copy(internal_);
  247. for (const auto &component : to_append.split())
  248. {
  249. copy = copy.append(component);
  250. }
  251. return copy;
  252. }
  253. char path::guess_separator() const
  254. {
  255. if (system_separator() == '/' || internal_.empty() || internal_.front() == '/') return '/';
  256. if (is_absolute()) return internal_.at(2);
  257. return internal_.find('\\') != std::string::npos ? '\\' : '/';
  258. }
  259. path path::relative_to(const path &base_path) const
  260. {
  261. if (is_relative()) return *this;
  262. auto base_split = base_path.split();
  263. auto this_split = split();
  264. auto index = std::size_t(0);
  265. while (index < base_split.size() && index < this_split.size() && base_split[index] == this_split[index])
  266. {
  267. index++;
  268. }
  269. auto result = path();
  270. for (auto i = index; i < this_split.size(); i++)
  271. {
  272. result = result.append(this_split[i]);
  273. }
  274. return result;
  275. }
  276. bool path::operator==(const path &other) const
  277. {
  278. return internal_ == other.internal_;
  279. }
  280. bool path::operator!=(const path &other) const
  281. {
  282. return !operator==(other);
  283. }
  284. } // namespace xlnt