/* -*- 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 #include #include "debug.h" #define luaL_checkbool(L, i) \ (lua_isboolean(L,i) ? lua_toboolean(L,i) : luaL_checkint(L,i)) #define error(L, m) \ { ERR(luapraxisd, "%s\n", m); lua_pushstring(L, m); lua_error(L); } typedef struct px_userdata { Praxisd *px; } px_userdata; 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); const char *txt = luaL_checkstring(L, 4); std::string sogeord; std::string sogetxt; try { std::vector cavelist = pxu->px->diverse_get_cave(""); std::vector::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, txt); return 0; } i++; } std::string text = cave; text += " "; text += txt; pxu->px->add_sogeord(cpr, "CA0003", text.c_str()); // CA0003 == 'ANDET' } catch(const char *msg) { error(L, msg); } return 0; } int px_addbehandling(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 *behandling = luaL_checkstring(L, 3); const char *text = luaL_checkstring(L, 4); std::string sogeord; std::string sogetxt; try { std::vector bhlst = pxu->px->diverse_get_behandling(""); std::vector::iterator i = bhlst.begin(); while(i != bhlst.end()) { Praxisd::behandling_t &b = *i; if(strcasecmp(behandling, b.kode.c_str()) == 0) { pxu->px->add_sogeord(cpr, b.sogenr, text); return 0; } i++; } } catch(const char *msg) { error(L, msg); } return 0; } int px_adddiagnose(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 *diagnose = luaL_checkstring(L, 3); const char *text = luaL_checkstring(L, 4); std::string sogeord; std::string sogetxt; try { std::vector dialst = pxu->px->diverse_get_diagnose(""); std::vector::iterator i = dialst.begin(); while(i != dialst.end()) { Praxisd::diagnose_t &d = *i; if(strcasecmp(diagnose, d.kode.c_str()) == 0) { pxu->px->add_sogeord(cpr, d.sogenr, text); return 0; } i++; } } catch(const char *msg) { error(L, msg); } return 0; } class cavedata_t : public Praxisd::cave_t { public: std::string sogetxt; std::string sogedato; }; 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 cavelist; try { Praxisd::patient_t pat = pxu->px->patient_get_by_cpr(cpr); std::vector::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 cl = pxu->px->diverse_get_cave(csogenr); if(cl.size() == 1) { cavedata_t cdata; cdata.cave = cl[0].cave; cdata.bemaerkning1 = cl[0].bemaerkning1; cdata.bemaerkning2 = cl[0].bemaerkning2; cdata.bemaerkning3 = cl[0].bemaerkning3; cdata.sogetxt = s.sogetxt; cdata.sogedato = s.sogedato; cavelist.push_back(cdata); } } i++; } } catch(const char *msg) { error(L, msg); } int num = cavelist.size(); int sz = 6; // 4 fields in cave_t + 1 sogetxt and 1 sogedato lua_createtable(L, num, 0); int toptop = lua_gettop(L); for(int i = 1; i <= num; i++) { lua_createtable(L, 0, sz); int top = lua_gettop(L); for(int j = 1; j <= sz; j++) { if(j == 1) lua_pushstring(L, cavelist[i - 1].cave.c_str()); if(j == 2) lua_pushstring(L, cavelist[i - 1].bemaerkning1.c_str()); if(j == 3) lua_pushstring(L, cavelist[i - 1].bemaerkning2.c_str()); if(j == 4) lua_pushstring(L, cavelist[i - 1].bemaerkning3.c_str()); if(j == 5) lua_pushstring(L, cavelist[i - 1].sogetxt.c_str()); if(j == 6) lua_pushstring(L, cavelist[i - 1].sogedato.c_str()); lua_rawseti(L, top, j); } lua_rawseti(L, toptop, i); } return 1; } class behandlingdata_t : public Praxisd::behandling_t { public: std::string sogetxt; std::string sogedato; }; int px_getbehandling(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 behandlinglist; try { Praxisd::patient_t pat = pxu->px->patient_get_by_cpr(cpr); std::vector::iterator i = pat.sogeord.begin(); while(i != pat.sogeord.end()) { Praxisd::sogeord_t &s = *i; if(s.sogenr[0] == 'B') { std::string csogenr = s.sogenr.substr(1, s.sogenr.length() - 1); std::vector cl = pxu->px->diverse_get_behandling(csogenr); if(cl.size() == 1) { behandlingdata_t cdata; cdata.kode = cl[0].kode; cdata.behandling = cl[0].behandling; cdata.bemaerkning = cl[0].bemaerkning; cdata.udregning = cl[0].udregning; cdata.sogetxt = s.sogetxt; cdata.sogedato = s.sogedato; behandlinglist.push_back(cdata); } } i++; } } catch(const char *msg) { error(L, msg); } int num = behandlinglist.size(); int sz = 6; // 4 fields in behandling_t + 1 sogetxt and 1 sogedato lua_createtable(L, num, 0); int toptop = lua_gettop(L); for(int i = 1; i <= num; i++) { lua_createtable(L, 0, sz); int top = lua_gettop(L); for(int j = 1; j <= sz; j++) { if(j == 1) lua_pushstring(L, behandlinglist[i - 1].kode.c_str()); if(j == 2) lua_pushstring(L, behandlinglist[i - 1].behandling.c_str()); if(j == 3) lua_pushstring(L, behandlinglist[i - 1].bemaerkning.c_str()); if(j == 4) lua_pushstring(L, behandlinglist[i - 1].udregning.c_str()); if(j == 5) lua_pushstring(L, behandlinglist[i - 1].sogetxt.c_str()); if(j == 6) lua_pushstring(L, behandlinglist[i - 1].sogedato.c_str()); lua_rawseti(L, top, j); } lua_rawseti(L, toptop, i); } return 1; } class diagnosedata_t : public Praxisd::diagnose_t { public: std::string sogetxt; std::string sogedato; }; int px_getdiagnose(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 diagnoselist; try { Praxisd::patient_t pat = pxu->px->patient_get_by_cpr(cpr); std::vector::iterator i = pat.sogeord.begin(); while(i != pat.sogeord.end()) { Praxisd::sogeord_t &s = *i; if(s.sogenr[0] == 'D') { std::string csogenr = s.sogenr.substr(1, s.sogenr.length() - 1); std::vector cl = pxu->px->diverse_get_diagnose(csogenr); if(cl.size() == 1) { diagnosedata_t cdata; cdata.kode = cl[0].kode; cdata.diagnose = cl[0].diagnose; cdata.bemaerkning = cl[0].bemaerkning; cdata.sogetxt = s.sogetxt; cdata.sogedato = s.sogedato; diagnoselist.push_back(cdata); } } i++; } } catch(const char *msg) { error(L, msg); } int num = diagnoselist.size(); int sz = 5; // 3 fields in diagnose_t + 1 sogetxt and 1 sogedato lua_createtable(L, num, 0); int toptop = lua_gettop(L); for(int i = 1; i <= num; i++) { lua_createtable(L, 0, sz); int top = lua_gettop(L); for(int j = 1; j <= sz; j++) { if(j == 1) lua_pushstring(L, diagnoselist[i - 1].kode.c_str()); if(j == 2) lua_pushstring(L, diagnoselist[i - 1].diagnose.c_str()); if(j == 3) lua_pushstring(L, diagnoselist[i - 1].bemaerkning.c_str()); if(j == 4) lua_pushstring(L, diagnoselist[i - 1].sogetxt.c_str()); if(j == 5) lua_pushstring(L, diagnoselist[i - 1].sogedato.c_str()); lua_rawseti(L, top, j); } lua_rawseti(L, toptop, i); } return 1; } 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 cavelist; try { cavelist = pxu->px->diverse_get_cave(sogenr); } catch(const char *msg) { error(L, msg); } int num = cavelist.size(); int sz = 4; // 4 fields in cave_t lua_createtable(L, num, 0); int toptop = lua_gettop(L); for(int i = 1; i <= num; i++) { lua_createtable(L, 0, sz); int top = lua_gettop(L); for(int j = 1; j <= sz; j++) { if(j == 1) lua_pushstring(L, cavelist[i - 1].cave.c_str()); if(j == 2) lua_pushstring(L, cavelist[i - 1].bemaerkning1.c_str()); if(j == 3) lua_pushstring(L, cavelist[i - 1].bemaerkning2.c_str()); if(j == 4) lua_pushstring(L, cavelist[i - 1].bemaerkning3.c_str()); lua_rawseti(L, top, j); } lua_rawseti(L, toptop, i); } return 1; } int px_behandlinglist(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 behandlinglist; try { behandlinglist = pxu->px->diverse_get_behandling(sogenr); } catch(const char *msg) { error(L, msg); } int num = behandlinglist.size(); int sz = 4; // 4 fields in behandling_t lua_createtable(L, num, 0); int toptop = lua_gettop(L); for(int i = 1; i <= num; i++) { lua_createtable(L, 0, sz); int top = lua_gettop(L); for(int j = 1; j <= sz; j++) { if(j == 1) lua_pushstring(L, behandlinglist[i - 1].kode.c_str()); if(j == 2) lua_pushstring(L, behandlinglist[i - 1].behandling.c_str()); if(j == 3) lua_pushstring(L, behandlinglist[i - 1].bemaerkning.c_str()); if(j == 4) lua_pushstring(L, behandlinglist[i - 1].udregning.c_str()); lua_rawseti(L, top, j); } lua_rawseti(L, toptop, i); } return 1; } int px_diagnoselist(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 diagnoselist; try { diagnoselist = pxu->px->diverse_get_diagnose(sogenr); } catch(const char *msg) { error(L, msg); } int num = diagnoselist.size(); int sz = 3; // 3 fields in diagnose_t lua_createtable(L, num, 0); int toptop = lua_gettop(L); for(int i = 1; i <= num; i++) { lua_createtable(L, 0, sz); int top = lua_gettop(L); for(int j = 1; j <= sz; j++) { if(j == 1) lua_pushstring(L, diagnoselist[i - 1].kode.c_str()); if(j == 2) lua_pushstring(L, diagnoselist[i - 1].diagnose.c_str()); if(j == 3) lua_pushstring(L, diagnoselist[i - 1].bemaerkning.c_str()); lua_rawseti(L, top, j); } lua_rawseti(L, toptop, i); } return 1; } 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); try { pxu->px = new Praxisd(host, port); } catch(const char *msg) { error(L, msg); } return 1; } 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; } 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 mutex.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', 'AZOPT', 'test')\n" "\n" "--pcl = px:addcave('1505050505', os.date('%H%M%S'))\n" "\n" "--pcl = px:getbehandling('1505050505')\n" "--print('#pcl: ' .. #pcl)\n" "--for i=1,#pcl do\n" "-- pc = pcl[i]\n" "-- print('#pc: ' .. #pc)\n" "-- for j=1,#pc do\n" "-- print(' pc: ' .. j .. ': ' .. pc[j])\n" "-- end\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))); printf("loadbuffer error: %s", 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))); printf("pcall error: %s", 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*/