diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1999-10-07 17:18:36 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1999-10-07 17:18:36 -0200 |
| commit | d435b7acd84cb3fbf94f5350da233ef1606319de (patch) | |
| tree | d01d88f5078fd19d85c29b78c974863590f23f79 | |
| parent | 63ccf42397c30d89497d5f1e1e953ce879bef724 (diff) | |
| download | lua-d435b7acd84cb3fbf94f5350da233ef1606319de.tar.gz lua-d435b7acd84cb3fbf94f5350da233ef1606319de.tar.bz2 lua-d435b7acd84cb3fbf94f5350da233ef1606319de.zip | |
read patterns are deprecated; new option `read(n)' to read n bytes.
| -rw-r--r-- | liolib.c | 105 |
1 files changed, 77 insertions, 28 deletions
| @@ -1,10 +1,11 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: liolib.c,v 1.44 1999/08/16 20:52:00 roberto Exp roberto $ | 2 | ** $Id: liolib.c,v 1.45 1999/09/13 19:42:02 roberto Exp roberto $ |
| 3 | ** Standard I/O (and system) library | 3 | ** Standard I/O (and system) library |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| 6 | 6 | ||
| 7 | 7 | ||
| 8 | #include <ctype.h> | ||
| 8 | #include <errno.h> | 9 | #include <errno.h> |
| 9 | #include <stdio.h> | 10 | #include <stdio.h> |
| 10 | #include <stdlib.h> | 11 | #include <stdlib.h> |
| @@ -221,6 +222,9 @@ static void io_appendto (void) { | |||
| 221 | */ | 222 | */ |
| 222 | 223 | ||
| 223 | 224 | ||
| 225 | |||
| 226 | #ifdef COMPAT_READPATTERN | ||
| 227 | |||
| 224 | /* | 228 | /* |
| 225 | ** We cannot lookahead without need, because this can lock stdin. | 229 | ** We cannot lookahead without need, because this can lock stdin. |
| 226 | ** This flag signals when we need to read a next char. | 230 | ** This flag signals when we need to read a next char. |
| @@ -276,6 +280,12 @@ static int read_pattern (FILE *f, const char *p) { | |||
| 276 | return (*p == '\0'); | 280 | return (*p == '\0'); |
| 277 | } | 281 | } |
| 278 | 282 | ||
| 283 | #else | ||
| 284 | |||
| 285 | #define read_pattern(f,p) (lua_error("read patterns are deprecated"), 0) | ||
| 286 | |||
| 287 | #endif | ||
| 288 | |||
| 279 | 289 | ||
| 280 | static int read_number (FILE *f) { | 290 | static int read_number (FILE *f) { |
| 281 | double d; | 291 | double d; |
| @@ -287,11 +297,21 @@ static int read_number (FILE *f) { | |||
| 287 | } | 297 | } |
| 288 | 298 | ||
| 289 | 299 | ||
| 300 | static void read_word (FILE *f) { | ||
| 301 | int c; | ||
| 302 | do { c = fgetc(f); } while isspace(c); /* skip spaces */ | ||
| 303 | while (c != EOF && !isspace(c)) { | ||
| 304 | luaL_addchar(c); | ||
| 305 | c = fgetc(f); | ||
| 306 | } | ||
| 307 | ungetc(c, f); | ||
| 308 | } | ||
| 309 | |||
| 310 | |||
| 290 | #define HUNK_LINE 256 | 311 | #define HUNK_LINE 256 |
| 291 | #define HUNK_FILE BUFSIZ | 312 | #define HUNK_FILE BUFSIZ |
| 292 | 313 | ||
| 293 | static int read_line (FILE *f) { | 314 | static int read_line (FILE *f) { |
| 294 | /* equivalent to: return read_pattern(f, "[^\n]*{\n}"); */ | ||
| 295 | int n; | 315 | int n; |
| 296 | char *b; | 316 | char *b; |
| 297 | do { | 317 | do { |
| @@ -306,46 +326,63 @@ static int read_line (FILE *f) { | |||
| 306 | 326 | ||
| 307 | 327 | ||
| 308 | static void read_file (FILE *f) { | 328 | static void read_file (FILE *f) { |
| 309 | /* equivalent to: return read_pattern(f, ".*"); */ | ||
| 310 | int n; | 329 | int n; |
| 311 | do { | 330 | do { |
| 312 | char *b = luaL_openspace(HUNK_FILE); | 331 | char *b = luaL_openspace(HUNK_FILE); |
| 313 | n = fread(b, sizeof(char), HUNK_FILE, f); | 332 | n = fread(b, sizeof(char), HUNK_FILE, f); |
| 314 | luaL_addsize(n); | 333 | luaL_addsize(n); |
| 315 | } while (n==HUNK_FILE); | 334 | } while (n==HUNK_FILE); |
| 316 | } | 335 | } |
| 317 | 336 | ||
| 318 | 337 | ||
| 338 | static int read_chars (FILE *f, int n) { | ||
| 339 | char *b = luaL_openspace(n); | ||
| 340 | int n1 = fread(b, sizeof(char), n, f); | ||
| 341 | luaL_addsize(n1); | ||
| 342 | return (n == n1); | ||
| 343 | } | ||
| 344 | |||
| 345 | |||
| 319 | static void io_read (void) { | 346 | static void io_read (void) { |
| 320 | static const char *const options[] = {"*n", "*l", "*a", ".*", "*w", NULL}; | ||
| 321 | int arg = FIRSTARG; | 347 | int arg = FIRSTARG; |
| 322 | FILE *f = getfileparam(FINPUT, &arg); | 348 | FILE *f = getfileparam(FINPUT, &arg); |
| 323 | const char *p = luaL_opt_string(arg++, "*l"); | 349 | lua_Object op = lua_getparam(arg); |
| 324 | do { /* repeat for each part */ | 350 | do { /* repeat for each part */ |
| 325 | long l; | 351 | long l; |
| 326 | int success; | 352 | int success; |
| 327 | luaL_resetbuffer(); | 353 | luaL_resetbuffer(); |
| 328 | switch (luaL_findstring(p, options)) { | 354 | if (lua_isnumber(op)) |
| 329 | case 0: /* number */ | 355 | success = read_chars(f, lua_getnumber(op)); |
| 330 | if (!read_number(f)) return; /* read fails */ | 356 | else { |
| 331 | continue; /* number is already pushed; avoid the "pushstring" */ | 357 | const char *p = luaL_opt_string(arg, "*l"); |
| 332 | case 1: /* line */ | 358 | if (p[0] != '*') |
| 333 | success = read_line(f); | 359 | success = read_pattern(f, p); /* deprecated! */ |
| 334 | break; | 360 | else { |
| 335 | case 2: case 3: /* file */ | 361 | switch (p[1]) { |
| 336 | read_file(f); | 362 | case 'n': /* number */ |
| 337 | success = 1; /* always success */ | 363 | if (!read_number(f)) return; /* read fails */ |
| 338 | break; | 364 | continue; /* number is already pushed; avoid the "pushstring" */ |
| 339 | case 4: /* word */ | 365 | case 'l': /* line */ |
| 340 | success = read_pattern(f, "{%s*}%S+"); | 366 | success = read_line(f); |
| 341 | break; | 367 | break; |
| 342 | default: | 368 | case 'a': /* file */ |
| 343 | success = read_pattern(f, p); | 369 | read_file(f); |
| 370 | success = 1; /* always success */ | ||
| 371 | break; | ||
| 372 | case 'w': /* word */ | ||
| 373 | read_word(f); | ||
| 374 | success = 0; /* must read something to succeed */ | ||
| 375 | break; | ||
| 376 | default: | ||
| 377 | luaL_argerror(arg, "invalid format"); | ||
| 378 | success = 0; /* to avoid warnings */ | ||
| 379 | } | ||
| 380 | } | ||
| 344 | } | 381 | } |
| 345 | l = luaL_getsize(); | 382 | l = luaL_getsize(); |
| 346 | if (!success && l==0) return; /* read fails */ | 383 | if (!success && l==0) return; /* read fails */ |
| 347 | lua_pushlstring(luaL_buffer(), l); | 384 | lua_pushlstring(luaL_buffer(), l); |
| 348 | } while ((p = luaL_opt_string(arg++, NULL)) != NULL); | 385 | } while ((op = lua_getparam(++arg)) != LUA_NOOBJECT); |
| 349 | } | 386 | } |
| 350 | 387 | ||
| 351 | /* }====================================================== */ | 388 | /* }====================================================== */ |
| @@ -355,10 +392,22 @@ static void io_write (void) { | |||
| 355 | int arg = FIRSTARG; | 392 | int arg = FIRSTARG; |
| 356 | FILE *f = getfileparam(FOUTPUT, &arg); | 393 | FILE *f = getfileparam(FOUTPUT, &arg); |
| 357 | int status = 1; | 394 | int status = 1; |
| 358 | const char *s; | 395 | lua_Object o; |
| 359 | long l; | 396 | while ((o = lua_getparam(arg++)) != LUA_NOOBJECT) { |
| 360 | while ((s = luaL_opt_lstr(arg++, NULL, &l)) != NULL) | 397 | switch (lua_type(o)[2]) { |
| 361 | status = status && ((long)fwrite(s, sizeof(char), l, f) == l); | 398 | case 'r': { /* stRing? */ |
| 399 | long l = lua_strlen(o); | ||
| 400 | status = status && | ||
| 401 | ((long)fwrite(lua_getstring(o), sizeof(char), l, f) == l); | ||
| 402 | break; | ||
| 403 | } | ||
| 404 | case 'm': /* nuMber? */ /* LUA_NUMBER */ | ||
| 405 | /* optimization: could be done exactly as for strings */ | ||
| 406 | status = status && fprintf(f, "%.16g", lua_getnumber(o)) > 0; | ||
| 407 | break; | ||
| 408 | default: luaL_argerror(arg-1, "string expected"); | ||
| 409 | } | ||
| 410 | } | ||
| 362 | pushresult(status); | 411 | pushresult(status); |
| 363 | } | 412 | } |
| 364 | 413 | ||
