CHDK_DE Vorschauversion  Trunk Rev. 5218
 Alle Datenstrukturen Dateien Funktionen Variablen Typdefinitionen Aufzählungen Aufzählungswerte Makrodefinitionen
liolib.c-Dateireferenz
#include <stdio.h>
#include <stdlib.h>
#include <string.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 29 der Datei liolib.c.

#define IO_OUTPUT   2

Definiert in Zeile 30 der Datei liolib.c.

#define liolib_c

Definiert in Zeile 16 der Datei liolib.c.

#define LUA_LIB

Definiert in Zeile 17 der Datei liolib.c.

#define SCAN_NUM_MAX_CHARS   11

Definiert in Zeile 286 der Datei liolib.c.

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

Definiert in Zeile 60 der Datei liolib.c.

Dokumentation der Funktionen

static int aux_close ( lua_State L)
static

Definiert in Zeile 136 der Datei liolib.c.

136  {
137  lua_getfenv(L, 1);
138  lua_getfield(L, -1, "__close");
139  return (lua_tocfunction(L, -1))(L);
140 }
static void aux_lines ( lua_State L,
int  idx,
int  toclose 
)
static

Definiert in Zeile 248 der Datei liolib.c.

248  {
249  lua_pushvalue(L, idx);
250  lua_pushboolean(L, toclose); /* close/not close file when finished */
252 }
static void createmeta ( lua_State L)
static

Definiert in Zeile 603 der Datei liolib.c.

603  {
604  luaL_newmetatable(L, LUA_FILEHANDLE); /* create metatable for file handles */
605  lua_pushvalue(L, -1); /* push metatable */
606  lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */
607  luaL_register(L, NULL, flib); /* file methods */
608 }
static void createstdfile ( lua_State L,
FILE f,
int  k,
const char *  fname 
)
static

Definiert in Zeile 611 der Datei liolib.c.

611  {
612  *newfile(L) = f;
613  if (k > 0) {
614  lua_pushvalue(L, -1);
616  }
617  lua_pushvalue(L, -2); /* copy environment */
618  lua_setfenv(L, -2); /* set it */
619  lua_setfield(L, -3, fname);
620 }
static int f_flush ( lua_State L)
static

Definiert in Zeile 555 der Datei liolib.c.

555  {
556  return pushresult(L, fflush(tofile(L)) == 0, NULL);
557 }
static int f_getfptr ( lua_State L)
static

Definiert in Zeile 559 der Datei liolib.c.

559  {
560  FILE *f = tofile(L);
561  lua_pushinteger(L, (int)f);
562  return 1;
563 }
static int f_lines ( lua_State L)
static

Definiert in Zeile 255 der Datei liolib.c.

255  {
256  tofile(L); /* check that it's a valid file handle */
257  aux_lines(L, 1, 0);
258  return 1;
259 }
static int f_read ( lua_State L)
static

Definiert in Zeile 455 der Datei liolib.c.

455  {
456  return g_read(L, tofile(L), 2);
457 }
static int f_seek ( lua_State L)
static

Definiert in Zeile 520 der Datei liolib.c.

520  {
521  static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END};
522  static const char *const modenames[] = {"set", "cur", "end", NULL};
523  FILE *f = tofile(L);
524  int op = luaL_checkoption(L, 2, "cur", modenames);
525  long offset = luaL_optlong(L, 3, 0);
526  op = fseek(f, offset, mode[op]);
527  if (op)
528  return pushresult(L, 0, NULL); /* error */
529  else {
530  lua_pushinteger(L, ftell(f));
531  return 1;
532  }
533 }
static int f_write ( lua_State L)
static

Definiert in Zeile 515 der Datei liolib.c.

515  {
516  return g_write(L, tofile(L), 2);
517 }
static void fileerror ( lua_State L,
int  arg,
const char *  filename 
)
static

Definiert in Zeile 54 der Datei liolib.c.

