CHDK_DE Vorschauversion  Trunk Rev. 5836
 Alle Datenstrukturen Dateien Funktionen Variablen Typdefinitionen Aufzählungen Aufzählungswerte Makrodefinitionen
liolib.c-Dateireferenz
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
+ Include-Abhängigkeitsdiagramm für liolib.c:

gehe zum Quellcode dieser Datei

Makrodefinitionen

#define liolib_c
 
#define LUA_LIB
 
#define IO_INPUT   1
 
#define IO_OUTPUT   2
 
#define tofilep(L)   ((FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE))
 
#define SCAN_NUM_MAX_CHARS   11
 

Funktionen

static int pushresult (lua_State *L, int i, const char *filename)
 
static void fileerror (lua_State *L, int arg, const char *filename)
 
static int io_type (lua_State *L)
 
static FILEtofile (lua_State *L)
 
static FILE ** newfile (lua_State *L)
 
static int io_noclose (lua_State *L)
 
static int io_fclose (lua_State *L)
 
static int aux_close (lua_State *L)
 
static int io_close (lua_State *L)
 
static int io_gc (lua_State *L)
 
static int io_tostring (lua_State *L)
 
static int io_open (lua_State *L)
 
static FILEgetiofile (lua_State *L, int findex)
 
static int g_iofile (lua_State *L, int f, const char *mode)
 
static int io_input (lua_State *L)
 
static int io_output (lua_State *L)
 
static int io_readline (lua_State *L)
 
static void aux_lines (lua_State *L, int idx, int toclose)
 
static int f_lines (lua_State *L)
 
static int io_lines (lua_State *L)
 
static int scan_num (FILE *f, int *r)
 
static int read_number (lua_State *L, FILE *f)
 
static int test_eof (lua_State *L, FILE *f)
 
static int read_line (lua_State *L, FILE *f)
 
static int read_chars (lua_State *L, FILE *f, size_t n)
 
static int g_read (lua_State *L, FILE *f, int first)
 
static int io_read (lua_State *L)
 
static int f_read (lua_State *L)
 
static int g_write (lua_State *L, FILE *f, int arg)
 
static int io_write (lua_State *L)
 
static int f_write (lua_State *L)
 
static int f_seek (lua_State *L)
 
static int io_flush (lua_State *L)
 
static int f_flush (lua_State *L)
 
static int f_getfptr (lua_State *L)
 
static void createmeta (lua_State *L)
 
static void createstdfile (lua_State *L, FILE *f, int k, const char *fname)
 
static void newfenv (lua_State *L, lua_CFunction cls)
 
LUALIB_API int luaopen_io (lua_State *L)
 

Variablen

static const char *const fnames [] = {"input", "output"}
 
static const luaL_Reg iolib []
 
static const luaL_Reg flib []
 

Makro-Dokumentation

#define IO_INPUT   1

Definiert in Zeile 21 der Datei liolib.c.

#define IO_OUTPUT   2

Definiert in Zeile 22 der Datei liolib.c.

#define liolib_c

Definiert in Zeile 13 der Datei liolib.c.

#define LUA_LIB

Definiert in Zeile 14 der Datei liolib.c.

#define SCAN_NUM_MAX_CHARS   11

Definiert in Zeile 278 der Datei liolib.c.

#define tofilep (   L)    ((FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE))

Definiert in Zeile 52 der Datei liolib.c.

Dokumentation der Funktionen

static int aux_close ( lua_State L)
static

Definiert in Zeile 128 der Datei liolib.c.

128  {
129  lua_getfenv(L, 1);
130  lua_getfield(L, -1, "__close");
131  return (lua_tocfunction(L, -1))(L);
132 }
static void aux_lines ( lua_State L,
int  idx,
int  toclose 
)
static

Definiert in Zeile 240 der Datei liolib.c.

240  {
241  lua_pushvalue(L, idx);
242  lua_pushboolean(L, toclose); /* close/not close file when finished */
244 }
static void createmeta ( lua_State L)
static

Definiert in Zeile 595 der Datei liolib.c.

595  {
596  luaL_newmetatable(L, LUA_FILEHANDLE); /* create metatable for file handles */
597  lua_pushvalue(L, -1); /* push metatable */
598  lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */
599  luaL_register(L, NULL, flib); /* file methods */
600 }
static void createstdfile ( lua_State L,
FILE f,
int  k,
const char *  fname 
)
static

