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 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
|
/*
*
* (c) COPYRIGHT INRIA, 1996.
* Please first read the full copyright statement in file COPYRIGHT.
*
*/
/*
* Warning:
* This module is part of the Thot library, which was originally
* developed in French. That's why some comments are still in
* French, but their translation is in progress and the full module
* will be available in English in the next release.
*
*/
/*
* platform.c : basic system functions
*
* Authors: I. Vatton, D. Veillard (INRIA)
*
*/
#include "thot_sys.h"
#include "constmedia.h"
#include "thotdir.h"
#include "fileaccess.h"
/*----------------------------------------------------------------------
TtaFileExist teste l'existence d'un fichier.
Rend 1 si le fichier a e't'e trouve' et 0 sinon.
Si filename est un repertoire, on retourne 0.
----------------------------------------------------------------------*/
#ifdef __STDC__
int TtaFileExist (CONST char *filename)
#else /* __STDC__ */
int TtaFileExist (filename)
CONST char *filename;
#endif /* __STDC__ */
{
int status = 0;
#if defined(_WINDOWS) && !defined(__GNUC__)
DWORD attribs;
attribs = GetFileAttributes (filename);
if (attribs == 0xFFFFFFFF)
status = 0;
else if (attribs & FILE_ATTRIBUTE_DIRECTORY)
status = 0;
else
status = 1;
#else /* _WINDOWS && !__GNUC__ */
int filedes;
struct stat statinfo;
#ifdef _WINDOWS
filedes = open (filename, _O_RDONLY | O_BINARY);
#else /* _WINDOWS */
filedes = open (filename, O_RDONLY);
#endif /* _WINDOWS */
if (filedes < 0)
status = 0;
else
{
if (fstat (filedes, &statinfo) != -1)
{
if (statinfo.st_mode & S_IFDIR)
/* on ne veut pas de directory */
status = 0;
else
status = 1;
}
close (filedes);
}
#endif /* _WINDOWS && !__GNUC__ */
return status;
}
/*----------------------------------------------------------------------
TtaFileUnlink : remove a file.
----------------------------------------------------------------------*/
#ifdef __STDC__
int TtaFileUnlink (CONST char *filename)
#else /* __STDC__ */
int TtaFileUnlink (filename)
CONST char *filename;
#endif /* __STDC__ */
{
#if defined(_WINDOWS) && !defined(__GNUC__)
return (remove (filename));
#else /* _WINDOWS && !__GNUC__ */
return (unlink (filename));
#endif /* _WINDOWS && !__GNUC__ */
}
/*----------------------------------------------------------------------
static ThotDirBrowseCopyFile - copy the filename from the
platform's directory structure
----------------------------------------------------------------------*/
#ifdef __STDC__
static int ThotDirBrowse_copyFile (ThotDirBrowse * me)
#else /* __STDC__ */
static int ThotDirBrowse_copyFile (me)
ThotDirBrowse *me;
#endif /* __STDC__ */
{
#if defined(_WINDOWS) && !defined(__GNUC__)
DWORD attr;
if (strlen (me->data.cFileName) + me->dirLen > me->bufLen)
return -2;
strcpy (me->buf + me->dirLen, me->data.cFileName);
if ((attr = GetFileAttributes (me->buf)) == 0xFFFFFFFF)
return -1;
if (attr & FILE_ATTRIBUTE_DIRECTORY && !(me->PicMask & ThotDirBrowse_DIRECTORIES))
return 0;
if (attr & FILE_ATTRIBUTE_NORMAL && !(me->PicMask & ThotDirBrowse_FILES))
return 0;
return 1;
#else /* _WINDOWS && !__GNUC__ */
int i;
int ls_car;
boolean notEof;
struct stat fileStat;
while (TRUE)
{
ls_car = fgetc (me->ls_stream);
/* saute les caracteres de separation */
while (((char) ls_car == ' ') || ((char) ls_car == '\t') ||
((char) ls_car == '\n'))
ls_car = fgetc (me->ls_stream);
notEof = TRUE;
i = 0;
while (((char) ls_car != ' ') && ((char) ls_car != '\t') &&
((char) ls_car != '\n') && (notEof))
{
if (ls_car == EOF)
notEof = FALSE;
else
{
me->buf[i] = (char) ls_car;
i++;
if (i == me->bufLen)
{
me->buf[i - 1] = 0;
return -2;
}
ls_car = fgetc (me->ls_stream);
}
}
me->buf[i] = EOS;
if (notEof == FALSE && !i)
return 0;
if (stat (me->buf, &fileStat) == -1)
return -1;
/* next if fileStat is not included in our PicMask */
if (S_ISDIR (fileStat.st_mode) && !(me->PicMask & ThotDirBrowse_DIRECTORIES))
continue;
if (S_ISREG (fileStat.st_mode) && !(me->PicMask & ThotDirBrowse_FILES))
continue;
return 1;
}
#endif /* _WINDOWS && !__GNUC__ */
}
/*----------------------------------------------------------------------
ThotDirBrowse_first - get first dir/name.ext and setup
platform dependent ThotDirBrowse structure
----------------------------------------------------------------------*/
#ifdef __STDC__
int ThotDirBrowse_first (ThotDirBrowse * me, char *dir, char *name, char *ext)
#else /* __STDC__ */
int ThotDirBrowse_first (me, dir, name, ext)
ThotDirBrowse *me;
char *dir;
char *name;
char *ext;
#endif /* __STDC__ */
{
char space[MAX_PATH];
int ret;
me->dirLen = strlen (dir);
strcpy (me->buf, dir);
strcpy (me->buf + (me->dirLen++), DIR_STR);
#if defined(_WINDOWS) && !defined(__GNUC__)
sprintf (space, "%s\\%s%s", dir ? dir : "", name ? name : "",
ext ? ext : "");
me->handle = INVALID_HANDLE_VALUE;
if ((me->handle = FindFirstFile (space, &me->data)) ==
INVALID_HANDLE_VALUE)
return -1;
if ((ret = ThotDirBrowse_copyFile (me)) == 1)
return 1;
ret = ThotDirBrowse_next (me);
if (ret == -1)
FindClose (me->handle);
return ret;
#else /* _WINDOWS && !__GNUC__ */
/* sprintf (space, "/bin/ls%s %s/%s%s 2>/dev/null",
ext && *ext ? "" : " -d", dir ? dir : "",
name ? name : "", ext ? ext : ""); - EGP */
sprintf (space, "/bin/ls -d %s/%s%s 2>/dev/null", dir ? dir : "",
name ? name : "", ext ? ext : "");
me->ls_stream = NULL;
if ((me->ls_stream = popen (space, "r")) == NULL)
return -1;
if ((ret = ThotDirBrowse_copyFile (me)) == 1)
return 1;
pclose (me->ls_stream);
me->ls_stream = NULL;
#endif /* _WINDOWS && !__GNUC__ */
return ret;
}
/*----------------------------------------------------------------------
ThotDirBrowse_next - get next file
----------------------------------------------------------------------*/
#ifdef __STDC__
int ThotDirBrowse_next (ThotDirBrowse * me)
#else /* __STDC__ */
int ThotDirBrowse_next (me)
ThotDirBrowse *me;
#endif /* __STDC__ */
{
#if defined(_WINDOWS) && !defined(__GNUC__)
int ret;
do
{
if (FindNextFile (me->handle, &me->data) != TRUE)
return GetLastError () == ERROR_NO_MORE_FILES ? 0 : -1;
}
while ((ret = ThotDirBrowse_copyFile (me)) == 0);
return ret;
#else /* _WINDOWS && !__GNUC__ */
return ThotDirBrowse_copyFile (me);
#endif /* _WINDOWS && !__GNUC__ */
}
/*----------------------------------------------------------------------
ThotDirBrowse_close - recover system resources
----------------------------------------------------------------------*/
#ifdef __STDC__
int ThotDirBrowse_close (ThotDirBrowse * me)
#else /* __STDC__ */
int ThotDirBrowse_close (me)
ThotDirBrowse *me;
#endif /* __STDC__ */
{
int ret;
#if defined(_WINDOWS) && !defined(__GNUC__)
if (me->handle == INVALID_HANDLE_VALUE)
return 0;
ret = (FindClose (me->handle) == 1 ? 1 : -1);
me->handle = INVALID_HANDLE_VALUE;
#else /* _WINDOWS && !__GNUC__ */
if (me->ls_stream == NULL)
return 0;
ret = (pclose (me->ls_stream) == -1 ? -1 : 1);
me->ls_stream = NULL;
#endif /* _WINDOWS && !__GNUC__ */
return ret;
}
/* uniform file access for unix, WIN32, and refrigerator
see ThotFile_test for sample usage
*/
#ifdef INCLUDE_TESTING_CODE
/* ThotFile_test - use to test ThotFile on any platform
*/
void ThotFile_test (char *name)
{
ThotFileHandle handle = ThotFile_BADHANDLE;
ThotFileOffset offset;
ThotFileInfo info;
int i;
char space[16];
CONST char *format = "%15d\0";
space[sizeof (space) - 1] = 0;
printf ("ThotFile_test: opening %s for CREATE/READ/WRITE\n", name);
handle = TtaFileOpen (name, ThotFile_CREATE | ThotFile_TRUNCATE | ThotFile_READWRITE);
if (handle == ThotFile_BADHANDLE)
{
printf ("ThotFile_test: handle == ThotFile_BADHANDLE\n");
return;
}
printf ("ThotFile_test: writing %s\n", name);
for (i = 0; i < 100; i++)
{
sprintf (space, format, i);
if (TtaFileWrite (handle, space, sizeof (space)) == -1)
{
printf ("ThotFile_test: bad write\n");
break;
}
}
printf ("ThotFile_test: closing %s\n", name);
if (TtaFileClose (handle) == 0)
{
printf ("ThotFile_test: bad write\n");
return;
}
printf ("ThotFile_test: reopening %s\n", name);
handle = TtaFileOpen (name, ThotFile_READWRITE);
if (handle == ThotFile_BADHANDLE)
{
printf ("ThotFile_test: handle == ThotFile_BADHANDLE\n");
return;
}
printf ("ThotFile_test: stating %s\n", name);
if (TtaFileStat (handle, &info) == 0)
{
printf ("TtaFileStat: bad stat\n");
return;
}
printf ("ThotFile_test: file %s is %d bytes long\n", name, info.size);
printf ("ThotFile_test: seeking end of %s\n", name);
offset = TtaFileSeek (handle, 0, ThotFile_SEEKEND);
if (offset == ThotFile_BADOFFSET)
{
printf ("ThotFile_test: offset == ThotFile_BADOFFSET\n");
return;
}
printf ("ThotFile_test: end found at %d\n", offset);
printf ("ThotFile_test: seeking to offset %d in %s\n", offset - 4 * sizeof (space), name);
offset = TtaFileSeek (handle, offset - 4 * sizeof (space), ThotFile_SEEKSET);
if (offset == ThotFile_BADOFFSET)
{
printf ("ThotFile_test: offset == ThotFile_BADOFFSET\n");
return;
}
printf ("ThotFile_test: reading %s\n", name);
for (i -= 4; 1; i++)
{
int red;
red = TtaFileRead (handle, space, sizeof (space));
if (red == -1)
{
printf ("ThotFile_test: bad write\n");
break;
}
if (red == 0)
{
printf ("ThotFile_test: end of file %s\n", name);
break;
}
printf ("ThotFile_test: %s should == %d\n", space, i);
}
printf ("ThotFile_test: closing %s\n", name);
if (TtaFileClose (handle) == 0)
{
printf ("ThotFile_test: bad write\n");
return;
}
}
#endif /* INCLUDE_TESTING_CODE */
/*----------------------------------------------------------------------
TtaFileOpen returns: ThotFile_BADHANDLE: error handle:
----------------------------------------------------------------------*/
#ifdef __STDC__
ThotFileHandle TtaFileOpen (CONST char *name, ThotFileMode mode)
#else /* __STDC__ */
ThotFileHandle TtaFileOpen (name, mode)
CONST char *name;
ThotFileMode mode;
#endif /* __STDC__ */
{
ThotFileHandle ret;
#if defined(_WINDOWS) && !defined(__GNUC__)
DWORD access = 0; /* access (read-write) mode */
SECURITY_ATTRIBUTES secAttribs;
DWORD creation; /* how to create */
secAttribs.nLength = sizeof (secAttribs);
secAttribs.lpSecurityDescriptor = NULL;
secAttribs.bInheritHandle = TRUE;
if (mode & ThotFile_READ)
access |= GENERIC_READ;
if (mode & ThotFile_WRITE)
access |= GENERIC_WRITE;
if (mode & ThotFile_CREATE && mode & ThotFile_TRUNCATE)
creation = CREATE_ALWAYS;
else if (mode & ThotFile_CREATE && mode & ThotFile_EXCLUSIVE)
creation = CREATE_NEW;
else if (mode & ThotFile_TRUNCATE && !(mode & ThotFile_CREATE))
creation = TRUNCATE_EXISTING;
else if (mode & ThotFile_CREATE)
creation = OPEN_ALWAYS;
else
creation = OPEN_EXISTING;
ret = CreateFile (name, access, FILE_SHARE_READ, &secAttribs, creation, FILE_ATTRIBUTE_NORMAL, NULL);
#else /* _WINDOWS && !__GNUC__ */
#ifdef _WINDOWS_
ret = open (name, mode | _O_BINARY, 0777);
#else
ret = open (name, mode, 0777);
#endif
#endif /* _WINDOWS && !__GNUC__ */
return ret;
}
/*----------------------------------------------------------------------
TtaFileClose returns, 0: error, 1: OK.
----------------------------------------------------------------------*/
#ifdef __STDC__
int TtaFileClose (ThotFileHandle handle)
#else /* __STDC__ */
int TtaFileClose (handle)
ThotFileHandle handle;
#endif /* __STDC__ */
{
int ret;
#if defined(_WINDOWS) && !defined(__GNUC__)
ret = CloseHandle (handle);
#else /* _WINDOWS && !__GNUC__ */
ret = close (handle) == 0;
#endif /* _WINDOWS && !__GNUC__ */
return ret;
}
/*----------------------------------------------------------------------
TtaFileRead returns +n: number of bytes read, 0: at EOF, -1: error
----------------------------------------------------------------------*/
#ifdef __STDC__
int TtaFileRead (ThotFileHandle handle, void *buffer, unsigned int count)
#else /* __STDC__ */
int TtaFileRead (handle, buffer, count)
ThotFileHandle handle;
void *buffer;
unsigned int count;
#endif /* __STDC__ */
{
int ret;
#if defined(_WINDOWS) && !defined(__GNUC__)
DWORD red;
ret = ReadFile (handle, buffer, count, &red, 0); /* OK as long as we don't open for overlapped IO */
if (ret == TRUE)
ret = (int) red;
else
ret = -1;
#else /* _WINDOWS && !__GNUC__ */
ret = read (handle, buffer, count);
#endif /* _WINDOWS && !__GNUC__ */
return ret;
}
/*----------------------------------------------------------------------
TtaFileWrite returns: n: number of bytes written, -1: error
----------------------------------------------------------------------*/
#ifdef __STDC__
int TtaFileWrite (ThotFileHandle handle, void *buffer, unsigned int count)
#else /* __STDC__ */
int TtaFileWrite (handle, buffer, count)
ThotFileHandle handle;
void *buffer;
unsigned int count;
#endif /* __STDC__ */
{
int ret;
#if defined(_WINDOWS) && !defined(__GNUC__)
DWORD writ;
/* OK as long as we don't open for overlapped IO */
ret = WriteFile (handle, buffer, count, &writ, NULL);
if (ret == TRUE)
ret = (int) writ;
else
ret = -1;
#else /* _WINDOWS && !__GNUC__ */
ret = write (handle, buffer, count);
#endif /* _WINDOWS && !__GNUC__ */
return ret;
}
/*----------------------------------------------------------------------
TtaFileSeek returns: ThotFile_BADOFFSET: error, ThotFileOffset
----------------------------------------------------------------------*/
#ifdef __STDC__
ThotFileOffset TtaFileSeek (ThotFileHandle handle, ThotFileOffset offset, ThotFileOrigin origin)
#else /* __STDC__ */
ThotFileOffset TtaFileSeek (handle, offset, origin)
ThotFileHandle handle;
ThotFileOffset offset;
ThotFileOrigin origin;
#endif /* __STDC__ */
{
ThotFileOffset ret;
#if defined(_WINDOWS) && !defined(__GNUC__)
ret = SetFilePointer (handle, offset, 0, origin);
#else /* _WINDOWS && !__GNUC__ */
ret = lseek (handle, offset, origin);
#endif /* _WINDOWS && !__GNUC__ */
return ret;
}
/*----------------------------------------------------------------------
TtaFileSeek returns: 1: your data is all there, sir, 0: error
----------------------------------------------------------------------*/
#ifdef __STDC__
int TtaFileStat (ThotFileHandle handle, ThotFileInfo * pInfo)
#else /* __STDC__ */
int TtaFileStat (handle, pInfo)
ThotFileHandle handle;
ThotFileInfo *pInfo;
#endif /* __STDC__ */
{
ThotFileOffset ret;
#if defined(_WINDOWS) && !defined(__GNUC__)
BY_HANDLE_FILE_INFORMATION info;
ret = GetFileInformationByHandle (handle, &info);
if (ret)
{
pInfo->atime = info.ftLastAccessTime;
pInfo->size = info.nFileSizeLow;
}
#else /* _WINDOWS && !__GNUC__ */
struct stat buf;
ret = fstat (handle, &buf) == 0;
if (ret)
{
pInfo->atime = buf.st_atime;
pInfo->size = buf.st_size;
}
#endif /* _WINDOWS && !__GNUC__ */
return ret;
}
/*----------------------------------------------------------------------
TtaFileCopy copies a source file into a target file.
----------------------------------------------------------------------*/
#ifdef __STDC__
void TtaFileCopy (CONST char *sourceFileName, CONST char *targetFileName)
#else
void TtaFileCopy (sourceFileName, targetFileName)
CONST char *sourceFileName;
CONST char *targetFileName;
#endif
{
FILE *targetf;
FILE *sourcef;
int size;
char buffer[8192];
if (strcmp (sourceFileName, targetFileName) != 0)
{
#ifdef _WINDOWS
if ((targetf = fopen (targetFileName, "wb")) == NULL)
#else
if ((targetf = fopen (targetFileName, "w")) == NULL)
#endif
/* cannot write into the target file */
return;
else
{
#ifdef _WINDOWS
if ((sourcef = fopen (sourceFileName, "rb")) == NULL)
#else
if ((sourcef = fopen (sourceFileName, "r")) == NULL)
#endif
{
/* cannot read the source file */
fclose (targetf);
unlink (targetFileName);
return;
}
else
{
/* copy the file contents */
while ((size = fread (buffer, 1, 8192, sourcef)) != 0)
fwrite (buffer, 1, size, targetf);
fclose (sourcef);
}
fclose (targetf);
}
}
}
/*----------------------------------------------------------------------
TtaCompareFiles
Compare the content of two files.
Returns FALSE if one of the files is not available for reading or
if their content differs, TRUE if they are identical.
----------------------------------------------------------------------*/
#ifdef __STDC__
boolean TtaCompareFiles(CONST char *file1, CONST char *file2)
#else
boolean TtaCompareFiles(file1, file2)
CONST char *file1;
CONST char *file2;
#endif
{
FILE *f1;
FILE *f2;
char buffer1[512];
char buffer2[512];
size_t res1;
size_t res2;
if (file1 == NULL) return(FALSE);
if (file2 == NULL) return(FALSE);
#ifdef _WINDOWS
f1 = fopen(file1,"rb");
#else
f1 = fopen(file1,"r");
#endif
if (f1 == NULL) return(FALSE);
#ifdef _WINDOWS
f2 = fopen(file2,"rb");
#else
f2 = fopen(file2,"r");
#endif
if (f2 == NULL) {
fclose(f1);
return(FALSE);
}
while (1) {
res1 = fread(&buffer1[0], 1, sizeof(buffer1), f1);
res2 = fread(&buffer2[0], 1, sizeof(buffer2), f2);
if (res1 != res2) {
fclose(f1);
fclose(f2);
return(FALSE);
}
if (memcmp(&buffer1[0], &buffer2[0], res2)) {
fclose(f1);
fclose(f2);
return(FALSE);
}
res1 = feof(f1);
res2 = feof(f2);
if (res1 != res2) {
fclose(f1);
fclose(f2);
return(FALSE);
}
if (res1) break;
}
fclose(f1);
fclose(f2);
return(TRUE);
}
|