Home | History | Annotate | Download | only in base
      1 /*
      2  *  Copyright 2004 The WebRTC Project Authors. All rights reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 #ifndef WEBRTC_BASE_PATHUTILS_H__
     12 #define WEBRTC_BASE_PATHUTILS_H__
     13 
     14 #include <string>
     15 // Temporary, until deprecated helpers are removed.
     16 #include "webrtc/base/fileutils.h"
     17 
     18 namespace rtc {
     19 
     20 ///////////////////////////////////////////////////////////////////////////////
     21 // Pathname - parsing of pathnames into components, and vice versa.
     22 //
     23 // To establish consistent terminology, a filename never contains a folder
     24 // component.  A folder never contains a filename.  A pathname may include
     25 // a folder and/or filename component.  Here are some examples:
     26 //
     27 //   pathname()      /home/john/example.txt
     28 //   folder()        /home/john/
     29 //   filename()                 example.txt
     30 //   parent_folder() /home/
     31 //   folder_name()         john/
     32 //   basename()                 example
     33 //   extension()                       .txt
     34 //
     35 // Basename may begin, end, and/or include periods, but no folder delimiters.
     36 // If extension exists, it consists of a period followed by zero or more
     37 // non-period/non-delimiter characters, and basename is non-empty.
     38 ///////////////////////////////////////////////////////////////////////////////
     39 
     40 class Pathname {
     41 public:
     42   // Folder delimiters are slash and backslash
     43   static bool IsFolderDelimiter(char ch);
     44   static char DefaultFolderDelimiter();
     45 
     46   Pathname();
     47   Pathname(const std::string& pathname);
     48   Pathname(const std::string& folder, const std::string& filename);
     49 
     50   // Set's the default folder delimiter for this Pathname
     51   char folder_delimiter() const { return folder_delimiter_; }
     52   void SetFolderDelimiter(char delimiter);
     53 
     54   // Normalize changes all folder delimiters to folder_delimiter()
     55   void Normalize();
     56 
     57   // Reset to the empty pathname
     58   void clear();
     59 
     60   // Returns true if the pathname is empty.  Note: this->pathname().empty()
     61   // is always false.
     62   bool empty() const;
     63 
     64   std::string url() const;
     65 
     66   // Returns the folder and filename components.  If the pathname is empty,
     67   // returns a string representing the current directory (as a relative path,
     68   // i.e., ".").
     69   std::string pathname() const;
     70   void SetPathname(const std::string& pathname);
     71   void SetPathname(const std::string& folder, const std::string& filename);
     72 
     73   // Append pathname to the current folder (if any).  Any existing filename
     74   // will be discarded.
     75   void AppendPathname(const std::string& pathname);
     76 
     77   std::string folder() const;
     78   std::string folder_name() const;
     79   std::string parent_folder() const;
     80   // SetFolder and AppendFolder will append a folder delimiter, if needed.
     81   void SetFolder(const std::string& folder);
     82   void AppendFolder(const std::string& folder);
     83 
     84   std::string basename() const;
     85   bool SetBasename(const std::string& basename);
     86 
     87   std::string extension() const;
     88   // SetExtension will prefix a period, if needed.
     89   bool SetExtension(const std::string& extension);
     90 
     91   std::string filename() const;
     92   bool SetFilename(const std::string& filename);
     93 
     94 #if defined(WEBRTC_WIN)
     95   bool GetDrive(char* drive, uint32_t bytes) const;
     96   static bool GetDrive(char* drive,
     97                        uint32_t bytes,
     98                        const std::string& pathname);
     99 #endif
    100 
    101 private:
    102   std::string folder_, basename_, extension_;
    103   char folder_delimiter_;
    104 };
    105 
    106 ///////////////////////////////////////////////////////////////////////////////
    107 // Global Helpers (deprecated)
    108 ///////////////////////////////////////////////////////////////////////////////
    109 
    110 inline void SetOrganizationName(const std::string& organization) {
    111   Filesystem::SetOrganizationName(organization);
    112 }
    113 inline void SetApplicationName(const std::string& application) {
    114   Filesystem::SetApplicationName(application);
    115 }
    116 inline void GetOrganizationName(std::string* organization) {
    117   Filesystem::GetOrganizationName(organization);
    118 }
    119 inline void GetApplicationName(std::string* application) {
    120   Filesystem::GetApplicationName(application);
    121 }
    122 inline bool CreateFolder(const Pathname& path) {
    123   return Filesystem::CreateFolder(path);
    124 }
    125 inline bool FinishPath(Pathname& path, bool create, const std::string& append) {
    126   if (!append.empty())
    127     path.AppendFolder(append);
    128   return !create || CreateFolder(path);
    129 }
    130 // Note: this method uses the convention of <temp>/<appname> for the temporary
    131 // folder.  Filesystem uses <temp>/<exename>.  We will be migrating exclusively
    132 // to <temp>/<orgname>/<appname> eventually.  Since these are temp folders,
    133 // it's probably ok to orphan them during the transition.
    134 inline bool GetTemporaryFolder(Pathname& path, bool create,
    135                                const std::string& append) {
    136   std::string application_name;
    137   Filesystem::GetApplicationName(&application_name);
    138   ASSERT(!application_name.empty());
    139   return Filesystem::GetTemporaryFolder(path, create, &application_name)
    140          && FinishPath(path, create, append);
    141 }
    142 inline bool GetAppDataFolder(Pathname& path, bool create,
    143                              const std::string& append) {
    144   ASSERT(!create); // TODO: Support create flag on Filesystem::GetAppDataFolder.
    145   return Filesystem::GetAppDataFolder(&path, true)
    146          && FinishPath(path, create, append);
    147 }
    148 inline bool CleanupTemporaryFolder() {
    149   Pathname path;
    150   if (!GetTemporaryFolder(path, false, ""))
    151     return false;
    152   if (Filesystem::IsAbsent(path))
    153     return true;
    154   if (!Filesystem::IsTemporaryPath(path)) {
    155     ASSERT(false);
    156     return false;
    157   }
    158   return Filesystem::DeleteFolderContents(path);
    159 }
    160 
    161 ///////////////////////////////////////////////////////////////////////////////
    162 
    163 }  // namespace rtc
    164 
    165 #endif // WEBRTC_BASE_PATHUTILS_H__
    166