Definiert in Zeile 603 der Datei liolib.c.

603  {
604  *newfile(L) = f;
605  if (k > 0) {
606  lua_pushvalue(L, -1);
608  }
609  lua_pushvalue(L, -2); /* copy environment */
610  lua_setfenv(L, -2); /* set it */
611  lua_setfield(L, -3, fname);
612 }
static int f_flush ( lua_State L)
static

Definiert in Zeile 547 der Datei liolib.c.

547  {
548  return pushresult(L, fflush(tofile(L)) == 0, NULL);
549 }
static int f_getfptr ( lua_State L)
static

Definiert in Zeile 551 der Datei liolib.c.

551  {
552  FILE *f = tofile(L);
553  lua_pushinteger(L, (int)f);
554  return 1;
555 }
static int f_lines ( lua_State L)
static

Definiert in Zeile 247 der Datei liolib.c.

247  {
248  tofile(L); /* check that it's a valid file handle */
249  aux_lines(L, 1, 0);
250  return 1;
251 }
static int f_read ( lua_State L)
static

Definiert in Zeile 447 der Datei liolib.c.

447  {
448  return g_read(L, tofile(L), 2);
449 }
static int f_seek ( lua_State L)
static

Definiert in Zeile 512 der Datei liolib.c.

512  {
513  static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END};
514  static const char *const modenames[] = {"set", "cur", "end", NULL};
515  FILE *f = tofile(L);
516  int op = luaL_checkoption(L, 2, "cur", modenames);
517  long offset = luaL_optlong(L, 3, 0);
518  op = fseek(f, offset, mode[op]);
519  if (op)
520  return pushresult(L, 0, NULL); /* error */
521  else {
522  lua_pushinteger(L, ftell(f));
523  return 1;
524  }
525 }
static int f_write ( lua_State L)
static

Definiert in Zeile 507 der Datei liolib.c.

507  {
508  return g_write(L, tofile(L), 2);
509 }
static void fileerror ( lua_State L,
int  arg,
const char *  filename 
)
static

Definiert in Zeile 46 der Datei liolib.c.

46  {
47  lua_pushfstring(L, "%s: %s", filename, strerror(errno));
48  luaL_argerror(L, arg, lua_tostring(L, -1));
49 }
static int g_iofile ( lua_State L,
int  f,
const char *  mode 
)
static

Definiert in Zeile 206 der Datei liolib.c.

206  {
207  if (!lua_isnoneornil(L, 1)) {
208  const char *filename = lua_tostring(L, 1);
209  if (filename) {
210  FILE **pf = newfile(L);
211  *pf = fopen(filename, mode);
212  if (*pf == NULL)
213  fileerror(L, 1, filename);
214  }
215  else {
216  tofile(L); /* check that it's a valid file handle */
217  lua_pushvalue(L, 1);
218  }
220  }
221  /* return current value */
223  return 1;
224 }
static int g_read ( lua_State L,
FILE f,
int  first 
)
static

Definiert in Zeile 389 der Datei liolib.c.

389  {
390  int nargs = lua_gettop(L) - 1;
391  int success;
392  int n;
393 // no error state to clear
394 #if 0
395  clearerr(f);
396 #endif
397  if (nargs == 0) { /* no arguments? */
398  success = read_line(L, f);
399  n = first+1; /* to return 1 result */
400  }
401  else { /* ensure stack space for all results and for auxlib's buffer */
402  luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments");
403  success = 1;
404  for (n = first; nargs-- && success; n++) {
405  if (lua_type(L, n) == LUA_TNUMBER) {
406  size_t l = (size_t)lua_tointeger(L, n);
407  success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);
408  }
409  else {
410  const char *p = lua_tostring(L, n);
411  luaL_argcheck(L, p && p[0] == '*', n, "invalid option");
412  switch (p[1]) {
413  case 'n': /* number */
414  success = read_number(L, f);
415  break;
416  case 'l': /* line */
417  success = read_line(L, f);
418  break;
419  case 'a': /* file */
420  read_chars(L, f, ~((size_t)0)); /* read MAX_SIZE_T chars */
421  success = 1; /* always success */
422  break;
423  default:
424  return luaL_argerror(L, n, "invalid format");
425  }
426  }
427  }
428  }
429 // we have no ferror
430 #if 0
431  if (ferror(f))
432  return pushresult(L, 0, NULL);
433 #endif
434  if (!success) {
435  lua_pop(L, 1); /* remove last result */
436  lua_pushnil(L); /* push nil instead */
437  }
438  return n - first;
439 }
static int g_write ( lua_State L,
FILE f,
int  arg 
)
static

