|
degate 0.1.1
|
00001 /* -*-c++-*- 00002 00003 This file is part of the IC reverse engineering tool degate. 00004 00005 Copyright 2008, 2009, 2010 by Martin Schobert 00006 00007 Degate is free software: you can redistribute it and/or modify 00008 it under the terms of the GNU General Public License as published by 00009 the Free Software Foundation, either version 3 of the License, or 00010 any later version. 00011 00012 Degate is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU General Public License for more details. 00016 00017 You should have received a copy of the GNU General Public License 00018 along with degate. If not, see <http://www.gnu.org/licenses/>. 00019 00020 */ 00021 00022 #ifndef __FILESYSTEM_H__ 00023 #define __FILESYSTEM_H__ 00024 00025 #include "globals.h" 00026 #include "degate_exceptions.h" 00027 00028 #include <stdexcept> 00029 #include <sstream> 00030 #include <list> 00031 #include <string> 00032 #include <sys/stat.h> 00033 #include <sys/types.h> 00034 00035 #include <boost/filesystem/path.hpp> 00036 00037 namespace degate{ 00038 00039 /** 00040 * Check if a path is a directory. 00041 * @return Returns true, if the path is a dirctory. 00042 * It returns false, if not or if the path doesn't exists. 00043 */ 00044 00045 bool is_directory(std::string const & path); 00046 00047 /** 00048 * Check if a path is a regular file. 00049 * @return Returns true, if the path is a regular file. 00050 * It returns false, if not or if the path doesn't exist. 00051 */ 00052 00053 bool is_file(std::string const & path); 00054 00055 /** 00056 * Check if a path is a symlink. 00057 * @return Returns true, if the path is a symlink. 00058 * It returns false, if not or if the path doesn't exist. 00059 */ 00060 00061 bool is_symlink(std::string const & path); 00062 00063 /** 00064 * Check if a file or directory exists. 00065 * @returns Returns true, if the file or directory exist. 00066 */ 00067 00068 bool file_exists(std::string const & path); 00069 00070 00071 /** 00072 * Get the base directory for file or directory. 00073 * It is no problem if you request the basedir for a symlink 00074 * on a directory, because the path is internally expanded 00075 * via get_realpath(), but only if the file exist. 00076 * If you use get_basedir() on a non existing path, the path 00077 * is treated as a path to a file. Then the file name part is 00078 * stripped. 00079 * @return Returns a string with the base directory. 00080 * @exception Throws an InvalidPathException if the path does not exists. 00081 * @see get_realpath() 00082 */ 00083 00084 std::string get_basedir(std::string const & path); 00085 00086 00087 /** 00088 * Get the canonicalized absolute pathname. 00089 * @return Returns the resolved path as an absolut path name. 00090 * @exception Throws an InvalidPathException if the path does not exists. 00091 */ 00092 00093 std::string get_realpath(std::string const& path); 00094 00095 00096 /** 00097 * Get a file suffix without the dot, e.g. "xml" from a file named "foobar.xml". 00098 * @return Returns the file suffix as a string. If the file has no suffix, an 00099 * empty string is returned. The suffix is returned as it is. There is no 00100 * lowercase conversion or somthing like that. 00101 */ 00102 00103 std::string get_file_suffix(std::string const& path); 00104 00105 00106 /** 00107 * Get filename part of a path. 00108 */ 00109 std::string get_filename_from_path(std::string const& path); 00110 00111 00112 /** 00113 * Get the basename of a file. That is the substring without the directory 00114 * part, the suffix and the dot. 00115 * @test FileSystemTest::test_get_basename() 00116 */ 00117 std::string get_basename(std::string const& path); 00118 00119 /** 00120 * Unlink a file. 00121 */ 00122 void remove_file(std::string const& filename); 00123 00124 /** 00125 * Unlink a directory with all files in it. 00126 * Because this function is only for degate. We make some sanity checks. 00127 */ 00128 void remove_directory(std::string const& path); 00129 00130 00131 /** 00132 * Create a directory. 00133 */ 00134 00135 void create_directory(std::string const& directory, mode_t mode = 0700); 00136 00137 /** 00138 * Create a temp directory. 00139 */ 00140 std::string create_temp_directory(); 00141 00142 /** 00143 * Create a temporary directory based on a path pattern specification. 00144 * The directory is created with mode 0700. 00145 * @param directory_pattern A pattern of the form e.g. "/tmp/temp.XXXXXXXXX". 00146 * @return Returns the path of the created directory. 00147 */ 00148 std::string create_temp_directory(std::string const & directory_pattern); 00149 00150 00151 /** 00152 * Generate a pattern within a basedir for temp files. 00153 * The pattern is in the form of e.g. "/tmp/temp.XXXXXXXX" that can be passed 00154 * to mkstemp(). Only the pattern is generated. 00155 */ 00156 00157 std::string generate_temp_file_pattern(std::string const & basedir); 00158 00159 /** 00160 * Read all entries from a directory, but not from subdirectories. 00161 * The path is expanded first with get_realpath(). 00162 * @param path The directory you want to read. 00163 * @param prefix_path Control whether you will get file names relative to \p path 00164 * or absolute file names. 00165 * @return Returns a list of strings containing files and subdirectory names. 00166 * The special directory entires ".." and "." are not in the list. 00167 * @see get_realpath() 00168 */ 00169 00170 std::list<std::string> read_directory(std::string const& path, bool prefix_path = false); 00171 00172 00173 /** 00174 * Join path specifications. 00175 * This is a helper function to create path names. E.g. if you call this 00176 * function with parameter "/etc" and "hosts", you will get a path 00177 * "/etc/hosts". You don't have to deal with path seperator in the first 00178 * or second parameter. 00179 */ 00180 00181 std::string join_pathes(std::string const& base_path, std::string const& extension_path); 00182 00183 00184 /** 00185 * Make a path specification relative to another. 00186 * 00187 */ 00188 00189 std::string get_relative_path(std::string const& path, 00190 std::string const& relative_to); 00191 00192 00193 00194 00195 /** 00196 * Strip the leading directory part from a path. 00197 * @param strip_from Is the path. 00198 * @param strip_what Is the base directory that should be removed. 00199 * @return Returnes a new path with the base directory stripped. 00200 */ 00201 boost::filesystem::path strip_path(boost::filesystem::path const& strip_from, 00202 boost::filesystem::path const& strip_what); 00203 00204 } 00205 00206 #endif
1.7.4