/* (void) reopen() reopen (rewind) input file (int) getline(Line *m) obtain next line from input, store in Line *m. Returns EndOfFile or !EndOfFile. If global !eof_okay and EndOfFile, exit()s. (int) getnbline(Line *m) Same as getline(), only skips blank lines. (int) getndtaline(Line *m) Same as getline(), only skips blank lines if global options.skipblnks. (int) input_line_no() returns current line number of input line. Before reading any lines, line number is 0. When first line has been read, line is 1, and so on. During all input: 1) tabs are expanded to multiple blanks 2) trailing blanks are trimimed Copyright (c) 1992 by CRC */ #include "creatdct.h" /* Forward references */ static int check_eof() ; static void trimline() ; static void detabline() ; static int line_no = 0 ; int input_line_no() { return(line_no) ; } void reopen() { Line ttl ; rewind(inf) ; end_of_file = 0 ; line_no = 0 ; if (!options.notitle) (void) getnbline(&ttl) ; } int getline(m) register Line *m ; { if (end_of_file) { initline(m) ; return(check_eof()) ; } line_no++ ; if (fgets(m->s, MAXWIDTH, inf)==NULL) { end_of_file = 1 ; initline(m) ; return(check_eof()) ; } m->n = strlen(m->s) - 1 ; if (m->n == -1) { /* can`t happen */ initline(m) ; return(!EndOfFile) ; } if (m->s[m->n] == '\n') { /* normal case */ m->s[m->n] = '\0' ; detabline(m) ; trimline(m) ; return(!EndOfFile) ; } fprintf(stderr,"%s: line longer than %d characters\n", progname, MAXWIDTH) ; exit(TOOLONG_ERROR) ; /*NOTREACHED*/ } int getnbline(m) register Line *m ; { while(getline(m)==0) { if (m->n) return(0) ; } return(check_eof()) ; } int getdtaline(m) register Line *m ; { if (options.skip_blanks) return(getnbline(m)) ; return(getline(m)) ; } /* ----------------------------------------------------------------------- */ /* Internal utilities */ static int check_eof() { if (eof_okay) return(EndOfFile) ; fprintf(stderr,"%s: unexpected end-of-file\n",progname) ; exit(UEOF_ERROR) ; /*NOTREACHED*/ } /* (void) trimline(Line *m) remove trailing blanks from line */ static void trimline(m) register Line *m ; { int i ; for (i=m->n - 1;i>=0 && m->s[i]==' ';i--) m->s[i] = '\0' ; m->n = i + 1 ; } /* (void) detabline(lask *m) expand tabs in line. The following may help in understanding the logic below: Typically, TABPOS=8 and thus: 0123456789012345678901234567890 T T T Ergo, if at position i in string, the new position is newi = (i/8+1)*8. The strength is then extended by newi-i characters. */ static void detabline(m) register Line *m ; { int i ; int newi ; char s2[MAXWIDTH] ; for (i=0;i<(m->n);i++) { if (m->s[i]=='\t') { newi = (i/TABPOS + 1)*TABPOS ; m->n = m->n + (newi-i) - 1 ; if (m->n >= MAXWIDTH) { fprintf(stderr, "%s: line longer than %d characters after detabbing\n", progname,MAXWIDTH) ; exit(TOOLONG_ERROR) ; /*NOTREACHED*/ } strcpy(s2,&(m->s[i+1])) ; for (;is[i] = ' ' ; strcpy(&(m->s[i]), s2) ; i-- ; } } }