You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

268 lines
7.6 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2005, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifdef WIN32
  28. #include "talk/base/win32.h"
  29. #include <shellapi.h>
  30. #include <shlobj.h>
  31. #include <tchar.h>
  32. #endif // WIN32
  33. #include "talk/base/common.h"
  34. #include "talk/base/fileutils.h"
  35. #include "talk/base/logging.h"
  36. #include "talk/base/pathutils.h"
  37. #include "talk/base/stringutils.h"
  38. #include "talk/base/urlencode.h"
  39. namespace talk_base {
  40. static const char EMPTY_STR[] = "";
  41. // EXT_DELIM separates a file basename from extension
  42. const char EXT_DELIM = '.';
  43. // FOLDER_DELIMS separate folder segments and the filename
  44. const char* const FOLDER_DELIMS = "/\\";
  45. // DEFAULT_FOLDER_DELIM is the preferred delimiter for this platform
  46. #if WIN32
  47. const char DEFAULT_FOLDER_DELIM = '\\';
  48. #else // !WIN32
  49. const char DEFAULT_FOLDER_DELIM = '/';
  50. #endif // !WIN32
  51. ///////////////////////////////////////////////////////////////////////////////
  52. // Pathname - parsing of pathnames into components, and vice versa
  53. ///////////////////////////////////////////////////////////////////////////////
  54. bool Pathname::IsFolderDelimiter(char ch) {
  55. return (NULL != ::strchr(FOLDER_DELIMS, ch));
  56. }
  57. char Pathname::DefaultFolderDelimiter() {
  58. return DEFAULT_FOLDER_DELIM;
  59. }
  60. Pathname::Pathname()
  61. : folder_delimiter_(DEFAULT_FOLDER_DELIM) {
  62. }
  63. Pathname::Pathname(const std::string& pathname)
  64. : folder_delimiter_(DEFAULT_FOLDER_DELIM) {
  65. SetPathname(pathname);
  66. }
  67. Pathname::Pathname(const std::string& folder, const std::string& filename)
  68. : folder_delimiter_(DEFAULT_FOLDER_DELIM) {
  69. SetPathname(folder, filename);
  70. }
  71. void Pathname::SetFolderDelimiter(char delimiter) {
  72. ASSERT(IsFolderDelimiter(delimiter));
  73. folder_delimiter_ = delimiter;
  74. }
  75. void Pathname::Normalize() {
  76. for (size_t i=0; i<folder_.length(); ++i) {
  77. if (IsFolderDelimiter(folder_[i])) {
  78. folder_[i] = folder_delimiter_;
  79. }
  80. }
  81. }
  82. void Pathname::clear() {
  83. folder_.clear();
  84. basename_.clear();
  85. extension_.clear();
  86. }
  87. bool Pathname::empty() const {
  88. return folder_.empty() && basename_.empty() && extension_.empty();
  89. }
  90. std::string Pathname::pathname() const {
  91. std::string pathname(folder_);
  92. pathname.append(basename_);
  93. pathname.append(extension_);
  94. if (pathname.empty()) {
  95. // Instead of the empty pathname, return the current working directory.
  96. pathname.push_back('.');
  97. pathname.push_back(folder_delimiter_);
  98. }
  99. return pathname;
  100. }
  101. std::string Pathname::url() const {
  102. std::string s = "file:///";
  103. for (size_t i=0; i<folder_.length(); ++i) {
  104. if (IsFolderDelimiter(folder_[i]))
  105. s += '/';
  106. else
  107. s += folder_[i];
  108. }
  109. s += basename_;
  110. s += extension_;
  111. return UrlEncodeStringForOnlyUnsafeChars(s);
  112. }
  113. void Pathname::SetPathname(const std::string& pathname) {
  114. std::string::size_type pos = pathname.find_last_of(FOLDER_DELIMS);
  115. if (pos != std::string::npos) {
  116. SetFolder(pathname.substr(0, pos + 1));
  117. SetFilename(pathname.substr(pos + 1));
  118. } else {
  119. SetFolder(EMPTY_STR);
  120. SetFilename(pathname);
  121. }
  122. }
  123. void Pathname::SetPathname(const std::string& folder,
  124. const std::string& filename) {
  125. SetFolder(folder);
  126. SetFilename(filename);
  127. }
  128. void Pathname::AppendPathname(const std::string& pathname) {
  129. std::string full_pathname(folder_);
  130. full_pathname.append(pathname);
  131. SetPathname(full_pathname);
  132. }
  133. std::string Pathname::folder() const {
  134. return folder_;
  135. }
  136. std::string Pathname::folder_name() const {
  137. std::string::size_type pos = std::string::npos;
  138. if (folder_.size() >= 2) {
  139. pos = folder_.find_last_of(FOLDER_DELIMS, folder_.length() - 2);
  140. }
  141. if (pos != std::string::npos) {
  142. return folder_.substr(pos + 1);
  143. } else {
  144. return folder_;
  145. }
  146. }
  147. std::string Pathname::parent_folder() const {
  148. std::string::size_type pos = std::string::npos;
  149. if (folder_.size() >= 2) {
  150. pos = folder_.find_last_of(FOLDER_DELIMS, folder_.length() - 2);
  151. }
  152. if (pos != std::string::npos) {
  153. return folder_.substr(0, pos + 1);
  154. } else {
  155. return EMPTY_STR;
  156. }
  157. }
  158. void Pathname::SetFolder(const std::string& folder) {
  159. folder_.assign(folder);
  160. // Ensure folder ends in a path delimiter
  161. if (!folder_.empty() && !IsFolderDelimiter(folder_[folder_.length()-1])) {
  162. folder_.push_back(folder_delimiter_);
  163. }
  164. }
  165. void Pathname::AppendFolder(const std::string& folder) {
  166. folder_.append(folder);
  167. // Ensure folder ends in a path delimiter
  168. if (!folder_.empty() && !IsFolderDelimiter(folder_[folder_.length()-1])) {
  169. folder_.push_back(folder_delimiter_);
  170. }
  171. }
  172. std::string Pathname::basename() const {
  173. return basename_;
  174. }
  175. bool Pathname::SetBasename(const std::string& basename) {
  176. if(basename.find_first_of(FOLDER_DELIMS) != std::string::npos) {
  177. return false;
  178. }
  179. basename_.assign(basename);
  180. return true;
  181. }
  182. std::string Pathname::extension() const {
  183. return extension_;
  184. }
  185. bool Pathname::SetExtension(const std::string& extension) {
  186. if (extension.find_first_of(FOLDER_DELIMS) != std::string::npos ||
  187. extension.find_first_of(EXT_DELIM, 1) != std::string::npos) {
  188. return false;
  189. }
  190. extension_.assign(extension);
  191. // Ensure extension begins with the extension delimiter
  192. if (!extension_.empty() && (extension_[0] != EXT_DELIM)) {
  193. extension_.insert(extension_.begin(), EXT_DELIM);
  194. }
  195. return true;
  196. }
  197. std::string Pathname::filename() const {
  198. std::string filename(basename_);
  199. filename.append(extension_);
  200. return filename;
  201. }
  202. bool Pathname::SetFilename(const std::string& filename) {
  203. std::string::size_type pos = filename.rfind(EXT_DELIM);
  204. if ((pos == std::string::npos) || (pos == 0)) {
  205. return SetExtension(EMPTY_STR) && SetBasename(filename);
  206. } else {
  207. return SetExtension(filename.substr(pos)) && SetBasename(filename.substr(0, pos));
  208. }
  209. }
  210. #ifdef WIN32
  211. bool Pathname::GetDrive(char *drive, uint32 bytes) const {
  212. return GetDrive(drive, bytes, folder_);
  213. }
  214. // static
  215. bool Pathname::GetDrive(char *drive, uint32 bytes,
  216. const std::string& pathname) {
  217. // need at lease 4 bytes to save c:
  218. if (bytes < 4 || pathname.size() < 3) {
  219. return false;
  220. }
  221. memcpy(drive, pathname.c_str(), 3);
  222. drive[3] = 0;
  223. // sanity checking
  224. return (isalpha(drive[0]) &&
  225. drive[1] == ':' &&
  226. drive[2] == '\\');
  227. }
  228. #endif
  229. ///////////////////////////////////////////////////////////////////////////////
  230. } // namespace talk_base