54  {
55  lua_pushfstring(L, "%s: %s", filename, strerror(errno));
56  luaL_argerror(L, arg, lua_tostring(L, -1));
57 }
static int g_iofile ( lua_State L,
int  f,
const char *  mode 
)
static

Definiert in Zeile 214 der Datei liolib.c.

214  {
215  if (!lua_isnoneornil(L, 1)) {
216  const char *filename = lua_tostring(L, 1);
217  if (filename) {
218  FILE **pf = newfile(L);
219  *pf = fopen(filename, mode);
220  if (*pf == NULL)
221  fileerror(L, 1, filename);
222  }
223  else {
224  tofile(L); /* check that it's a valid file handle */
225  lua_pushvalue(L, 1);
226  }
228  }
229  /* return current value */
231  return 1;
232 }
static int g_read ( lua_State L,
FILE f,
int  first 
)
static

Definiert in Zeile 397 der Datei liolib.c.

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

Definiert in Zeile 485 der Datei liolib.c.

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

Definiert in Zeile 204 der Datei liolib.c.

204  {
205  FILE *f;
206  lua_rawgeti(L, LUA_ENVIRONINDEX, findex);
207  f = *(FILE **)lua_touserdata(L, -1);
208  if (f == NULL)
209  luaL_error(L, "standard %s file is closed", fnames[findex - 1]);
210  return f;
211 }
static int io_close ( lua_State L)
static

Definiert in Zeile 143 der Datei liolib.c.

143  {
144  if (lua_isnone(L, 1))
146  tofile(L); /* make sure argument is a file */
147  return aux_close(L);
148 }
static int io_fclose ( lua_State L)
static

Definiert in Zeile 128 der Datei liolib.c.

128  {
129  FILE **p = tofilep(L);
130  int ok = (fclose(*p) == 0);
131  *p = NULL;
132  return pushresult(L, ok, NULL);
133 }
static int io_flush ( lua_State L)
static

Definiert in Zeile 550 der Datei liolib.c.

550  {
551  return pushresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL);
552 }
static int io_gc ( lua_State L)
static

Definiert in Zeile 151 der Datei liolib.c.

151  {
152  FILE *f = *tofilep(L);
153  /* ignore closed files */
154  if (f != NULL)
155  aux_close(L);
156  return 0;
157 }
static int io_input ( lua_State L)
static

Definiert in Zeile 235 der Datei liolib.c.

235  {
236  return g_iofile(L, IO_INPUT, "r");
237 }
static int io_lines ( lua_State L)
static

Definiert in Zeile 262 der Datei liolib.c.

262  {
263  if (lua_isnoneornil(L, 1)) { /* no arguments? */
264  /* will iterate over default input */
266  return f_lines(L);
267  }
268  else {
269  const char *filename = luaL_checkstring(L, 1);
270  FILE **pf = newfile(L);
271  *pf = fopen(filename, "r");
272  if (*pf == NULL)
273  fileerror(L, 1, filename);
274  aux_lines(L, lua_gettop(L), 1);
275  return 1;
276  }
277 }
static int io_noclose ( lua_State L)
static

Definiert in Zeile 104 der Datei liolib.c.

104  {
105  lua_pushnil(L);
106  lua_pushliteral(L, "cannot close standard file");
107  return 2;
108 }
static int io_open ( lua_State L)
static

Definiert in Zeile 170 der Datei liolib.c.

170  {
171  const char *filename = luaL_checkstring(L, 1);
172  const char *mode = luaL_optstring(L, 2, "r");
173  FILE **pf = newfile(L);
174  *pf = fopen(filename, mode);
175  return (*pf == NULL) ? pushresult(L, 0, filename) : 1;
176 }
static int io_output ( lua_State L)
static

Definiert in Zeile 240 der Datei liolib.c.

240  {
241  return g_iofile(L, IO_OUTPUT, "w");
242 }
static int io_read ( lua_State L)
static

Definiert in Zeile 450 der Datei liolib.c.

