Lolly 1.4.28
Loading...
Searching...
No Matches
Functions
file.hpp File Reference
#include "url.hpp"
Include dependency graph for file.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

bool is_local_and_single (url u)
 
bool is_directory (url u)
 
bool is_regular (url u)
 
bool is_symbolic_link (url u)
 
bool is_newer (url which, url than)
 
bool is_of_type (url name, string filter)
 
int file_size (url u)
 
int last_modified (url u)
 
array< stringread_directory (url u, bool &error_flag)
 
url subdirectories (url u)
 
void mkdir (url u)
 
void make_dir (url which)
 
void rmdir (url u)
 
void chdir (url u)
 
url url_temp_dir ()
 
url url_temp (string suffix="")
 
void remove (url u)
 
void move (url u1, url u2)
 
void copy (url u1, url u2)
 
bool load_string (url file_name, string &s, bool fatal)
 
string string_load (url u)
 
bool save_string (url file_name, const string &s, bool fatal=false)
 
void string_save (const string &s, url u)
 
bool append_string (url u, const string &s, bool fatal)
 
void string_append_to_file (const string &s, url u)
 
void append_to (url what, url to)
 

Function Documentation

◆ is_local_and_single()

bool is_local_and_single ( url u)

Definition at line 22 of file file.cpp.

22 {
23 string label = u.label ();
24 string protocol= u.protocol ();
25 return ((label == "") || (label == "concat") || (label == "root")) &&
26 (protocol == "default" || protocol == "file");
27}
The list class represents a linked list.
Definition list.hpp:48

◆ is_directory()

bool is_directory ( url u)

Definition at line 38 of file file.cpp.

38 {
39 if (!is_local_and_single (u)) return false;
40
41 c_string path (as_local_path (u));
43 if (tb_file_info (path, &info)) {
44 switch (info.type) {
46 return true;
47 default:
48 return false;
49 }
50 }
51 else {
52 return false;
53 }
54}
bool is_local_and_single(url u)
Definition file.cpp:22
static string as_local_path(url u)
Definition file.cpp:30

◆ is_regular()

bool is_regular ( url u)

Definition at line 57 of file file.cpp.

57 {
58 if (!is_local_and_single (u)) return false;
59
60 c_string path (as_local_path (u));
62 if (tb_file_info (path, &info)) {
63 switch (info.type) {
65 return true;
66 default:
67 return false;
68 }
69 }
70 else {
71 return false;
72 }
73}

◆ is_symbolic_link()

bool is_symbolic_link ( url u)

Definition at line 76 of file file.cpp.

76 {
77 if (!is_local_and_single (u)) return false;
78
79 c_string path (as_local_path (u));
81 if (tb_file_info (path, &info)) {
82 return (info.flags & TB_FILE_FLAG_LINK) != 0;
83 }
84 else {
85 return false;
86 }
87}

◆ is_newer()

bool is_newer ( url which,
url than )

Definition at line 90 of file file.cpp.

90 {
91 if (!is_local_and_single (which)) return false;
92 if (!is_local_and_single (than)) return false;
93
97 return info1.mtime > info2.mtime;
98 }
99 else {
100 return false;
101 }
102}

◆ is_of_type()

bool is_of_type ( url name,
string filter )

Definition at line 105 of file file.cpp.

105 {
106 if (is_ramdisc (name)) return true;
107
108 if (!is_local_and_single (name)) return false;
109
110 if (filter == "") return true;
111 int i, n= N (filter);
112
113 // Normal files
114 if (os_win () || os_mingw ()) {
115 string suf;
116 if (filter == "x") {
117 suf= suffix (name);
118 if ((suf != "exe") && (suf != "bat") && (suf != "com")) {
119 name= glue (name, ".exe");
120 suf = "exe";
121 }
122 }
123 }
124 c_string path (as_local_path (name));
126 if (!tb_file_info (path, &info)) {
127 return false;
128 }
129 for (i= 0; i < n; i++)
130 switch (filter[i]) {
131 // FIXME: should check user id and group id for r, w and x
132 case 'f':
133 if (info.type != TB_FILE_TYPE_FILE) return false;
134 break;
135 case 'd':
136 if (info.type != TB_FILE_TYPE_DIRECTORY) return false;
137 break;
138 case 'l':
139 if (info.flags != TB_FILE_FLAG_LINK) return false;
140 break;
141 case 'r':
142 if (!tb_file_access (path, TB_FILE_MODE_RO)) return false;
143 break;
144 case 'w':
145 if (!tb_file_access (path, TB_FILE_MODE_WO)) return false;
146 break;
147 case 'x':
148 if (!tb_file_access (path, TB_FILE_MODE_EXEC)) return false;
149 break;
150 }
151 return true;
152}
int N(array< T > a)
Get the length of the array.
Definition array.hpp:170
bool os_mingw()
bool os_win()
Definition sys_utils.cpp:94
string suffix(url u, bool use_locase)
Definition url.cpp:381
bool is_ramdisc(url u)
Definition url.cpp:724
url glue(url u, string s)
Definition url.cpp:421