Definiert in Zeile 477 der Datei liolib.c.

477  {
478  int nargs = lua_gettop(L) - 1;
479  int status = 1;
480  for (; nargs--; arg++) {
481  if (lua_type(L, arg) == LUA_TNUMBER) {
482  char s[12];
484  /* optimization: could be done exactly as for strings */
485  status = status &&
486 #if 0
487  fprintf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0;
488 #else
489  fwrite(s, sizeof(char), strlen(s), f);
490 #endif
491  }
492  else {
493  size_t l;
494  const char *s = luaL_checklstring(L, arg, &l);
495  status = status && ((size_t)fwrite(s, sizeof(char), l, f) == l);
496  }
497  }
498  return pushresult(L, status, NULL);
499 }
static FILE* getiofile ( lua_State L,
int  findex 
)
static

Definiert in Zeile 196 der Datei liolib.c.

196  {
197  FILE *f;
198  lua_rawgeti(L, LUA_ENVIRONINDEX, findex);
199  f = *(FILE **)lua_touserdata(L, -1);
200  if (f == NULL)
201  luaL_error(L, "standard %s file is closed", fnames[findex - 1]);
202  return f;
203 }
static int io_close ( lua_State L)
static

Definiert in Zeile 135 der Datei liolib.c.

135  {
136  if (lua_isnone(L, 1))
138  tofile(L); /* make sure argument is a file */
139  return aux_close(L);
140 }
static int io_fclose ( lua_State L)
static

Definiert in Zeile 120 der Datei liolib.c.

120  {
121  FILE **p = tofilep(L);
122  int ok = (fclose(*p) == 0);
123  *p = NULL;
124  return pushresult(L, ok, NULL);
125 }
static int io_flush ( lua_State L)
static

Definiert in Zeile 542 der Datei liolib.c.

542  {
543  return pushresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL);
544 }
static int io_gc ( lua_State L)
static

Definiert in Zeile 143 der Datei liolib.c.

143  {
144  FILE *f = *tofilep(L);
145  /* ignore closed files */
146  if (f != NULL)
147  aux_close(L);
148  return 0;
149 }
static int io_input ( lua_State L)
static

Definiert in Zeile 227 der Datei liolib.c.

227  {
228  return g_iofile(L, IO_INPUT, "r");
229 }
static int io_lines ( lua_State L)
static

Definiert in Zeile 254 der Datei liolib.c.

254  {
255  if (lua_isnoneornil(L, 1)) { /* no arguments? */
256  /* will iterate over default input */
258  return f_lines(L);
259  }
260  else {
261  const char *filename = luaL_checkstring(L, 1);
262  FILE **pf = newfile(L);
263  *pf = fopen(filename, "r");
264  if (*pf == NULL)
265  fileerror(L, 1, filename);
266  aux_lines(L, lua_gettop(L), 1);
267  return 1;
268  }
269 }
static int io_noclose ( lua_State L)
static

Definiert in Zeile 96 der Datei liolib.c.

96  {
97  lua_pushnil(L);
98  lua_pushliteral(L, "cannot close standard file");
99  return 2;
100 }
static int io_open ( lua_State L)
static

Definiert in Zeile 162 der Datei liolib.c.

162  {
163  const char *filename = luaL_checkstring(L, 1);
164  const char *mode = luaL_optstring(L, 2, "r");
165  FILE **pf = newfile(L);
166  *pf = fopen(filename, mode);
167  return (*pf == NULL) ? pushresult(L, 0, filename) : 1;
168 }
static int io_output ( lua_State L)
static

Definiert in Zeile 232 der Datei liolib.c.

232  {
233  return g_iofile(L, IO_OUTPUT, "w");
234 }
static int io_read ( lua_State L)
static

Definiert in Zeile 442 der Datei liolib.c.

442  {
443  return g_read(L, getiofile(L, IO_INPUT), 1);
444 }
static int io_readline ( lua_State L)
static

Definiert in Zeile 452 der Datei liolib.c.

