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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set et sw=2 ts=2: */
/***************************************************************************
* luapraxisd.cc
*
* Wed Apr 27 11:59:53 CEST 2011
* Copyright 2011 Bent Bisballe Nyeng
* deva@aasimon.org
****************************************************************************/
/*
* This file is part of Pracro.
*
* Pracro 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; either version 2 of the License, or
* (at your option) any later version.
*
* Pracro 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 Pracro; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
#include "luapraxisd.h"
#include "praxisd.h"
#include <lauxlib.h>
#include <strings.h>
#include "debug.h"
#define luaL_checkbool(L, i) \
(lua_isboolean(L,i) ? lua_toboolean(L,i) : luaL_checkint(L,i))
typedef struct px_userdata {
Praxisd *px;
} px_userdata;
static int px_addcave(lua_State *L)
{
px_userdata *pxu;
pxu = (px_userdata *)luaL_checkudata(L, 1, "Praxisd");
luaL_argcheck(L, pxu, 1, "Praxisd expected");
const char *cpr = luaL_checkstring(L, 2);
const char *cave = luaL_checkstring(L, 3);
std::string sogeord;
std::string sogetxt;
std::vector<Praxisd::cave_t> cavelist = pxu->px->diverse_get_cave("");
std::vector<Praxisd::cave_t>::iterator i = cavelist.begin();
while(i != cavelist.end()) {
Praxisd::cave_t &c = *i;
if(strcasecmp(cave, c.cave.c_str()) == 0) {
pxu->px->add_sogeord(cpr, c.sogenr, "");
return 0;
}
i++;
}
pxu->px->add_sogeord(cpr, "CA0003", cave); // CA0003 == 'ANDET'
return 0;
}
static int px_getcave(lua_State *L)
{
px_userdata *pxu;
pxu = (px_userdata *)luaL_checkudata(L, 1, "Praxisd");
luaL_argcheck(L, pxu, 1, "Praxisd expected");
const char *cpr = luaL_checkstring(L, 2);
std::vector<Praxisd::cave_t> cavelist;
Praxisd::patient_t pat = pxu->px->patient_get_by_cpr(cpr);
std::vector<Praxisd::sogeord_t>::iterator i = pat.sogeord.begin();
while(i != pat.sogeord.end()) {
Praxisd::sogeord_t &s = *i;
if(s.sogenr[0] == 'C') {
std::string csogenr = s.sogenr.substr(1, s.sogenr.length() - 1);
std::vector<Praxisd::cave_t> cl = pxu->px->diverse_get_cave(csogenr);
if(cl.size() == 1) {
if(cl[0].cave == "ANDET") cl[0].cave = s.sogetxt;
cavelist.push_back(cl[0]);
}
}
i++;
}
lua_createtable(L, 0, cavelist.size());
int top = lua_gettop(L);
for(size_t i = 0; i < cavelist.size(); i++) {
lua_pushstring(L, cavelist[i].cave.c_str());
lua_rawseti(L, top, i);
}
return 1;
}
static int px_cavelist(lua_State *L)
{
px_userdata *pxu;
pxu = (px_userdata *)luaL_checkudata(L, 1, "Praxisd");
luaL_argcheck(L, pxu, 1, "Praxisd expected");
const char *sogenr = luaL_checkstring(L, 2);
std::vector<Praxisd::cave_t> cavelist = pxu->px->diverse_get_cave(sogenr);
lua_createtable(L, 0, cavelist.size());
int top = lua_gettop(L);
for(size_t i = 0; i < cavelist.size(); i++) {
lua_pushstring(L, cavelist[i].cave.c_str());
lua_rawseti(L, top, i);
}
return 1;
}
static int px_new(lua_State *L)
{
const char *host = luaL_checkstring(L, 1);
int port = (int)luaL_checknumber(L, 2);
px_userdata *pxu;
pxu = (px_userdata *)lua_newuserdata(L, sizeof(px_userdata));
luaL_getmetatable(L, "Praxisd");
lua_setmetatable(L, -2);
pxu->px = new Praxisd(host, port);
return 1;
}
static int px_gc(lua_State *L)
{
px_userdata *pxu;
pxu = (px_userdata *)luaL_checkudata(L, 1, "Praxisd");
luaL_argcheck(L, pxu, 1, "Praxisd expected");
delete pxu->px;
return 0;
}
static const struct luaL_Reg px_meths[] = {
{"__gc" ,px_gc},
{"cavelist", px_cavelist},
{"getcave", px_getcave},
{"addcave", px_addcave},
{NULL, NULL}
};
static const struct luaL_reg px_funcs[] = {
{"new", px_new},
{NULL, NULL}
};
void register_praxisd(lua_State *L)
{
luaL_newmetatable(L, "Praxisd");
lua_pushliteral(L, "__index");
lua_pushvalue(L, -2);
lua_rawset(L, -3);
luaL_register(L, NULL, px_meths);
luaL_openlib (L, "Praxisd", px_funcs, 0);
}
#ifdef TEST_LUAPRAXISD
//deps: praxisd.cc debug.cc saxparser.cc log.cc
//cflags: -I.. $(LUA_CFLAGS) $(CURL_CFLAGS) $(EXPAT_CFLAGS)
//libs: $(LUA_LIBS) $(CURL_LIBS) $(EXPAT_LIBS)
#include "test.h"
TEST_BEGIN;
lua_State *L = luaL_newstate();
if(L == NULL) {
ERR(luaresume, "Could not create LUA state.\n");
}
luaL_openlibs(L);
register_praxisd(L);
/////
std::string program =
"px = Praxisd.new('localhost', 10000)\n"
"cl = px:cavelist('')\n"
"for i=1,#cl do\n"
" print(cl[i])\n"
"end\n"
"\n"
"--pcl = px:addcave('1505050505', 'LUMIGAN')\n"
"\n"
"pcl = px:addcave('1505050505', os.date('%H%M%S'))\n"
"\n"
"pcl = px:getcave('1505050505')\n"
"for i=1,#pcl do\n"
" print('p: ' .. pcl[i])\n"
"end\n"
;
//int top = lua_gettop(L);
if(luaL_loadbuffer(L, program.c_str(), program.size(), "luapraxisd test")) {
ERR(luaresume, "loadbufer: %s\n", lua_tostring(L, lua_gettop(L)));
}
if(lua_pcall(L, 0, LUA_MULTRET, 0)) {
ERR(luaresume, "pcall: %s\n" , lua_tostring(L, lua_gettop(L)));
}
/*
if(top != lua_gettop(L) - 1) {
ERR(luaresume, "Program did not return a single value.\n");
}
if(lua_isstring(L, lua_gettop(L)) == false) {
ERR(luaresume, "Program did not return a string value.\n");
}
std::string res = lua_tostring(L, lua_gettop(L));
lua_pop(L, 1);
*/
lua_close(L);
TEST_END;
#endif/*TEST_LUAPRAXISD*/
|