diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-02-06 14:01:29 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-02-06 14:01:29 -0200 |
| commit | 1f917e709ca3fe41cf07fd4bf99a080883521394 (patch) | |
| tree | 00e89f7218a38e028bebe111dbf682925fe4cca0 /liolib.c | |
| parent | d444153dbea006f5e0547f27a11256af96ba1829 (diff) | |
| download | lua-1f917e709ca3fe41cf07fd4bf99a080883521394.tar.gz lua-1f917e709ca3fe41cf07fd4bf99a080883521394.tar.bz2 lua-1f917e709ca3fe41cf07fd4bf99a080883521394.zip | |
better use of extra include files (both for tests and for old_ansi)
Diffstat (limited to 'liolib.c')
| -rw-r--r-- | liolib.c | 34 |
1 files changed, 15 insertions, 19 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: liolib.c,v 1.102 2001/01/26 12:12:16 roberto Exp roberto $ | 2 | ** $Id: liolib.c,v 1.103 2001/02/02 19:02:40 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 | */ |
| @@ -21,19 +21,13 @@ | |||
| 21 | #ifndef OLD_ANSI | 21 | #ifndef OLD_ANSI |
| 22 | #include <errno.h> | 22 | #include <errno.h> |
| 23 | #include <locale.h> | 23 | #include <locale.h> |
| 24 | #define realloc(b,s) ((b) == NULL ? malloc(s) : (realloc)(b, s)) | 24 | #endif |
| 25 | #define free(b) if (b) (free)(b) | 25 | |
| 26 | #else | 26 | |
| 27 | /* no support for locale and for strerror: fake them */ | 27 | #ifndef l_realloc |
| 28 | #define setlocale(a,b) ((void)a, strcmp((b),"C")==0?"C":NULL) | 28 | #define l_malloc(s) malloc(s) |
| 29 | #define LC_ALL 0 | 29 | #define l_realloc(b,os,s) realloc(b, s) |
| 30 | #define LC_COLLATE 0 | 30 | #define l_free(b, os) free(b) |
| 31 | #define LC_CTYPE 0 | ||
| 32 | #define LC_MONETARY 0 | ||
| 33 | #define LC_NUMERIC 0 | ||
| 34 | #define LC_TIME 0 | ||
| 35 | #define strerror(e) "I/O error" | ||
| 36 | #define errno (-1) | ||
| 37 | #endif | 31 | #endif |
| 38 | 32 | ||
| 39 | 33 | ||
| @@ -258,20 +252,22 @@ static int read_line (lua_State *L, FILE *f) { | |||
| 258 | static void read_file (lua_State *L, FILE *f) { | 252 | static void read_file (lua_State *L, FILE *f) { |
| 259 | size_t len = 0; | 253 | size_t len = 0; |
| 260 | size_t size = LUAL_BUFFERSIZE; | 254 | size_t size = LUAL_BUFFERSIZE; |
| 255 | size_t oldsize = 0; | ||
| 261 | char *buffer = NULL; | 256 | char *buffer = NULL; |
| 262 | for (;;) { | 257 | for (;;) { |
| 263 | char *newbuffer = (char *)realloc(buffer, size); | 258 | char *newbuffer = (char *)l_realloc(buffer, oldsize, size); |
| 264 | if (newbuffer == NULL) { | 259 | if (newbuffer == NULL) { |
| 265 | free(buffer); | 260 | l_free(buffer, oldsize); |
| 266 | lua_error(L, "not enough memory to read a file"); | 261 | lua_error(L, "not enough memory to read a file"); |
| 267 | } | 262 | } |
| 268 | buffer = newbuffer; | 263 | buffer = newbuffer; |
| 269 | len += fread(buffer+len, sizeof(char), size-len, f); | 264 | len += fread(buffer+len, sizeof(char), size-len, f); |
| 270 | if (len < size) break; /* did not read all it could */ | 265 | if (len < size) break; /* did not read all it could */ |
| 266 | oldsize = size; | ||
| 271 | size *= 2; | 267 | size *= 2; |
| 272 | } | 268 | } |
| 273 | lua_pushlstring(L, buffer, len); | 269 | lua_pushlstring(L, buffer, len); |
| 274 | free(buffer); | 270 | l_free(buffer, size); |
| 275 | } | 271 | } |
| 276 | 272 | ||
| 277 | 273 | ||
| @@ -282,13 +278,13 @@ static int read_chars (lua_State *L, FILE *f, size_t n) { | |||
| 282 | if (n <= LUAL_BUFFERSIZE) | 278 | if (n <= LUAL_BUFFERSIZE) |
| 283 | buffer = statbuff; | 279 | buffer = statbuff; |
| 284 | else { | 280 | else { |
| 285 | buffer = (char *)malloc(n); | 281 | buffer = (char *)l_malloc(n); |
| 286 | if (buffer == NULL) | 282 | if (buffer == NULL) |
| 287 | lua_error(L, "not enough memory to read a file"); | 283 | lua_error(L, "not enough memory to read a file"); |
| 288 | } | 284 | } |
| 289 | n1 = fread(buffer, sizeof(char), n, f); | 285 | n1 = fread(buffer, sizeof(char), n, f); |
| 290 | lua_pushlstring(L, buffer, n1); | 286 | lua_pushlstring(L, buffer, n1); |
| 291 | if (buffer != statbuff) free(buffer); | 287 | if (buffer != statbuff) l_free(buffer, n); |
| 292 | return (n1 > 0 || n == 0); | 288 | return (n1 > 0 || n == 0); |
| 293 | } | 289 | } |
| 294 | 290 | ||
