Doxygen Source Code Documentation
niml_fileread.c File Reference
#include "niml_private.h"Go to the source code of this file.
Defines | |
| #define | SKIP_COMMENT_FLAG 1 |
| #define | LBUF 65536 |
Functions | |
| char * | my_fgets (char *, int, FILE *, int) |
| NI_element * | NI_read_file_nohead (char *fname) |
Define Documentation
|
|
Length of line buffer Definition at line 44 of file niml_fileread.c. Referenced by my_fgets(). |
|
|
Definition at line 4 of file niml_fileread.c. Referenced by my_fgets(). |
Function Documentation
|
||||||||||||||||||||
|
Like fgets, but also
Definition at line 54 of file niml_fileread.c. References flags, LBUF, NI_free, NI_malloc, and SKIP_COMMENT_FLAG. Referenced by mri_read_ascii(), and mri_read_ascii_ragged().
00055 {
00056 char *ptr ;
00057 int nbuf , ll,ii , cflag ;
00058 static char *qbuf=NULL ;
00059 int skip_comm = (flags & SKIP_COMMENT_FLAG) != 0 ;
00060
00061 if( buf == NULL || size < 1 || fts == NULL ){
00062 NI_free(qbuf); qbuf = NULL; return NULL;
00063 }
00064
00065 if( qbuf == NULL ) qbuf = NI_malloc(char, LBUF) ; /* 1st time in */
00066
00067 nbuf = 0 ; /* num bytes stored in buf so far */
00068 cflag = 0 ; /* flag if we're catenating lines */
00069
00070 while(1){ /* loop and read lines, creating a logical line */
00071
00072 ptr = fgets( qbuf , LBUF , fts ) ; /* read next whole line */
00073
00074 if( ptr == NULL ) break ; /* must be end-of-file */
00075
00076 /* skip leading whitespace */
00077
00078 for( ; *ptr != '\0' && isspace(*ptr) ; ptr++ ) ; /* nada */
00079
00080 /* skip entirely blank lines, unless we are catenating */
00081
00082 if( *ptr == '\0' ){ if(cflag) break; else continue; }
00083
00084 /* skip comment lines (even if we are catenating) */
00085
00086 if( skip_comm &&
00087 (*ptr == '#' || (*ptr == '/' && *(ptr+1) == '/')) ) continue ;
00088
00089 /* strip trailing whitespace */
00090
00091 ll = strlen(ptr) ; /* will be > 0 */
00092 for( ii=ll-1 ; isspace(ptr[ii]) && ii > 0 ; ii-- ) /* blank => NUL */
00093 ptr[ii] = '\0' ;
00094
00095 ll = strlen(ptr) ; /* number of chars left */
00096 if( ll == 0 ) continue ; /* should not happen */
00097
00098 cflag = (ptr[ll-1] == '\\') ; /* catenate next line? */
00099 if( cflag && ll > 1 && ptr[ll-2] == '\\' ) cflag = 0 ;
00100 if( cflag ) ptr[ll-1] = ' ' ; /* replace '\' with ' ' */
00101
00102 /* now copy what's left (ll+1 bytes) at tail of output buffer */
00103
00104 if( nbuf+ll+1 > size ){ /* too much for output buffer? */
00105 ll = size - (nbuf+1) ;
00106 if( ll <= 0 ) break ; /* should not happen */
00107 }
00108
00109 memcpy(buf+nbuf,ptr,ll+1) ; nbuf += ll ;
00110 if( !cflag ) break ;
00111
00112 } /* loop to get next line if catenation is turned on */
00113
00114 /* and we is done */
00115
00116 if( nbuf > 0 ) return buf ; /* return what we read already */
00117 return NULL ; /* signal of failure get data */
00118 }
|
|
|
Read an un-headered file into a data element, guessing at its structure from the 1st non-comment line. ----------------------------------------------------------------- Definition at line 13 of file niml_fileread.c. References NI_free, NI_malloc, NI_read_element(), NI_stream_close(), and NI_stream_open().
00014 {
00015 FILE *fp ;
00016 char prefix[32] , *ptr ;
00017 NI_element *nel ;
00018
00019 if( fname == NULL || *fname == '\0' ) return NULL ;
00020
00021 fp = fopen( fname , "r" ) ; if( fp == NULL ) return NULL ;
00022
00023 /** see if this looks like a NIML-formatted file **/
00024
00025 memset(prefix,0,32) ; fread(prefix,1,31,fp) ; rewind(fp) ;
00026 ptr = strchr(prefix,'<') ;
00027 if( ptr != NULL && isalpha(*(ptr+1)) ){
00028 NI_stream ns ;
00029 fclose(fp) ;
00030 ptr = NI_malloc(char, strlen(fname)+16) ;
00031 sprintf(ptr,"file:%s",fname) ;
00032 ns = NI_stream_open(ptr,"r") ; NI_free(ptr) ;
00033 if( ns == NULL ) return NULL ;
00034 nel = NI_read_element( ns , 66 ) ;
00035 NI_stream_close(ns) ;
00036 return nel ;
00037 }
00038
00039 }
|