diff options
author | Aaron M. Ucko <ucko@debian.org> | 2005-03-24 18:32:05 +0000 |
---|---|---|
committer | Aaron M. Ucko <ucko@debian.org> | 2005-03-24 18:32:05 +0000 |
commit | f06fc23cbc179836f402001f24176fc9d5725482 (patch) | |
tree | 39e97ad8f13a33296b32a3907f3409b056cf851b /corelib | |
parent | ccba467ae4f393d7acce357a9847bfe1fb77ccc7 (diff) |
Load ncbi (6.1.20040616) into ncbi-tools6/branches/upstream/current.
Diffstat (limited to 'corelib')
-rw-r--r-- | corelib/ncbifile.c | 239 | ||||
-rw-r--r-- | corelib/ncbifile.h | 36 |
2 files changed, 273 insertions, 2 deletions
diff --git a/corelib/ncbifile.c b/corelib/ncbifile.c index 45cac220..1b881b88 100644 --- a/corelib/ncbifile.c +++ b/corelib/ncbifile.c @@ -29,7 +29,7 @@ * * Version Creation Date: 3/4/91 * -* $Revision: 6.34 $ +* $Revision: 6.35 $ * * File Description: * portable file routines @@ -43,6 +43,9 @@ * 11-27-94 Ostell moved includes to ncbiwin.h to avoid conflict MSC * * $Log: ncbifile.c,v $ +* Revision 6.35 2004/05/07 15:57:14 kans +* added FileCache functions for buffered read, graceful handing of Unix, Mac, and DOS line endings +* * Revision 6.34 2004/01/23 20:07:16 kans * fix to FileGets under Darwin, was losing last character if buffer was shorter than line being read * @@ -1362,3 +1365,237 @@ NLM_EXTERN Nlm_CharPtr LIBCALL Nlm_TmpNam (Nlm_CharPtr s) #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) */ + +NLM_EXTERN Nlm_Boolean Nlm_FileCacheSetup ( + Nlm_FileCache PNTR fcp, + FILE *fp +) + +{ + if (fp == NULL || fcp == NULL) return FALSE; + + 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); + } +} + +/* 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 Nlm_Character in buffer */ + + if (fcp->ctr < fcp->total) { + ch = fcp->buf [(int) fcp->ctr]; + (fcp->ctr)++; + } + + if (ch == '\n' || ch == '\r') { + if (fcp->ctr >= fcp->total) { + Nlm_FileCacheReadBlock (fcp); + } + if (fcp->ctr < fcp->total) { + + /* look for carriage return / linefeed pair - DOS file read on Mac or Unix platform */ + + nxt = fcp->buf [(int) fcp->ctr]; + + /* advance past second Nlm_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'; + } + + 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_Int2 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; +} + diff --git a/corelib/ncbifile.h b/corelib/ncbifile.h index e46c0a74..853e3e64 100644 --- a/corelib/ncbifile.h +++ b/corelib/ncbifile.h @@ -32,7 +32,7 @@ * * Version Creation Date: 1/1/91 * -* $Revision: 6.5 $ +* $Revision: 6.6 $ * * File Description: * prototypes for portable file routines @@ -40,6 +40,9 @@ * Modifications: * -------------------------------------------------------------------------- * $Log: ncbifile.h,v $ +* Revision 6.6 2004/05/07 15:57:14 kans +* added FileCache functions for buffered read, graceful handing of Unix, Mac, and DOS line endings +* * Revision 6.5 2001/04/05 21:36:05 juran * EjectCd and MountCd #defined to FALSE. * @@ -98,6 +101,25 @@ NLM_EXTERN ValNodePtr LIBCALL Nlm_DirCatalog (Nlm_CharPtr pathname); NLM_EXTERN Nlm_CharPtr LIBCALL Nlm_TmpNam(Nlm_CharPtr s); NLM_EXTERN void LIBCALL Nlm_SetFileOpenHook(Nlm_FileOpenHook hook); +/* FileCache provides buffered text read for handling Unix, Mac, and DOS line endings gracefully */ + +typedef struct nlm_filecachedata { + FILE *fp; + Nlm_Char buf [516]; + Nlm_Int2 ctr; + Nlm_Int2 total; + Nlm_Int4 offset; +} Nlm_FileCache, PNTR Nlm_FileCachePtr; + +NLM_EXTERN Nlm_Boolean Nlm_FileCacheSetup (Nlm_FileCache PNTR fcp, FILE *fp); +NLM_EXTERN Nlm_CharPtr Nlm_FileCacheGetString (Nlm_FileCache PNTR fcp, Nlm_CharPtr str, size_t size); +NLM_EXTERN Nlm_CharPtr Nlm_FileCacheReadLine (Nlm_FileCache PNTR fcp, Nlm_CharPtr str, size_t size, Nlm_BoolPtr nonewline); +NLM_EXTERN void Nlm_FileCacheSeek (Nlm_FileCache PNTR fcp, Nlm_Int4 pos); +NLM_EXTERN Nlm_Int4 Nlm_FileCacheTell (Nlm_FileCache PNTR fcp); +NLM_EXTERN Nlm_Boolean Nlm_FileCacheFree (Nlm_FileCache PNTR fcp, Nlm_Boolean restoreFilePos); + + + #define FileOpen Nlm_FileOpen #define FileClose Nlm_FileClose #define FileRead Nlm_FileRead @@ -115,6 +137,18 @@ NLM_EXTERN void LIBCALL Nlm_SetFileOpenHook(Nlm_FileOpenHook hook); #define CreateDir Nlm_CreateDir #define DirCatalog Nlm_DirCatalog #define TmpNam Nlm_TmpNam + +#define FileCache Nlm_FileCache +#define FileCacheSetup Nlm_FileCacheSetup +#define FileCachePtr Nlm_FileCachePtr + +#define FileCacheSetup Nlm_FileCacheSetup +#define FileCacheGetString Nlm_FileCacheGetString +#define FileCacheReadLine Nlm_FileCacheReadLine +#define FileCacheSeek Nlm_FileCacheSeek +#define FileCacheTell Nlm_FileCacheTell +#define FileCacheFree Nlm_FileCacheFree + #define EjectCd(sVolume, deviceName, rawDeviceName, mountPoint, mountCmd) FALSE #define MountCd(sVolume, deviceName, mountPoint, mountCmd) FALSE |