452  {
453  FILE *f = *(FILE **)lua_touserdata(L, lua_upvalueindex(1));
454  int sucess;
455  if (f == NULL) /* file is already closed? */
456  luaL_error(L, "file is already closed");
457  sucess = read_line(L, f);
458 // we have no ferror
459 #if 0
460  if (ferror(f))
461  return luaL_error(L, "%s", strerror(errno));
462 #endif
463  if (sucess) return 1;
464  else { /* EOF */
465  if (lua_toboolean(L, lua_upvalueindex(2))) { /* generator created file? */
466  lua_settop(L, 0);
468  aux_close(L); /* close it */
469  }
470  return 0;
471  }
472 }
static int io_tostring ( lua_State L)
static

Definiert in Zeile 152 der Datei liolib.c.

152  {
153  FILE *f = *tofilep(L);
154  if (f == NULL)
155  lua_pushliteral(L, "file (closed)");
156  else
157  lua_pushfstring(L, "file (%p)", f);
158  return 1;
159 }
static int io_type ( lua_State L)
static

Definiert in Zeile 55 der Datei liolib.c.

55  {
56  void *ud;
57  luaL_checkany(L, 1);
58  ud = lua_touserdata(L, 1);
60  if (ud == NULL || !lua_getmetatable(L, 1) || !lua_rawequal(L, -2, -1))
61  lua_pushnil(L); /* not a file */
62  else if (*((FILE **)ud) == NULL)
63  lua_pushliteral(L, "closed file");
64  else
65  lua_pushliteral(L, "file");
66  return 1;
67 }
static int io_write ( lua_State L)
static

Definiert in Zeile 502 der Datei liolib.c.

502  {
503  return g_write(L, getiofile(L, IO_OUTPUT), 1);
504 }
LUALIB_API int luaopen_io ( lua_State L)

Definiert in Zeile 622 der Datei liolib.c.

622  {
623  createmeta(L);
624  /* create (private) environment (with fields IO_INPUT, IO_OUTPUT, __close) */
625  newfenv(L, io_fclose);
627  /* open library */
629  /* create (and set) default files */
630  newfenv(L, io_noclose); /* close function for default files */
631 #ifdef HOST_LUA
632  createstdfile(L, stdin, IO_INPUT, "stdin");
633  createstdfile(L, stdout, IO_OUTPUT, "stdout");
634  createstdfile(L, stderr, 0, "stderr");
635 #else
636 // initialize them in the closed state
637 // stderr/stdout could go to regular files or script console
638  createstdfile(L, NULL, IO_INPUT, "stdin");
639  createstdfile(L, NULL, IO_OUTPUT, "stdout");
640  createstdfile(L, NULL, 0, "stderr");
641 #endif
642  lua_pop(L, 1); /* pop environment for default files */
643  // reyalp - no popen
644 #if 0
645  lua_getfield(L, -1, "popen");
646  newfenv(L, io_pclose); /* create environment for 'popen' */
647  lua_setfenv(L, -2); /* set fenv for 'popen' */
648  lua_pop(L, 1); /* pop 'popen' */
649 #endif
650  return 1;
651 }
static void newfenv ( lua_State L,
lua_CFunction  cls 
)
static

Definiert in Zeile 615 der Datei liolib.c.

615  {
616  lua_createtable(L, 0, 1);
617  lua_pushcfunction(L, cls);
618  lua_setfield(L, -2, "__close");
619 }
static FILE** newfile ( lua_State L)
static

Definiert in Zeile 84 der Datei liolib.c.

84  {
85  FILE **pf = (FILE **)lua_newuserdata(L, sizeof(FILE *));
86  *pf = NULL; /* file handle is currently `closed' */
88  lua_setmetatable(L, -2);
89  return pf;
90 }
static int pushresult ( lua_State L,
int  i,
const char *  filename 
)
static

Definiert in Zeile 28 der Datei liolib.c.

28  {
29  int en = errno; /* calls to Lua API may change this value */
30  if (i) {
31  lua_pushboolean(L, 1);
32  return 1;
33  }
34  else {
35  lua_pushnil(L);
36  if (filename)
37  lua_pushfstring(L, "%s: %s", filename, strerror(en));
38  else
39  lua_pushfstring(L, "%s", strerror(en));
40  lua_pushinteger(L, en);
41  return 3;
42  }
43 }
static int read_chars ( lua_State L,
FILE f,
size_t  n 
)
static

Definiert in Zeile 371 der Datei liolib.c.

