43
43
* 11-27-94 Ostell moved includes to ncbiwin.h to avoid conflict MSC
45
45
* $Log: ncbifile.c,v $
46
* Revision 6.37 2004/07/21 18:08:30 kans
47
* FileCacheSetup calls _setmode (_fileno (fp), _O_BINARY) if OS_MSWIN
49
* Revision 6.36 2004/07/20 19:37:30 kans
50
* FileCacheSeek always initializes fields, calls fseek to keep file pointer in sync
52
* Revision 6.35 2004/05/07 15:57:14 kans
53
* added FileCache functions for buffered read, graceful handing of Unix, Mac, and DOS line endings
55
* Revision 6.34 2004/01/23 20:07:16 kans
56
* fix to FileGets under Darwin, was losing last character if buffer was shorter than line being read
58
* Revision 6.33 2003/11/17 17:17:57 kans
59
* changed C++ comments to C comments
61
* Revision 6.32 2003/05/05 11:53:37 rsmith
62
* Codewarrior compiling for Win32 does not know about setmode or tempnam.
64
* Revision 6.31 2003/03/11 14:26:42 rsmith
65
* previous change to Nlm_DirCatalog made for only OS_UNIX_DARWIN, not all OS_UNIX
67
* Revision 6.30 2003/03/10 15:55:49 rsmith
68
* for OS_UNIX change implementation of DirCatalog from using popen with a ls command to using the opendir and readdir library calls.
70
* Revision 6.29 2003/02/25 15:31:03 rsmith
71
* OS_UNIX_DARWIN (Mach on Macs) uses tempnam for temporary files, not tmpnam.
73
* Revision 6.28 2002/11/06 21:23:04 ucko
74
* Don't assume MIPS is IRIX; allow Linux too.
76
* Revision 6.27 2002/10/03 17:22:29 kans
77
* for OS_MAC or OS_UNIX_DARWIN, Nlm_FileGets does fgetc loop, stopping at the first \n or \r character, and leaving a \n as the last character before the null byte
79
* Revision 6.26 2002/10/02 14:34:17 kans
80
* Nlm_GetOSType and Nlm_FileCreate have ifdef OS_MAC, not WIN_MAC
82
* Revision 6.25 2002/06/13 16:14:07 kans
83
* fix includes for OS_UNIX_DARWIN with WIN_MAC (EN)
85
* Revision 6.24 2002/04/05 19:02:47 ivanov
86
* Changed L_tmpnam to PATH_MAX in Nlm_TmpNam()
46
88
* Revision 6.23 2001/08/29 17:33:15 juran
427
474
if (inPathname[0] == ':') {
428
// Relative pathname including directory path
475
/* Relative pathname including directory path */
432
//Str31 volName; // We would use Str28 if it was defined -- 27, plus 1 for ':'.
478
/* Absolute pathname */
479
/* Str31 volName; We would use Str28 if it was defined -- 27, plus 1 for ':'. */
433
480
nameLen = p - inPathname;
434
481
assert(nameLen <= 27);
435
482
name[0] = nameLen + 1;
436
memcpy(name + 1, inPathname, nameLen + 1); // Copy the volume name and the colon.
483
memcpy(name + 1, inPathname, nameLen + 1); /* Copy the volume name and the colon. */
437
484
err = DetermineVRefNum(name, 0, &vRefNum);
438
485
if (err != noErr) return err;
441
// vRefNum and dirID now specify the directory in which we should descend
442
// the path pointed to by p (pointing to the first colon).
488
/* vRefNum and dirID now specify the directory in which we should descend
489
the path pointed to by p (pointing to the first colon). */
444
491
while (p != NULL && *p != '\0') {
445
492
char *q = strchr(p, ':');
711
758
*****************************************************************************/
712
759
NLM_EXTERN char * LIBCALL Nlm_FileGets (Nlm_CharPtr ptr, size_t size, FILE *fp)
714
if ((ptr == NULL) || (size <= 0) || (fp == NULL))
761
#if defined(OS_MAC) || defined (OS_UNIX_DARWIN)
767
if ((ptr == NULL) || (size < 1) || (fp == NULL))
769
#if defined(OS_MAC) || defined (OS_UNIX_DARWIN)
773
while (ch != EOF && ch != '\0' && ch != '\n' && ch != '\r' && count < size - 2) {
779
if (ch == '\n' || ch == '\r') {
783
} else if (ch != EOF && ch != '\0') {
716
793
return fgets(ptr,size,fp);
958
1036
if (fileName != NULL && fileName [0] != '\0') {
961
// note: the following assumes either full pathname or that the current
962
// directory is the proper location to find/create the file
1039
/* note: the following assumes either full pathname or that the current
1040
directory is the proper location to find/create the file */
964
1042
Nlm_StringNCpy_0(temp, fileName, sizeof(temp));
965
1043
Nlm_CtoPstr ( temp);
966
1044
fError = FSMakeFSSpec( 0, 0, (StringPtr)temp, &spec);
968
// file not found, so create it...
1046
/* file not found, so create it... */
969
1047
if( fError == fnfErr){
970
1048
fType = Nlm_GetOSType (type, 'TEXT');
971
1049
fCreator = Nlm_GetOSType (creator, ' ');
1222
#ifdef OS_UNIX_DARWIN
1223
dirp = opendir(pathname);
1224
if (dirp == NULL) return NULL;
1225
while ((dep = readdir(dirp)) != NULL) {
1226
/* ignore 'invisible' files. */
1227
if (dep->d_namlen < 1 || dep->d_name[0] == '.')
1229
if (dep->d_type == DT_DIR) /* directory */
1231
else /* all other file types. */
1233
ValNodeCopyStr (&vnp, choice, dep->d_name);
1135
1237
sprintf (cmmd, "ls -1p %s 2>/dev/null", pathname);
1136
1238
fp = popen (cmmd, "r");
1137
1239
if (fp == NULL) return NULL;
1374
/* FileCache provides buffered text read for handling Unix, Mac, and DOS line endings gracefully */
1376
/* attach file pointer (text read mode expected) to cache object (usually on stack) */
1383
NLM_EXTERN Nlm_Boolean Nlm_FileCacheSetup (
1384
Nlm_FileCache PNTR fcp,
1389
if (fp == NULL || fcp == NULL) return FALSE;
1392
_setmode (_fileno (fp), _O_BINARY);
1395
MemSet ((Nlm_VoidPtr) fcp, 0, sizeof (Nlm_FileCache));
1398
fcp->offset = ftell (fp);
1403
static void Nlm_FileCacheReadBlock (
1404
Nlm_FileCache PNTR fcp
1410
if (fcp == NULL || fcp->fp == NULL) return;
1412
if (fcp->ctr >= fcp->total) {
1413
fcp->offset += (Nlm_Int4) fcp->total;
1417
fcp->buf [0] = '\0';
1418
total = (int) Nlm_FileRead ((Nlm_VoidPtr) fcp->buf, sizeof (Nlm_Char), (size_t) 512, fcp->fp);
1419
if (total < 0 || total > 512) {
1423
fcp->buf [total] = '\0';
1424
fcp->total = Nlm_StringLen (fcp->buf);
1428
/* equivalent of getc */
1430
static Nlm_Char Nlm_FileCacheGetChar (
1431
Nlm_FileCache PNTR fcp
1435
Nlm_Char ch = '\0', nxt;
1437
if (fcp == NULL || fcp->fp == NULL) return ch;
1439
/* read a fresh block if buffer is empty */
1441
if (fcp->ctr >= fcp->total) {
1442
Nlm_FileCacheReadBlock (fcp);
1445
/* get next character in buffer */
1447
if (fcp->ctr < fcp->total) {
1448
ch = fcp->buf [(int) fcp->ctr];
1452
if (ch == '\n' || ch == '\r') {
1453
if (fcp->ctr >= fcp->total) {
1454
Nlm_FileCacheReadBlock (fcp);
1456
if (fcp->ctr < fcp->total) {
1458
/* look for carriage return / linefeed pair - DOS file read on Mac or Unix platform */
1460
nxt = fcp->buf [(int) fcp->ctr];
1462
/* advance past second character in cr/lf pair */
1464
if (ch == '\n' && nxt == '\r') {
1466
} else if (ch == '\r' && nxt == '\n') {
1471
/* cr or lf returned as newline */
1479
/* equivalent of fgets, leaves /n at end of string */
1481
NLM_EXTERN Nlm_CharPtr Nlm_FileCacheGetString (
1482
Nlm_FileCache PNTR fcp,
1492
if (fcp == NULL || fcp->fp == NULL || str == NULL || size < 1) return NULL;
1494
ch = Nlm_FileCacheGetChar (fcp);
1498
while (ch != '\0' && ch != '\n' && ch != '\r' && count < size - 2) {
1502
ch = Nlm_FileCacheGetChar (fcp);
1505
if (ch == '\n' || ch == '\r') {
1509
} else if (ch != '\0') {
1516
if (count < 1) return NULL;
1521
/* smarter fgets removes newline from end of string */
1523
NLM_EXTERN Nlm_CharPtr Nlm_FileCacheReadLine (
1524
Nlm_FileCache PNTR fcp,
1527
Nlm_BoolPtr nonewline
1535
if (fcp == NULL || fcp->fp == NULL || str == NULL || size < 1) return NULL;
1537
tmp = Nlm_FileCacheGetString (fcp, str, size);
1541
while (ch != '\0' && ch != '\n' && ch != '\r') {
1546
if (nonewline != NULL) {
1547
if (ch != '\n' && ch != '\r') {
1557
NLM_EXTERN void Nlm_FileCacheSeek (
1558
Nlm_FileCache PNTR fcp,
1563
if (fcp == NULL || fcp->fp == NULL) return;
1566
if (fcp->offset <= pos && fcp->offset + (Nlm_Int4) fcp->total >= pos) {
1567
fcp->ctr = (Nlm_Int2) (pos - fcp->offset);
1576
fseek (fcp->fp, pos, SEEK_SET);
1579
NLM_EXTERN Nlm_Int4 Nlm_FileCacheTell (
1580
Nlm_FileCache PNTR fcp
1587
if (fcp == NULL || fcp->fp == NULL) return 0L;
1589
offset = ftell (fcp->fp);
1590
bytes = (Nlm_Int4) (fcp->total - fcp->ctr);
1596
NLM_EXTERN Nlm_Boolean Nlm_FileCacheFree (
1597
Nlm_FileCache PNTR fcp,
1598
Nlm_Boolean restoreFilePos
1604
if (fcp == NULL || fcp->fp == NULL) return FALSE;
1606
if (restoreFilePos) {
1608
/* correct position of file pointer */
1610
pos = Nlm_FileCacheTell (fcp);
1611
fseek (fcp->fp, pos, SEEK_SET);
1614
MemSet ((Nlm_VoidPtr) fcp, 0, sizeof (Nlm_FileCache));