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 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564
|
/* ncbifile.c
* ===========================================================================
*
* PUBLIC DOMAIN NOTICE
* National Center for Biotechnology Information
*
* This software/database is a "United States Government Work" under the
* terms of the United States Copyright Act. It was written as part of
* the author's official duties as a United States Government employee and
* thus cannot be copyrighted. This software/database is freely available
* to the public for use. The National Library of Medicine and the U.S.
* Government have not placed any restriction on its use or reproduction.
*
* Although all reasonable efforts have been taken to ensure the accuracy
* and reliability of the software and data, the NLM and the U.S.
* Government do not and cannot warrant the performance or results that
* may be obtained by using this software or data. The NLM and the U.S.
* Government disclaim all warranties, express or implied, including
* warranties of performance, merchantability or fitness for any particular
* purpose.
*
* Please cite the author in any work or product based on this material.
*
* ===========================================================================
*
* File Name: ncbifile.c
*
* Author: Gish, Kans, Ostell, Schuler
*
* Version Creation Date: 3/4/91
*
* $Revision: 6.43 $
*
* File Description:
* portable file routines
*
* Modifications:
* --------------------------------------------------------------------------
* Date Name Description of modification
* ------- ---------- -----------------------------------------------------
*
* ==========================================================================
*/
#define THIS_MODULE g_corelib
#define THIS_FILE _this_file
#include <ncbilcl.h>
#include <assert.h>
#include "corepriv.h"
#ifdef OS_MAC
#ifdef OS_UNIX_DARWIN
#include <Carbon.h>
#endif
#include <Navigation.h>
#include <Script.h>
#endif
#ifdef OS_UNIX_SUN
#define DEFAULT_CDROM "/dev/sr0"
#define DEFAULT_RAW_CDROM "/dev/rsr0"
#endif
#if defined(PROC_MIPS) && !defined(OS_UNIX_LINUX)
#define DEFAULT_CDROM "/dev/scsi/sc0d4l0"
#endif
#ifdef OS_UNIX
#ifndef DEFAULT_CDROM
#define DEFAULT_CDROM "/dev/cdrom"
#endif
#endif
#if defined(OS_MSWIN) || defined (OS_NT)
#ifdef COMP_MSC
#ifndef mkdir
#define mkdir _mkdir
#endif
#ifndef stat
#define stat _stat
#endif
#endif
#endif
#ifdef OS_VMS
#ifndef DEFAULT_CDROM
#define DEFAULT_CDROM "cdrom:"
#endif
#endif
extern char *g_corelib;
static char * _this_file = __FILE__;
#ifdef OS_MAC
#define INLINE_MOREFILES
/* #include "FullPath.h" */
/* #include "MoreFilesExtra.h" */
#ifdef INLINE_MOREFILES
/* MoreFilesExtras.c */
/* ----------------- */
/*
** Apple Macintosh Developer Technical Support
**
** A collection of useful high-level File Manager routines.
**
** by Jim Luther, Apple Developer Technical Support Emeritus
**
** File: MoreFilesExtras.c
**
** Copyright (c) 1992-1999 Apple Computer, Inc.
** All rights reserved.
**
** You may incorporate this sample code into your applications without
** restriction, though the sample code has been provided "AS IS" and the
** responsibility for its operation is 100% yours. However, what you are
** not permitted to do is to redistribute the source as "DSC Sample Code"
** after having made changes. If you're going to re-distribute the source,
** we require that you make it clear in the source that the code was
** descended from Apple Sample Code, but that you've made changes.
*/
#include <Types.h>
#include <Traps.h>
#include <OSUtils.h>
#include <Errors.h>
#include <Files.h>
#include <Devices.h>
#include <Finder.h>
#include <Folders.h>
#include <FSM.h>
#include <Disks.h>
#include <Gestalt.h>
#include <TextUtils.h>
#include <Script.h>
#include <Math64.h>
#include <CodeFragments.h>
#include <stddef.h>
#define __COMPILINGMOREFILES
#if 0
#include "MoreFiles.h"
#include "MoreFilesExtras.h"
#include "MoreDesktopMgr.h"
#include "FSpCompat.h"
#endif
/*
** GetVolumeInfoNoName uses pathname and vRefNum to call PBHGetVInfoSync
** in cases where the returned volume name is not needed by the caller.
** The pathname and vRefNum parameters are not touched, and the pb
** parameter is initialized by PBHGetVInfoSync except that ioNamePtr in
** the parameter block is always returned as NULL (since it might point
** to the local tempPathname).
**
** I noticed using this code in several places, so here it is once.
** This reduces the code size of MoreFiles.
*/
static pascal OSErr GetVolumeInfoNoName(ConstStr255Param pathname,
short vRefNum,
HParmBlkPtr pb)
{
Str255 tempPathname;
OSErr error;
/* Make sure pb parameter is not NULL */
if ( pb != NULL )
{
pb->volumeParam.ioVRefNum = vRefNum;
if ( pathname == NULL )
{
pb->volumeParam.ioNamePtr = NULL;
pb->volumeParam.ioVolIndex = 0; /* use ioVRefNum only */
}
else
{
BlockMoveData(pathname, tempPathname, pathname[0] + 1); /* make a copy of the string and */
pb->volumeParam.ioNamePtr = (StringPtr)tempPathname; /* use the copy so original isn't trashed */
pb->volumeParam.ioVolIndex = -1; /* use ioNamePtr/ioVRefNum combination */
}
error = PBHGetVInfoSync(pb);
pb->volumeParam.ioNamePtr = NULL; /* ioNamePtr may point to local tempPathname, so don't return it */
}
else
{
error = paramErr;
}
return ( error );
}
/*****************************************************************************/
static pascal OSErr DetermineVRefNum(ConstStr255Param pathname,
short vRefNum,
short *realVRefNum)
{
HParamBlockRec pb;
OSErr error;
error = GetVolumeInfoNoName(pathname,vRefNum, &pb);
if ( error == noErr )
{
*realVRefNum = pb.volumeParam.ioVRefNum;
}
return ( error );
}
/*****************************************************************************/
static pascal OSErr GetCatInfoNoName(short vRefNum,
long dirID,
ConstStr255Param name,
CInfoPBPtr pb)
{
Str31 tempName;
OSErr error;
/* Protection against File Sharing problem */
if ( (name == NULL) || (name[0] == 0) )
{
tempName[0] = 0;
pb->dirInfo.ioNamePtr = tempName;
pb->dirInfo.ioFDirIndex = -1; /* use ioDirID */
}
else
{
pb->dirInfo.ioNamePtr = (StringPtr)name;
pb->dirInfo.ioFDirIndex = 0; /* use ioNamePtr and ioDirID */
}
pb->dirInfo.ioVRefNum = vRefNum;
pb->dirInfo.ioDrDirID = dirID;
error = PBGetCatInfoSync(pb);
pb->dirInfo.ioNamePtr = NULL;
return ( error );
}
/*****************************************************************************/
static pascal OSErr GetDirectoryID(short vRefNum,
long dirID,
ConstStr255Param name,
long *theDirID,
Boolean *isDirectory)
{
CInfoPBRec pb;
OSErr error;
error = GetCatInfoNoName(vRefNum, dirID, name, &pb);
if ( error == noErr )
{
*isDirectory = (pb.hFileInfo.ioFlAttrib & kioFlAttribDirMask) != 0;
if ( *isDirectory )
{
*theDirID = pb.dirInfo.ioDrDirID;
}
else
{
*theDirID = pb.hFileInfo.ioFlParID;
}
}
return ( error );
}
/*****************************************************************************/
static pascal OSErr FSpGetDirectoryID(const FSSpec *spec,
long *theDirID,
Boolean *isDirectory)
{
return ( GetDirectoryID(spec->vRefNum, spec->parID, spec->name,
theDirID, isDirectory) );
}
#endif /* INLINE_MOREFILES */
#endif /* OS_MAC */
/*****************************************************************************
*
* Macintosh file utilities
*
*****************************************************************************/
#ifdef OS_MAC
static OSErr MacPathname2FSSpec(const char *inPathname, FSSpec *outFSS)
{
OSErr err;
size_t len;
char *p;
short vRefNum;
long dirID;
FSSpec fss;
if (inPathname == NULL || outFSS == NULL) {
return paramErr;
}
err = HGetVol(NULL, &vRefNum, &dirID); /* default volume and directory */
if (err != noErr) return err;
len = strlen(inPathname);
p = strchr(inPathname, ':');
if (p == NULL) {
/* Partial pathname -- filename only */
Str31 filename;
assert(len <= 31);
c2pstrcpy(filename, inPathname);
err = FSMakeFSSpec(vRefNum, dirID, filename, outFSS);
} else {
Str31 name;
int nameLen;
if (inPathname[0] == ':') {
/* Relative pathname including directory path */
} else {
/* Absolute pathname */
/* Str31 volName; We would use Str28 if it was defined -- 27, plus 1 for ':'. */
nameLen = p - inPathname;
assert(nameLen <= 27);
name[0] = nameLen + 1;
memcpy(name + 1, inPathname, nameLen + 1); /* Copy the volume name and the colon. */
err = DetermineVRefNum(name, 0, &vRefNum);
if (err != noErr) return err;
dirID = 2;
}
/* vRefNum and dirID now specify the directory in which we should descend
the path pointed to by p (pointing to the first colon). */
p++;
while (p != NULL && *p != '\0') {
char *q = strchr(p, ':');
if (q != NULL) {
Boolean isDir;
nameLen = q - p;
assert(nameLen <= 31);
name[0] = nameLen;
memcpy(name + 1, p, nameLen);
err = FSMakeFSSpec(vRefNum, dirID, name, &fss);
if (err != noErr) return err;
if (q[1] == '\0') {
p = NULL;
*outFSS = fss;
} else {
err = FSpGetDirectoryID(&fss, &dirID, &isDir);
assert(isDir == true);
if (err != noErr) return err;
p = q + 1;
}
} else {
q = strchr(p, '\0'); /* go to end of string */
nameLen = q - p;
assert(nameLen > 0);
assert(nameLen <= 31);
c2pstrcpy(name, p);
p = NULL;
err = FSMakeFSSpec(vRefNum, dirID, name, outFSS);
}
}
}
return err;
}
#if 0
static OSErr MacFSSpec2FullPathname(const FSSpec *inFSS, char **outPathname)
{
OSErr err;
Handle h;
short fullPathLength;
static char *fullPath = NULL;
if (fullPath != NULL) {
Nlm_Free(fullPath);
fullPath = NULL;
}
err = FSpGetFullPath(inFSS, &fullPathLength, &h);
if (err != noErr) return err;
assert(fullPathLength >= 2); /* An absolute pathname must be at least two chars long */
fullPath = (char *)Nlm_Malloc(fullPathLength + 1);
if (fullPath == NULL) {
err = memFullErr;
} else {
strncpy(fullPath, *h, fullPathLength);
}
DisposeHandle(h);
*outPathname = fullPath;
return err;
}
#endif
static OSErr MacCreateDirectory(const char *inPathname)
{
OSErr err;
FSSpec fss;
ScriptCode scriptTag = 0;
long createdDirID;
err = MacPathname2FSSpec(inPathname, &fss);
if (err != noErr) return err;
err = FSpDirCreate(&fss, scriptTag, &createdDirID);
return err;
}
#endif
/*****************************************************************************
*
* FileOpen(filename, mode)
* if (filename == "stdin" or "stdout" or "stderr"
* returns those predefined
* streams on non-windowing systems)
*
*****************************************************************************/
/* p_churchill 12/99 removed MPW conditional compilation
*/
static Nlm_FileOpenHook _hookFile = NULL;
NLM_EXTERN FILE * LIBCALL Nlm_FileOpen(const char *filename, const char *mode)
{
FILE *f = NULL;
if ( _hookFile )
return _hookFile(filename, mode);
#if defined(WIN_DUMB)
if ( Nlm_HasConsole )
{
if ( !StringCmp("stdin", filename))
f = stdin;
else if ( !StringCmp("stdout", filename) )
f = stdout;
else if ( !StringCmp("stderr", filename))
f = stderr;
else
f = fopen(filename, mode);
#if defined(WIN32) && ! defined(COMP_METRO)
if (strchr(mode, 'b') &&
(f == stdin || f == stdout || f == stderr))
setmode(fileno(f), O_BINARY);
#endif
}
else
f = fopen(filename, mode);
#elif defined(OS_MAC)
{
OSType fCreator;
Nlm_Int2 fError;
FInfo fInfo;
OSType fType;
Nlm_Char temp [256];
Nlm_StringNCpy_0(temp, filename, sizeof(temp));
Nlm_CtoPstr ((Nlm_CharPtr) temp);
fError = HGetFInfo( 0, 0, (StringPtr) temp, &fInfo);
if (fError == noErr) {
fCreator = fInfo.fdCreator;
fType = fInfo.fdType;
} else {
fCreator = ' ';
if (strchr(mode, 'b') != NULL)
fType = ' ';
else
fType = 'TEXT';
}
f = fopen( filename, mode);
fError = HGetFInfo( 0, 0, (StringPtr) temp, &fInfo);
if (fError == noErr) {
fInfo.fdCreator = fCreator;
fInfo.fdType = fType;
fError = HSetFInfo ( 0, 0, (StringPtr) temp,&fInfo);
}
} /* def OS_MAC */
#elif defined(OS_VMS) && defined(DCC4DW12)
/* never used */
f = fopen (filename, mode);
if (f &&
fstat(fileno(f), &statbuf) == 0 &&
statbuf.st_fab_rfm == FAB$C_UDF)
{
fclose(f);
f = fopen(filename,mode,"ctx=stm");
}
#else
f = fopen(filename, mode);
#endif
if (f == NULL)
ErrPostEx(SEV_INFO, E_File, E_FOpen, "FileOpen(\"%s\",\"%s\") failed",
filename, mode);
return f;
}
/*****************************************************************************
*
* SetFileOpenHook(hook)
*
*****************************************************************************/
NLM_EXTERN void LIBCALL Nlm_SetFileOpenHook (Nlm_FileOpenHook hook)
{
_hookFile = hook;
}
/*****************************************************************************
*
* FileClose(fp)
*
*****************************************************************************/
NLM_EXTERN void LIBCALL Nlm_FileClose (FILE *stream)
{
if (stream == NULL)
return;
#ifdef WIN_DUMB
if (stream == stdin || stream == stdout || stream == stderr)
{
#if defined(WIN32) && ! defined(COMP_METRO)
setmode(fileno(stream), O_TEXT);
#endif
return;
}
#endif
fclose(stream);
}
/*****************************************************************************
* FileRead(buf, size, fp)
*****************************************************************************/
NLM_EXTERN size_t LIBCALL Nlm_FileRead
(void *ptr, size_t size, size_t n, FILE *stream)
{
if (n && (SIZE_MAX / n) < size) {
ErrPostEx(SEV_WARNING,E_Programmer,0,"FileRead: size > SIZE_MAX");
return 0;
}
if (!ptr || !stream)
return 0;
return fread(ptr,size,n,stream);
}
/*****************************************************************************
* FileWrite(buf, size, fp)
*****************************************************************************/
NLM_EXTERN size_t LIBCALL Nlm_FileWrite
(const void *ptr, size_t size, size_t n, FILE *stream)
{
size_t cnt;
if (n && (SIZE_MAX / n) < size) {
ErrPostEx(SEV_WARNING,E_Programmer,0,"FileWrite: size > SIZE_MAX");
return 0;
}
if (!ptr || !stream || !size)
return 0;
cnt = fwrite(ptr,size,n,stream);
if (cnt != n)
ErrPostEx(SEV_FATAL,E_File,E_FWrite,"File write error");
return cnt;
}
/*****************************************************************************
*
* FilePuts(ptr, fp)
*
*****************************************************************************/
NLM_EXTERN int LIBCALL Nlm_FilePuts (const char *ptr, FILE *fp)
{
int retval;
if ((ptr == NULL) || (fp == NULL))
return EOF;
if ((retval = fputs(ptr,fp)) ==EOF)
ErrPostEx(SEV_FATAL,E_File,E_FWrite,"File write error");
return retval;
}
/*****************************************************************************
*
* FileGets()
*
*****************************************************************************/
NLM_EXTERN char * LIBCALL Nlm_FileGets (Nlm_CharPtr ptr, size_t size, FILE *fp)
{
#if defined(OS_MAC) || defined (OS_UNIX_DARWIN)
int ch;
int count;
Nlm_CharPtr tmp;
#endif
if ((ptr == NULL) || (size < 1) || (fp == NULL))
return NULL;
#if defined(OS_MAC) || defined (OS_UNIX_DARWIN)
ch = fgetc (fp);
count = 0;
tmp = ptr;
while (ch != EOF && ch != '\0' && ch != '\n' && ch != '\r' && count < size - 2) {
*tmp = ch;
tmp++;
count++;
ch = fgetc (fp);
}
if (ch == '\n' || ch == '\r') {
*tmp = '\n';
tmp++;
count++;
} else if (ch != EOF && ch != '\0') {
*tmp = ch;
tmp++;
count++;
}
*tmp = '\0';
if (count < 1)
return NULL;
return ptr;
#else
return fgets(ptr,size,fp);
#endif
}
/*****************************************************************************
*
* FileBuildPath()
*
*****************************************************************************/
NLM_EXTERN Nlm_CharPtr LIBCALL Nlm_FileBuildPath (Nlm_CharPtr root, Nlm_CharPtr sub_path, Nlm_CharPtr filename)
{
Nlm_CharPtr tmp;
Nlm_Boolean dir_start = FALSE;
#ifdef OS_VMS
Nlm_Boolean had_root = FALSE;
#endif
if (root == NULL) /* no place to put it */
return NULL;
tmp = root;
if (*tmp != '\0') /* if not empty */
{
#ifndef OS_VMS
dir_start = TRUE;
#else
had_root = TRUE;
#endif
while (*tmp != '\0')
{
#ifdef OS_VMS
if (*tmp == '[')
dir_start = TRUE;
#endif
tmp++;
}
if ((*(tmp - 1) != DIRDELIMCHR) && (dir_start))
{
*tmp = DIRDELIMCHR;
tmp++; *tmp = '\0';
}
}
if (sub_path != NULL)
{
#ifdef OS_VMS
if (dir_start)
{
*(tmp-1) = '.';
if (*sub_path == '[')
sub_path++;
}
else if ((had_root) && (*sub_path != '['))
{
*tmp = '[';
tmp++; *tmp = '\0';
}
#else
if ((dir_start) && (*sub_path == DIRDELIMCHR))
sub_path++;
#endif
tmp = StringMove(tmp, sub_path);
if (*(tmp-1) != DIRDELIMCHR)
{
*tmp = DIRDELIMCHR;
tmp++; *tmp = '\0';
}
}
if (filename != NULL)
StringMove(tmp, filename);
return root;
}
/*****************************************************************************
*
* FileNameFind()
*
*****************************************************************************/
NLM_EXTERN Nlm_CharPtr LIBCALL Nlm_FileNameFind (Nlm_CharPtr pathname)
{
Nlm_CharPtr filename;
Nlm_Int2 len;
if (pathname != NULL) {
len = Nlm_StringLen (pathname);
filename = &(pathname [len]);
while (len > 0 && pathname [len - 1] != DIRDELIMCHR) {
len--;
filename--;
}
return filename;
} else {
return NULL;
}
}
/*****************************************************************************
*
* FilePathFind()
*
*****************************************************************************/
NLM_EXTERN Nlm_CharPtr LIBCALL Nlm_FilePathFind(const Nlm_Char* fullname)
{
Nlm_CharPtr str;
size_t len = Nlm_StringLen(fullname);
if ( !len )
return 0;
while (len && fullname[len] != DIRDELIMCHR)
len--;
str = (Nlm_Char*)Nlm_MemGet(len + 1, MGET_ERRPOST);
Nlm_MemCpy(str, fullname, len);
str[len] = '\0';
return str;
}
/*****************************************************************************
*
* FileLength()
*
*****************************************************************************/
NLM_EXTERN Nlm_Int8 LIBCALL Nlm_FileLength(Nlm_CharPtr fileName)
{
Nlm_Int8 file_len = Nlm_FileLengthEx(fileName);
return (file_len > 0) ? file_len : 0;
}
/*****************************************************************************
*
* FileLengthEx()
*
*****************************************************************************/
NLM_EXTERN Nlm_Int8 LIBCALL Nlm_FileLengthEx(const Nlm_Char* fileName)
{
if (!fileName || !*fileName)
return -1;
#ifdef OS_MAC
{{
OSErr err;
HParamBlockRec params;
Nlm_Char path[256];
Nlm_MemSet ((Nlm_VoidPtr) ¶ms, 0, sizeof (HParamBlockRec));
Nlm_StringNCpy_0(path, fileName, sizeof(path));
Nlm_CtoPstr((Nlm_CharPtr) path);
params.fileParam.ioNamePtr = (StringPtr)path;
params.fileParam.ioVRefNum = 0;
params.fileParam.ioFDirIndex = 0;
err = PBHGetFInfo(¶ms, FALSE);
return (err == noErr) ?
params.fileParam.ioFlLgLen : -1;
}}
#else
{{
struct stat sbuf;
return (stat(fileName, &sbuf) == 0) ? sbuf.st_size : -1;
}}
#endif
}
/*****************************************************************************
*
* FileDelete()
*
*****************************************************************************/
NLM_EXTERN Nlm_Boolean LIBCALL Nlm_FileRemove (Nlm_CharPtr fileName)
{
Nlm_Char local [256];
if (fileName != NULL && fileName [0] != '\0') {
Nlm_StringNCpy_0(local, fileName, sizeof(local));
return (Nlm_Boolean) (remove (local) == 0);
} else {
return FALSE;
}
}
/*****************************************************************************
*
* FileRename()
*
*****************************************************************************/
NLM_EXTERN Nlm_Boolean LIBCALL Nlm_FileRename (Nlm_CharPtr oldFileName, Nlm_CharPtr newFileName)
{
Nlm_Char localnew [256];
Nlm_Char localold [256];
if (oldFileName != NULL && oldFileName [0] != '\0'
&& newFileName != NULL && newFileName [0] != '\0') {
Nlm_StringNCpy_0(localold, oldFileName, sizeof(localold));
Nlm_StringNCpy_0(localnew, newFileName, sizeof(localnew));
return (Nlm_Boolean) (rename (localold, localnew) == 0);
} else {
return FALSE;
}
}
/*****************************************************************************
*
* FileCreate()
*
*****************************************************************************/
#ifdef OS_MAC
static OSType Nlm_GetOSType (Nlm_CharPtr str, OSType dfault)
{
OSType rsult;
rsult = dfault;
if (str != NULL && str [0] != '\0') {
rsult = *(OSType*) str;
}
return rsult;
}
#endif
NLM_EXTERN void LIBCALL Nlm_FileCreate (Nlm_CharPtr fileName, Nlm_CharPtr type, Nlm_CharPtr creator)
{
#ifdef OS_MAC
Nlm_Int2 fError;
Nlm_Char temp [256];
OSType fType;
OSType fCreator;
FSSpec spec;
#else
FILE *fp;
#endif
if (fileName != NULL && fileName [0] != '\0') {
#ifdef OS_MAC
/* note: the following assumes either full pathname or that the current
directory is the proper location to find/create the file */
Nlm_StringNCpy_0(temp, fileName, sizeof(temp));
Nlm_CtoPstr ( temp);
fError = FSMakeFSSpec( 0, 0, (StringPtr)temp, &spec);
/* file not found, so create it... */
if( fError == fnfErr){
fType = Nlm_GetOSType (type, 'TEXT');
fCreator = Nlm_GetOSType (creator, ' ');
FSpCreate( &spec, fCreator, fType, smSystemScript);
}
#else
fp = Nlm_FileOpen (fileName, "w");
if (fp != NULL) {
Nlm_FileClose (fp);
}
#endif
}
}
/*****************************************************************************
*
* CreateDir(pathname)
*
*****************************************************************************/
#ifdef OS_MAC
NLM_EXTERN Nlm_Boolean LIBCALL Nlm_CreateDir (Nlm_CharPtr pathname)
{
if (pathname != NULL && pathname [0] != '\0') {
OSErr err;
err = MacCreateDirectory(pathname);
return (Nlm_Boolean) (err == noErr || err == dupFNErr);
}
return FALSE;
}
#endif
#ifndef OS_MAC
NLM_EXTERN Nlm_Boolean LIBCALL Nlm_CreateDir (Nlm_CharPtr pathname)
{
#ifndef OS_VMS
size_t len;
Nlm_Char path[PATH_MAX];
#endif
#ifdef OS_UNIX
mode_t oldmask;
#endif
Nlm_Boolean rsult = FALSE;
if (pathname != NULL && pathname [0] != '\0') {
#if defined(OS_MSWIN) || defined(OS_NT)
Nlm_StringNCpy_0(path, pathname, sizeof(path));
len = Nlm_StringLen (path);
if (len > 0 && path [len - 1] == DIRDELIMCHR) {
path [len - 1] = '\0';
}
rsult = (Nlm_Boolean) (mkdir ((char *) path) == 0);
if (errno == EACCES) { /* it's O.K. if it was already there */
rsult = TRUE;
}
#endif
#ifdef OS_UNIX
oldmask = umask (0000);
Nlm_StringNCpy_0(path, pathname, sizeof(path));
len = Nlm_StringLen (path);
if (len > 0 && path [len - 1] == DIRDELIMCHR) {
path [len - 1] = '\0';
}
rsult = (Nlm_Boolean) (mkdir ((char *) path, 0755) == 0);
if (errno == EEXIST) { /* it's O.K. if it was already there */
rsult = TRUE;
}
umask (oldmask);
#endif
#ifdef OS_VMS
rsult = (Nlm_Boolean) (mkdir ((char *) pathname, 0755) == 0);
#endif
}
return rsult;
}
#endif
/*****************************************************************************
*
* DirectoryContents()
*
*****************************************************************************/
#ifdef OS_UNIX_DARWIN
#include <dirent.h>
#endif
NLM_EXTERN ValNodePtr LIBCALL Nlm_DirCatalog (Nlm_CharPtr pathname)
{
#ifdef OS_MAC
long dirID;
OSErr err;
short index;
unsigned short num;
Nlm_Char path[PATH_MAX];
CInfoPBRec pbc;
HParamBlockRec pbh;
short vRefNum;
#endif
#ifdef OS_UNIX
Nlm_Uint1 choice;
#ifdef OS_UNIX_DARWIN
DIR *dirp;
struct dirent *dep;
#else
Nlm_Char buf [256];
Nlm_Char ch;
Nlm_Char cmmd [PATH_MAX + 20];
FILE *fp;
Nlm_CharPtr ptr;
#endif
#endif
ValNodePtr vnp = NULL;
if (pathname != NULL && pathname [0] != '\0') {
#ifdef OS_MAC
Nlm_StringNCpy_0 (path, pathname, sizeof (path));
Nlm_CtoPstr ((Nlm_CharPtr) path);
Nlm_MemSet ((Nlm_VoidPtr) (&pbh), 0, sizeof (HParamBlockRec));
pbh.volumeParam.ioNamePtr = (StringPtr) path;
pbh.volumeParam.ioVolIndex = -1;
err = PBHGetVInfo (&pbh, FALSE);
if (err != noErr) return NULL;
vRefNum = pbh.volumeParam.ioVRefNum;
Nlm_StringNCpy_0 (path, pathname, sizeof (path));
Nlm_CtoPstr ((Nlm_CharPtr) path);
Nlm_MemSet ((Nlm_VoidPtr) (&pbc), 0, sizeof (CInfoPBRec));
pbc.dirInfo.ioNamePtr = (StringPtr) path;
pbc.dirInfo.ioVRefNum = vRefNum;
err = PBGetCatInfo (&pbc, FALSE);
if (err != noErr) return NULL;
if (pbc.dirInfo.ioFlAttrib & 16) {
num = pbc.dirInfo.ioDrNmFls;
dirID = pbc.dirInfo.ioDrDirID;
for (index = 1; index <= num; index++) {
Nlm_MemSet ((Nlm_VoidPtr) (&pbc), 0, sizeof (CInfoPBRec));
pbc.dirInfo.ioNamePtr = (StringPtr) path;
pbc.dirInfo.ioVRefNum = vRefNum;
pbc.dirInfo.ioFDirIndex = index;
pbc.dirInfo.ioDrDirID = dirID;
pbc.dirInfo.ioACUser = 0;
err = PBGetCatInfo (&pbc, FALSE);
if (err == noErr) {
Nlm_PtoCstr ((Nlm_CharPtr) path);
if (pbc.dirInfo.ioFlAttrib & 16) {
ValNodeCopyStr (&vnp, 1, path);
} else {
ValNodeCopyStr (&vnp, 0, path);
}
}
}
}
#endif
#if defined(WIN32)
{{
Nlm_Char x_path[PATH_MAX];
WIN32_FIND_DATA fData;
HANDLE hFindFile;
Nlm_StringNCpy_0(x_path, pathname, sizeof(x_path) - 5);
Nlm_StringCat(x_path, "\\*.*");
hFindFile = FindFirstFile(x_path, &fData);
if (hFindFile == INVALID_HANDLE_VALUE)
return 0;
do {
if (fData.cFileName[0] != '.' ||
(fData.cFileName[1] != '.' && fData.cFileName[1] != '\0'))
ValNodeCopyStr
(&vnp, (fData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? 1 : 0,
fData.cFileName);
} while ( FindNextFile(hFindFile, &fData) );
FindClose(hFindFile);
}}
#endif
#ifdef OS_UNIX
#ifdef OS_UNIX_DARWIN
dirp = opendir(pathname);
if (dirp == NULL) return NULL;
while ((dep = readdir(dirp)) != NULL) {
/* ignore 'invisible' files. */
if (dep->d_namlen < 1 || dep->d_name[0] == '.')
continue;
if (dep->d_type == DT_DIR) /* directory */
choice = 1;
else /* all other file types. */
choice = 0;
ValNodeCopyStr (&vnp, choice, dep->d_name);
}
closedir(dirp);
#else
sprintf (cmmd, "ls -1p %s 2>/dev/null", pathname);
fp = popen (cmmd, "r");
if (fp == NULL) return NULL;
while (Nlm_FileGets (buf, sizeof (buf), fp) != NULL) {
ptr = buf;
ch = *ptr;
while (ch != '\0' && ch != '\n' && ch != '\r') {
ptr++;
ch = *ptr;
}
*ptr = '\0';
choice = 0;
ptr = Nlm_StringChr (buf, '/');
if (ptr != NULL) {
*ptr = '\0';
choice = 1;
}
ValNodeCopyStr (&vnp, choice, buf);
}
pclose (fp);
#endif
#endif
#ifdef OS_VMS
#endif
}
return vnp;
}
/*****************************************************************************
*
* general file recursion functio
*
*****************************************************************************/
NLM_EXTERN Nlm_Int4 Nlm_DirExplore (
Nlm_CharPtr directory,
Nlm_CharPtr filter,
Nlm_CharPtr suffix,
Nlm_Boolean recurse,
Nlm_DirExpProc proc,
Nlm_VoidPtr userdata
)
{
Nlm_Int4 count = 0;
Nlm_Char file [FILENAME_MAX], path [PATH_MAX];
Nlm_CharPtr ptr, str;
ValNodePtr head, vnp;
if (proc == NULL) return 0;
if (Nlm_StringHasNoText (directory) /* || Nlm_StringHasNoText (suffix) */ ) return 0;
/* get list of all files in source directory */
head = Nlm_DirCatalog (directory);
for (vnp = head; vnp != NULL; vnp = vnp->next) {
if (vnp->choice == 0) {
str = (Nlm_CharPtr) vnp->data.ptrvalue;
if (! Nlm_StringHasNoText (str)) {
/* check filename for indicated suffix */
ptr = Nlm_StringStr (str, suffix);
if (ptr != NULL) {
/* make sure detected suffix is really at end of filename */
if (Nlm_StringCmp (ptr, suffix) == 0) {
*ptr = '\0';
} else {
ptr = NULL;
}
}
if (Nlm_StringHasNoText (suffix) || ptr != NULL) {
Nlm_StringNCpy_0 (path, directory, sizeof (path));
sprintf (file, "%s%s", str, suffix);
Nlm_FileBuildPath (path, NULL, file);
/* check file name (NOT full path) for desired filter */
if (Nlm_StringHasNoText (filter) || Nlm_StringStr (file, filter) != NULL) {
/* process file that satisfies optional filter and suffix constraints */
proc (path, userdata);
count++;
}
}
}
} else if (vnp->choice == 1 && recurse) {
/* recurse into subdirectory */
Nlm_StringNCpy_0 (path, directory, sizeof (path));
str = (Nlm_CharPtr) vnp->data.ptrvalue;
Nlm_FileBuildPath (path, str, NULL);
count += Nlm_DirExplore (path, filter, suffix, recurse, proc, userdata);
}
}
/* clean up file list */
ValNodeFreeData (head);
return count;
}
/*****************************************************************************
*
* TmpNam()
*
*****************************************************************************/
NLM_EXTERN Nlm_CharPtr LIBCALL Nlm_TmpNam (Nlm_CharPtr s)
{
#ifdef TEMPNAM_AVAIL
char *filename;
static Nlm_Char save_filename[PATH_MAX];
/* emulate tmpnam(), except get the benefits of tempnam()'s ability to */
/* place the files in another directory specified by the environment */
/* variable TMPDIR */
#ifdef OS_UNIX_DARWIN
filename = tempnam("/tmp", "ncbi.");
#else
filename = tempnam(NULL, NULL);
#endif
if (s == NULL)
{ /* return pointer to static string */
if (filename != NULL) {
strcpy ((char *) save_filename, (char *) filename);
free ((void *) filename);
} else {
save_filename [0] = '\0';
}
return save_filename;
} else {
if (filename != NULL) {
strcpy ((char *) save_filename, (char *) filename);
Nlm_StrCpy (s, save_filename);
free ((void *) filename);
} else {
*s = '\0';
}
return s;
}
#else
#ifdef OS_MAC
static Nlm_Char directory [PATH_MAX];
OSErr err;
long gesResponse;
long newDirID;
short newVRefNum;
CInfoPBRec params;
Nlm_Char temp [PATH_MAX];
Nlm_CharPtr tmp;
Nlm_Boolean useTempFolder;
char * filename;
useTempFolder = FALSE;
if (! Gestalt (gestaltFindFolderAttr, &gesResponse) &&
(gesResponse & (1 << gestaltFindFolderPresent))) {
err = FindFolder(kOnSystemDisk, kTemporaryFolderType,
kCreateFolder, &newVRefNum, &newDirID);
if (err == noErr) {
useTempFolder = TRUE;
}
}
filename = tmpnam (NULL);
if (useTempFolder) {
temp [0] = '\0';
params.dirInfo.ioNamePtr = (StringPtr) directory;
params.dirInfo.ioDrParID = newDirID;
do {
params.dirInfo.ioVRefNum = newVRefNum;
params.dirInfo.ioFDirIndex = -1;
params.dirInfo.ioDrDirID = params.dirInfo.ioDrParID;
err = PBGetCatInfo (¶ms, FALSE);
Nlm_PtoCstr ((Nlm_CharPtr) directory);
Nlm_StringCat (directory, DIRDELIMSTR);
Nlm_StringCat (directory, temp);
Nlm_StringCpy (temp, directory);
} while (params.dirInfo.ioDrDirID != fsRtDirID);
tmp = Nlm_StringMove (directory, temp);
tmp = Nlm_StringMove (tmp, (Nlm_CharPtr) filename);
if (s == NULL) {
return (Nlm_CharPtr) directory;
} else {
s [0] = '\0';
Nlm_StringCpy (s, directory);
return s;
}
} else {
if (s == NULL) {
return (Nlm_CharPtr) filename;
} else {
s [0] = '\0';
Nlm_StringCpy (s, filename);
return s;
}
}
#else
char * filename;
filename = tmpnam (NULL);
if (s == NULL) {
return (Nlm_CharPtr) filename;
} else {
s [0] = '\0';
Nlm_StringCpy (s, filename);
return s;
}
#endif
#endif
}
/* FileCache provides buffered text read for handling Unix, Mac, and DOS line endings gracefully */
/* attach file pointer (text read mode expected) to cache object (usually on stack) */
#ifdef OS_MSWIN
#include <fcntl.h>
#include <io.h>
#endif
NLM_EXTERN Nlm_Boolean Nlm_FileCacheSetup (
Nlm_FileCache PNTR fcp,
FILE *fp
)
{
if (fp == NULL || fcp == NULL) return FALSE;
#ifdef OS_MSWIN
_setmode (_fileno (fp), _O_BINARY);
#endif
MemSet ((Nlm_VoidPtr) fcp, 0, sizeof (Nlm_FileCache));
fcp->fp = fp;
fcp->offset = ftell (fp);
return TRUE;
}
static void Nlm_FileCacheReadBlock (
Nlm_FileCache PNTR fcp
)
{
int total;
if (fcp == NULL || fcp->fp == NULL) return;
if (fcp->ctr >= fcp->total) {
fcp->offset += (Nlm_Int4) fcp->total;
fcp->ctr = 0;
fcp->total = 0;
fcp->buf [0] = '\0';
total = (int) Nlm_FileRead ((Nlm_VoidPtr) fcp->buf, sizeof (Nlm_Char), (size_t) 512, fcp->fp);
if (total < 0 || total > 512) {
total = 512;
}
fcp->buf [total] = '\0';
fcp->total = /* Nlm_StringLen (fcp->buf) */ total;
}
}
/* equivalent of getc */
static Nlm_Char Nlm_FileCacheGetChar (
Nlm_FileCache PNTR fcp
)
{
Nlm_Char ch = '\0', nxt;
if (fcp == NULL || fcp->fp == NULL) return ch;
/* read a fresh block if buffer is empty */
if (fcp->ctr >= fcp->total) {
Nlm_FileCacheReadBlock (fcp);
}
/* get next character in buffer */
if (fcp->ctr < fcp->total) {
ch = fcp->buf [(int) fcp->ctr];
(fcp->ctr)++;
}
if (ch == '\n' || ch == '\r') {
/* look for carriage return / linefeed pair - DOS file read on Mac or Unix platform */
if (fcp->ctr >= fcp->total) {
Nlm_FileCacheReadBlock (fcp);
}
if (fcp->ctr < fcp->total) {
nxt = fcp->buf [(int) fcp->ctr];
/* advance past second character in cr/lf pair */
if (ch == '\n' && nxt == '\r') {
(fcp->ctr)++;
} else if (ch == '\r' && nxt == '\n') {
(fcp->ctr)++;
}
}
/* cr or lf returned as newline */
ch = '\n';
} else if (ch == '\0') {
/* look for unicode carriage return / linefeed */
if (fcp->ctr >= fcp->total) {
Nlm_FileCacheReadBlock (fcp);
}
if (fcp->ctr < fcp->total) {
nxt = fcp->buf [(int) fcp->ctr];
/* return newline if second character is cr or lf */
if (nxt == '\n' || nxt == '\r') {
(fcp->ctr)++;
ch = '\n';
}
}
}
return ch;
}
/* equivalent of fgets, leaves /n at end of string */
NLM_EXTERN Nlm_CharPtr Nlm_FileCacheGetString (
Nlm_FileCache PNTR fcp,
Nlm_CharPtr str,
size_t size
)
{
Nlm_Char ch;
Nlm_Uint2 count;
Nlm_CharPtr ptr;
if (fcp == NULL || fcp->fp == NULL || str == NULL || size < 1) return NULL;
ch = Nlm_FileCacheGetChar (fcp);
count = 0;
ptr = str;
while (ch != '\0' && ch != '\n' && ch != '\r' && count < size - 2) {
*ptr = ch;
ptr++;
count++;
ch = Nlm_FileCacheGetChar (fcp);
}
if (ch == '\n' || ch == '\r') {
*ptr = '\n';
ptr++;
count++;
} else if (ch != '\0') {
*ptr = ch;
ptr++;
count++;
}
*ptr = '\0';
if (count < 1) return NULL;
return str;
}
/* smarter fgets removes newline from end of string */
NLM_EXTERN Nlm_CharPtr Nlm_FileCacheReadLine (
Nlm_FileCache PNTR fcp,
Nlm_CharPtr str,
size_t size,
Nlm_BoolPtr nonewline
)
{
Nlm_Char ch;
Nlm_CharPtr ptr;
Nlm_CharPtr tmp;
if (fcp == NULL || fcp->fp == NULL || str == NULL || size < 1) return NULL;
*str = '\0';
tmp = Nlm_FileCacheGetString (fcp, str, size);
if (tmp != NULL) {
ptr = str;
ch = *ptr;
while (ch != '\0' && ch != '\n' && ch != '\r') {
ptr++;
ch = *ptr;
}
*ptr = '\0';
if (nonewline != NULL) {
if (ch != '\n' && ch != '\r') {
*nonewline = TRUE;
} else {
*nonewline = FALSE;
}
}
}
return tmp;
}
NLM_EXTERN void Nlm_FileCacheSeek (
Nlm_FileCache PNTR fcp,
Nlm_Int4 pos
)
{
if (fcp == NULL || fcp->fp == NULL) return;
/*
if (fcp->offset <= pos && fcp->offset + (Nlm_Int4) fcp->total >= pos) {
fcp->ctr = (Nlm_Int2) (pos - fcp->offset);
return;
}
*/
fcp->ctr = 0;
fcp->total = 0;
fcp->offset = pos;
fseek (fcp->fp, pos, SEEK_SET);
}
NLM_EXTERN Nlm_Int4 Nlm_FileCacheTell (
Nlm_FileCache PNTR fcp
)
{
Nlm_Int4 bytes;
Nlm_Int4 offset;
if (fcp == NULL || fcp->fp == NULL) return 0L;
offset = ftell (fcp->fp);
bytes = (Nlm_Int4) (fcp->total - fcp->ctr);
offset -= bytes;
return offset;
}
NLM_EXTERN Nlm_Boolean Nlm_FileCacheFree (
Nlm_FileCache PNTR fcp,
Nlm_Boolean restoreFilePos
)
{
Nlm_Int4 pos;
if (fcp == NULL || fcp->fp == NULL) return FALSE;
if (restoreFilePos) {
/* correct position of file pointer */
pos = Nlm_FileCacheTell (fcp);
fseek (fcp->fp, pos, SEEK_SET);
}
MemSet ((Nlm_VoidPtr) fcp, 0, sizeof (Nlm_FileCache));
return TRUE;
}
|