~ubuntu-branches/ubuntu/trusty/advancecomp/trusty

« back to all changes in this revision

Viewing changes to file.cc

  • Committer: Bazaar Package Importer
  • Author(s): Piotr Ozarowski
  • Date: 2006-05-13 21:15:49 UTC
  • Revision ID: james.westby@ubuntu.com-20060513211549-2vu7peis643ojcm5
Tags: upstream-1.15
ImportĀ upstreamĀ versionĀ 1.15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file is part of the Advance project.
 
3
 *
 
4
 * Copyright (C) 2002, 2004, 2005 Andrea Mazzoleni
 
5
 *
 
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.
 
10
 *
 
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. 
 
15
 *
 
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.
 
19
 */
 
20
 
 
21
#include "portable.h"
 
22
 
 
23
#include "file.h"
 
24
 
 
25
#include <zlib.h>
 
26
 
 
27
using namespace std;
 
28
 
 
29
crc_t crc_compute(const char* data, unsigned len)
 
30
{
 
31
        return crc32(0, (unsigned char*)data, len);
 
32
}
 
33
 
 
34
crc_t crc_compute(crc_t pred, const char* data, unsigned len)
 
35
{
 
36
        return crc32(pred, (unsigned char*)data, len);
 
37
}
 
38
 
 
39
filepath::filepath()
 
40
{
 
41
}
 
42
 
 
43
filepath::filepath(const filepath& A)
 
44
        : file(A.file)
 
45
{
 
46
}
 
47
 
 
48
filepath::filepath(const string& Afile)
 
49
        : file(Afile)
 
50
{
 
51
}
 
52
 
 
53
filepath::~filepath()
 
54
{
 
55
}
 
56
 
 
57
void filepath::file_set(const string& Afile)
 
58
{
 
59
        file = Afile;
 
60
}
 
61
 
 
62
infopath::infopath()
 
63
{
 
64
        readonly = true;
 
65
        good = false;
 
66
        size = 0;
 
67
}
 
68
 
 
69
infopath::infopath(const infopath& A)
 
70
        : filepath(A), good(A.good), size(A.size), readonly(A.readonly)
 
71
{
 
72
}
 
73
 
 
74
infopath::infopath(const string& Afile, bool Agood, unsigned Asize, bool Areadonly)
 
75
        : filepath(Afile), good(Agood), size(Asize), readonly(Areadonly)
 
76
{
 
77
}
 
78
 
 
79
infopath::~infopath()
 
80
{
 
81
}
 
82
 
 
83
void infopath::good_set(bool Agood)
 
84
{
 
85
        good = Agood;
 
86
}
 
87
 
 
88
void infopath::size_set(unsigned Asize)
 
89
{
 
90
        size = Asize;
 
91
}
 
92
 
 
93
void infopath::readonly_set(bool Areadonly)
 
94
{
 
95
        readonly = Areadonly;
 
96
}
 
97
 
 
98
/**
 
99
 * Check if a file exists.
 
100
 */
 
101
bool file_exists(const string& path) throw (error)
 
102
{
 
103
        struct stat s;
 
104
        if (stat(path.c_str(), &s) != 0) {
 
105
                if (errno!=ENOENT)
 
106
                        throw error() << "Failed stat file " << path;
 
107
 
 
108
                return false;
 
109
        }
 
110
 
 
111
        return !S_ISDIR(s.st_mode);
 
112
}
 
113
 
 
114
/**
 
115
 * Write a whole file.
 
116
 */
 
117
void file_write(const string& path, const char* data, unsigned size) throw (error)
 
118
{
 
119
        FILE* f = fopen(path.c_str(), "wb");
 
120
        if (!f)
 
121
                throw error() << "Failed open for write file " << path;
 
122
 
 
123
        if (fwrite(data, size, 1, f)!=1) {
 
124
                fclose(f);
 
125
 
 
126
                remove(path.c_str());
 
127
 
 
128
                throw error() << "Failed write file " << path;
 
129
        }
 
130
 
 
131
        fclose(f);
 
132
}
 
133
 
 
134
/**
 
135
 * Read a whole file.
 
136
 */
 
137
void file_read(const string& path, char* data, unsigned size) throw (error)
 
138
{
 
139
        file_read(path, data, 0, size);
 
140
}
 
141
 
 
142
/**
 
143
 * Read a whole file.
 
144
 */
 
