想像一个场景:你的c程序须要有一个窗体,你想让用户能够自己定义窗体大小。方法非常多。比方使用环境变量,或键值对的文件。

无论如何,你须要解析它。

使用lua配置文件是个不错的选择。


首先,你能够定义例如以下的配置文件:
--define window size

width = 100

height = 50

然后,我们写个函数来解析它,使用lua API 来指导lua解析配置。

,以下是完整的程序:


#include <stdio.h>

#include <string.h>

#include <lua.h>

#include <lauxlib.h>

#include <lualib.h>



void load(lua_State* L, const char* fname, int *w, int *h)

{

     if (luaL_loadfile(L, fname) || lua_pcall(L, 0, 0, 0)) {

          error(L, "error:%s", lua_tostring(L, -1));

     }

     lua_getglobal(L, "width");

     lua_getglobal(L, "height");

     if (!lua_isnumber(L, -2)) {

          error(L, "width shuld be num.");

     }

     if (!lua_isnumber(L, -1)) {

          error(L, "height shuld be num");

     }

     *w = lua_tointeger(L, -2);

     *h = lua_tointeger(L, -1);

}



int main()

{

     lua_State *L = luaL_newstate();

     luaL_openlibs(L);

     int w, h;

     load(L, "config", &w, &h);

     printf("%d,%d", w, h);

     return 0;

}

使用lua配置文件有什么优点呢?我想。大概有下面理由:
1.Lua为你处理了全部语法细节(包含错误)
2.配置内容可读性好,甚至你能够写上凝视。

3.能够非常easy加入新的配置信息。

(完)

Lua,Lua API,配置文件的更多相关文章

  1. VC和VS调用Lua设置以及Lua C API使用。

    通过c++调用lua 脚本,    环境VC++6.0    lua  sdk 5.1.4 在调用前先认识几个函数.1.调用lua_open()将创建一个指向Lua解释器的指针.2. luaL_ope ...

  2. 使用openresty + lua 搭建api 网关(一)安装openresty ,并添加lua模块

    openresty 有点不多说,网上各种介绍,先安装吧. 官方操作在此,http://openresty.org/cn/installation.html, tar -xzvf openresty-V ...

  3. Lua常用API

    转自:http://www.cnblogs.com/ringofthec/archive/2010/10/22/lua.html 1.  建一个新表 void lua_createtable (lua ...

  4. Lua C API的正确用法

    http://blog.codingnow.com/2015/05/lua_c_api.html http://blog.csdn.net/oilcode/article/details/510861 ...

  5. [译] Closures in Lua - Lua中的闭包

    原文:(PDF) . 摘要 一等(first-class)函数是一种非常强大的语言结构,并且是函数式语言的基础特性.少数过程式语言由于其基于栈的实现,也支持一等函数.本文讨论了Lua 5.x用于实现一 ...

  6. [转][译] Closures in Lua - Lua中的闭包

    http://www.cnblogs.com/plodsoft/p/5900270.html?utm_source=tuicool&utm_medium=referral 原文:(PDF) . ...

  7. lua c api

    #include <stdio.h> #include <string.h> extern "C"{ #include <lua.h> #inc ...

  8. Lua C Api lua_gettable 、lua_settable 、lua_next 使用详解

    之前一直没理清lua_gettable和lua_settable的使用,今天理清了,顺便就做下笔记了.1.lua_gettable void lua_gettable (lua_State *L, i ...

  9. Lua的API函数

    1. 基础库 我们在整个教程中使用了各种主题下的基本库. 下表提供了相关页面的链接,并列出了本Lua教程各部分所涵盖的功能. 编号 库/方法 作用 1 错误处理 包括错误处理函数,如断言, 错误,如L ...

  10. Lua C API 书籍

    https://www.oreilly.com/library/view/creating-solid-apis/9781491986301/ https://www.lua.org/notes/lt ...

随机推荐

  1. SQL Server索引进阶:第七级,过滤的索引

    原文地址: Stairway to SQL Server Indexes: Level 7,Filtered Indexes 本文是SQL Server索引进阶系列(Stairway to SQL S ...

  2. result 相关

    1.dispatcher 2.redirect 3.chain 4.redirectAction 5.freemarker 6.httpheader 7.stream 8.velocity 9.xsl ...

  3. Insert into a Cyclic Sorted List

    Given a node from a cyclic linked list which has been sorted, write a function to insert a value int ...

  4. BZOJ 1649: [Usaco2006 Dec]Cow Roller Coaster( dp )

    有点类似背包 , 就是那样子搞... --------------------------------------------------------------------------------- ...

  5. Android 开发笔记“关闭默认键盘”

    1.打开AndroidManifest.xml文件 2.在对应的activity中增加配置信息 android:windowSoftInputMode="stateHidden"

  6. lambda, reduce, map求阶乘之和

    学完这几个优雅的内建函数,就可以做一些有趣的小练习来激发兴趣了.而python最大的好处便是简洁,看下边要求 用1行代码求 1! + 2! + 3! + ... + 10! 求阶乘 reduce函数用 ...

  7. android LayoutInflater的使用

    看其继承关系: public abstract class LayoutInflater extends Object java.lang.Object ↳ android.view.LayoutIn ...

  8. ISO/IEC 14443协议浅谈

    一. 非接触IC卡简介 非接触IC卡又称射频卡,是射频识别技术和IC卡技术有机结合的产物.它解决了无源(卡中无电源)和免接触这一难题,具有更加方便.快捷的特点,广泛用于电子支付.通道控制.公交收费.停 ...

  9. cocos2d基础篇笔记四

    1.//有两种集合 //第一种是array 特点:插入,删除效率低,可是查找效率高 //另外一种是list  特点:插入,删除效率高,可是查找效率低 //分析这个游戏: 插入的时候:怪物,射弹出现时, ...

  10. ListView列表项

    方法,在xml文件中添加一个ListView,然后在MainActivity中 private ListView listView; private ArrayAdapter<String> ...