inout.c 代碼分析
主要看下對於文件的處理
函數
/* ** Function to open a file to be input unit. ** Return 0 on success or 1 on error. */ int lua_openfile (char *fn) { lua_linenumber = 1; lua_setinput (fileinput); lua_setunput (fileunput); fp = fopen (fn, "r"); if (fp == NULL) return 1; if (lua_addfile (fn)) return 1; return 0; }
傳入腳本文件名,設置當前的輸入行爲1
設置文件讀取和放回。
打開文件,設置文件句柄。
把文件加到文件列表裏。lua_addfile 在 table.c 中定義,等到 table.c 中再作分析。
this
/* ** Function to get the next character from the input file */ static int fileinput (void) { int c = fgetc (fp); return (c == EOF ? 0 : c); }
從文件句柄裏讀取一個字符
lua
/* ** Function to unget the next character from to input file */ static void fileunput (int c) { ungetc (c, fp); }
把一個字符放回文件句柄
int lua_openstring (char *s) 和 lua_openfile 差很少,只不過文件名用的是一個 「String" 開頭的字符串表示。
code
/* ** Called to execute SETFUNCTION opcode, this function pushs a function into ** function stack. Return 0 on success or 1 on error. */ int lua_pushfunction (int file, int function) { if (nfuncstack >= MAXFUNCSTACK-1) { lua_error ("function stack overflow"); return 1; } funcstack[nfuncstack].file = file; funcstack[nfuncstack].function = function; nfuncstack++; return 0; }
設置腳本中的函數到函數這個函數棧中。記錄其文件名和函數地址。
字符串