371  {
372  size_t rlen; /* how much to read */
373  size_t nr; /* number of chars actually read */
374  luaL_Buffer b;
375  luaL_buffinit(L, &b);
376  rlen = LUAL_BUFFERSIZE; /* try to read that much each time */
377  do {
378  char *p = luaL_prepbuffer(&b);
379  if (rlen > n) rlen = n; /* cannot read more than asked */
380  nr = fread(p, sizeof(char), rlen, f);
381  luaL_addsize(&b, nr);
382  n -= nr; /* still have to read `n' chars */
383  } while (n > 0 && nr == rlen); /* until end of count or eof */
384  luaL_pushresult(&b); /* close buffer */
385  return (n == 0 || lua_objlen(L, -1) > 0);
386 }
static int read_line ( lua_State L,
FILE f 
)
static

Definiert in Zeile 349 der Datei liolib.c.

349  {
350  luaL_Buffer b;
351  luaL_buffinit(L, &b);
352  for (;;) {
353  size_t l;
354  char *p = luaL_prepbuffer(&b);
355  if (fgets(p, LUAL_BUFFERSIZE, f) == NULL) { /* eof? */
356  luaL_pushresult(&b); /* close buffer */
357  return (lua_objlen(L, -1) > 0); /* check whether read something */
358  }
359  l = strlen(p);
360  if (l == 0 || p[l-1] != '\n')
361  luaL_addsize(&b, l);
362  else {
363  luaL_addsize(&b, l - 1); /* do not include `eol' */
364  luaL_pushresult(&b); /* close buffer */
365  return 1; /* read at least an `eol' */
366  }
367  }
368 }
static int read_number ( lua_State L,
FILE f 
)
static

Definiert in Zeile 315 der Datei liolib.c.

315  {
316  lua_Number d;
317 // no fscanf
318 #if 0
319  if (fscanf(f, LUA_NUMBER_SCAN, &d) == 1) {
320 #else
321  if (scan_num(f,&d) == 1) {
322 #endif
323  lua_pushnumber(L, d);
324  return 1;
325  }
326  else {
327  lua_pushnil(L); /* "result" to be removed */
328  return 0; /* read fails */
329  }
330 }
static int scan_num ( FILE f,
int *  r 
)
static

Definiert in Zeile 282 der Datei liolib.c.

282  {
283  char c;
284  int count=0;
285  int neg=0;
286  char s[SCAN_NUM_MAX_CHARS];
287  do {
288  if(fread(&c,1,1,f) != 1) return 0;
289  } while( isspace(c) );
290  if(c == '-') {
291  neg=1;
292  if(fread(&c,1,1,f) != 1) return 0;
293  }
294  while( count < SCAN_NUM_MAX_CHARS ) {
295  if(isdigit(c)) {
296  s[count++] = c;
297  }
298  else {
299  fseek(f,-1,SEEK_CUR);
300  if(count < 1 )
301  return 0;
302  else
303  break;
304  }
305  if( fread(&c,1,1,f) != 1 ) break;
306  }
307  s[count] = 0;
308  if(count) {
309  *r = (neg) ? -atoi(s) : atoi(s);
310  return 1;
311  }
312  return 0;
313 }
static int test_eof ( lua_State L,
FILE f 
)
static

Definiert in Zeile 341 der Datei liolib.c.

341  {
342  lua_pushlstring(L, NULL, 0);
343  // TODO not sure why getc/ungetc was used
344  return !feof(f);
345 }
static FILE* tofile ( lua_State L)
static

Definiert in Zeile 70 der Datei liolib.c.

70  {
71  FILE **f = tofilep(L);
72  if (*f == NULL)
73  luaL_error(L, "attempt to use a closed file");
74  return *f;
75 }

Variablen-Dokumentation

const luaL_Reg flib[]
static
Initialisierung:
= {
{"close", io_close},
{"flush", f_flush},
{"lines", f_lines},
{"read", f_read},
{"seek", f_seek},
{"write", f_write},
{"_getfptr",f_getfptr},
{"__gc", io_gc},
{"__tostring", io_tostring},
}

Definiert in Zeile 578 der Datei liolib.c.

const char* const fnames[] = {"input", "output"}
static

Definiert in Zeile 25 der Datei liolib.c.

const luaL_Reg iolib[]
static
Initialisierung:
= {
{"close", io_close},
{"flush", io_flush},
{"input", io_input},
{"lines", io_lines},
{"open", io_open},
{"output", io_output},
{"read", io_read},
{"type", io_type},
{"write", io_write},
}

Definiert in Zeile 557 der Datei liolib.c.