1 // Copyright (c) 2013 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.
5 #ifndef TOOLS_GN_FILESYSTEM_UTILS_H_
6 #define TOOLS_GN_FILESYSTEM_UTILS_H_
10 #include "base/files/file_path.h"
11 #include "base/strings/string_piece.h"
12 #include "tools/gn/settings.h"
13 #include "tools/gn/target.h"
31 SourceFileType
GetSourceFileType(const SourceFile
& file
,
32 Settings::TargetOS os
);
34 // Returns the extension, not including the dot, for the given file type on the
37 // Some targets make multiple files (like a .dll and an import library). This
38 // function returns the name of the file other targets should depend on and
39 // link to (so in this example, the import library).
40 const char* GetExtensionForOutputType(Target::OutputType type
,
41 Settings::TargetOS os
);
43 std::string
FilePathToUTF8(const base::FilePath::StringType
& str
);
44 inline std::string
FilePathToUTF8(const base::FilePath
& path
) {
45 return FilePathToUTF8(path
.value());
47 base::FilePath
UTF8ToFilePath(const base::StringPiece
& sp
);
49 // Extensions -----------------------------------------------------------------
51 // Returns the index of the extension (character after the last dot not after a
52 // slash). Returns std::string::npos if not found. Returns path.size() if the
53 // file ends with a dot.
54 size_t FindExtensionOffset(const std::string
& path
);
56 // Returns a string piece pointing into the input string identifying the
57 // extension. Note that the input pointer must outlive the output.
58 base::StringPiece
FindExtension(const std::string
* path
);
60 // Filename parts -------------------------------------------------------------
62 // Returns the offset of the character following the last slash, or
63 // 0 if no slash was found. Returns path.size() if the path ends with a slash.
64 // Note that the input pointer must outlive the output.
65 size_t FindFilenameOffset(const std::string
& path
);
67 // Returns a string piece pointing into the input string identifying the
68 // file name (following the last slash, including the extension). Note that the
69 // input pointer must outlive the output.
70 base::StringPiece
FindFilename(const std::string
* path
);
72 // Like FindFilename but does not include the extension.
73 base::StringPiece
FindFilenameNoExtension(const std::string
* path
);
75 // Removes everything after the last slash. The last slash, if any, will be
77 void RemoveFilename(std::string
* path
);
79 // Returns true if the given path ends with a slash.
80 bool EndsWithSlash(const std::string
& s
);
82 // Path parts -----------------------------------------------------------------
84 // Returns a string piece pointing into the input string identifying the
85 // directory name of the given path, including the last slash. Note that the
86 // input pointer must outlive the output.
87 base::StringPiece
FindDir(const std::string
* path
);
89 // Verifies that the given string references a file inside of the given
90 // directory. This is pretty stupid and doesn't handle "." and "..", etc.,
91 // it is designed for a sanity check to keep people from writing output files
92 // to the source directory accidentally.
94 // The originating value will be blamed in the error.
96 // If the file isn't in the dir, returns false and sets the error. Otherwise
97 // returns true and leaves the error untouched.
98 bool EnsureStringIsInOutputDir(const SourceDir
& dir
,
99 const std::string
& str
,
100 const Value
& originating
,
103 // ----------------------------------------------------------------------------
105 // Returns true if the input string is absolute. Double-slashes at the
106 // beginning are treated as source-relative paths. On Windows, this handles
107 // paths of both the native format: "C:/foo" and ours "/C:/foo"
108 bool IsPathAbsolute(const base::StringPiece
& path
);
110 // Given an absolute path, checks to see if is it is inside the source root.
111 // If it is, fills a source-absolute path into the given output and returns
112 // true. If it isn't, clears the dest and returns false.
114 // The source_root should be a base::FilePath converted to UTF-8. On Windows,
115 // it should begin with a "C:/" rather than being our SourceFile's style
116 // ("/C:/"). The source root can end with a slash or not.
118 // Note that this does not attempt to normalize slashes in the output.
119 bool MakeAbsolutePathRelativeIfPossible(const base::StringPiece
& source_root
,
120 const base::StringPiece
& path
,
123 // Converts a directory to its inverse (e.g. "/foo/bar/" -> "../../").
124 // This will be the empty string for the root directories ("/" and "//"), and
125 // in all other cases, this is guaranteed to end in a slash.
126 std::string
InvertDir(const SourceDir
& dir
);
128 // Collapses "." and sequential "/"s and evaluates "..".
129 void NormalizePath(std::string
* path
);
131 // Converts slashes to backslashes for Windows. Keeps the string unchanged
132 // for other systems.
133 void ConvertPathToSystem(std::string
* path
);
134 std::string
PathToSystem(const std::string
& path
);
136 // Takes a source-absolute path (must begin with "//") and makes it relative
137 // to the given directory, which also must be source-absolute.
138 std::string
RebaseSourceAbsolutePath(const std::string
& input
,
139 const SourceDir
& dest_dir
);
141 #endif // TOOLS_GN_FILESYSTEM_UTILS_H_