File: parse.c

package info (click to toggle)
mcl 1%3A02-035-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,108 kB
  • ctags: 1,361
  • sloc: ansic: 18,031; sh: 453; makefile: 234
file content (179 lines) | stat: -rw-r--r-- 3,628 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
 *    Copyright (C) 1999-2002 Stijn van Dongen.
*/

#include <ctype.h>

#include "parse.h"
#include "types.h"


/* bug: newlines in str thrash correct character counting */

mcxstatus mcxFpParse
(  mcxIOstream*         xf
,  const char*          str
,  const char*          caller
,  mcxOnFail            ON_FAIL
)
   {  const char*       s        =  str
   ;  short             c        =  0
   ;  short             d        =  0
   ;  long              bct      =  0
   ;  FILE*             fp       =  xf->fp
   ;  long              pos      =  ftell(fp)
   ;  int               bReset   =  0

   ;  while
      (  c = s[0]
      ,  
         (  c
         && (  d = fgetc(fp)
            ,  bct++
            ,  c == d
            )  
         )  
      )
      {  s++
   ;  }

   ;  if (c)
      {  if (ON_FAIL == EXIT_ON_FAIL)
         {  fprintf
            (  stderr
            ,  "[%s] Parse error: expected to see `%s'\n"
            ,  caller
            ,  str
            )
         ;  mcxIOstreamReport(xf, stderr)
         ;  exit(1)
      ;  }
         else
         {  if (bReset)
            {  fseek(fp, SEEK_SET,  pos)
         ;  }
            else
            {  ungetc(d, fp)     /* put back first nonmatching character */
            ;  xf->bc += bct-1   /* note that bct >= 1 since c != '\0'    */
            ;  xf->lo += bct-1
         ;  }
      ;  }
      ;  return STATUS_FAIL
   ;  }
      else
      {  xf->bc += bct
      ;  xf->lo += bct
      ;  return STATUS_OK
   ;  }
;  }


float mcxFpParseNumber
(  mcxIOstream*             xf
,  const char*          caller
)
   {  float             num
   ;  int               n_read   =  0

   ;  if (1 != fscanf(xf->fp, " %f%n", &num, &n_read))
      {  fprintf
         (  stderr
         ,  "[%s] Parse error: expected a number\n"
         ,  caller
         )
      ;  mcxIOstreamReport(xf, stderr)
      ;  exit(1)
   ;  }
   ;  xf->bc += n_read
   ;  xf->lo += n_read
   ;  return num
;  }


int mcxFpSkipSpace
(  mcxIOstream*         xf
,  const char*          caller
)
   {  short             c
   ;  long              lct    =    0
   ;  long              bct    =    0
   ;  long              lo     =    xf->lo

   ;  while ((c = fgetc(xf->fp)) != EOF)
      {  if (c=='\n')
         {  lct++
         ;  bct++
         ;  lo   =  1
      ;  }
         else if (isspace(c))
         {  bct++
         ;  lo++
      ;  }
         else
         {  break
      ;  }
   ;  }

   ;  xf->lc +=  lct
   ;  xf->bc +=  bct
   ;  xf->lo  =  lo

   ;  if (c == EOF)
      {  xf->ateof   =  1
      ;  return EOF
   ;  }
      else
      {  ungetc(c, xf->fp)
      ;  return c
   ;  }
;  }


mcxstatus mcxFpFindInFile
(  mcxIOstream*         xf
,  const char*          str
,  const char*          caller
,  mcxOnFail            ON_FAIL
)
   {  short             c

   ;  for (;;)
      {  
         c  =  fgetc(xf->fp)
      ;  mcxIOstreamStep(xf, c)

      ;  if (c == EOF)
         {  
            fprintf
            (  stderr
            ,  "[%s] In file [%s] at EOF, expected `%s' not found\n"
            ,  caller
            ,  xf->fn->str
            ,  str
            )
         ;  mcxIOstreamReport(xf, stderr)

         ;  if (ON_FAIL == RETURN_ON_FAIL)
            return STATUS_FAIL
         ;  else
            exit(1)
      ;  }

      ;  if
         (  c == str[0] 
         && (  mcxFpParse
               (  xf
               ,  str + 1
               ,  caller
               ,  1
               )
            == STATUS_OK
            )
         )
         {  return STATUS_OK
      ;  }
   ;  }
   ;  return STATUS_FAIL
;  }