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