OpenASIP  2.0
FileSystem.hh
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file FileSystem.hh
26  *
27  * Declaration of FileSystem class.
28  *
29  * @author Pekka Jääskeläinen 2005 (pekka.jaaskelainen-no.spam-tut.fi)
30  * @author Viljami Korhonen 2007 (viljami.korhonen-no.spam-tut.fi)
31  * @author Esa Määttä 2007 (esa.maatta-no.spam-tut.fi)
32  * @note rating: red
33  */
34 
35 #ifndef TTA_FILESYSTEM_HH
36 #define TTA_FILESYSTEM_HH
37 
38 #include <string>
39 #include <vector>
40 
41 #include <boost/version.hpp>
42 
43 #include "TCEString.hh"
44 
45 /*
46  * Useful to know in case you need to adjust this threshold:
47  *
48  * Boost 1.44 is the oldest version of Boost that ships
49  * boost filesystem v3. 1.49 seems to be the newest that
50  * ships v2.
51  *
52  * Also note that value of BOOST_FILESYSTEM_VERSION
53  * is currently #ifdef'd against in FileSystem.{cc,hh,icc}
54  * files, to support using either of the Boost filesystem
55  * APIs.
56  */
57 #if BOOST_VERSION < 104400
58 #define BOOST_FILESYSTEM_VERSION 2
59 #else
60 #define BOOST_FILESYSTEM_VERSION 3
61 #endif
62 
63 // Needs to be declared if C++11 standard is used with boost/filesystem,
64 // otherwise linker errors occur.
65 #if BOOST_VERSION <= 105100
66 #define BOOST_NO_SCOPED_ENUMS
67 #else
68 #define BOOST_NO_CXX11_SCOPED_ENUMS
69 #endif
70 
71 #include <boost/filesystem/path.hpp>
72 
73 #include <unistd.h> // access()
74 
75 #include "Application.hh"
76 #include "Exception.hh"
77 
78 
79 class Path;
80 
81 
82 /**
83  * Portability layer for platform-dependent functions that access the
84  * filesystem.
85  */
86 class FileSystem {
87 public:
88 
89  // file tests
90  static bool fileExists(const std::string fileName);
91  static bool fileIsWritable(const std::string fileName);
92  static bool fileIsReadable(const std::string fileName);
93  static bool fileIsExecutable(const std::string fileName);
94  static bool fileIsCreatable(const std::string fileName);
95  static bool fileIsDirectory(const std::string fileName);
96 
97  // path name string parsing functions
98  static std::string directoryOfPath(const std::string fileName);
99  static std::string fileOfPath(const std::string pathName);
100  static bool isAbsolutePath(const std::string& pathName);
101  static bool isRelativePath(const std::string& pathName);
102  static bool isPath(const std::string& pathName);
103  static std::string fileExtension(const std::string& fileName);
104  static std::string fileNameBody(const std::string& fileName);
105  static std::string absolutePathOf(const std::string& pathName);
106 
107  // file system environment functions
108  static std::string currentWorkingDir();
109  static bool changeWorkingDir(const std::string& path);
110  static std::string homeDirectory();
111 
112  // file permission changing functions
113  static bool setFileExecutable(const std::string fileName);
114 
115  // file detail functions
116  static std::time_t lastModificationTime(const std::string& filePath);
117  static uintmax_t sizeInBytes(const std::string& filePath);
118 
119  // should this be moved to Application instead?
120  static bool runShellCommand(const std::string command);
121 
122  // path name string expansion functions
123  static void globPath(const std::string& pattern,
124  std::vector<std::string>& filenames);
125  static std::string expandTilde(const std::string& stringWithTilde);
126 
127  static bool createDirectory(const std::string& path);
128  static std::string createTempDirectory(
129  const std::string& path="/tmp",
130  const std::string& tempDirPrefix = "tmp_tce_");
131  static bool createFile(const std::string& file);
132  static bool removeFileOrDirectory(const std::string& path);
133 
134  static void copy(const std::string& source, const std::string& target);
135 
136  static std::string findFileInSearchPaths(
137  const std::vector<std::string>& searchPaths, const std::string& file);
138 
139  static std::vector<std::string> directoryContents(
140  const std::string& directory, const bool absolutePaths = true);
141 
142  static std::vector<std::string> directorySubTrees(
143  const std::string& directory);
144 
145  static bool findFileInDirectoryTree(
146  const Path& startDirectory,
147  const std::string& fileName,
148  Path& pathFound);
149 
150  template <typename STLCONT>
151  static bool findFromDirectory(
152  const std::string& regex, const std::string& directory, STLCONT& found);
153 
154  template <typename STLCONT>
155  static bool findFromDirectoryRecursive(
156  const std::string& regex,
157  const std::string& directory,
158  STLCONT& found);
159 
160  static bool compareFileNames(
161  const std::string& first,
162  const std::string& second,
163  const std::string& rootDirectory);
164 
165  static bool relativeDir(
166  const std::string& baseDir,
167  std::string& toRelDir);
168 
169  static bool makeRelativePath(
170  const std::vector<std::string>& searchPaths,
171  const std::string& basePath,
172  std::string& toRelPath);
173 
174  static bool readBlockFromFile(
175  const std::string& sourceFile,
176  const std::string& blockStartRE,
177  const std::string& blockEndRE,
178  std::string& readBlock,
179  const bool includeMatchingLines = true);
180 
181  static bool appendReplaceFile(
182  const std::string& targetFile,
183  const std::string& ARStartRE,
184  const std::string& writeToFile,
185  const std::string& AREndRE = "",
186  const bool discardBlockBorder = "true");
187  static int countLines(const std::string& filepath);
188 
189  static const std::string DIRECTORY_SEPARATOR;
190  static const std::string CURRENT_DIRECTORY;
191  static const std::string STRING_WILD_CARD;
192 };
193 
194 /**
195  * Class for handling paths.
196  */
197 class Path : public boost::filesystem::path {
198 public:
199  Path();
200  explicit Path(const boost::filesystem::path& path);
201  Path& operator=(const boost::filesystem::path& pathName);
202  operator std::string() const;
203  operator TCEString() const;
204  const char* c_str() const;
205  virtual ~Path();
206 };
207 
208 Path operator/(const Path& path, const std::string& fileOrDir);
209 
210 #include "FileSystem.icc"
211 
212 #endif
Path
Definition: FileSystem.hh:197
operator/
Path operator/(const Path &path, const std::string &fileOrDir)
Definition: FileSystem.cc:1135
FileSystem::removeFileOrDirectory
static bool removeFileOrDirectory(const std::string &path)
Definition: FileSystem.cc:493
FileSystem::createDirectory
static bool createDirectory(const std::string &path)
Definition: FileSystem.cc:400
FileSystem::setFileExecutable
static bool setFileExecutable(const std::string fileName)
Definition: FileSystem.cc:322
Exception.hh
FileSystem::isRelativePath
static bool isRelativePath(const std::string &pathName)
Definition: FileSystem.cc:252
FileSystem::directorySubTrees
static std::vector< std::string > directorySubTrees(const std::string &directory)
Definition: FileSystem.cc:647
FileSystem::globPath
static void globPath(const std::string &pattern, std::vector< std::string > &filenames)
Definition: FileSystem.cc:197
FileSystem::findFromDirectory
static bool findFromDirectory(const std::string &regex, const std::string &directory, STLCONT &found)
FileSystem::absolutePathOf
static std::string absolutePathOf(const std::string &pathName)
Definition: FileSystem.cc:303
FileSystem::relativeDir
static bool relativeDir(const std::string &baseDir, std::string &toRelDir)
Definition: FileSystem.cc:762
FileSystem::fileOfPath
static std::string fileOfPath(const std::string pathName)
Definition: FileSystem.cc:101
Path::~Path
virtual ~Path()
Definition: FileSystem.cc:1093
FileSystem::fileIsCreatable
static bool fileIsCreatable(const std::string fileName)
Definition: FileSystem.cc:123
TCEString.hh
Path::c_str
const char * c_str() const
Definition: FileSystem.cc:1128
FileSystem::makeRelativePath
static bool makeRelativePath(const std::vector< std::string > &searchPaths, const std::string &basePath, std::string &toRelPath)
Definition: FileSystem.cc:844
FileSystem::fileIsDirectory
static bool fileIsDirectory(const std::string fileName)
FileSystem::homeDirectory
static std::string homeDirectory()
FileSystem::fileIsWritable
static bool fileIsWritable(const std::string fileName)
Path::Path
Path()
Definition: FileSystem.cc:1080
FileSystem::lastModificationTime
static std::time_t lastModificationTime(const std::string &filePath)
Definition: FileSystem.cc:352
FileSystem::fileExtension
static std::string fileExtension(const std::string &fileName)
Definition: FileSystem.cc:279
Application.hh
FileSystem::findFromDirectoryRecursive
static bool findFromDirectoryRecursive(const std::string &regex, const std::string &directory, STLCONT &found)
FileSystem::directoryOfPath
static std::string directoryOfPath(const std::string fileName)
Definition: FileSystem.cc:79
FileSystem::copy
static void copy(const std::string &source, const std::string &target)
Definition: FileSystem.cc:524
FileSystem::isPath
static bool isPath(const std::string &pathName)
Definition: FileSystem.cc:265
FileSystem::expandTilde
static std::string expandTilde(const std::string &stringWithTilde)
Definition: FileSystem.cc:217
FileSystem::sizeInBytes
static uintmax_t sizeInBytes(const std::string &filePath)
Definition: FileSystem.cc:376
FileSystem::createFile
static bool createFile(const std::string &file)
Definition: FileSystem.cc:468
FileSystem
Definition: FileSystem.hh:86
FileSystem::readBlockFromFile
static bool readBlockFromFile(const std::string &sourceFile, const std::string &blockStartRE, const std::string &blockEndRE, std::string &readBlock, const bool includeMatchingLines=true)
Definition: FileSystem.cc:894
FileSystem::DIRECTORY_SEPARATOR
static const std::string DIRECTORY_SEPARATOR
Definition: FileSystem.hh:189
FileSystem.icc
FileSystem::CURRENT_DIRECTORY
static const std::string CURRENT_DIRECTORY
Definition: FileSystem.hh:190
FileSystem::changeWorkingDir
static bool changeWorkingDir(const std::string &path)
Definition: FileSystem.cc:185
Path::operator=
Path & operator=(const boost::filesystem::path &pathName)
Definition: FileSystem.cc:1100
FileSystem::countLines
static int countLines(const std::string &filepath)
Definition: FileSystem.cc:1060
FileSystem::fileExists
static bool fileExists(const std::string fileName)
FileSystem::appendReplaceFile
static bool appendReplaceFile(const std::string &targetFile, const std::string &ARStartRE, const std::string &writeToFile, const std::string &AREndRE="", const bool discardBlockBorder="true")
Definition: FileSystem.cc:958
TCEString
Definition: TCEString.hh:53
FileSystem::fileIsExecutable
static bool fileIsExecutable(const std::string fileName)
FileSystem::currentWorkingDir
static std::string currentWorkingDir()
Definition: FileSystem.cc:142
FileSystem::STRING_WILD_CARD
static const std::string STRING_WILD_CARD
Definition: FileSystem.hh:191
FileSystem::runShellCommand
static bool runShellCommand(const std::string command)
FileSystem::fileIsReadable
static bool fileIsReadable(const std::string fileName)
FileSystem::fileNameBody
static std::string fileNameBody(const std::string &fileName)
Definition: FileSystem.cc:291
FileSystem::findFileInSearchPaths
static std::string findFileInSearchPaths(const std::vector< std::string > &searchPaths, const std::string &file)
Definition: FileSystem.cc:562
FileSystem::compareFileNames
static bool compareFileNames(const std::string &first, const std::string &second, const std::string &rootDirectory)
Definition: FileSystem.cc:724
FileSystem::isAbsolutePath
static bool isAbsolutePath(const std::string &pathName)
Definition: FileSystem.cc:234
FileSystem::createTempDirectory
static std::string createTempDirectory(const std::string &path="/tmp", const std::string &tempDirPrefix="tmp_tce_")
Definition: FileSystem.cc:441
FileSystem::directoryContents
static std::vector< std::string > directoryContents(const std::string &directory, const bool absolutePaths=true)
Definition: FileSystem.cc:600
FileSystem::findFileInDirectoryTree
static bool findFileInDirectoryTree(const Path &startDirectory, const std::string &fileName, Path &pathFound)
Definition: FileSystem.cc:682