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.

180 lines
6.8 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. #ifndef TALK_BASE_PATHUTILS_H__
  28. #define TALK_BASE_PATHUTILS_H__
  29. #include <string>
  30. // Temporary, until deprecated helpers are removed.
  31. #include "talk/base/fileutils.h"
  32. namespace talk_base {
  33. ///////////////////////////////////////////////////////////////////////////////
  34. // Pathname - parsing of pathnames into components, and vice versa.
  35. //
  36. // To establish consistent terminology, a filename never contains a folder
  37. // component. A folder never contains a filename. A pathname may include
  38. // a folder and/or filename component. Here are some examples:
  39. //
  40. // pathname() /home/john/example.txt
  41. // folder() /home/john/
  42. // filename() example.txt
  43. // parent_folder() /home/
  44. // folder_name() john/
  45. // basename() example
  46. // extension() .txt
  47. //
  48. // Basename may begin, end, and/or include periods, but no folder delimiters.
  49. // If extension exists, it consists of a period followed by zero or more
  50. // non-period/non-delimiter characters, and basename is non-empty.
  51. ///////////////////////////////////////////////////////////////////////////////
  52. class Pathname {
  53. public:
  54. // Folder delimiters are slash and backslash
  55. static bool IsFolderDelimiter(char ch);
  56. static char DefaultFolderDelimiter();
  57. Pathname();
  58. Pathname(const std::string& pathname);
  59. Pathname(const std::string& folder, const std::string& filename);
  60. // Set's the default folder delimiter for this Pathname
  61. char folder_delimiter() const { return folder_delimiter_; }
  62. void SetFolderDelimiter(char delimiter);
  63. // Normalize changes all folder delimiters to folder_delimiter()
  64. void Normalize();
  65. // Reset to the empty pathname
  66. void clear();
  67. // Returns true if the pathname is empty. Note: this->pathname().empty()
  68. // is always false.
  69. bool empty() const;
  70. std::string url() const;
  71. // Returns the folder and filename components. If the pathname is empty,
  72. // returns a string representing the current directory (as a relative path,
  73. // i.e., ".").
  74. std::string pathname() const;
  75. void SetPathname(const std::string& pathname);
  76. void SetPathname(const std::string& folder, const std::string& filename);
  77. // Append pathname to the current folder (if any). Any existing filename
  78. // will be discarded.
  79. void AppendPathname(const std::string& pathname);
  80. std::string folder() const;
  81. std::string folder_name() const;
  82. std::string parent_folder() const;
  83. // SetFolder and AppendFolder will append a folder delimiter, if needed.
  84. void SetFolder(const std::string& folder);
  85. void AppendFolder(const std::string& folder);
  86. std::string basename() const;
  87. bool SetBasename(const std::string& basename);
  88. std::string extension() const;
  89. // SetExtension will prefix a period, if needed.
  90. bool SetExtension(const std::string& extension);
  91. std::string filename() const;
  92. bool SetFilename(const std::string& filename);
  93. #ifdef WIN32
  94. bool GetDrive(char *drive, uint32 bytes) const;
  95. static bool GetDrive(char *drive, uint32 bytes,const std::string& pathname);
  96. #endif
  97. private:
  98. std::string folder_, basename_, extension_;
  99. char folder_delimiter_;
  100. };
  101. ///////////////////////////////////////////////////////////////////////////////
  102. // Global Helpers (deprecated)
  103. ///////////////////////////////////////////////////////////////////////////////
  104. inline void SetOrganizationName(const std::string& organization) {
  105. Filesystem::SetOrganizationName(organization);
  106. }
  107. inline void SetApplicationName(const std::string& application) {
  108. Filesystem::SetApplicationName(application);
  109. }
  110. inline void GetOrganizationName(std::string* organization) {
  111. Filesystem::GetOrganizationName(organization);
  112. }
  113. inline void GetApplicationName(std::string* application) {
  114. Filesystem::GetApplicationName(application);
  115. }
  116. inline bool CreateFolder(const Pathname& path) {
  117. return Filesystem::CreateFolder(path);
  118. }
  119. inline bool FinishPath(Pathname& path, bool create, const std::string& append) {
  120. if (!append.empty())
  121. path.AppendFolder(append);
  122. return !create || CreateFolder(path);
  123. }
  124. // Note: this method uses the convention of <temp>/<appname> for the temporary
  125. // folder. Filesystem uses <temp>/<exename>. We will be migrating exclusively
  126. // to <temp>/<orgname>/<appname> eventually. Since these are temp folders,
  127. // it's probably ok to orphan them during the transition.
  128. inline bool GetTemporaryFolder(Pathname& path, bool create,
  129. const std::string& append) {
  130. std::string application_name;
  131. Filesystem::GetApplicationName(&application_name);
  132. ASSERT(!application_name.empty());
  133. return Filesystem::GetTemporaryFolder(path, create, &application_name)
  134. && FinishPath(path, create, append);
  135. }
  136. inline bool GetAppDataFolder(Pathname& path, bool create,
  137. const std::string& append) {
  138. ASSERT(!create); // TODO: Support create flag on Filesystem::GetAppDataFolder.
  139. return Filesystem::GetAppDataFolder(&path, true)
  140. && FinishPath(path, create, append);
  141. }
  142. inline bool CleanupTemporaryFolder() {
  143. Pathname path;
  144. if (!GetTemporaryFolder(path, false, ""))
  145. return false;
  146. if (Filesystem::IsAbsent(path))
  147. return true;
  148. if (!Filesystem::IsTemporaryPath(path)) {
  149. ASSERT(false);
  150. return false;
  151. }
  152. return Filesystem::DeleteFolderContents(path);
  153. }
  154. ///////////////////////////////////////////////////////////////////////////////
  155. } // namespace talk_base
  156. #endif // TALK_BASE_PATHUTILS_H__