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;
    }

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

  • 相关阅读:
    HBase学习笔记-基础(一)
    系统架构师之旅(一)——软件架构设计:程序员向架构师转型必备总结
    高效程序员的特征:聪明,懒惰
    TOMCAT源码分析(启动框架)
    Java线程池的原理及几类线程池的介绍
    Java 并发:Executors 和线程池
    Memcache基础教程
    发邮件
    图片验证码
    上传图片添加水印
  • 原文地址:https://www.cnblogs.com/vd01/p/4934268.html
Copyright © 2011-2022 走看看