strusBase  0.17
fileio.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Patrick P. Frey
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
9 #ifndef _STRUS_BASE_FILE_IO_HPP_INCLUDED
10 #define _STRUS_BASE_FILE_IO_HPP_INCLUDED
11 #include <vector>
12 #include <string>
13 #include <cstdio>
14 
15 namespace strus
16 {
17 
22 int readFileSize( const std::string& filename, std::size_t& size);
23 
28 int readFile( const std::string& filename, std::string& res);
29 
33 int readStdin( std::string& res);
34 
39 int writeFile( const std::string& filename, const std::string& content);
40 
45 int appendFile( const std::string& filename, const std::string& content);
46 
51 int removeFile( const std::string& filename, bool fail_ifnofexist=false);
52 
57 int removeDir( const std::string& dirname, bool fail_ifnofexist=false);
58 
63 int removeDirRecursive( const std::string& dirname, bool fail_ifnofexist=false);
64 
69 int renameFile( const std::string& old_filename, const std::string& new_filename);
70 
75 int createDir( const std::string& dirname, bool fail_ifexist=true);
76 
80 int mkdirp( const std::string& dirname);
81 
85 int changeDir( const std::string& dirname);
86 
92 int readDirFiles( const std::string& path, const std::string& ext, std::vector<std::string>& res);
93 
98 int readDirSubDirs( const std::string& path, std::vector<std::string>& res);
99 
104 int readDirItems( const std::string& path, std::vector<std::string>& res);
105 
110 int expandFilePattern( const std::string& pathPattern, std::vector<std::string>& res);
111 
115 bool isFile( const std::string& path);
116 
120 bool isDir( const std::string& path);
121 
125 bool isRelativePath( const std::string& path);
126 
130 bool isAbsolutePath( const std::string& path);
131 
135 bool isExplicitPath( const std::string& path);
136 
141 bool isTextFile( const std::string& path);
142 
147 int getParentPath( const std::string& path, std::string& dest);
148 
154 int getAncestorPath( const std::string& path, int level, std::string& dest);
155 
160 int getFileName( const std::string& path, std::string& dest, bool withExtension=true);
161 
166 int getFileExtension( const std::string& path, std::string& ext);
167 
170 char dirSeparator();
171 
175 bool hasUpdirReference( const std::string& path);
176 
181 std::string joinFilePath( const std::string& parentpath, const std::string& childpath);
182 
189 int resolveUpdirReferences( std::string& path);
190 
191 }//namespace
192 #endif
193 
int renameFile(const std::string &old_filename, const std::string &new_filename)
Moves (renames) a file.
bool isFile(const std::string &path)
Check if a file system path points to a file.
int readFile(const std::string &filename, std::string &res)
Reads the complete content of a file to a string.
bool isExplicitPath(const std::string &path)
Check if a file path is explicit (relative path or absolute path)
int appendFile(const std::string &filename, const std::string &content)
Appends a string to a file creating the file if it does not exist.
int getAncestorPath(const std::string &path, int level, std::string &dest)
Get an ancestor (containing) path of a file or directory.
int readFileSize(const std::string &filename, std::size_t &size)
Evaluate the size of a file in bytes.
int createDir(const std::string &dirname, bool fail_ifexist=true)
Creates a directory if it does not exist yet.
int writeFile(const std::string &filename, const std::string &content)
Writes a string to a file creating the file if it does not exist.
int removeDir(const std::string &dirname, bool fail_ifnofexist=false)
Removes an empty directory.
int resolveUpdirReferences(std::string &path)
Resolve upper directory references in a path, e.g. replace "/home/john/../jack" by "/home/jack"...
char dirSeparator()
Get the OS path element separator.
bool isRelativePath(const std::string &path)
Check if a file system path is a path starting with a name and not with a directive referencing the c...
bool isTextFile(const std::string &path)
Check if a file system path points to a file with text content (not binary)
int readDirSubDirs(const std::string &path, std::vector< std::string > &res)
Reads all subdirectories of a directory to an array of strings.
int readStdin(std::string &res)
Reads the complete input of stdin to a string.
bool isAbsolutePath(const std::string &path)
Check if a file system path is a path starting the file system root '/'.
int expandFilePattern(const std::string &pathPattern, std::vector< std::string > &res)
Get all items matching a file path pattern (with '?' and '*' as substitutes)
int readDirFiles(const std::string &path, const std::string &ext, std::vector< std::string > &res)
Reads all file names matching to the extension ext of a directory to an array of strings.
int getFileName(const std::string &path, std::string &dest, bool withExtension=true)
Get the file name without parent path of a file or directory.
bool isDir(const std::string &path)
Check if a file system path points to a directory.
bool hasUpdirReference(const std::string &path)
Return true, if the path contains an upper directory reference '..'.
int removeDirRecursive(const std::string &dirname, bool fail_ifnofexist=false)
Removes a directory with all its contents.
int removeFile(const std::string &filename, bool fail_ifnofexist=false)
Removes a file.
int changeDir(const std::string &dirname)
Change current directory of the process.
int getFileExtension(const std::string &path, std::string &ext)
Get the extension (including '.') of a file or directory.
int readDirItems(const std::string &path, std::vector< std::string > &res)
Reads all items (directory or file) of a directory to an array of strings.
int mkdirp(const std::string &dirname)
Creates a directory path (mkdir -p)
int getParentPath(const std::string &path, std::string &dest)
Get the parent (containing) path of a file or directory.
std::string joinFilePath(const std::string &parentpath, const std::string &childpath)
Join two path with directory separator.