comparison files.c @ 154:a2c2fe8dbea3

Wrap up the current and next line position when invoking directives. (in preparation for implementing #line)
author David A. Holland
date Fri, 12 Jun 2015 00:56:12 -0400
parents ed45f2d8d3bc
children f14f5352956c
comparison
equal deleted inserted replaced
153:28ac21a359d1 154:a2c2fe8dbea3
170 170
171 static 171 static
172 void 172 void
173 file_read(const struct placefile *pf, int fd, const char *name, bool toplevel) 173 file_read(const struct placefile *pf, int fd, const char *name, bool toplevel)
174 { 174 {
175 struct place linestartplace, nextlinestartplace, ptmp; 175 struct lineplace places;
176 struct place ptmp;
176 size_t bufend, bufmax, linestart, lineend, nextlinestart, tmp; 177 size_t bufend, bufmax, linestart, lineend, nextlinestart, tmp;
177 ssize_t result; 178 ssize_t result;
178 bool ateof = false; 179 bool ateof = false;
179 char *buf; 180 char *buf;
180 181
181 place_setfilestart(&linestartplace, pf); 182 place_setfilestart(&places.current, pf);
182 nextlinestartplace = linestartplace; 183 places.nextline = places.current;
183 184
184 bufmax = 128; 185 bufmax = 128;
185 bufend = 0; 186 bufend = 0;
186 linestart = 0; 187 linestart = 0;
187 lineend = 0; 188 lineend = 0;
221 ateof = true; 222 ateof = true;
222 break; 223 break;
223 } else if (result == 0) { 224 } else if (result == 0) {
224 /* eof in middle of line */ 225 /* eof in middle of line */
225 ateof = true; 226 ateof = true;
226 ptmp = linestartplace; 227 ptmp = places.current;
227 ptmp.column += bufend - linestart; 228 ptmp.column += bufend - linestart;
228 complain(&ptmp, "No newline at end of file"); 229 complain(&ptmp, "No newline at end of file");
229 if (mode.werror) { 230 if (mode.werror) {
230 complain_fail(); 231 complain_fail();
231 } 232 }
242 243
243 /* have a line */ 244 /* have a line */
244 assert(buf[lineend] == '\n'); 245 assert(buf[lineend] == '\n');
245 buf[lineend] = '\0'; 246 buf[lineend] = '\0';
246 nextlinestart = lineend+1; 247 nextlinestart = lineend+1;
247 nextlinestartplace.line++; 248 places.nextline.line++;
248 249
249 /* check for CR/NL */ 250 /* check for CR/NL */
250 if (lineend > 0 && buf[lineend-1] == '\r') { 251 if (lineend > 0 && buf[lineend-1] == '\r') {
251 buf[lineend-1] = '\0'; 252 buf[lineend-1] = '\0';
252 lineend--; 253 lineend--;
269 270
270 /* line now goes from linestart to lineend */ 271 /* line now goes from linestart to lineend */
271 assert(buf[lineend] == '\0'); 272 assert(buf[lineend] == '\0');
272 273
273 /* count how many commented-out newlines we swallowed */ 274 /* count how many commented-out newlines we swallowed */
274 nextlinestartplace.line += countnls(buf, linestart, lineend); 275 places.nextline.line += countnls(buf, linestart, lineend);
275 276
276 /* if the line isn't empty, process it */ 277 /* if the line isn't empty, process it */
277 if (lineend > linestart) { 278 if (lineend > linestart) {
278 directive_gotline(&linestartplace, 279 directive_gotline(&places,
279 buf+linestart, lineend-linestart); 280 buf+linestart, lineend-linestart);
280 } 281 }
281 282
282 linestart = nextlinestart; 283 linestart = nextlinestart;
283 lineend = findeol(buf, linestart, bufend); 284 lineend = findeol(buf, linestart, bufend);
284 linestartplace = nextlinestartplace; 285 places.current = places.nextline;
285 } 286 }
286 287
287 if (toplevel) { 288 if (toplevel) {
288 directive_goteof(&linestartplace); 289 directive_goteof(&places.current);
289 } 290 }
290 dofree(buf, bufmax); 291 dofree(buf, bufmax);
291 } 292 }
292 293
293 //////////////////////////////////////////////////////////// 294 ////////////////////////////////////////////////////////////