Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(509)

Side by Side Diff: base/file_path.h

Issue 6025: Add a FilePath object (Closed) Base URL: svn://chrome-svn.corp.google.com/chrome/trunk/src/
Patch Set: '' Created 12 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/build/base_unittests.vcproj ('k') | base/file_path.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
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 platform's
7 // conventions for pathnames. It supports the following path types:
8 //
9 // POSIX Windows
10 // --------------- ----------------------------------
11 // Fundamental type char[] wchar_t[]
12 // Encoding unspecified* UTF-16
13 // Separator / \, tolerant of /
14 // Drive letters no case-insensitive A-Z followed by :
15 // Alternate root // (surprise!) \\, for UNC paths
16 //
17 // * The encoding need not be specified on POSIX systems, although some
18 // POSIX-compliant systems do specify an encoding. Mac OS X uses UTF-8.
19 // Linux does not specify an encoding, but in practice, the locale's
20 // character set may be used.
21 //
22 // FilePath objects are intended to be used anywhere paths are. An application
23 // may pass FilePath objects around internally, masking the underlying
24 // differences between systems, only differing in implementation where
25 // interfacing directly with the system. For example, a single
26 // OpenFile(const FilePath &) function may be made available, allowing all
27 // callers to operate without regard to the underlying implementation. On
28 // POSIX-like platforms, OpenFile might wrap fopen, and on Windows, it might
29 // wrap _wfopen_s, perhaps both by calling file_path.value().c_str(). This
30 // allows each platform to pass pathnames around without requiring conversions
31 // between encodings, which has an impact on performance, but more imporantly,
32 // has an impact on correctness on platforms that do not have well-defined
33 // encodings for pathnames.
34 //
35 // Several methods are available to perform common operations on a FilePath
36 // object, such as determining the parent directory (DirName), isolating the
37 // final path component (BaseName), and appending a relative pathname string
38 // to an existing FilePath object (Append). These methods are highly
39 // recommended over attempting to split and concatenate strings directly.
40 // These methods do not function as mutators but instead return distinct
41 // instances of FilePath objects, and are therefore safe to use on const
42 // objects. The objects themselves are safe to share.
darin (slow to review) 2008/10/03 16:51:50 "share [between threads]" ?
43 //
44 // To aid in initialization of FilePath objects from string literals, a
45 // FILE_PATH_LITERAL macro is provided, which accounts for the difference
46 // between char[]-based pathnames on POSIX systems and wchar_t[]-based
47 // pathnames on Windows.
48 //
49 // Because a FilePath object should not be instantiated at the global scope,
50 // instead, use a FilePath::CharType[] and initialize it with
51 // FILE_PATH_LITERAL. At runtime, a FilePath object can be created from the
52 // character array. Example:
53 //
54 // | const FilePath::CharType kLogFileName[] = FILE_PATH_LITERAL("log.txt");
55 // |
56 // | void Function() {
57 // | FilePath log_file_path(kLogFileName);
58 // | [...]
59 // | }
60
61 #ifndef BASE_FILE_PATH_H_
62 #define BASE_FILE_PATH_H_
63
64 #include <string>
65
66 #include "base/basictypes.h"
67
68 // Windows-style drive letter support and pathname separator characters can be
69 // enabled and disabled independently, to aid testing. These #defines are
70 // here so that the same setting can be used in both the implementation and
71 // in the unit test.
72 #if defined(OS_WIN)
73 #define FILE_PATH_USES_DRIVE_LETTERS
74 #define FILE_PATH_USES_WIN_SEPARATORS
75 #endif // OS_WIN
76
77 // An abstraction to isolate users from the differences between native
78 // pathnames on different platforms.
79 class FilePath {
80 public:
81 #if defined(OS_POSIX)
82 // On most platforms, native pathnames are char arrays, and the encoding
83 // may or may not be specified. On Mac OS X, native pathnames are encoded
84 // in UTF-8.
85 typedef std::string StringType;
86 #elif defined(OS_WIN)
87 // On Windows, for Unicode-aware applications, native pathnames are wchar_t
88 // arrays encoded in UTF-16.
89 typedef std::wstring StringType;
90 #endif // OS_WIN
91
92 typedef StringType::value_type CharType;
93
94 // Null-terminated array of separators used to separate components in
95 // hierarchical paths. Each character in this array is a valid separator,
96 // but kSeparators[0] is treated as the canonical separator and will be used
97 // when composing pathnames.
98 static const CharType kSeparators[];
99
100 // A special path component meaning "this directory."
101 static const CharType kCurrentDirectory[];
102
103 // A special path component meaning "the parent directory."
104 static const CharType kParentDirectory[];
105
106 FilePath() {}
107 FilePath(const FilePath& that) : path_(that.path_) {}
108 explicit FilePath(const StringType& path) : path_(path) {}
109
110 FilePath& operator=(const FilePath& that) {
111 path_ = that.path_;
112 return *this;
113 }
114
115 const StringType& value() const { return path_; }
116
117 // Returns a FilePath corresponding to the directory containing the path
118 // named by this object, stripping away the file component. If this object
119 // only contains one component, returns a FilePath identifying
120 // kCurrentDirectory. If this object already refers to the root directory,
121 // returns a FilePath identifying the root directory.
122 FilePath DirName() const;
123
124 // Returns a FilePath corresponding to the last path component of this
125 // object, either a file or a directory. If this object already refers to
126 // the root directory, returns a FilePath identifying the root directory;
127 // this is the only situation in which BaseName will return an absolute path.
128 FilePath BaseName() const;
129
130 // Returns a FilePath by appending a separator and the supplied path
131 // component to this object's path. Append takes care to avoid adding
132 // excessive separators if this object's path already ends with a separator.
133 // If this object's path is kCurrentDirectory, a new FilePath corresponding
134 // only to |component| is returned. |component| must be a relative path;
135 // it is an error to pass an absolute path.
136 FilePath Append(const StringType& component) const;
137
138 // Returns true if this FilePath contains an absolute path. On Windows, an
139 // absolute path begins with either a drive letter specification followed by
140 // a separator character, or with two separator characters. On POSIX
141 // platforms, an absolute path begins with a separator character.
142 bool IsAbsolute() const;
143
144 private:
145 // If this FilePath contains a drive letter specification, returns the
146 // position of the last character of the drive letter specification,
147 // otherwise returns npos. This can only be true on Windows, when a pathname
148 // begins with a letter followed by a colon. On other platforms, this always
149 // returns npos.
150 StringType::size_type FindDriveLetter() const;
151
152 // Remove trailing separators from this object. If the path is absolute, it
153 // will never be stripped any more than to refer to the absolute root
154 // directory, so "////" will become "/", not "". A leading pair of
155 // separators is never stripped, to support alternate roots. This is used to
156 // support UNC paths on Windows.
157 void StripTrailingSeparators();
158
159 StringType path_;
160 };
161
162 // Macros for string literal initialization of FilePath::CharType[].
163 #if defined(OS_POSIX)
164 #define FILE_PATH_LITERAL(x) x
165 #elif defined(OS_WIN)
166 #define FILE_PATH_LITERAL(x) L ## x
167 #endif // OS_WIN
168
169 #endif // BASE_FILE_PATH_H_
OLDNEW
« no previous file with comments | « base/build/base_unittests.vcproj ('k') | base/file_path.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698