Reprint Source: http://my.oschina.net/xhan/blog/307797
INOUT.C Code Analysis
The main view of the processing of files
?
1234567891011121314 |
/*
** 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;
}
|
Pass in the script file name, set the current input behavior 1 set the file to read and put back. Open the file and set the file handle. Add the file to the file list. Lua_addfile is defined in the table.c, wait until the table.c to do the analysis again.
?
12345678 |
/* ** Function to get the next character from the input file */ static int fileinput ( void ) { int c = fgetc (fp); return (c == EOF ? 0 : c); } |
Read a character from a file handle
?
1234567 |
/* ** Function to unget the next character from to input file */ static void fileunput ( int c) { ungetc (c, fp); } |
Put a character back in the file handle
int lua_openstring (char *s) is similar to Lua_openfile, except that the file name is represented by a string that begins with a "string".
?
12345678910111213141516 |
/*
** 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;
}
|
Set the function in the script to the function stack. Record its file name and function address.
Lua1.0 Code Analysis INOUT.C