◆ file_size()

int file_size ( url u)

Definition at line 155 of file file.cpp.

155 {
156 if (!is_local_and_single (u)) return -1;
157
158 c_string path (as_local_path (u));
160 if (tb_file_info (path, &info)) {
161 return info.size;
162 }
163 else {
164 return -1;
165 }
166}

◆ last_modified()

int last_modified ( url u)

Definition at line 169 of file file.cpp.

169 {
170 if (!is_local_and_single (u)) return -1;
171
172 c_string path (as_local_path (u));
174 if (tb_file_info (path, &info)) {
175 return info.mtime;
176 }
177 else {
178 return -1;
179 }
180}

◆ read_directory()

array< string > read_directory ( url u,
bool & error_flag )

Definition at line 194 of file file.cpp.

194 {
195 if (!is_local_and_single (u)) {
196 error_flag= false;
197 return array<string> ();
198 }
199
200 c_string path (as_local_path (u));
203 if (error_flag) {
204 return arr_result;
205 }
207 return arr_result;
208}
bool is_directory(url u)
Definition file.cpp:38
static tb_long_t tb_directory_walk_func(tb_char_t const *path, tb_file_info_t const *info, tb_cpointer_t priv)
Definition file.cpp:183

◆ subdirectories()

url subdirectories ( url u)

Definition at line 211 of file file.cpp.

211 {
212 if (is_or (u)) return subdirectories (u[1]) | subdirectories (u[2]);
213 else if (is_directory (u)) {
214 url ret= u;
215 bool error_flag;
217 for (int i= 0; i < N (dir); i++)
218 if (!starts (dir[i], ".") && is_directory (u * dir[i]))
219 ret= ret | subdirectories (u * dir[i]);
220 return ret;
221 }
222 else return url_none ();
223}
bool starts(string s, const char *what)
Definition analyze.cpp:566
Definition url.hpp:37
url subdirectories(url u)
Definition file.cpp:211
array< string > read_directory(url u, bool &error_flag)
Definition file.cpp:194
url url_none()
Definition url.cpp:91
bool is_or(url u)
Definition url.hpp:178

◆ mkdir()

void mkdir ( url u)

Definition at line 226 of file file.cpp.

226 {
227 string label= u.label ();
228 if (label == "none" || label == "root" || label == "wildcard") return;
229 if (is_local_and_single (u)) { // label == "" or label == "concat"
230 c_string path (as_local_path (u));
231 tb_directory_create (path);
232 }
233 if (is_or (u)) { // label == "or"
234 mkdir (u[1]);
235 mkdir (u[2]);
236 }
237}
void mkdir(url u)
Definition file.cpp:226

◆ make_dir()

void make_dir ( url which)

Definition at line 240 of file file.cpp.

240 {
241 if (is_none (which)) return;
242 if (!is_directory (which)) {
243 make_dir (head (which));
244 mkdir (which);
245 }
246}
void make_dir(url which)
Definition file.cpp:240
list< T > head(list< T > l, int n=1)
Get the first n items of a list.
Definition list.ipp:185
bool is_none(url u)
Definition url.hpp:166

◆ rmdir()

void rmdir ( url u)

Definition at line 249 of file file.cpp.

249 {
250 string label= u.label ();
251 if (label == "none" || label == "root" || label == "wildcard") return;
252 if (is_local_and_single (u)) { // label == "" or label == "concat"
253 c_string path (as_local_path (u));
254 tb_directory_remove (path);
255 }
256 if (is_or (u)) { // label == "or"
257 rmdir (u[1]);
258 rmdir (u[2]);
259 }
260}
void rmdir(url u)
Definition file.cpp:249

