]> Dogcows Code - chaz/yoink/blob - src/stlplus/portability/file_system.hpp
cleanup stlplus files
[chaz/yoink] / src / stlplus / portability / file_system.hpp
1 #ifndef STLPLUS_FILE_SYSTEM
2 #define STLPLUS_FILE_SYSTEM
3 ////////////////////////////////////////////////////////////////////////////////
4
5 // Author: Andy Rushton
6 // Copyright: (c) Southampton University 1999-2004
7 // (c) Andy Rushton 2004-2009
8 // License: BSD License, see ../docs/license.html
9
10 // Simplified access to the File system
11
12 // All file system access and filename manipulation should be done
13 // with this package. Then it is only necessary to port this package
14 // to port all file handling.
15
16 ////////////////////////////////////////////////////////////////////////////////
17 #include "portability_fixes.hpp"
18 #include <string>
19 #include <vector>
20 #include <time.h>
21 ////////////////////////////////////////////////////////////////////////////////
22
23 namespace stlplus
24 {
25
26 ////////////////////////////////////////////////////////////////////////////////
27 // implement string comparison of paths - Unix is case-sensitive, Windows is case-insensitive
28
29 bool path_compare(const std::string& l, const std::string& r);
30
31 ////////////////////////////////////////////////////////////////////////////////
32 // classifying functions
33
34 // test for whether there's something (i.e. folder or file) with this name
35 bool is_present(const std::string& thing);
36 // test for whether there's something present and its a folder
37 bool is_folder(const std::string& thing);
38 // test for whether there's something present and its a file
39 bool is_file(const std::string& thing);
40
41 ////////////////////////////////////////////////////////////////////////////////
42 // file functions
43
44 // tests whether there's a file of this name
45 bool file_exists(const std::string& filespec);
46 // tests whether the file is readable - i.e. exists and has read mode set
47 bool file_readable(const std::string& filespec);
48 // tests whether file is writable - either it exists and is writable or doesn't exist but is in a writable folder
49 bool file_writable(const std::string& filespec);
50 // the size of the file in bytes - 0 if doesn't exist
51 size_t file_size(const std::string& filespec);
52 // delete the file - returns true if the delete succeeded
53 bool file_delete(const std::string& filespec);
54 // rename the file - returns true if the rename succeeded
55 bool file_rename (const std::string& old_filespec, const std::string& new_filespec);
56 // make an exact copy of the file - returns true if it succeeded
57 bool file_copy (const std::string& old_filespec, const std::string& new_filespec);
58 // move the file - tries to rename, if that fails, tries to copy - returns true if either of these succeeded
59 bool file_move (const std::string& old_filespec, const std::string& new_filespec);
60
61 // get the file's time stamps as a time_t - see the stlplus time.hpp package
62
63 // time the file was originally created
64 time_t file_created(const std::string& filespec);
65 // time the file was last modified
66 time_t file_modified(const std::string& filespec);
67 // time the file was accessed
68 time_t file_accessed(const std::string& filespec);
69
70 // platform-specific string handling to combine a directory and filename into a path
71
72 // combine a folder with a filename (basename.extension)
73 std::string create_filespec(const std::string& folder, const std::string& filename);
74 // combine a folder, a basename and an extension - extension does not need the .
75 std::string create_filespec(const std::string& folder, const std::string& basename, const std::string& extension);
76 // combine a basename and an extension - extension does not need the .
77 std::string create_filename(const std::string& basename, const std::string& extension);
78
79 ////////////////////////////////////////////////////////////////////////////////
80 // folder functions
81
82 // craete a folder - returns true if successful
83 bool folder_create(const std::string& folder);
84 // tests for whether the folder exists, i.e. there is something of that name and its a folder
85 bool folder_exists(const std::string& folder);
86 // test whether the folder contents are readable
87 bool folder_readable(const std::string& folder);
88 // tests whether the folder can be written to - for example to create a new file
89 bool folder_writable(const std::string& folder);
90 // delete the folder, optionally deleting the contents first - only succeeds if everything could be deleted
91 bool folder_delete(const std::string& folder, bool recurse = false);
92 // rename the folder - this probably only works within a disk/partition
93 bool folder_rename (const std::string& old_directory, const std::string& new_directory);
94 // test whether the folder is empty (of files)
95 bool folder_empty(const std::string& folder);
96
97 // set the current folder
98 bool folder_set_current(const std::string& folder);
99
100 // platform-specific string handling to retrieve some special locations
101 // these do not check whether the folder exists, they just process strings
102
103 // get the current folder
104 std::string folder_current(void);
105 // get the current folder as a full path
106 std::string folder_current_full(void);
107 // get the home folder - $HOME or %HOMEDRIVE%%HOMEPATH%
108 std::string folder_home(void);
109 // go down a level in the folder hierarchy
110 std::string folder_down(const std::string& folder, const std::string& subfolder);
111 // go up a level in the folder hierarchy
112 std::string folder_up(const std::string& folder, unsigned levels = 1);
113
114 // get folder contents
115
116 // the set of all subdirectories
117 std::vector<std::string> folder_subdirectories(const std::string& folder);
118 // the set of all files
119 std::vector<std::string> folder_files(const std::string& folder);
120 // the set of all folders and files
121 std::vector<std::string> folder_all(const std::string& folder);
122 // the set of all folder contents matching a wildcard string
123 // if folders is true, include folders; if files is true, include files
124 std::vector<std::string> folder_wildcard(const std::string& folder,
125 const std::string& wildcard,
126 bool folders = true,
127 bool files = true);
128
129 ////////////////////////////////////////////////////////////////////////////////
130 // path functions
131
132 // string manipulations of paths
133
134 // test whether a string represents a full path or a relative one
135 bool is_full_path(const std::string& path);
136 bool is_relative_path(const std::string& path);
137
138 // convert to a full path relative to the root path
139 std::string folder_to_path(const std::string& root, const std::string& folder);
140 std::string filespec_to_path(const std::string& root, const std::string& filespec);
141
142 // convert to a full path relative to the current working directory
143 std::string folder_to_path(const std::string& folder);
144 std::string filespec_to_path(const std::string& filespec);
145
146 // convert to a relative path relative to the root path
147 std::string folder_to_relative_path(const std::string& root, const std::string& folder);
148 std::string filespec_to_relative_path(const std::string& root, const std::string& filespec);
149
150 // convert to a relative path relative to the current working directory
151 std::string folder_to_relative_path(const std::string& folder);
152 std::string filespec_to_relative_path(const std::string& filespec);
153
154 // append a folder separator to the path to make it absolutely clear that it is a folder
155 std::string folder_append_separator(const std::string& folder);
156
157 ////////////////////////////////////////////////////////////////////////////////
158 // access functions split a filespec into its elements
159
160 // get the basename - that is, the name of the file without folder or extension parts
161 std::string basename_part(const std::string& filespec);
162 // get the filename - that is, the name of the file without folder part but with extension
163 std::string filename_part(const std::string& filespec);
164 // get the extension - that is the part of the filename after the . (and excluding the .)
165 std::string extension_part(const std::string& filespec);
166 // get the folder part - that is the filespec with the filename removed
167 std::string folder_part(const std::string& filespec);
168
169 // split a path into a vector of elements - i.e. split at the folder separator
170 std::vector<std::string> folder_elements(const std::string& folder);
171 std::vector<std::string> filespec_elements(const std::string& filespec);
172
173 ////////////////////////////////////////////////////////////////////////////////
174 // Path lookup functions
175
176 #ifdef MSWINDOWS
177 #define PATH_SPLITTER ";"
178 #else
179 #define PATH_SPLITTER ":"
180 #endif
181
182 // The lookup normally carried out by the shell to find a command in a
183 // directory in the PATH. Give this function the name of a command and it
184 // will return the full path. It returns an empty string on failure.
185 std::string path_lookup (const std::string& command);
186
187 // Generalised form of the above, takes a second argument
188 // - the list to search. This can be used to do other path lookups,
189 // such as LD_LIBRARY_PATH. The third argument specifies the splitter -
190 // the default value of PATH_SPLITTER is appropriate for environment variables.
191 std::string lookup (const std::string& file, const std::string& path, const std::string& splitter = PATH_SPLITTER);
192
193 // utility function for finding the folder that contains the current executable
194 // the argument is the argv[0] parameter passed to main
195 std::string install_path(const std::string& argv0);
196
197 ////////////////////////////////////////////////////////////////////////////////
198
199 } // end namespace stlplus
200
201 #endif
This page took 0.043556 seconds and 4 git commands to generate.