-
Notifications
You must be signed in to change notification settings - Fork 9
/
ex24.c
31 lines (27 loc) · 970 Bytes
/
ex24.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Example 24. Push the entire contents of a file as a string
//8<----------------------------------------------------------------------------
#include <stdio.h>
#include <string.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
int main(int argc, char **argv) {
lua_State *L = luaL_newstate();
luaL_openlibs(L);
const char *filename = "/home/mitchell/code/textadept/src/textadept.c";
//8<----------------------------------------------------------------------------
FILE *f = fopen(filename, "r");
luaL_Buffer b;
luaL_buffinit(L, &b);
char buf[BUFSIZ];
while (fgets(buf, BUFSIZ, f) != NULL)
luaL_addlstring(&b, buf, strlen(buf));
luaL_pushresult(&b);
fclose(f);
//8<----------------------------------------------------------------------------
printf("%s\n", lua_tostring(L, -1));
if (lua_gettop(L) > 1) printf("stack size != 1\n");
lua_close(L);
return 0;
}
//8<----------------------------------------------------------------------------