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