Lua1.0 Code Analysis INOUT.C

Source: Internet
Author: User

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.*/intlua_openfile (char*fn){ lua_linenumber = 1; lua_setinput (fileinput); lua_setunput (fileunput); fp = fopen(fn, "r"); if(fp == NULL) return1; if(lua_addfile (fn)) return1; return0;}

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*/staticint 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*/staticvoid 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.*/intlua_pushfunction (intfile, intfunction){ if (nfuncstack >= MAXFUNCSTACK-1) {  lua_error ("function stack overflow");  return1; } funcstack[nfuncstack].file = file; funcstack[nfuncstack].function = function; nfuncstack++; return0;}

Set the function in the script to the function stack. Record its file name and function address.

Lua1.0 Code Analysis INOUT.C

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.