zoukankan      html  css  js  c++  java
  • Lua1.0 代码分析 inout.c

    转载出处:http://my.oschina.net/xhan/blog/307797

    inout.c 代码分析
    主要看下对于文件的处理

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    /*
    ** 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 中再做分析。

    1
    2
    3
    4
    5
    6
    7
    8
    /*
    ** Function to get the next character from the input file
    */
    static int fileinput (void)
    {
     int c = fgetc (fp);
     return (c == EOF ? 0 : c);
    }

    从文件句柄里读取一个字符

    1
    2
    3
    4
    5
    6
    7
    /*
    ** 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" 开头的字符串表示。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    /*
    ** 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;
    }

    设置脚本中的函数到函数这个函数栈中。记录其文件名和函数地址。

  • 相关阅读:
    delphi debug release区别是什么?
    Delphi异形窗口之PNG
    WebBrowser实现编辑网页
    父子窗体滚动条支持鼠标滚轮移动
    Com进程通信(Delphi2007)
    Android实例-Delphi开发蓝牙官方实例解析(XE10+小米2+小米5)
    教程-Close、Halt、terminate、ExitProcess的区别
    各国特种部队名称
    关于c#字典key不存在的测试
    关于scut使用WebService
  • 原文地址:https://www.cnblogs.com/vd01/p/4934268.html
Copyright © 2011-2022 走看看