OpenASIP 2.2
Loading...
Searching...
No Matches
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
79class Path;
80
81
82/**
83 * Portability layer for platform-dependent functions that access the
84 * filesystem.
85 */
87public:
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>
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 */
197class Path : public boost::filesystem::path {
198public:
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
208Path operator/(const Path& path, const std::string& fileOrDir);
209
210#include "FileSystem.icc"
211
212#endif
Path operator/(const Path &path, const std::string &fileOrDir)
static std::string homeDirectory()
static bool createFile(const std::string &file)
static bool isRelativePath(const std::string &pathName)
static bool findFileInDirectoryTree(const Path &startDirectory, const std::string &fileName, Path &pathFound)
static std::string absolutePathOf(const std::string &pathName)
static bool isAbsolutePath(const std::string &pathName)
static bool readBlockFromFile(const std::string &sourceFile, const std::string &blockStartRE, const std::string &blockEndRE, std::string &readBlock, const bool includeMatchingLines=true)
static bool appendReplaceFile(const std::string &targetFile, const std::string &ARStartRE, const std::string &writeToFile, const std::string &AREndRE="", const bool discardBlockBorder="true")
static bool runShellCommand(const std::string command)
static void globPath(const std::string &pattern, std::vector< std::string > &filenames)
static bool fileIsReadable(const std::string fileName)
static bool fileIsExecutable(const std::string fileName)
static bool createDirectory(const std::string &path)
static uintmax_t sizeInBytes(const std::string &filePath)
static bool removeFileOrDirectory(const std::string &path)
static bool changeWorkingDir(const std::string &path)
static const std::string DIRECTORY_SEPARATOR
static bool findFromDirectory(const std::string &regex, const std::string &directory, STLCONT &found)
static std::time_t lastModificationTime(const std::string &filePath)
static std::string createTempDirectory(const std::string &path="/tmp", const std::string &tempDirPrefix="tmp_tce_")
static std::string fileNameBody(const std::string &fileName)
static std::vector< std::string > directoryContents(const std::string &directory, const bool absolutePaths=true)
static std::string fileOfPath(const std::string pathName)
static bool compareFileNames(const std::string &first, const std::string &second, const std::string &rootDirectory)
static const std::string CURRENT_DIRECTORY
static void copy(const std::string &source, const std::string &target)
static std::string directoryOfPath(const std::string fileName)
Definition FileSystem.cc:79
static std::string fileExtension(const std::string &fileName)
static std::string currentWorkingDir()
static std::string findFileInSearchPaths(const std::vector< std::string > &searchPaths, const std::string &file)
static const std::string STRING_WILD_CARD
static bool fileIsDirectory(const std::string fileName)
static bool setFileExecutable(const std::string fileName)
static bool relativeDir(const std::string &baseDir, std::string &toRelDir)
static bool findFromDirectoryRecursive(const std::string &regex, const std::string &directory, STLCONT &found)
static std::vector< std::string > directorySubTrees(const std::string &directory)
static bool makeRelativePath(const std::vector< std::string > &searchPaths, const std::string &basePath, std::string &toRelPath)
static bool fileIsWritable(const std::string fileName)
static bool isPath(const std::string &pathName)
static int countLines(const std::string &filepath)
static bool fileIsCreatable(const std::string fileName)
static bool fileExists(const std::string fileName)
static std::string expandTilde(const std::string &stringWithTilde)
virtual ~Path()
Path & operator=(const boost::filesystem::path &pathName)
const char * c_str() const