summaryrefslogtreecommitdiff
path: root/client/luapraxisd.cc
blob: 00ed961f39cda014e95777cd3221595f030c9456 (plain)
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
/* -*- 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"

#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 {
  PraxisdSync *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<QString> cavelist;

  Patient patient = pxu->px->patient_get_by_cpr(cpr);
  if(pxu->px->hasError()) {
    lua_pushstring(L, pxu->px->errorString().toStdString().c_str());
    lua_error(L);
    return 1;
  }
  QVector<sogeord_t>::iterator i = patient.sogeord.begin();
  while(i != patient.sogeord.end()) {
    QString cavesogeord = i->sogenr;//.mid(1, i->sogenr.size() - 1);
    QVector<cave_t> cave = pxu->px->diverse_get_cave(cavesogeord);
    if(cave.size() == 1) {
      if(cave[0].cave != "ANDET") cavelist.push_back(cave[0].cave);
      else cavelist.push_back(i->sogetxt);
    }
    i++;
  }

  lua_createtable(L, 0, cavelist.size());
  int top = lua_gettop(L);

  for(int i = 0; i < cavelist.size(); i++) {
    QString c = cavelist[i];
    DEBUG(cavelist, "CAVE '%s'\n", c.toStdString().c_str());
    lua_pushstring(L, c.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");

  QVector<cave_t> cavelist = pxu->px->diverse_get_cave("C");
  if(pxu->px->hasError()) {
    lua_pushstring(L, pxu->px->errorString().toStdString().c_str());
    lua_error(L);
    return 1;
  }

  lua_createtable(L, 0, cavelist.size());
  int top = lua_gettop(L);

  for(size_t i = 0; i < (size_t)cavelist.size(); i++) {
    QString c = cavelist[i].cave;
    DEBUG(cavelist, "CAVE '%s'\n", c.toStdString().c_str());
    lua_pushstring(L, c.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 PraxisdSync(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);
}