145
void file_read(const string& path, char* data, unsigned offset, unsigned size) throw (error)
 
146
{
 
147
        FILE* f = fopen(path.c_str(), "rb");
 
148
        if (!f)
 
149
                throw error() << "Failed open for read file " << path;
 
150
 
 
151
        if (fseek(f, offset, SEEK_SET)!=0) {
 
152
                fclose(f);
 
153
 
 
154
                throw error() << "Failed seek file " << path;
 
155
        }
 
156
 
 
157
        if (fread(data, size, 1, f)!=1) {
 
158
                fclose(f);
 
159
 
 
160
                throw error() << "Failed read file " << path;
 
161
        }
 
162
 
 
163
        fclose(f);
 
164
}
 
165
 
 
166
/**
 
167
 * Get the time of a file.
 
168
 */
 
169
time_t file_time(const string& path) throw (error)
 
170
{
 
171
        struct stat s;
 
172
        if (stat(path.c_str(), &s)!=0)
 
173
                throw error() << "Failed stat file " << path;
 
174
 
 
175
        return s.st_mtime;
 
176
}
 
177
 
 
178
/**
 
179
 * Set the time of a file.
 
180
 */
 
181
void file_utime(const string& path, time_t tod) throw (error)
 
182
{
 
183
        struct utimbuf u;
 
184
 
 
185
        u.actime = tod;
 
186
        u.modtime = tod;
 
187
 
 
188
        if (utime(path.c_str(), &u) != 0)
 
189
                throw error() << "Failed utime file " << path;
 
190
}
 
191
 
 
192
/**
 
193
 * Get the size of a file.
 
194
 */
 
195
unsigned file_size(const string& path) throw (error)
 
196
{
 
197
        struct stat s;
 
198
        if (stat(path.c_str(), &s)!=0)
 
199
                throw error() << "Failed stat file " << path;
 
200
 
 
201
        return s.st_size;
 
202
}
 
203
 
 
204
/**
 
205
 * Get the crc of a file.
 
206
 */
 
207
crc_t file_crc(const string& path) throw (error)
 
208
{
 
209
        unsigned size = file_size(path);
 
210
 
 
211
        char* data = (char*)operator new(size);
 
212
 
 
213
        try {
 
214
                file_read(path, data, size);
 
215
        } catch (...) {
 
216
                operator delete(data);
 
217
                throw;
 
218
        }
 
219
 
 
220
        crc_t crc = crc_compute(data, size);
 
221
 
 
222
        operator delete(data);
 
223
 
 
224
        return crc;
 
225
}
 
226
 
 
227
/**
 
228
 * Copy a file.
 
229
 */
 
230
void file_copy(const string& path1, const string& path2) throw (error)
 
231
{
 
232
        unsigned size;
 
233
 
 
234
        size = file_size(path1);
 
235
 
 
236
        char* data = (char*)operator new(size);
 
237
 
 
238
        try {
 
239
                file_read(path1, data, size);
 
240
                file_write(path2, data, size);
 
241
        } catch (...) {
 
242
                operator delete(data);
 
243
                throw;
 
244
        }
 
245
 
 
246
        operator delete(data);
 
247
}
 
248
 
 
249
/**
 
250
 * Move a file.
 
251
 */
 
252
void file_move(const string& path1, const string& path2) throw (error)
 
253
{
 
254
        if (rename(path1.c_str(), path2.c_str())!=0
 
255
                && errno==EXDEV) {
 
256
 
 
257
                try {
 
258
                        file_copy(path1, path2);
 
259
                } catch (...) {
 
260
                        try {
 
261
                                file_remove(path2);
 
262
                        } catch (...) {
 
263
                        }
 
264
                        throw error() << "Failed move of " << path1 << " to " << path2;
 
265
                }
 
266
 
 
267
                file_remove(path1);
 
268
        }
 
269
}
 
270
 
 
271
/**
 
272
 * Remove a file.
 
273
 */
 
274
void file_remove(const string& path1) throw (error)
 
275
{
 
276
        if (remove(path1.c_str())!=0) {
 
277
                throw error() << "Failed remove of " << path1;
 
278
        }
 
279
}
 
280
 
 
281
/**
 
282
 * Rename a file.
 
283
 */
 
284
void file_rename(const string& path1, const string& path2) throw (error)
 
285
{
 
286
        if (rename(path1.c_str(), path2.c_str())!=0) {
 
287
                throw error() << "Failed rename of " << path1 << " to " << path2;
 
288
        }
 
289
}
 
