/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set et sw=2 ts=2: */ /*************************************************************************** * luapraxisd.cc * * Thu May 5 11:16:20 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" #ifdef WITH_PRAXISD #include "../server/src/praxisd.h" #include #include #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_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); QVector cavelist; Praxisd::patient_t patient = pxu->px->patient_get_by_cpr(cpr); std::vector::iterator i = patient.sogeord.begin(); while(i != patient.sogeord.end()) { std::string cavesogeord = i->sogenr.substr(1, i->sogenr.size() - 1); std::vector cave = pxu->px->diverse_get_cave(cavesogeord); if(cave.size() == 1) { if(cave[0].cave != "ANDET") cavelist.push_back(cave[0].cave.c_str()); else cavelist.push_back(i->sogetxt.c_str()); } i++; } lua_createtable(L, 0, cavelist.size()); int top = lua_gettop(L); for(int i = 0; i < cavelist.size(); i++) { lua_pushstring(L, QString::fromUtf8(cavelist[i].toStdString().c_str()).toStdString().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"); std::vector cavelist = pxu->px->diverse_get_cave(""); lua_createtable(L, 0, cavelist.size()); int top = lua_gettop(L); for(size_t i = 0; i < cavelist.size(); i++) { lua_pushstring(L, QString::fromUtf8(cavelist[i].cave.c_str()).toStdString().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 = 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}, {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); } #else/*WITH_PRAXISD*/ void register_praxisd(lua_State *L){} #endif/*WITH_PRAXISD*/