450  {
451  return g_read(L, getiofile(L, IO_INPUT), 1);
452 }
static int io_readline ( lua_State L)
static

Definiert in Zeile 460 der Datei liolib.c.

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

Definiert in Zeile 160 der Datei liolib.c.

160  {
161  FILE *f = *tofilep(L);
162  if (f == NULL)
163  lua_pushliteral(L, "file (closed)");
164  else
165  lua_pushfstring(L, "file (%p)", f);
166  return 1;
167 }
static int io_type ( lua_State L)
static

Definiert in Zeile 63 der Datei liolib.c.

63  {
64  void *ud;
65  luaL_checkany(L, 1);
66  ud = lua_touserdata(L, 1);
68  if (ud == NULL || !lua_getmetatable(L, 1) || !lua_rawequal(L, -2, -1))
69  lua_pushnil(L); /* not a file */
70  else if (*((FILE **)ud) == NULL)
71  lua_pushliteral(L, "closed file");
72  else
73  lua_pushliteral(L, "file");
74  return 1;
75 }
static int io_write ( lua_State L)
static

Definiert in Zeile 510 der Datei liolib.c.

510  {
511  return g_write(L, getiofile(L, IO_OUTPUT), 1);
512 }
LUALIB_API int luaopen_io ( lua_State L)

Definiert in Zeile 630 der Datei liolib.c.

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

Definiert in Zeile 623 der Datei liolib.c.

623  {
624  lua_createtable(L, 0, 1);
625  lua_pushcfunction(L, cls);
626  lua_setfield(L, -2, "__close");
627 }
static FILE** newfile ( lua_State L)
static

Definiert in Zeile 92 der Datei liolib.c.

92  {
93  FILE **pf = (FILE **)lua_newuserdata(L, sizeof(FILE *));
94  *pf = NULL; /* file handle is currently `closed' */
96  lua_setmetatable(L, -2);
97  return pf;
98 }
static int pushresult ( lua_State L,
int  i,
const char *  filename 
)
static

Definiert in Zeile 36 der Datei liolib.c.

36  {
37  int en = errno; /* calls to Lua API may change this value */
38  if (i) {
39  lua_pushboolean(L, 1);
40  return 1;
41  }
42  else {
43  lua_pushnil(L);
44  if (filename)
45  lua_pushfstring(L, "%s: %s", filename, strerror(en));
46  else
47  lua_pushfstring(L, "%s", strerror(en));
48  lua_pushinteger(L, en);
49  return 3;
50  }
51 }
static int read_chars ( lua_State L,
FILE f,
size_t  n 
)
static

Definiert in Zeile 379 der Datei liolib.c.

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

Definiert in Zeile 357 der Datei liolib.c.

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

Definiert in Zeile 323 der Datei liolib.c.

323  {
324  lua_Number d;
325 // no fscanf
326 #if 0
327  if (fscanf(f, LUA_NUMBER_SCAN, &d) == 1) {
328 #else
329  if (scan_num(f,&d) == 1) {
330 #endif
331  lua_pushnumber(L, d);
332  return 1;
333  }
334  else {
335  lua_pushnil(L); /* "result" to be removed */
336  return 0; /* read fails */
337  }
338 }
static int scan_num ( FILE f,
int *  r 
)
static

Definiert in Zeile 290 der Datei liolib.c.

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

Definiert in Zeile 349 der Datei liolib.c.

349  {
350  lua_pushlstring(L, NULL, 0);
351  // TODO not sure why getc/ungetc was used
352  return !feof(f);
353 }
static FILE* tofile ( lua_State L)
static

Definiert in Zeile 78 der Datei liolib.c.

78  {
79  FILE **f = tofilep(L);
80  if (*f == NULL)
81  luaL_error(L, "attempt to use a closed file");
82  return *f;
83 }

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 586 der Datei liolib.c.

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

Definiert in Zeile 33 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 565 der Datei liolib.c.