📄 liolib.c
字号:
/*** $Id: liolib.c,v 1.6 2003/06/30 19:15:26 ahowe Exp $** Standard I/O (and system) library
** This file modified from it's original form.** See Copyright Notice in lua.h*/#include <ctype.h>#include <stdio.h>#define __USE_MISC /* for mkstemp() */#include <stdlib.h>#include <string.h>#include <time.h>#include <assert.h>#ifndef macintosh
#ifndef WIN32# include <unistd.h> /* for close() */#endif
#endif
#ifdef WIN32
# define close _close
#endif#include "lua.h"#include "lauxlib.h"#include "luadebug.h"#include "lualib.h"#ifndef OLD_ANSI#include <errno.h>#include <locale.h>#define realloc(b,s) ((b) == NULL ? malloc(s) : (realloc)(b, s))#define free(b) if (b) (free)(b)#else/* no support for locale and for strerror: fake them */#define setlocale(a,b) ((void)a, strcmp((b),"C")==0?"C":NULL)#define LC_ALL 0#define LC_COLLATE 0#define LC_CTYPE 0#define LC_MONETARY 0#define LC_NUMERIC 0#define LC_TIME 0#define strerror(e) "generic I/O error"#define errno (-1)#endif#ifdef POPEN/* FILE *popen();int pclose(); */#define CLOSEFILE(L, f) ((pclose(f) == -1) ? fclose(f) : 0)#else/* no support for popen */#define popen(x,y) NULL /* that is, popen always fails */#define CLOSEFILE(L, f) (fclose(f))#endif#define INFILE 0#define OUTFILE 1typedef struct IOCtrl { int ref[2]; /* ref for strings _INPUT/_OUTPUT */ int iotag; /* tag for file handles */ int closedtag; /* tag for closed handles */} IOCtrl;static const char *const filenames[] = {"_INPUT", "_OUTPUT"};static int pushresult (lua_State *L, int i) { if (i) { lua_pushuserdata(L, NULL); return 1; } else { lua_pushnil(L); lua_pushstring(L, strerror(errno)); lua_pushnumber(L, errno); return 3;; }}/*** {======================================================** FILE Operations** =======================================================*/static FILE *gethandle (lua_State *L, IOCtrl *ctrl, int f) { void *p = lua_touserdata(L, f); if (p != NULL) { /* is `f' a userdata ? */ int ftag = lua_tag(L, f); if (ftag == ctrl->iotag) /* does it have the correct tag? */ return (FILE *)p; else if (ftag == ctrl->closedtag) lua_error(L, "cannot access a closed file"); /* else go through */ } return NULL;}static FILE *getnonullfile (lua_State *L, IOCtrl *ctrl, int arg) { FILE *f = gethandle(L, ctrl, arg); luaL_arg_check(L, f, arg, "invalid file handle"); return f;}static FILE *getfilebyref (lua_State *L, IOCtrl *ctrl, int inout) { FILE *f; lua_getglobals(L); lua_getref(L, ctrl->ref[inout]); lua_rawget(L, -2); f = gethandle(L, ctrl, -1); if (f == NULL) luaL_verror(L, "global variable `%.10s' is not a file handle", filenames[inout]); return f;}static void setfilebyname (lua_State *L, IOCtrl *ctrl, FILE *f, const char *name) { lua_pushusertag(L, f, ctrl->iotag); lua_setglobal(L, name);}#define setfile(L,ctrl,f,inout) (setfilebyname(L,ctrl,f,filenames[inout]))static int setreturn (lua_State *L, IOCtrl *ctrl, FILE *f, int inout) { if (f == NULL) return pushresult(L, 0); else { setfile(L, ctrl, f, inout); lua_pushusertag(L, f, ctrl->iotag); return 1; }}static int closefile (lua_State *L, IOCtrl *ctrl, FILE *f) { if (f == stdin || f == stdout || f == stderr) return 1; else { lua_pushusertag(L, f, ctrl->iotag); lua_settag(L, ctrl->closedtag); return (CLOSEFILE(L, f) == 0); }}static int io_close (lua_State *L) { IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1); lua_pop(L, 1); /* remove upvalue */ return pushresult(L, closefile(L, ctrl, getnonullfile(L, ctrl, 1)));}static int file_collect (lua_State *L) { IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1); FILE *f = getnonullfile(L, ctrl, 1); if (f != stdin && f != stdout && f != stderr) CLOSEFILE(L, f); return 0;}static int io_open (lua_State *L) { IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1); FILE *f; lua_pop(L, 1); /* remove upvalue */ f = fopen(luaL_check_string(L, 1), luaL_check_string(L, 2)); if (f) { lua_pushusertag(L, f, ctrl->iotag); return 1; } else return pushresult(L, 0);}static int io_fromto (lua_State *L, int inout, const char *mode) { IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1); FILE *current; lua_pop(L, 1); /* remove upvalue */ if (lua_isnull(L, 1)) { closefile(L, ctrl, getfilebyref(L, ctrl, inout)); current = (inout == 0) ? stdin : stdout; } else if (lua_tag(L, 1) == ctrl->iotag) /* deprecated option */ current = (FILE *)lua_touserdata(L, 1); else { const char *s = luaL_check_string(L, 1); current = (*s == '|') ? popen(s+1, mode) : fopen(s, mode); } return setreturn(L, ctrl, current, inout);}static int io_readfrom (lua_State *L) { return io_fromto(L, INFILE, "r");}static int io_writeto (lua_State *L) { return io_fromto(L, OUTFILE, "w");}static int io_appendto (lua_State *L) { IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1); FILE *current; lua_pop(L, 1); /* remove upvalue */ current = fopen(luaL_check_string(L, 1), "a"); return setreturn(L, ctrl, current, OUTFILE);}/*** {======================================================** READ** =======================================================*/#ifdef LUA_COMPAT_READPATTERN/*** We cannot lookahead without need, because this can lock stdin.** This flag signals when we need to read a next char.*/#define NEED_OTHER (EOF-1) /* just some flag different from EOF */static int read_pattern (lua_State *L, FILE *f, const char *p) { int inskip = 0; /* {skip} level */ int c = NEED_OTHER; luaL_Buffer b; luaL_buffinit(L, &b); while (*p != '\0') { switch (*p) { case '{': inskip++; p++; continue; case '}': if (!inskip) lua_error(L, "unbalanced braces in read pattern"); inskip--; p++; continue; default: { const char *ep = luaI_classend(L, p); /* get what is next */ int m; /* match result */ if (c == NEED_OTHER) c = getc(f); m = (c==EOF) ? 0 : luaI_singlematch(c, p, ep); if (m) { if (!inskip) luaL_putchar(&b, c); c = NEED_OTHER; } switch (*ep) { case '+': /* repetition (1 or more) */ if (!m) goto break_while; /* pattern fails? */ /* else go through */ case '*': /* repetition (0 or more) */ while (m) { /* reads the same item until it fails */ c = getc(f); m = (c==EOF) ? 0 : luaI_singlematch(c, p, ep); if (m && !inskip) luaL_putchar(&b, c); } /* go through to continue reading the pattern */ case '?': /* optional */ p = ep+1; /* continues reading the pattern */ continue; default: if (!m) goto break_while; /* pattern fails? */ p = ep; /* else continues reading the pattern */ } } } } break_while: if (c != NEED_OTHER) ungetc(c, f); luaL_pushresult(&b); /* close buffer */ return (*p == '\0');}#else#define read_pattern(L, f, p) (lua_error(L, "read patterns are deprecated"), 0)#endifstatic int read_number (lua_State *L, FILE *f) { double d; if (fscanf(f, "%lf", &d) == 1) { lua_pushnumber(L, d); return 1; } else return 0; /* read fails */}static int read_word (lua_State *L, FILE *f) { int c; luaL_Buffer b; luaL_buffinit(L, &b); do { c = fgetc(f); } while (isspace(c)); /* skip spaces */ while (c != EOF && !isspace(c)) { luaL_putchar(&b, c); c = fgetc(f); } ungetc(c, f); luaL_pushresult(&b); /* close buffer */ return (lua_strlen(L, -1) > 0);}static int read_line (lua_State *L, FILE *f) { int n = 0; luaL_Buffer b; luaL_buffinit(L, &b); for (;;) { char *p = luaL_prepbuffer(&b); if (!fgets(p, LUAL_BUFFERSIZE, f)) /* read fails? */ break; n = strlen(p); if (p[n-1] != '\n') luaL_addsize(&b, n); else { luaL_addsize(&b, n-1); /* do not add the `\n' */ break; } } luaL_pushresult(&b); /* close buffer */ return (n > 0); /* read something? */}static void read_file (lua_State *L, FILE *f) { size_t len = 0; size_t size = BUFSIZ; char *buffer = NULL; for (;;) { char *newbuffer = (char *)realloc(buffer, size); if (newbuffer == NULL) { free(buffer); lua_error(L, "not enough memory to read a file"); } buffer = newbuffer; len += fread(buffer+len, sizeof(char), size-len, f); if (len < size) break; /* did not read all it could */ size *= 2; } lua_pushlstring(L, buffer, len); free(buffer);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -