Qucs-core  0.0.19
gtest-filepath.h
Go to the documentation of this file.
00001 // Copyright 2008, Google Inc.
00002 // All rights reserved.
00003 //
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are
00006 // met:
00007 //
00008 //     * Redistributions of source code must retain the above copyright
00009 // notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above
00011 // copyright notice, this list of conditions and the following disclaimer
00012 // in the documentation and/or other materials provided with the
00013 // distribution.
00014 //     * Neither the name of Google Inc. nor the names of its
00015 // contributors may be used to endorse or promote products derived from
00016 // this software without specific prior written permission.
00017 //
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // Author: keith.ray@gmail.com (Keith Ray)
00031 //
00032 // Google Test filepath utilities
00033 //
00034 // This header file declares classes and functions used internally by
00035 // Google Test.  They are subject to change without notice.
00036 //
00037 // This file is #included in <gtest/internal/gtest-internal.h>.
00038 // Do not include this header file separately!
00039 
00040 #ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_
00041 #define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_
00042 
00043 #include "gtest/internal/gtest-string.h"
00044 
00045 namespace testing {
00046 namespace internal {
00047 
00048 // FilePath - a class for file and directory pathname manipulation which
00049 // handles platform-specific conventions (like the pathname separator).
00050 // Used for helper functions for naming files in a directory for xml output.
00051 // Except for Set methods, all methods are const or static, which provides an
00052 // "immutable value object" -- useful for peace of mind.
00053 // A FilePath with a value ending in a path separator ("like/this/") represents
00054 // a directory, otherwise it is assumed to represent a file. In either case,
00055 // it may or may not represent an actual file or directory in the file system.
00056 // Names are NOT checked for syntax correctness -- no checking for illegal
00057 // characters, malformed paths, etc.
00058 
00059 class GTEST_API_ FilePath {
00060  public:
00061   FilePath() : pathname_("") { }
00062   FilePath(const FilePath& rhs) : pathname_(rhs.pathname_) { }
00063 
00064   explicit FilePath(const std::string& pathname) : pathname_(pathname) {
00065     Normalize();
00066   }
00067 
00068   FilePath& operator=(const FilePath& rhs) {
00069     Set(rhs);
00070     return *this;
00071   }
00072 
00073   void Set(const FilePath& rhs) {
00074     pathname_ = rhs.pathname_;
00075   }
00076 
00077   const std::string& string() const { return pathname_; }
00078   const char* c_str() const { return pathname_.c_str(); }
00079 
00080   // Returns the current working directory, or "" if unsuccessful.
00081   static FilePath GetCurrentDir();
00082 
00083   // Given directory = "dir", base_name = "test", number = 0,
00084   // extension = "xml", returns "dir/test.xml". If number is greater
00085   // than zero (e.g., 12), returns "dir/test_12.xml".
00086   // On Windows platform, uses \ as the separator rather than /.
00087   static FilePath MakeFileName(const FilePath& directory,
00088                                const FilePath& base_name,
00089                                int number,
00090                                const char* extension);
00091 
00092   // Given directory = "dir", relative_path = "test.xml",
00093   // returns "dir/test.xml".
00094   // On Windows, uses \ as the separator rather than /.
00095   static FilePath ConcatPaths(const FilePath& directory,
00096                               const FilePath& relative_path);
00097 
00098   // Returns a pathname for a file that does not currently exist. The pathname
00099   // will be directory/base_name.extension or
00100   // directory/base_name_<number>.extension if directory/base_name.extension
00101   // already exists. The number will be incremented until a pathname is found
00102   // that does not already exist.
00103   // Examples: 'dir/foo_test.xml' or 'dir/foo_test_1.xml'.
00104   // There could be a race condition if two or more processes are calling this
00105   // function at the same time -- they could both pick the same filename.
00106   static FilePath GenerateUniqueFileName(const FilePath& directory,
00107                                          const FilePath& base_name,
00108                                          const char* extension);
00109 
00110   // Returns true iff the path is "".
00111   bool IsEmpty() const { return pathname_.empty(); }
00112 
00113   // If input name has a trailing separator character, removes it and returns
00114   // the name, otherwise return the name string unmodified.
00115   // On Windows platform, uses \ as the separator, other platforms use /.
00116   FilePath RemoveTrailingPathSeparator() const;
00117 
00118   // Returns a copy of the FilePath with the directory part removed.
00119   // Example: FilePath("path/to/file").RemoveDirectoryName() returns
00120   // FilePath("file"). If there is no directory part ("just_a_file"), it returns
00121   // the FilePath unmodified. If there is no file part ("just_a_dir/") it
00122   // returns an empty FilePath ("").
00123   // On Windows platform, '\' is the path separator, otherwise it is '/'.
00124   FilePath RemoveDirectoryName() const;
00125 
00126   // RemoveFileName returns the directory path with the filename removed.
00127   // Example: FilePath("path/to/file").RemoveFileName() returns "path/to/".
00128   // If the FilePath is "a_file" or "/a_file", RemoveFileName returns
00129   // FilePath("./") or, on Windows, FilePath(".\\"). If the filepath does
00130   // not have a file, like "just/a/dir/", it returns the FilePath unmodified.
00131   // On Windows platform, '\' is the path separator, otherwise it is '/'.
00132   FilePath RemoveFileName() const;
00133 
00134   // Returns a copy of the FilePath with the case-insensitive extension removed.
00135   // Example: FilePath("dir/file.exe").RemoveExtension("EXE") returns
00136   // FilePath("dir/file"). If a case-insensitive extension is not
00137   // found, returns a copy of the original FilePath.
00138   FilePath RemoveExtension(const char* extension) const;
00139 
00140   // Creates directories so that path exists. Returns true if successful or if
00141   // the directories already exist; returns false if unable to create
00142   // directories for any reason. Will also return false if the FilePath does
00143   // not represent a directory (that is, it doesn't end with a path separator).
00144   bool CreateDirectoriesRecursively() const;
00145 
00146   // Create the directory so that path exists. Returns true if successful or
00147   // if the directory already exists; returns false if unable to create the
00148   // directory for any reason, including if the parent directory does not
00149   // exist. Not named "CreateDirectory" because that's a macro on Windows.
00150   bool CreateFolder() const;
00151 
00152   // Returns true if FilePath describes something in the file-system,
00153   // either a file, directory, or whatever, and that something exists.
00154   bool FileOrDirectoryExists() const;
00155 
00156   // Returns true if pathname describes a directory in the file-system
00157   // that exists.
00158   bool DirectoryExists() const;
00159 
00160   // Returns true if FilePath ends with a path separator, which indicates that
00161   // it is intended to represent a directory. Returns false otherwise.
00162   // This does NOT check that a directory (or file) actually exists.
00163   bool IsDirectory() const;
00164 
00165   // Returns true if pathname describes a root directory. (Windows has one
00166   // root directory per disk drive.)
00167   bool IsRootDirectory() const;
00168 
00169   // Returns true if pathname describes an absolute path.
00170   bool IsAbsolutePath() const;
00171 
00172  private:
00173   // Replaces multiple consecutive separators with a single separator.
00174   // For example, "bar///foo" becomes "bar/foo". Does not eliminate other
00175   // redundancies that might be in a pathname involving "." or "..".
00176   //
00177   // A pathname with multiple consecutive separators may occur either through
00178   // user error or as a result of some scripts or APIs that generate a pathname
00179   // with a trailing separator. On other platforms the same API or script
00180   // may NOT generate a pathname with a trailing "/". Then elsewhere that
00181   // pathname may have another "/" and pathname components added to it,
00182   // without checking for the separator already being there.
00183   // The script language and operating system may allow paths like "foo//bar"
00184   // but some of the functions in FilePath will not handle that correctly. In
00185   // particular, RemoveTrailingPathSeparator() only removes one separator, and
00186   // it is called in CreateDirectoriesRecursively() assuming that it will change
00187   // a pathname from directory syntax (trailing separator) to filename syntax.
00188   //
00189   // On Windows this method also replaces the alternate path separator '/' with
00190   // the primary path separator '\\', so that for example "bar\\/\\foo" becomes
00191   // "bar\\foo".
00192 
00193   void Normalize();
00194 
00195   // Returns a pointer to the last occurence of a valid path separator in
00196   // the FilePath. On Windows, for example, both '/' and '\' are valid path
00197   // separators. Returns NULL if no path separator was found.
00198   const char* FindLastPathSeparator() const;
00199 
00200   std::string pathname_;
00201 };  // class FilePath
00202 
00203 }  // namespace internal
00204 }  // namespace testing
00205 
00206 #endif  // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_