diff options
author | Norbert Preining <norbert@preining.info> | 2019-09-02 13:46:59 +0900 |
---|---|---|
committer | Norbert Preining <norbert@preining.info> | 2019-09-02 13:46:59 +0900 |
commit | e0c6872cf40896c7be36b11dcc744620f10adf1d (patch) | |
tree | 60335e10d2f4354b0674ec22d7b53f0f8abee672 /biblio/tib/src/streams.c |
Initial commit
Diffstat (limited to 'biblio/tib/src/streams.c')
-rw-r--r-- | biblio/tib/src/streams.c | 136 |
1 files changed, 136 insertions, 0 deletions
diff --git a/biblio/tib/src/streams.c b/biblio/tib/src/streams.c new file mode 100644 index 0000000000..9fa362c355 --- /dev/null +++ b/biblio/tib/src/streams.c @@ -0,0 +1,136 @@ +#include "stdio.h" +#include "ctype.h" +#include "tib.h" +#define pos(x) fseek(stream,x,0) + +long int nextrecord(), recsize(), nextline(); + +/* getword(stream,p,ignore): + read next sequence of nonspaces on current line into *p. + null if no more words on current line. + %x (x in ignore) terminates line. + all words of the form %a are returned as null. + *p is a null terminated string (char p[MAXSTR]). +*/ +getword(stream,p,ignore) +FILE *stream; +char *p, *ignore; +{ char c; + char *oldp, *stop; + + oldp= p; + stop= p+MAXSTR-1; + do{ c= getc(stream); + } while (isspace(c) && c!='\n'); + + while (!isspace(c)) + { *p= c; + if (p < stop) p++; + c= getc(stream); + } + *p= NULL; + + if (oldp[0]=='%') + { oldp[0]= NULL; + if (index(ignore, oldp[1]) != NULL) + while (c!='\n') c=getc(stream); + } +} + + + +/* recsize(stream,start): + returns length of record beginning at start + (record ends at blank line or eof) + assumes and retains stream positioned at start +*/ +long int recsize(stream,start) +FILE *stream; +long int start; +{ char c; /* length = # of chars from start to beginning */ + long int length; /* of current line. c in current line. */ + int nonspaces; /* nonspaces = # of nonspaces in current line. */ + + nonspaces= 0; + c= getc(stream); + length= 0L; + + while ( (c!='\n' || nonspaces!=0) && c!=EOF) + { if (c=='\n') + { length= ftell(stream)-start; + nonspaces= 0; + } + else if (!isspace(c)) nonspaces++; + + c= getc(stream); + } + + pos(start); + return(length); +} + + +/* nextrecord(stream,x): seeks in stream for first non-blank line + at or after char x in stream. seeks to eof if x is past last record. + x is the index of a character in the file (not eof). + returns position in stream. (returns EOF, if seeks to EOF) +*/ +long int nextrecord(stream,x) +FILE *stream; +long int x; +{ long int start; /* position of the beginning of the line */ + char c; /* containing c */ + + pos(x); + start= x; + /* find start of first non-blank record */ + for(;;) + { c= getc(stream); + if (c=='\n') start= ftell(stream); + else if (!isspace(c)) break; + } + + if (feof(stream)) { pos(start); start= EOF; } + else pos(start); + return(start); +} + +/* nextline(stream,x): seeks in stream after first newline at or after + char x in stream. seeks to eof if x is in last line. + x is the index of a character in the file (not eof). + returns position in stream +*/ +long int nextline(stream,x) +FILE *stream; +long int x; +{ char c; + + pos(x); + do { + c = getc(stream); + } while ((c != '\n') && (c != EOF)) ; + return(ftell(stream)); +} + +/* printline(stream): copies stream up to a newline +*/ +printline(stream) +FILE *stream; +{ char c; + while ((c=getc(stream)) != '\n' && c!=EOF) putchar(c); + putchar('\n'); +} + +/* getline(stream,p): store in *p next chars in stream up to \n + advance stream past \n. + limit of MAXSTR-1 chars may be stored at p. +*/ +getline(stream,p) +FILE *stream; +char *p; +{ char *stop; + stop= p+MAXSTR-1; + while ( (*p= getc(stream)) != '\n' && *p!=EOF) + if (p<stop) p++; + *p= NULL; +} |