◆ chdir()

void chdir ( url u)

Definition at line 263 of file file.cpp.

263 {
264 if (is_local_and_single (u)) {
265 c_string path (as_local_path (u));
266 if (tb_directory_current_set (path) != tb_true) {
267 TM_FAILED ("Failed to change the dir");
268 }
269 }
270 else TM_FAILED ("file path invalid");
271}
#define TM_FAILED(msg)
Macro used to throw an exception with a specified error message.
Definition basic.hpp:93

◆ url_temp_dir()

url url_temp_dir ( )

Definition at line 300 of file file.cpp.

300 {
301 static url u;
302 if (u == url_none ()) {
304 make_dir (u);
305 }
306 return u;
307}
url url_temp_dir_sub()
Definition file.cpp:289

◆ url_temp()

url url_temp ( string suffix = "")

Definition at line 274 of file file.cpp.

274 {
275 string file_name= replace (lolly::hash::uuid_make (), "-", "_");
276 if (!is_empty (suffix)) {
277 file_name= file_name * string (".") * suffix;
278 }
280 if (file_size (u) == -1) {
281 return u;
282 }
283 else {
284 return url_temp (suffix);
285 }
286}
string replace(string s, string what, string by)
Definition analyze.cpp:899
url url_temp(string suffix)
Definition file.cpp:274
url url_temp_dir()
Definition file.cpp:300
int file_size(url u)
Definition file.cpp:155
string uuid_make()
Definition uuid.cpp:20
bool is_empty(string s)
Definition string.cpp:316

◆ remove()

void remove ( url u)

Definition at line 326 of file file.cpp.

326 {
327 string label= u.label ();
328 if (label == "none" || label == "root" || label == "wildcard") return;
329 if (is_local_and_single (u)) {
330 c_string path (as_local_path (u));
331 tb_file_remove (path);
332 }
333 else if (is_or (u)) { // label == "or"
334 remove (u[1]);
335 remove (u[2]);
336 }
337}
void remove(url u)
Definition file.cpp:326

◆ move()

void move ( url u1,
url u2 )

Definition at line 310 of file file.cpp.

310 {
313
315}

◆ copy()

void copy ( url u1,
url u2 )

Definition at line 318 of file file.cpp.

◆ load_string()

bool load_string ( url file_name,
string & s,
bool fatal )

Definition at line 434 of file file.cpp.

434 {
436 return cleanup_and_return_finally (stat, u, fatal, "load url");
437}
static bool cleanup_and_return_finally(const file_status &status, const url &u, bool fatal, const string &reason)
Definition file.cpp:373
file_status load_string_try(url u, string &s)
Definition file.cpp:394

◆ string_load()

string string_load ( url u)

Definition at line 440 of file file.cpp.

440 {
441 string s;
442 // file_url f= u;
443 (void) load_string (u, s, false);
444 return s;
445}
bool load_string(url u, string &s, bool fatal)
Definition file.cpp:434

◆ save_string()

bool save_string ( url file_name,
const string & s,
bool fatal = false )

Definition at line 490 of file file.cpp.

490 {
492 return cleanup_and_return_finally (stat, u, fatal, "save to url");
493}
file_status save_string_try(url u, const string &s)
Definition file.cpp:448

◆ string_save()

void string_save ( const string & s,
url u )

Definition at line 496 of file file.cpp.

496 {
497 (void) save_string (u, s, false);
498}
bool save_string(url u, const string &s, bool fatal)
Definition file.cpp:490

◆ append_string()

bool append_string ( url u,
const string & s,
bool fatal )

Definition at line 543 of file file.cpp.

543 {
545 return cleanup_and_return_finally (stat, u, fatal, "append to url");
546}
file_status append_string_try(url u, const string &s)
Definition file.cpp:501

◆ string_append_to_file()

void string_append_to_file ( const string & s,
url u )

Definition at line 549 of file file.cpp.

549 {
550 (void) append_string (u, s, false);
551}
bool append_string(url u, const string &s, bool fatal)
Definition file.cpp:543

◆ append_to()

void append_to ( url what,
url to )

Definition at line 554 of file file.cpp.

554 {
555 string what_s;
556 if (load_string (what, what_s, false) || append_string (to, what_s, false))
557 cerr << "Append failed for " << to << LF;
558}
@ LF
Definition basic.hpp:287
tm_ostream & cerr