/home/docs/checkouts/readthedocs.org/user_builds/ratpac/checkouts/latest/src/external/stlplus/include/stlplus/file_system.hpp Source File

Ratpac-two: /home/docs/checkouts/readthedocs.org/user_builds/ratpac/checkouts/latest/src/external/stlplus/include/stlplus/file_system.hpp Source File
Ratpac-two
file_system.hpp
1 #ifndef FILE_SYSTEM_HPP
2 #define FILE_SYSTEM_HPP
3 /*------------------------------------------------------------------------------
4 
5  Author: Andy Rushton
6  Copyright: (c) Andy Rushton, 2004
7  License: BSD License, see ../docs/license.html
8 
9  Simplified access to the File system
10 
11  All file system access and filename manipulation should be done
12  with this package. Then it is only necessary to port this package
13  to port all file handling.
14 
15 ------------------------------------------------------------------------------*/
16 #include <time.h>
17 
18 #include <string>
19 #include <vector>
20 
21 #include "os_fixes.hpp"
22 
24 // classifying functions
25 
26 bool is_present(const std::string& thing);
27 bool is_folder(const std::string& thing);
28 bool is_file(const std::string& thing);
29 
31 // file functions
32 
33 bool file_exists(const std::string& filespec);
34 bool file_readable(const std::string& filespec);
35 bool file_writable(const std::string& filespec);
36 size_t file_size(const std::string& filespec);
37 bool file_delete(const std::string& filespec);
38 bool file_rename(const std::string& old_filespec, const std::string& new_filespec);
39 bool file_move(const std::string& old_filespec, const std::string& new_filespec);
40 bool file_copy(const std::string& old_filespec, const std::string& new_filespec);
41 
42 // Read-only versus read-write control. This is equivalent to chmod on Unix,
43 // but I've insulated the user from the low-level routine because of
44 // differences in the OSs' interpretation of the mode parameter. I've also
45 // defined a new set of constants to control this, again because of
46 // inconsistencies. The idea is to combine the constants as bit-masks so as to
47 // build up a set of permissions. The modes are ORed together to build up a
48 // set of permissions and then ANDed with a mask to control which people have
49 // that permission. Permissions can be ORed together too. So, for example, to
50 // give the owner read-write access and all others only read access, you would
51 // use the expression:
52 // ((read_mode | write_mode) & owner_mask) | (read_mode & (group_mask | other_mask))
53 // This can be simplified by using combined modes and combined masks to:
54 // (read_write_mode & owner_mask) | (read_mode & non_owner_mask)
55 
56 // basic modes
57 extern const int read__mode;
58 extern const int write_mode;
59 extern const int execute_mode;
60 // combined modes
61 extern const int none_mode;
62 extern const int read_write_mode;
63 extern const int all_mode;
64 // basic users
65 extern const int owner_mask;
66 extern const int group_mask;
67 extern const int other_mask;
68 // combined users
69 extern const int non_owner_mask;
70 extern const int all_mask;
71 // common settings
72 extern const int read_mode_all;
73 extern const int read_write_mode_owner_read_mode_all;
74 extern const int read_mode_owner_only;
75 extern const int read_write_mode_owner_only;
76 // the function itself
77 bool file_set_mode(const std::string& filespec, int mode);
78 
79 // get the file's time stamps as a time_t - see the stlplus time.hpp package
80 time_t file_created(const std::string& filespec);
81 time_t file_modified(const std::string& filespec);
82 time_t file_accessed(const std::string& filespec);
83 
84 std::string create_filespec(const std::string& folder, const std::string& filename);
85 std::string create_filespec(const std::string& folder, const std::string& basename, const std::string& extension);
86 std::string create_filename(const std::string& basename, const std::string& extension);
87 
89 // folder functions
90 
91 bool folder_create(const std::string& folder);
92 bool folder_exists(const std::string& folder);
93 bool folder_readable(const std::string& folder);
94 bool folder_writable(const std::string& folder);
95 bool folder_delete(const std::string& folder, bool recurse = false);
96 bool folder_rename(const std::string& old_directory, const std::string& new_directory);
97 bool folder_empty(const std::string& folder);
98 
99 bool folder_set_current(const std::string& folder);
100 std::string folder_current(void);
101 std::string folder_current_full(void);
102 std::string folder_home(void);
103 std::string folder_down(const std::string& folder, const std::string& subfolder);
104 std::string folder_up(const std::string& folder, unsigned levels = 1);
105 
106 std::vector<std::string> folder_subdirectories(const std::string& folder);
107 std::vector<std::string> folder_files(const std::string& folder);
108 std::vector<std::string> folder_all(const std::string& folder);
109 std::vector<std::string> folder_wildcard(const std::string& folder, const std::string& wildcard, bool subdirs = true,
110  bool files = true);
111 
113 // path functions
114 
115 bool is_full_path(const std::string& path);
116 bool is_relative_path(const std::string& path);
117 
118 // convert to a full path relative to the root path
119 std::string folder_to_path(const std::string& root, const std::string& folder);
120 std::string filespec_to_path(const std::string& root, const std::string& filespec);
121 
122 // convert to a full path relative to the current working directory
123 std::string folder_to_path(const std::string& folder);
124 std::string filespec_to_path(const std::string& filespec);
125 
126 // convert to a relative path relative to the root path
127 std::string folder_to_relative_path(const std::string& root, const std::string& folder);
128 std::string filespec_to_relative_path(const std::string& root, const std::string& filespec);
129 
130 // convert to a relative path relative to the current working directory
131 std::string folder_to_relative_path(const std::string& folder);
132 std::string filespec_to_relative_path(const std::string& filespec);
133 
134 // append a folder separator to the path to make it absolutely clear that it is a folder
135 std::string folder_append_separator(const std::string& folder);
136 
138 // access functions split a filespec into its elements
139 
140 std::string basename_part(const std::string& filespec);
141 std::string filename_part(const std::string& filespec);
142 std::string extension_part(const std::string& filespec);
143 std::string folder_part(const std::string& filespec);
144 
145 // split a path into a vector of elements - i.e. split at the folder separator
146 std::vector<std::string> folder_elements(const std::string& folder);
147 std::vector<std::string> filespec_elements(const std::string& filespec);
148 
150 // Path lookup functions
151 
152 #ifdef _WIN32
153 #define PATH_SPLITTER ";"
154 #else
155 #define PATH_SPLITTER ":"
156 #endif
157 
158 // The lookup normally carried out by the shell to find a command in a
159 // directory in the PATH. Give this function the name of a command and it
160 // will return the full path. It returns an empty string on failure.
161 std::string path_lookup(const std::string& command);
162 
163 // Generalised form of the above, takes a second argument
164 // - the list to search. This can be used to do other path lookups,
165 // such as LD_LIBRARY_PATH. The third argument specifies the splitter -
166 // the default value of PATH_SPLITTER is appropriate for environment variables.
167 std::string lookup(const std::string& file, const std::string& path, const std::string& splitter = PATH_SPLITTER);
168 
169 // utility function for finding the folder that contains the current executable
170 // the argument is the argv[0] parameter passed to main
171 std::string install_path(const std::string& argv0);
172 
174 
175 #endif