comparison librftab/rdcommon.c @ 314:a0f79bba0ad8

librftab: reading of Tx ramp template files split from rftablerd module
author Mychaela Falconia <falcon@freecalypso.org>
date Wed, 22 Nov 2017 18:12:04 +0000
parents
children
comparison
equal deleted inserted replaced
313:a9bd4b15f502 314:a0f79bba0ad8
1 /*
2 * This C file is not a compilation unit in itself, but is the common piece
3 * (a set of static variables and functions) included in the librftab modules
4 * responsible for reading different kinds of ASCII tables.
5 */
6
7 #define MAX_FIELDS_PER_LINE 64
8
9 static char *filename;
10 static FILE *rdfile;
11 static unsigned lineno;
12 static char linebuf[256], *line_fields[MAX_FIELDS_PER_LINE];
13 static unsigned line_nfields, line_field_ptr;
14
15 static int
16 read_line()
17 {
18 char *cp;
19
20 if (!fgets(linebuf, sizeof linebuf, rdfile))
21 return(0);
22 lineno++;
23 cp = linebuf;
24 for (line_nfields = 0; ; ) {
25 while (isspace(*cp))
26 cp++;
27 if (*cp == '\0' || *cp == '#')
28 break;
29 if (line_nfields >= MAX_FIELDS_PER_LINE) {
30 printf("%s line %d: too many fields on one line\n",
31 filename, lineno);
32 return(-1);
33 }
34 line_fields[line_nfields++] = cp;
35 while (*cp && !isspace(*cp))
36 cp++;
37 if (*cp)
38 *cp++ = '\0';
39 }
40 return(1);
41 }
42
43 static
44 get_field(retp)
45 char **retp;
46 {
47 int rc;
48
49 if (line_field_ptr < line_nfields) {
50 *retp = line_fields[line_field_ptr++];
51 return(1);
52 }
53 do {
54 rc = read_line();
55 if (rc <= 0)
56 return(rc);
57 } while (!line_nfields);
58 *retp = line_fields[0];
59 line_field_ptr = 1;
60 return(1);
61 }