290
 
 
291
/**
 
292
 * Randomize a name file.
 
293
 */
 
294
string file_randomize(const string& path, int n) throw ()
 
295
{
 
296
        ostringstream os;
 
297
 
 
298
        size_t pos = path.rfind('.');
 
299
 
 
300
        if (pos == string::npos) 
 
301
                os << path << ".";
 
302
        else
 
303
                os << string(path, 0, pos+1);
 
304
        
 
305
        os << n << ends;
 
306
        
 
307
        return os.str();
 
308
}
 
309
 
 
310
/**
 
311
 * Get the directory from a path.
 
312
 */
 
313
string file_dir(const string& path) throw ()
 
314
{
 
315
        size_t pos = path.rfind('/');
 
316
        if (pos == string::npos) {
 
317
                return "";
 
318
        } else {
 
319
                return string(path, 0, pos+1);
 
320
        }
 
321
}
 
322
 
 
323
/**
 
324
 * Get the file name from a path.
 
325
 */
 
326
string file_name(const string& path) throw ()
 
327
{
 
328
        size_t pos = path.rfind('/');
 
329
        if (pos == string::npos) {
 
330
                return path;
 
331
        } else {
 
332
                return string(path, pos+1);
 
333
        }
 
334
}
 
335
 
 
336
/**
 
337
 * Get the basepath (path without extension) from a path.
 
338
 */
 
339
string file_basepath(const string& path) throw ()
 
340
{
 
341
        size_t dot = path.rfind('.');
 
342
        if (dot == string::npos)
 
343
                return path;
 
344
        else
 
345
                return string(path, 0, dot);
 
346
}
 
347
 
 
348
/**
 
349
 * Get the basename (name without extension) from a path.
 
350
 */
 
351
string file_basename(const string& path) throw ()
 
352
 
353
        string name = file_name(path);
 
354
        size_t dot = name.rfind('.');
 
355
        if (dot == string::npos)
 
356
                return name;
 
357
        else
 
358
                return string(name, 0, dot);
 
359
}
 
360
 
 
361
/**
 
362
 * Get the extension from a path.
 
363
 */
 
364
string file_ext(const string& path) throw ()
 
365
 
366
        string name = file_name(path);
 
367
        size_t dot = name.rfind('.');
 
368
        if (dot == string::npos)
 
369
                return "";
 
370
        else
 
371
                return string(name, dot);
 
372
 
373
 
 
374
/**
 
375
 * Compare two path.
 
376
 */
 
377
int file_compare(const string& path1, const string& path2) throw ()
 
378
{
 
379
        return strcasecmp(path1.c_str(), path2.c_str());
 
380
}
 
381
 
 
382
/**
 
383
 * Convert a path to the C format.
 
384
 */
 
385
string file_adjust(const string& path) throw ()
 
386
{
 
387
        string r;
 
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, '/');
 
392
                } else {
 
393
                        r.insert(r.length(), 1, path[i]);
 
394
                }
 
395
                
 
396
        }
 
397
        return r;
 
398
}
 
399
 
 
400
/**
 
401
 * Make a drectory tree.
 
402
 */
 
403
void file_mktree(const std::string& path) throw (error)
 
404
{
 
405
        string dir = file_dir(path);
 
406
        string name = file_name(path);
 
407
 
 
408
        if (dir.length() && dir[dir.length() - 1] == '/')
 
409
                dir.erase(dir.length() - 1, 1);
 
410
 
 
411
        if (dir.length()) {
 
412
                file_mktree(dir);
 
413
 
 
414
                struct stat s;
 
415
                if (stat(dir.c_str(), &s) != 0) {
 
416
                        if (errno!=ENOENT)
 
417
                                throw error() << "Failed stat dir " << dir;
 
418
#if HAVE_FUNC_MKDIR_ONEARG
 
419
                        if (mkdir(dir.c_str()) != 0)
 
420
#else
 
421
                        if (mkdir(dir.c_str(), S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) != 0)
 
422
#endif
 
423
                                throw error() << "Failed mkdir " << dir;
 
424
                } else {
 
425
                        if (!S_ISDIR(s.st_mode))
 
426
                                throw error() << "Failed mkdir " << dir << " because a file with the same name exists";
 
427
                }
 
428
        }
 
429
}
 
430
 
 
431