2
* This file is part of the Advance project.
4
* Copyright (C) 2002, 2004, 2005 Andrea Mazzoleni
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29
crc_t crc_compute(const char* data, unsigned len)
31
return crc32(0, (unsigned char*)data, len);
34
crc_t crc_compute(crc_t pred, const char* data, unsigned len)
36
return crc32(pred, (unsigned char*)data, len);
43
filepath::filepath(const filepath& A)
48
filepath::filepath(const string& Afile)
57
void filepath::file_set(const string& Afile)
69
infopath::infopath(const infopath& A)
70
: filepath(A), good(A.good), size(A.size), readonly(A.readonly)
74
infopath::infopath(const string& Afile, bool Agood, unsigned Asize, bool Areadonly)
75
: filepath(Afile), good(Agood), size(Asize), readonly(Areadonly)
83
void infopath::good_set(bool Agood)
88
void infopath::size_set(unsigned Asize)
93
void infopath::readonly_set(bool Areadonly)
99
* Check if a file exists.
101
bool file_exists(const string& path) throw (error)
104
if (stat(path.c_str(), &s) != 0) {
106
throw error() << "Failed stat file " << path;
111
return !S_ISDIR(s.st_mode);
115
* Write a whole file.
117
void file_write(const string& path, const char* data, unsigned size) throw (error)
119
FILE* f = fopen(path.c_str(), "wb");
121
throw error() << "Failed open for write file " << path;
123
if (fwrite(data, size, 1, f)!=1) {
126
remove(path.c_str());
128
throw error() << "Failed write file " << path;
137
void file_read(const string& path, char* data, unsigned size) throw (error)
139
file_read(path, data, 0, size);
145
void file_read(const string& path, char* data, unsigned offset, unsigned size) throw (error)
147
FILE* f = fopen(path.c_str(), "rb");
149
throw error() << "Failed open for read file " << path;
151
if (fseek(f, offset, SEEK_SET)!=0) {
154
throw error() << "Failed seek file " << path;
157
if (fread(data, size, 1, f)!=1) {
160
throw error() << "Failed read file " << path;
167
* Get the time of a file.
169
time_t file_time(const string& path) throw (error)
172
if (stat(path.c_str(), &s)!=0)
173
throw error() << "Failed stat file " << path;
179
* Set the time of a file.
181
void file_utime(const string& path, time_t tod) throw (error)
188
if (utime(path.c_str(), &u) != 0)
189
throw error() << "Failed utime file " << path;
193
* Get the size of a file.
195
unsigned file_size(const string& path) throw (error)
198
if (stat(path.c_str(), &s)!=0)
199
throw error() << "Failed stat file " << path;
205
* Get the crc of a file.
207
crc_t file_crc(const string& path) throw (error)
209
unsigned size = file_size(path);
211
char* data = (char*)operator new(size);
214
file_read(path, data, size);
216
operator delete(data);
220
crc_t crc = crc_compute(data, size);
222
operator delete(data);
230
void file_copy(const string& path1, const string& path2) throw (error)
234
size = file_size(path1);
236
char* data = (char*)operator new(size);
239
file_read(path1, data, size);
240
file_write(path2, data, size);
242
operator delete(data);
246
operator delete(data);
252
void file_move(const string& path1, const string& path2) throw (error)
254
if (rename(path1.c_str(), path2.c_str())!=0
258
file_copy(path1, path2);
264
throw error() << "Failed move of " << path1 << " to " << path2;
274
void file_remove(const string& path1) throw (error)
276
if (remove(path1.c_str())!=0) {
277
throw error() << "Failed remove of " << path1;
284
void file_rename(const string& path1, const string& path2) throw (error)
286
if (rename(path1.c_str(), path2.c_str())!=0) {
287
throw error() << "Failed rename of " << path1 << " to " << path2;
292
* Randomize a name file.
294
string file_randomize(const string& path, int n) throw ()
298
size_t pos = path.rfind('.');
300
if (pos == string::npos)
303
os << string(path, 0, pos+1);
311
* Get the directory from a path.
313
string file_dir(const string& path) throw ()
315
size_t pos = path.rfind('/');
316
if (pos == string::npos) {
319
return string(path, 0, pos+1);
324
* Get the file name from a path.
326
string file_name(const string& path) throw ()
328
size_t pos = path.rfind('/');
329
if (pos == string::npos) {
332
return string(path, pos+1);
337
* Get the basepath (path without extension) from a path.
339
string file_basepath(const string& path) throw ()
341
size_t dot = path.rfind('.');
342
if (dot == string::npos)
345
return string(path, 0, dot);
349
* Get the basename (name without extension) from a path.
351
string file_basename(const string& path) throw ()
353
string name = file_name(path);
354
size_t dot = name.rfind('.');
355
if (dot == string::npos)
358
return string(name, 0, dot);
362
* Get the extension from a path.
364
string file_ext(const string& path) throw ()
366
string name = file_name(path);
367
size_t dot = name.rfind('.');
368
if (dot == string::npos)
371
return string(name, dot);
377
int file_compare(const string& path1, const string& path2) throw ()
379
return strcasecmp(path1.c_str(), path2.c_str());
383
* Convert a path to the C format.
385
string file_adjust(const string& path) throw ()
388
for(unsigned i=0;i<path.length();++i) {
389
if (path[i]=='\\' || path[i]=='/') {
390
if (i+1<path.length())
391
r.insert(r.length(), 1, '/');
393
r.insert(r.length(), 1, path[i]);
401
* Make a drectory tree.
403
void file_mktree(const std::string& path) throw (error)
405
string dir = file_dir(path);
406
string name = file_name(path);
408
if (dir.length() && dir[dir.length() - 1] == '/')
409
dir.erase(dir.length() - 1, 1);
415
if (stat(dir.c_str(), &s) != 0) {
417
throw error() << "Failed stat dir " << dir;
418
#if HAVE_FUNC_MKDIR_ONEARG
419
if (mkdir(dir.c_str()) != 0)
421
if (mkdir(dir.c_str(), S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) != 0)
423
throw error() << "Failed mkdir " << dir;
425
if (!S_ISDIR(s.st_mode))
426
throw error() << "Failed mkdir " << dir << " because a file with the same name exists";