LCOV - code coverage report
Current view: directory - ipc/chromium/src/base - file_path.h (source / functions) Found Hit Coverage
Test: app.info Lines: 11 0 0.0 %
Date: 2012-06-02 Functions: 8 0 0.0 %

       1                 : // Copyright (c) 2008 The Chromium Authors. All rights reserved.
       2                 : // Use of this source code is governed by a BSD-style license that can be
       3                 : // found in the LICENSE file.
       4                 : 
       5                 : // FilePath is a container for pathnames stored in a platform's native string
       6                 : // type, providing containers for manipulation in according with the
       7                 : // platform's conventions for pathnames.  It supports the following path
       8                 : // types:
       9                 : //
      10                 : //                   POSIX            Windows
      11                 : //                   ---------------  ----------------------------------
      12                 : // Fundamental type  char[]           wchar_t[]
      13                 : // Encoding          unspecified*     UTF-16
      14                 : // Separator         /                \, tolerant of /
      15                 : // Drive letters     no               case-insensitive A-Z followed by :
      16                 : // Alternate root    // (surprise!)   \\, for UNC paths
      17                 : //
      18                 : // * The encoding need not be specified on POSIX systems, although some
      19                 : //   POSIX-compliant systems do specify an encoding.  Mac OS X uses UTF-8.
      20                 : //   Linux does not specify an encoding, but in practice, the locale's
      21                 : //   character set may be used.
      22                 : //
      23                 : // FilePath objects are intended to be used anywhere paths are.  An
      24                 : // application may pass FilePath objects around internally, masking the
      25                 : // underlying differences between systems, only differing in implementation
      26                 : // where interfacing directly with the system.  For example, a single
      27                 : // OpenFile(const FilePath &) function may be made available, allowing all
      28                 : // callers to operate without regard to the underlying implementation.  On
      29                 : // POSIX-like platforms, OpenFile might wrap fopen, and on Windows, it might
      30                 : // wrap _wfopen_s, perhaps both by calling file_path.value().c_str().  This
      31                 : // allows each platform to pass pathnames around without requiring conversions
      32                 : // between encodings, which has an impact on performance, but more imporantly,
      33                 : // has an impact on correctness on platforms that do not have well-defined
      34                 : // encodings for pathnames.
      35                 : //
      36                 : // Several methods are available to perform common operations on a FilePath
      37                 : // object, such as determining the parent directory (DirName), isolating the
      38                 : // final path component (BaseName), and appending a relative pathname string
      39                 : // to an existing FilePath object (Append).  These methods are highly
      40                 : // recommended over attempting to split and concatenate strings directly.
      41                 : // These methods are based purely on string manipulation and knowledge of
      42                 : // platform-specific pathname conventions, and do not consult the filesystem
      43                 : // at all, making them safe to use without fear of blocking on I/O operations.
      44                 : // These methods do not function as mutators but instead return distinct
      45                 : // instances of FilePath objects, and are therefore safe to use on const
      46                 : // objects.  The objects themselves are safe to share between threads.
      47                 : //
      48                 : // To aid in initialization of FilePath objects from string literals, a
      49                 : // FILE_PATH_LITERAL macro is provided, which accounts for the difference
      50                 : // between char[]-based pathnames on POSIX systems and wchar_t[]-based
      51                 : // pathnames on Windows.
      52                 : //
      53                 : // Because a FilePath object should not be instantiated at the global scope,
      54                 : // instead, use a FilePath::CharType[] and initialize it with
      55                 : // FILE_PATH_LITERAL.  At runtime, a FilePath object can be created from the
      56                 : // character array.  Example:
      57                 : //
      58                 : // | const FilePath::CharType kLogFileName[] = FILE_PATH_LITERAL("log.txt");
      59                 : // |
      60                 : // | void Function() {
      61                 : // |   FilePath log_file_path(kLogFileName);
      62                 : // |   [...]
      63                 : // | }
      64                 : 
      65                 : #ifndef BASE_FILE_PATH_H_
      66                 : #define BASE_FILE_PATH_H_
      67                 : 
      68                 : #include <string>
      69                 : 
      70                 : #include "base/basictypes.h"
      71                 : #include "base/compiler_specific.h"
      72                 : #include "base/hash_tables.h"
      73                 : 
      74                 : // Windows-style drive letter support and pathname separator characters can be
      75                 : // enabled and disabled independently, to aid testing.  These #defines are
      76                 : // here so that the same setting can be used in both the implementation and
      77                 : // in the unit test.
      78                 : #if defined(OS_WIN)
      79                 : #define FILE_PATH_USES_DRIVE_LETTERS
      80                 : #define FILE_PATH_USES_WIN_SEPARATORS
      81                 : #endif  // OS_WIN
      82                 : 
      83                 : // An abstraction to isolate users from the differences between native
      84                 : // pathnames on different platforms.
      85               0 : class FilePath {
      86                 :  public:
      87                 : #if defined(OS_POSIX)
      88                 :   // On most platforms, native pathnames are char arrays, and the encoding
      89                 :   // may or may not be specified.  On Mac OS X, native pathnames are encoded
      90                 :   // in UTF-8.
      91                 :   typedef std::string StringType;
      92                 : #elif defined(OS_WIN)
      93                 :   // On Windows, for Unicode-aware applications, native pathnames are wchar_t
      94                 :   // arrays encoded in UTF-16.
      95                 :   typedef std::wstring StringType;
      96                 : #endif  // OS_WIN
      97                 : 
      98                 :   typedef StringType::value_type CharType;
      99                 : 
     100                 :   // Null-terminated array of separators used to separate components in
     101                 :   // hierarchical paths.  Each character in this array is a valid separator,
     102                 :   // but kSeparators[0] is treated as the canonical separator and will be used
     103                 :   // when composing pathnames.
     104                 :   static const CharType kSeparators[];
     105                 : 
     106                 :   // A special path component meaning "this directory."
     107                 :   static const CharType kCurrentDirectory[];
     108                 : 
     109                 :   // A special path component meaning "the parent directory."
     110                 :   static const CharType kParentDirectory[];
     111                 : 
     112                 :   // The character used to identify a file extension.
     113                 :   static const CharType kExtensionSeparator;
     114                 : 
     115               0 :   FilePath() {}
     116               0 :   FilePath(const FilePath& that) : path_(that.path_) {}
     117               0 :   explicit FilePath(const StringType& path) : path_(path) {}
     118                 : 
     119               0 :   FilePath& operator=(const FilePath& that) {
     120               0 :     path_ = that.path_;
     121               0 :     return *this;
     122                 :   }
     123                 : 
     124               0 :   bool operator==(const FilePath& that) const {
     125               0 :     return path_ == that.path_;
     126                 :   }
     127                 : 
     128                 :   bool operator!=(const FilePath& that) const {
     129                 :     return path_ != that.path_;
     130                 :   }
     131                 : 
     132                 :   // Required for some STL containers and operations
     133                 :   bool operator<(const FilePath& that) const {
     134                 :     return path_ < that.path_;
     135                 :   }
     136                 : 
     137               0 :   const StringType& value() const { return path_; }
     138                 : 
     139               0 :   bool empty() const { return path_.empty(); }
     140                 : 
     141                 :   // Returns true if |character| is in kSeparators.
     142                 :   static bool IsSeparator(CharType character);
     143                 : 
     144                 :   // Returns a FilePath corresponding to the directory containing the path
     145                 :   // named by this object, stripping away the file component.  If this object
     146                 :   // only contains one component, returns a FilePath identifying
     147                 :   // kCurrentDirectory.  If this object already refers to the root directory,
     148                 :   // returns a FilePath identifying the root directory.
     149                 :   FilePath DirName() const;
     150                 : 
     151                 :   // Returns a FilePath corresponding to the last path component of this
     152                 :   // object, either a file or a directory.  If this object already refers to
     153                 :   // the root directory, returns a FilePath identifying the root directory;
     154                 :   // this is the only situation in which BaseName will return an absolute path.
     155                 :   FilePath BaseName() const;
     156                 : 
     157                 :   // Returns ".jpg" for path "C:\pics\jojo.jpg", or an empty string if
     158                 :   // the file has no extension.  If non-empty, Extension() will always start
     159                 :   // with precisely one ".".  The following code should always work regardless
     160                 :   // of the value of path.
     161                 :   // new_path = path.RemoveExtension().value().append(path.Extension());
     162                 :   // ASSERT(new_path == path.value());
     163                 :   // NOTE: this is different from the original file_util implementation which
     164                 :   // returned the extension without a leading "." ("jpg" instead of ".jpg")
     165                 :   StringType Extension() const;
     166                 : 
     167                 :   // Returns "C:\pics\jojo" for path "C:\pics\jojo.jpg"
     168                 :   // NOTE: this is slightly different from the similar file_util implementation
     169                 :   // which returned simply 'jojo'.
     170                 :   FilePath RemoveExtension() const;
     171                 : 
     172                 :   // Inserts |suffix| after the file name portion of |path| but before the
     173                 :   // extension.  Returns "" if BaseName() == "." or "..".
     174                 :   // Examples:
     175                 :   // path == "C:\pics\jojo.jpg" suffix == " (1)", returns "C:\pics\jojo (1).jpg"
     176                 :   // path == "jojo.jpg"         suffix == " (1)", returns "jojo (1).jpg"
     177                 :   // path == "C:\pics\jojo"     suffix == " (1)", returns "C:\pics\jojo (1)"
     178                 :   // path == "C:\pics.old\jojo" suffix == " (1)", returns "C:\pics.old\jojo (1)"
     179                 :   FilePath InsertBeforeExtension(const StringType& suffix) const;
     180                 : 
     181                 :   // Replaces the extension of |file_name| with |extension|.  If |file_name|
     182                 :   // does not have an extension, them |extension| is added.  If |extension| is
     183                 :   // empty, then the extension is removed from |file_name|.
     184                 :   // Returns "" if BaseName() == "." or "..".
     185                 :   FilePath ReplaceExtension(const StringType& extension) const;
     186                 : 
     187                 :   // Returns a FilePath by appending a separator and the supplied path
     188                 :   // component to this object's path.  Append takes care to avoid adding
     189                 :   // excessive separators if this object's path already ends with a separator.
     190                 :   // If this object's path is kCurrentDirectory, a new FilePath corresponding
     191                 :   // only to |component| is returned.  |component| must be a relative path;
     192                 :   // it is an error to pass an absolute path.
     193                 :   FilePath Append(const StringType& component) const WARN_UNUSED_RESULT;
     194                 :   FilePath Append(const FilePath& component) const WARN_UNUSED_RESULT;
     195                 : 
     196                 :   // Although Windows StringType is std::wstring, since the encoding it uses for
     197                 :   // paths is well defined, it can handle ASCII path components as well.
     198                 :   // Mac uses UTF8, and since ASCII is a subset of that, it works there as well.
     199                 :   // On Linux, although it can use any 8-bit encoding for paths, we assume that
     200                 :   // ASCII is a valid subset, regardless of the encoding, since many operating
     201                 :   // system paths will always be ASCII.
     202                 :   FilePath AppendASCII(const std::string& component) const WARN_UNUSED_RESULT;
     203                 : 
     204                 :   // Returns true if this FilePath contains an absolute path.  On Windows, an
     205                 :   // absolute path begins with either a drive letter specification followed by
     206                 :   // a separator character, or with two separator characters.  On POSIX
     207                 :   // platforms, an absolute path begins with a separator character.
     208                 :   bool IsAbsolute() const;
     209                 : 
     210                 :   // Returns a copy of this FilePath that does not end with a trailing
     211                 :   // separator.
     212                 :   FilePath StripTrailingSeparators() const;
     213                 : 
     214                 :   // Calls open on given ifstream instance
     215                 :   void OpenInputStream(std::ifstream &stream) const;
     216                 : 
     217                 :   // Older Chromium code assumes that paths are always wstrings.
     218                 :   // This function converts a wstring to a FilePath, and is useful to smooth
     219                 :   // porting that old code to the FilePath API.
     220                 :   // It has "Hack" in its name so people feel bad about using it.
     221                 :   // TODO(port): remove these functions.
     222                 :   static FilePath FromWStringHack(const std::wstring& wstring);
     223                 : 
     224                 :   // Older Chromium code assumes that paths are always wstrings.
     225                 :   // This function produces a wstring from a FilePath, and is useful to smooth
     226                 :   // porting that old code to the FilePath API.
     227                 :   // It has "Hack" in its name so people feel bad about using it.
     228                 :   // TODO(port): remove these functions.
     229                 :   std::wstring ToWStringHack() const;
     230                 : 
     231                 :  private:
     232                 :   // Remove trailing separators from this object.  If the path is absolute, it
     233                 :   // will never be stripped any more than to refer to the absolute root
     234                 :   // directory, so "////" will become "/", not "".  A leading pair of
     235                 :   // separators is never stripped, to support alternate roots.  This is used to
     236                 :   // support UNC paths on Windows.
     237                 :   void StripTrailingSeparatorsInternal();
     238                 : 
     239                 :   StringType path_;
     240                 : };
     241                 : 
     242                 : // Macros for string literal initialization of FilePath::CharType[].
     243                 : #if defined(OS_POSIX)
     244                 : #define FILE_PATH_LITERAL(x) x
     245                 : #elif defined(OS_WIN)
     246                 : #define FILE_PATH_LITERAL(x) L ## x
     247                 : #endif  // OS_WIN
     248                 : 
     249                 : // Implement hash function so that we can use FilePaths in hashsets and maps.
     250                 : #if defined(COMPILER_GCC) && !defined(ANDROID)
     251                 : namespace __gnu_cxx {
     252                 : 
     253                 : template<>
     254                 : struct hash<FilePath> {
     255                 :   size_t operator()(const FilePath& f) const {
     256                 :     return hash<FilePath::StringType>()(f.value());
     257                 :   }
     258                 : };
     259                 : 
     260                 : }  // namespace __gnu_cxx
     261                 : #elif defined(COMPILER_MSVC)
     262                 : namespace stdext {
     263                 : 
     264                 : inline size_t hash_value(const FilePath& f) {
     265                 :   return hash_value(f.value());
     266                 : }
     267                 : 
     268                 : }  // namespace stdext
     269                 : #endif  // COMPILER
     270                 : 
     271                 : #endif  // BASE_FILE_PATH_H_

Generated by: LCOV version 1.7