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. 4 5#ifndef TOOLS_GN_FILESYSTEM_UTILS_H_ 6#define TOOLS_GN_FILESYSTEM_UTILS_H_ 7 8#include <string> 9 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" 14 15class Err; 16class Location; 17class Value; 18 19enum SourceFileType { 20 SOURCE_UNKNOWN, 21 SOURCE_ASM, 22 SOURCE_C, 23 SOURCE_CC, 24 SOURCE_H, 25 SOURCE_M, 26 SOURCE_MM, 27 //SOURCE_S, // TODO(brettw) what is this? 28 SOURCE_RC, 29}; 30 31SourceFileType GetSourceFileType(const SourceFile& file, 32 Settings::TargetOS os); 33 34// Returns the extension, not including the dot, for the given file type on the 35// given system. 36// 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). 40const char* GetExtensionForOutputType(Target::OutputType type, 41 Settings::TargetOS os); 42 43std::string FilePathToUTF8(const base::FilePath& path); 44base::FilePath UTF8ToFilePath(const base::StringPiece& sp); 45 46// Extensions ----------------------------------------------------------------- 47 48// Returns the index of the extension (character after the last dot not after a 49// slash). Returns std::string::npos if not found. Returns path.size() if the 50// file ends with a dot. 51size_t FindExtensionOffset(const std::string& path); 52 53// Returns a string piece pointing into the input string identifying the 54// extension. Note that the input pointer must outlive the output. 55base::StringPiece FindExtension(const std::string* path); 56 57// Filename parts ------------------------------------------------------------- 58 59// Returns the offset of the character following the last slash, or 60// 0 if no slash was found. Returns path.size() if the path ends with a slash. 61// Note that the input pointer must outlive the output. 62size_t FindFilenameOffset(const std::string& path); 63 64// Returns a string piece pointing into the input string identifying the 65// file name (following the last slash, including the extension). Note that the 66// input pointer must outlive the output. 67base::StringPiece FindFilename(const std::string* path); 68 69// Like FindFilename but does not include the extension. 70base::StringPiece FindFilenameNoExtension(const std::string* path); 71 72// Removes everything after the last slash. The last slash, if any, will be 73// preserved. 74void RemoveFilename(std::string* path); 75 76// Returns true if the given path ends with a slash. 77bool EndsWithSlash(const std::string& s); 78 79// Path parts ----------------------------------------------------------------- 80 81// Returns a string piece pointing into the input string identifying the 82// directory name of the given path, including the last slash. Note that the 83// input pointer must outlive the output. 84base::StringPiece FindDir(const std::string* path); 85 86// Verifies that the given string references a file inside of the given 87// directory. This is pretty stupid and doesn't handle "." and "..", etc., 88// it is designed for a sanity check to keep people from writing output files 89// to the source directory accidentally. 90// 91// The originating value will be blamed in the error. 92// 93// If the file isn't in the dir, returns false and sets the error. Otherwise 94// returns true and leaves the error untouched. 95bool EnsureStringIsInOutputDir(const SourceDir& dir, 96 const std::string& str, 97 const Value& originating, 98 Err* err); 99 100// ---------------------------------------------------------------------------- 101 102// Converts a directory to its inverse (e.g. "/foo/bar/" -> "../../"). 103// This will be the empty string for the root directories ("/" and "//"), and 104// in all other cases, this is guaranteed to end in a slash. 105std::string InvertDir(const SourceDir& dir); 106 107// Collapses "." and sequential "/"s and evaluates "..". 108void NormalizePath(std::string* path); 109 110// Converts slashes to backslashes for Windows. Keeps the string unchanged 111// for other systems. 112void ConvertPathToSystem(std::string* path); 113std::string PathToSystem(const std::string& path); 114 115#endif // TOOLS_GN_FILESYSTEM_UTILS_H_ 116