michael@0: // Copyright (c) 2008 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: // FilePath is a container for pathnames stored in a platform's native string michael@0: // type, providing containers for manipulation in according with the michael@0: // platform's conventions for pathnames. It supports the following path michael@0: // types: michael@0: // michael@0: // POSIX Windows michael@0: // --------------- ---------------------------------- michael@0: // Fundamental type char[] wchar_t[] michael@0: // Encoding unspecified* UTF-16 michael@0: // Separator / \, tolerant of / michael@0: // Drive letters no case-insensitive A-Z followed by : michael@0: // Alternate root // (surprise!) \\, for UNC paths michael@0: // michael@0: // * The encoding need not be specified on POSIX systems, although some michael@0: // POSIX-compliant systems do specify an encoding. Mac OS X uses UTF-8. michael@0: // Linux does not specify an encoding, but in practice, the locale's michael@0: // character set may be used. michael@0: // michael@0: // FilePath objects are intended to be used anywhere paths are. An michael@0: // application may pass FilePath objects around internally, masking the michael@0: // underlying differences between systems, only differing in implementation michael@0: // where interfacing directly with the system. For example, a single michael@0: // OpenFile(const FilePath &) function may be made available, allowing all michael@0: // callers to operate without regard to the underlying implementation. On michael@0: // POSIX-like platforms, OpenFile might wrap fopen, and on Windows, it might michael@0: // wrap _wfopen_s, perhaps both by calling file_path.value().c_str(). This michael@0: // allows each platform to pass pathnames around without requiring conversions michael@0: // between encodings, which has an impact on performance, but more imporantly, michael@0: // has an impact on correctness on platforms that do not have well-defined michael@0: // encodings for pathnames. michael@0: // michael@0: // Several methods are available to perform common operations on a FilePath michael@0: // object, such as determining the parent directory (DirName), isolating the michael@0: // final path component (BaseName), and appending a relative pathname string michael@0: // to an existing FilePath object (Append). These methods are highly michael@0: // recommended over attempting to split and concatenate strings directly. michael@0: // These methods are based purely on string manipulation and knowledge of michael@0: // platform-specific pathname conventions, and do not consult the filesystem michael@0: // at all, making them safe to use without fear of blocking on I/O operations. michael@0: // These methods do not function as mutators but instead return distinct michael@0: // instances of FilePath objects, and are therefore safe to use on const michael@0: // objects. The objects themselves are safe to share between threads. michael@0: // michael@0: // To aid in initialization of FilePath objects from string literals, a michael@0: // FILE_PATH_LITERAL macro is provided, which accounts for the difference michael@0: // between char[]-based pathnames on POSIX systems and wchar_t[]-based michael@0: // pathnames on Windows. michael@0: // michael@0: // Because a FilePath object should not be instantiated at the global scope, michael@0: // instead, use a FilePath::CharType[] and initialize it with michael@0: // FILE_PATH_LITERAL. At runtime, a FilePath object can be created from the michael@0: // character array. Example: michael@0: // michael@0: // | const FilePath::CharType kLogFileName[] = FILE_PATH_LITERAL("log.txt"); michael@0: // | michael@0: // | void Function() { michael@0: // | FilePath log_file_path(kLogFileName); michael@0: // | [...] michael@0: // | } michael@0: michael@0: #ifndef BASE_FILE_PATH_H_ michael@0: #define BASE_FILE_PATH_H_ michael@0: michael@0: #include michael@0: michael@0: #include "base/basictypes.h" michael@0: #include "base/compiler_specific.h" michael@0: #include "base/hash_tables.h" michael@0: michael@0: // Windows-style drive letter support and pathname separator characters can be michael@0: // enabled and disabled independently, to aid testing. These #defines are michael@0: // here so that the same setting can be used in both the implementation and michael@0: // in the unit test. michael@0: #if defined(OS_WIN) michael@0: #define FILE_PATH_USES_DRIVE_LETTERS michael@0: #define FILE_PATH_USES_WIN_SEPARATORS michael@0: #endif // OS_WIN michael@0: michael@0: // An abstraction to isolate users from the differences between native michael@0: // pathnames on different platforms. michael@0: class FilePath { michael@0: public: michael@0: #if defined(OS_POSIX) michael@0: // On most platforms, native pathnames are char arrays, and the encoding michael@0: // may or may not be specified. On Mac OS X, native pathnames are encoded michael@0: // in UTF-8. michael@0: typedef std::string StringType; michael@0: #elif defined(OS_WIN) michael@0: // On Windows, for Unicode-aware applications, native pathnames are wchar_t michael@0: // arrays encoded in UTF-16. michael@0: typedef std::wstring StringType; michael@0: #endif // OS_WIN michael@0: michael@0: typedef StringType::value_type CharType; michael@0: michael@0: // Null-terminated array of separators used to separate components in michael@0: // hierarchical paths. Each character in this array is a valid separator, michael@0: // but kSeparators[0] is treated as the canonical separator and will be used michael@0: // when composing pathnames. michael@0: static const CharType kSeparators[]; michael@0: michael@0: // A special path component meaning "this directory." michael@0: static const CharType kCurrentDirectory[]; michael@0: michael@0: // A special path component meaning "the parent directory." michael@0: static const CharType kParentDirectory[]; michael@0: michael@0: // The character used to identify a file extension. michael@0: static const CharType kExtensionSeparator; michael@0: michael@0: FilePath() {} michael@0: FilePath(const FilePath& that) : path_(that.path_) {} michael@0: explicit FilePath(const StringType& path) : path_(path) {} michael@0: michael@0: FilePath& operator=(const FilePath& that) { michael@0: path_ = that.path_; michael@0: return *this; michael@0: } michael@0: michael@0: bool operator==(const FilePath& that) const { michael@0: return path_ == that.path_; michael@0: } michael@0: michael@0: bool operator!=(const FilePath& that) const { michael@0: return path_ != that.path_; michael@0: } michael@0: michael@0: // Required for some STL containers and operations michael@0: bool operator<(const FilePath& that) const { michael@0: return path_ < that.path_; michael@0: } michael@0: michael@0: const StringType& value() const { return path_; } michael@0: michael@0: bool empty() const { return path_.empty(); } michael@0: michael@0: // Returns true if |character| is in kSeparators. michael@0: static bool IsSeparator(CharType character); michael@0: michael@0: // Returns a FilePath corresponding to the directory containing the path michael@0: // named by this object, stripping away the file component. If this object michael@0: // only contains one component, returns a FilePath identifying michael@0: // kCurrentDirectory. If this object already refers to the root directory, michael@0: // returns a FilePath identifying the root directory. michael@0: FilePath DirName() const; michael@0: michael@0: // Returns a FilePath corresponding to the last path component of this michael@0: // object, either a file or a directory. If this object already refers to michael@0: // the root directory, returns a FilePath identifying the root directory; michael@0: // this is the only situation in which BaseName will return an absolute path. michael@0: FilePath BaseName() const; michael@0: michael@0: // Returns ".jpg" for path "C:\pics\jojo.jpg", or an empty string if michael@0: // the file has no extension. If non-empty, Extension() will always start michael@0: // with precisely one ".". The following code should always work regardless michael@0: // of the value of path. michael@0: // new_path = path.RemoveExtension().value().append(path.Extension()); michael@0: // ASSERT(new_path == path.value()); michael@0: // NOTE: this is different from the original file_util implementation which michael@0: // returned the extension without a leading "." ("jpg" instead of ".jpg") michael@0: StringType Extension() const; michael@0: michael@0: // Returns "C:\pics\jojo" for path "C:\pics\jojo.jpg" michael@0: // NOTE: this is slightly different from the similar file_util implementation michael@0: // which returned simply 'jojo'. michael@0: FilePath RemoveExtension() const; michael@0: michael@0: // Inserts |suffix| after the file name portion of |path| but before the michael@0: // extension. Returns "" if BaseName() == "." or "..". michael@0: // Examples: michael@0: // path == "C:\pics\jojo.jpg" suffix == " (1)", returns "C:\pics\jojo (1).jpg" michael@0: // path == "jojo.jpg" suffix == " (1)", returns "jojo (1).jpg" michael@0: // path == "C:\pics\jojo" suffix == " (1)", returns "C:\pics\jojo (1)" michael@0: // path == "C:\pics.old\jojo" suffix == " (1)", returns "C:\pics.old\jojo (1)" michael@0: FilePath InsertBeforeExtension(const StringType& suffix) const; michael@0: michael@0: // Replaces the extension of |file_name| with |extension|. If |file_name| michael@0: // does not have an extension, them |extension| is added. If |extension| is michael@0: // empty, then the extension is removed from |file_name|. michael@0: // Returns "" if BaseName() == "." or "..". michael@0: FilePath ReplaceExtension(const StringType& extension) const; michael@0: michael@0: // Returns a FilePath by appending a separator and the supplied path michael@0: // component to this object's path. Append takes care to avoid adding michael@0: // excessive separators if this object's path already ends with a separator. michael@0: // If this object's path is kCurrentDirectory, a new FilePath corresponding michael@0: // only to |component| is returned. |component| must be a relative path; michael@0: // it is an error to pass an absolute path. michael@0: FilePath Append(const StringType& component) const WARN_UNUSED_RESULT; michael@0: FilePath Append(const FilePath& component) const WARN_UNUSED_RESULT; michael@0: michael@0: // Although Windows StringType is std::wstring, since the encoding it uses for michael@0: // paths is well defined, it can handle ASCII path components as well. michael@0: // Mac uses UTF8, and since ASCII is a subset of that, it works there as well. michael@0: // On Linux, although it can use any 8-bit encoding for paths, we assume that michael@0: // ASCII is a valid subset, regardless of the encoding, since many operating michael@0: // system paths will always be ASCII. michael@0: FilePath AppendASCII(const std::string& component) const WARN_UNUSED_RESULT; michael@0: michael@0: // Returns true if this FilePath contains an absolute path. On Windows, an michael@0: // absolute path begins with either a drive letter specification followed by michael@0: // a separator character, or with two separator characters. On POSIX michael@0: // platforms, an absolute path begins with a separator character. michael@0: bool IsAbsolute() const; michael@0: michael@0: // Returns a copy of this FilePath that does not end with a trailing michael@0: // separator. michael@0: FilePath StripTrailingSeparators() const; michael@0: michael@0: // Calls open on given ifstream instance michael@0: void OpenInputStream(std::ifstream &stream) const; michael@0: michael@0: // Older Chromium code assumes that paths are always wstrings. michael@0: // This function converts a wstring to a FilePath, and is useful to smooth michael@0: // porting that old code to the FilePath API. michael@0: // It has "Hack" in its name so people feel bad about using it. michael@0: // TODO(port): remove these functions. michael@0: static FilePath FromWStringHack(const std::wstring& wstring); michael@0: michael@0: // Older Chromium code assumes that paths are always wstrings. michael@0: // This function produces a wstring from a FilePath, and is useful to smooth michael@0: // porting that old code to the FilePath API. michael@0: // It has "Hack" in its name so people feel bad about using it. michael@0: // TODO(port): remove these functions. michael@0: std::wstring ToWStringHack() const; michael@0: michael@0: private: michael@0: // Remove trailing separators from this object. If the path is absolute, it michael@0: // will never be stripped any more than to refer to the absolute root michael@0: // directory, so "////" will become "/", not "". A leading pair of michael@0: // separators is never stripped, to support alternate roots. This is used to michael@0: // support UNC paths on Windows. michael@0: void StripTrailingSeparatorsInternal(); michael@0: michael@0: StringType path_; michael@0: }; michael@0: michael@0: // Macros for string literal initialization of FilePath::CharType[]. michael@0: #if defined(OS_POSIX) michael@0: #define FILE_PATH_LITERAL(x) x michael@0: #elif defined(OS_WIN) michael@0: #define FILE_PATH_LITERAL(x) L ## x michael@0: #endif // OS_WIN michael@0: michael@0: // Implement hash function so that we can use FilePaths in hashsets and maps. michael@0: #if defined(COMPILER_GCC) && !defined(ANDROID) michael@0: namespace __gnu_cxx { michael@0: michael@0: template<> michael@0: struct hash { michael@0: size_t operator()(const FilePath& f) const { michael@0: return hash()(f.value()); michael@0: } michael@0: }; michael@0: michael@0: } // namespace __gnu_cxx michael@0: #elif defined(COMPILER_MSVC) michael@0: namespace stdext { michael@0: michael@0: inline size_t hash_value(const FilePath& f) { michael@0: return hash_value(f.value()); michael@0: } michael@0: michael@0: } // namespace stdext michael@0: #endif // COMPILER michael@0: michael@0: #endif // BASE_FILE_PATH_H_