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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
/* $%BEGINLICENSE%$
Copyright (C) 2007-2008 MySQL AB, 2008 Sun Microsystems, Inc
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
$%ENDLICENSE%$ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <errno.h>
#include <assert.h>
#include <string.h>
#include <glib.h>
#ifdef HAVE_LUA_H
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#endif
#include "lua-load-factory.h"
typedef enum {
LOAD_STATE_PREFIX,
LOAD_STATE_BUFFER,
LOAD_STATE_POSTFIX,
LOAD_STATE_END
} load_factory_state_t;
typedef enum {
LOAD_TYPE_FILE,
LOAD_TYPE_BUFFER
} load_factory_type_t;
typedef struct {
union {
struct {
const char *str;
} string;
struct {
const char *filename;
FILE *f;
char content[1024];
} file;
} data;
const char *prefix;
const char *postfix;
load_factory_type_t type;
load_factory_state_t state;
} load_factory_t;
#ifdef HAVE_LUA_H
const char *loadstring_factory_reader(lua_State G_GNUC_UNUSED *L, void *data, size_t *size) {
load_factory_t *factory = data;
switch (factory->state) {
case LOAD_STATE_PREFIX:
*size = strlen(factory->prefix);
factory->state = LOAD_STATE_BUFFER;
return factory->prefix;
case LOAD_STATE_BUFFER:
switch (factory->type) {
case LOAD_TYPE_BUFFER:
*size = strlen(factory->data.string.str);
factory->state = LOAD_STATE_POSTFIX;
return factory->data.string.str;
case LOAD_TYPE_FILE:
g_assert(NULL != factory->data.file.f);
*size = fread(factory->data.file.content, 1, sizeof(factory->data.file.content), factory->data.file.f);
if (*size == 0) {
/* eof */
factory->data.file.content[0] = '\n';
factory->data.file.content[1] = '\0';
factory->state = LOAD_STATE_POSTFIX;
*size = 1;
}
return factory->data.file.content;
}
case LOAD_STATE_POSTFIX:
*size = strlen(factory->postfix);
factory->state = LOAD_STATE_END;
return factory->postfix;
case LOAD_STATE_END:
return NULL;
}
return NULL;
}
int luaL_loadstring_factory(lua_State *L, const char *s) {
load_factory_t factory;
factory.type = LOAD_TYPE_BUFFER;
factory.data.string.str = s;
factory.state = LOAD_STATE_PREFIX;
factory.prefix = "return function()\n";
factory.postfix = "end\n";
return lua_load(L, loadstring_factory_reader, &factory, s);
}
int luaL_loadfile_factory(lua_State *L, const char *filename) {
int ret;
load_factory_t factory;
factory.type = LOAD_TYPE_FILE;
factory.data.file.filename = filename;
factory.state = LOAD_STATE_PREFIX;
factory.prefix = "return function()\n";
factory.postfix = "end\n";
factory.data.file.f = fopen(filename, "rb");
ret = lua_load(L, loadstring_factory_reader, &factory, filename);
fclose(factory.data.file.f);
return ret;
}
#endif
|