annotate OrthancFramework/Sources/Lua/LuaFunctionCall.cpp @ 5853:4d932683049d get-scu tip

very first implementation of C-Get SCU
author Alain Mazy <am@orthanc.team>
date Tue, 29 Oct 2024 17:25:49 +0100
parents f7adfb22e20e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
386
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
1900
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1658
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
1288
6e7e5ed91c2d upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1051
diff changeset
4 * Department, University Hospital of Liege, Belgium
5640
f7adfb22e20e updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5485
diff changeset
5 * Copyright (C) 2017-2023 Osimis S.A., Belgium
f7adfb22e20e updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5485
diff changeset
6 * Copyright (C) 2024-2024 Orthanc Team SRL, Belgium
5485
48b8dae6dc77 upgrade to year 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5185
diff changeset
7 * Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
386
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 *
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * This program is free software: you can redistribute it and/or
4119
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4105
diff changeset
10 * modify it under the terms of the GNU Lesser General Public License
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4105
diff changeset
11 * as published by the Free Software Foundation, either version 3 of
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4105
diff changeset
12 * the License, or (at your option) any later version.
386
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 *
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * This program is distributed in the hope that it will be useful, but
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
4119
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4105
diff changeset
17 * Lesser General Public License for more details.
386
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 *
4119
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4105
diff changeset
19 * You should have received a copy of the GNU Lesser General Public
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4105
diff changeset
20 * License along with this program. If not, see
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4105
diff changeset
21 * <http://www.gnu.org/licenses/>.
386
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 **/
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24
824
a811bdf8b8eb precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 689
diff changeset
25 #include "../PrecompiledHeaders.h"
386
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 #include "LuaFunctionCall.h"
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27
1583
9ea3d082b064 got rid of custom exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1486
diff changeset
28 #include "../OrthancException.h"
9ea3d082b064 got rid of custom exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1486
diff changeset
29 #include "../Logging.h"
9ea3d082b064 got rid of custom exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1486
diff changeset
30
4105
c02a2d9efbc2 move FromDcmtkBridge::ExecuteToDicom() to LuaFunctionCall, to remove dependency of DCMTK on Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
31 #if ORTHANC_ENABLE_DCMTK == 1
c02a2d9efbc2 move FromDcmtkBridge::ExecuteToDicom() to LuaFunctionCall, to remove dependency of DCMTK on Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
32 # include "../DicomParsing/FromDcmtkBridge.h"
c02a2d9efbc2 move FromDcmtkBridge::ExecuteToDicom() to LuaFunctionCall, to remove dependency of DCMTK on Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
33 #endif
c02a2d9efbc2 move FromDcmtkBridge::ExecuteToDicom() to LuaFunctionCall, to remove dependency of DCMTK on Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
34
996
cf52f3bcb2b3 clarification of Lua classes wrt multithreading
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
35 #include <cassert>
997
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
36 #include <stdio.h>
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
37 #include <boost/lexical_cast.hpp>
386
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 namespace Orthanc
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 {
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 void LuaFunctionCall::CheckAlreadyExecuted()
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 {
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 if (isExecuted_)
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 {
1583
9ea3d082b064 got rid of custom exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1486
diff changeset
45 throw OrthancException(ErrorCode_LuaAlreadyExecuted);
386
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 }
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 }
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 LuaFunctionCall::LuaFunctionCall(LuaContext& context,
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 const char* functionName) :
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 context_(context),
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 isExecuted_(false)
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 {
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 // Clear the stack to fulfill the invariant
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 lua_settop(context_.lua_, 0);
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 lua_getglobal(context_.lua_, functionName);
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 }
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 void LuaFunctionCall::PushString(const std::string& value)
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 {
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 CheckAlreadyExecuted();
1465
905842836ad4 sample Lua script to write DICOM series to disk
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1448
diff changeset
62 lua_pushlstring(context_.lua_, value.c_str(), value.size());
386
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 }
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 void LuaFunctionCall::PushBoolean(bool value)
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 {
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 CheckAlreadyExecuted();
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 lua_pushboolean(context_.lua_, value);
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 }
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 void LuaFunctionCall::PushInteger(int value)
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 {
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 CheckAlreadyExecuted();
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 lua_pushinteger(context_.lua_, value);
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 }
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 void LuaFunctionCall::PushDouble(double value)
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 {
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 CheckAlreadyExecuted();
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 lua_pushnumber(context_.lua_, value);
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 }
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82
997
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
83 void LuaFunctionCall::PushJson(const Json::Value& value)
386
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 {
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 CheckAlreadyExecuted();
1051
92f4bf2c5d73 HTTP GET in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1010
diff changeset
86 context_.PushJson(value);
386
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 }
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88
997
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
89 void LuaFunctionCall::ExecuteInternal(int numOutputs)
386
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 {
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 CheckAlreadyExecuted();
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 assert(lua_gettop(context_.lua_) >= 1);
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 int nargs = lua_gettop(context_.lua_) - 1;
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 int error = lua_pcall(context_.lua_, nargs, numOutputs, 0);
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 if (error)
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 {
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 assert(lua_gettop(context_.lua_) >= 1);
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 std::string description(lua_tostring(context_.lua_, -1));
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 lua_pop(context_.lua_, 1); /* pop error message from the stack */
1583
9ea3d082b064 got rid of custom exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1486
diff changeset
103
2954
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
104 throw OrthancException(ErrorCode_CannotExecuteLua, description);
386
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 }
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 if (lua_gettop(context_.lua_) < numOutputs)
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 {
1583
9ea3d082b064 got rid of custom exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1486
diff changeset
109 throw OrthancException(ErrorCode_LuaBadOutput);
386
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 }
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 isExecuted_ = true;
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 }
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 bool LuaFunctionCall::ExecutePredicate()
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 {
997
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
117 ExecuteInternal(1);
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
118
386
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 if (!lua_isboolean(context_.lua_, 1))
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 {
1583
9ea3d082b064 got rid of custom exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1486
diff changeset
121 throw OrthancException(ErrorCode_NotLuaPredicate);
386
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 }
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 return lua_toboolean(context_.lua_, 1) != 0;
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 }
997
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
126
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
127
1658
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1583
diff changeset
128 void LuaFunctionCall::ExecuteToJson(Json::Value& result,
54bafe0e7e7b Optional argument "keepStrings" in "DumpJson()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1583
diff changeset
129 bool keepStrings)
997
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
130 {
1448
3f7722179467 refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
131 ExecuteInternal(1);
3442
dd1e68f2d0c0 Fix issue #106 (Unable to export preview as jpeg from Lua script)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
132 context_.GetJson(result, context_.lua_, lua_gettop(context_.lua_), keepStrings);
1448
3f7722179467 refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
133 }
997
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
134
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
135
1448
3f7722179467 refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
136 void LuaFunctionCall::ExecuteToString(std::string& result)
3f7722179467 refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
137 {
3f7722179467 refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
138 ExecuteInternal(1);
3f7722179467 refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
139
3f7722179467 refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
140 int top = lua_gettop(context_.lua_);
3f7722179467 refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
141 if (lua_isstring(context_.lua_, top))
997
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
142 {
1448
3f7722179467 refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
143 result = lua_tostring(context_.lua_, top);
1010
160dfe770618 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 997
diff changeset
144 }
997
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
145 else
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
146 {
1583
9ea3d082b064 got rid of custom exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1486
diff changeset
147 throw OrthancException(ErrorCode_LuaReturnsNoString);
997
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
148 }
1b1d51e9f1a2 return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 996
diff changeset
149 }
1959
45c4387a379c Access to the HTTP headers in the "IncomingHttpRequestFilter()" callback
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
150
4796
94616af363ec added ReceivedCStoreInstanceFilter lua callback + OrthancPluginRegisterIncomingCStoreInstanceFilter in sdk
Alain Mazy <am@osimis.io>
parents: 4437
diff changeset
151 void LuaFunctionCall::ExecuteToInt(int& result)
94616af363ec added ReceivedCStoreInstanceFilter lua callback + OrthancPluginRegisterIncomingCStoreInstanceFilter in sdk
Alain Mazy <am@osimis.io>
parents: 4437
diff changeset
152 {
94616af363ec added ReceivedCStoreInstanceFilter lua callback + OrthancPluginRegisterIncomingCStoreInstanceFilter in sdk
Alain Mazy <am@osimis.io>
parents: 4437
diff changeset
153 ExecuteInternal(1);
94616af363ec added ReceivedCStoreInstanceFilter lua callback + OrthancPluginRegisterIncomingCStoreInstanceFilter in sdk
Alain Mazy <am@osimis.io>
parents: 4437
diff changeset
154
94616af363ec added ReceivedCStoreInstanceFilter lua callback + OrthancPluginRegisterIncomingCStoreInstanceFilter in sdk
Alain Mazy <am@osimis.io>
parents: 4437
diff changeset
155 int top = lua_gettop(context_.lua_);
94616af363ec added ReceivedCStoreInstanceFilter lua callback + OrthancPluginRegisterIncomingCStoreInstanceFilter in sdk
Alain Mazy <am@osimis.io>
parents: 4437
diff changeset
156 if (lua_isnumber(context_.lua_, top))
94616af363ec added ReceivedCStoreInstanceFilter lua callback + OrthancPluginRegisterIncomingCStoreInstanceFilter in sdk
Alain Mazy <am@osimis.io>
parents: 4437
diff changeset
157 {
94616af363ec added ReceivedCStoreInstanceFilter lua callback + OrthancPluginRegisterIncomingCStoreInstanceFilter in sdk
Alain Mazy <am@osimis.io>
parents: 4437
diff changeset
158 result = static_cast<int>(lua_tointeger(context_.lua_, top));
94616af363ec added ReceivedCStoreInstanceFilter lua callback + OrthancPluginRegisterIncomingCStoreInstanceFilter in sdk
Alain Mazy <am@osimis.io>
parents: 4437
diff changeset
159 }
94616af363ec added ReceivedCStoreInstanceFilter lua callback + OrthancPluginRegisterIncomingCStoreInstanceFilter in sdk
Alain Mazy <am@osimis.io>
parents: 4437
diff changeset
160 else
94616af363ec added ReceivedCStoreInstanceFilter lua callback + OrthancPluginRegisterIncomingCStoreInstanceFilter in sdk
Alain Mazy <am@osimis.io>
parents: 4437
diff changeset
161 {
94616af363ec added ReceivedCStoreInstanceFilter lua callback + OrthancPluginRegisterIncomingCStoreInstanceFilter in sdk
Alain Mazy <am@osimis.io>
parents: 4437
diff changeset
162 throw OrthancException(ErrorCode_LuaReturnsNoString);
94616af363ec added ReceivedCStoreInstanceFilter lua callback + OrthancPluginRegisterIncomingCStoreInstanceFilter in sdk
Alain Mazy <am@osimis.io>
parents: 4437
diff changeset
163 }
94616af363ec added ReceivedCStoreInstanceFilter lua callback + OrthancPluginRegisterIncomingCStoreInstanceFilter in sdk
Alain Mazy <am@osimis.io>
parents: 4437
diff changeset
164 }
1959
45c4387a379c Access to the HTTP headers in the "IncomingHttpRequestFilter()" callback
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
165
45c4387a379c Access to the HTTP headers in the "IncomingHttpRequestFilter()" callback
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
166 void LuaFunctionCall::PushStringMap(const std::map<std::string, std::string>& value)
45c4387a379c Access to the HTTP headers in the "IncomingHttpRequestFilter()" callback
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
167 {
45c4387a379c Access to the HTTP headers in the "IncomingHttpRequestFilter()" callback
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
168 Json::Value json = Json::objectValue;
45c4387a379c Access to the HTTP headers in the "IncomingHttpRequestFilter()" callback
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
169
45c4387a379c Access to the HTTP headers in the "IncomingHttpRequestFilter()" callback
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
170 for (std::map<std::string, std::string>::const_iterator
45c4387a379c Access to the HTTP headers in the "IncomingHttpRequestFilter()" callback
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
171 it = value.begin(); it != value.end(); ++it)
45c4387a379c Access to the HTTP headers in the "IncomingHttpRequestFilter()" callback
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
172 {
45c4387a379c Access to the HTTP headers in the "IncomingHttpRequestFilter()" callback
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
173 json[it->first] = it->second;
45c4387a379c Access to the HTTP headers in the "IncomingHttpRequestFilter()" callback
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
174 }
45c4387a379c Access to the HTTP headers in the "IncomingHttpRequestFilter()" callback
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
175
45c4387a379c Access to the HTTP headers in the "IncomingHttpRequestFilter()" callback
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
176 PushJson(json);
45c4387a379c Access to the HTTP headers in the "IncomingHttpRequestFilter()" callback
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
177 }
2218
3eefb84ac0bd dynamically fix outgoing C-Find requests using "OutgoingFindRequestFilter()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1959
diff changeset
178
3eefb84ac0bd dynamically fix outgoing C-Find requests using "OutgoingFindRequestFilter()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1959
diff changeset
179
3eefb84ac0bd dynamically fix outgoing C-Find requests using "OutgoingFindRequestFilter()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1959
diff changeset
180 void LuaFunctionCall::PushDicom(const DicomMap& dicom)
3eefb84ac0bd dynamically fix outgoing C-Find requests using "OutgoingFindRequestFilter()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1959
diff changeset
181 {
3eefb84ac0bd dynamically fix outgoing C-Find requests using "OutgoingFindRequestFilter()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1959
diff changeset
182 DicomArray a(dicom);
3eefb84ac0bd dynamically fix outgoing C-Find requests using "OutgoingFindRequestFilter()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1959
diff changeset
183 PushDicom(a);
3eefb84ac0bd dynamically fix outgoing C-Find requests using "OutgoingFindRequestFilter()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1959
diff changeset
184 }
3eefb84ac0bd dynamically fix outgoing C-Find requests using "OutgoingFindRequestFilter()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1959
diff changeset
185
3eefb84ac0bd dynamically fix outgoing C-Find requests using "OutgoingFindRequestFilter()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1959
diff changeset
186
3eefb84ac0bd dynamically fix outgoing C-Find requests using "OutgoingFindRequestFilter()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1959
diff changeset
187 void LuaFunctionCall::PushDicom(const DicomArray& dicom)
3eefb84ac0bd dynamically fix outgoing C-Find requests using "OutgoingFindRequestFilter()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1959
diff changeset
188 {
3eefb84ac0bd dynamically fix outgoing C-Find requests using "OutgoingFindRequestFilter()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1959
diff changeset
189 Json::Value value = Json::objectValue;
3eefb84ac0bd dynamically fix outgoing C-Find requests using "OutgoingFindRequestFilter()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1959
diff changeset
190
3eefb84ac0bd dynamically fix outgoing C-Find requests using "OutgoingFindRequestFilter()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1959
diff changeset
191 for (size_t i = 0; i < dicom.GetSize(); i++)
3eefb84ac0bd dynamically fix outgoing C-Find requests using "OutgoingFindRequestFilter()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1959
diff changeset
192 {
3eefb84ac0bd dynamically fix outgoing C-Find requests using "OutgoingFindRequestFilter()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1959
diff changeset
193 const DicomValue& v = dicom.GetElement(i).GetValue();
3eefb84ac0bd dynamically fix outgoing C-Find requests using "OutgoingFindRequestFilter()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1959
diff changeset
194 std::string s = (v.IsNull() || v.IsBinary()) ? "" : v.GetContent();
3eefb84ac0bd dynamically fix outgoing C-Find requests using "OutgoingFindRequestFilter()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1959
diff changeset
195 value[dicom.GetElement(i).GetTag().Format()] = s;
3eefb84ac0bd dynamically fix outgoing C-Find requests using "OutgoingFindRequestFilter()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1959
diff changeset
196 }
3eefb84ac0bd dynamically fix outgoing C-Find requests using "OutgoingFindRequestFilter()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1959
diff changeset
197
3eefb84ac0bd dynamically fix outgoing C-Find requests using "OutgoingFindRequestFilter()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1959
diff changeset
198 PushJson(value);
3eefb84ac0bd dynamically fix outgoing C-Find requests using "OutgoingFindRequestFilter()"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1959
diff changeset
199 }
4105
c02a2d9efbc2 move FromDcmtkBridge::ExecuteToDicom() to LuaFunctionCall, to remove dependency of DCMTK on Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
200
4300
b30a8de92ad9 abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
201 void LuaFunctionCall::Execute()
b30a8de92ad9 abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
202 {
b30a8de92ad9 abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
203 ExecuteInternal(0);
b30a8de92ad9 abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
204 }
b30a8de92ad9 abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
205
4105
c02a2d9efbc2 move FromDcmtkBridge::ExecuteToDicom() to LuaFunctionCall, to remove dependency of DCMTK on Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
206
c02a2d9efbc2 move FromDcmtkBridge::ExecuteToDicom() to LuaFunctionCall, to remove dependency of DCMTK on Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
207 #if ORTHANC_ENABLE_DCMTK == 1
c02a2d9efbc2 move FromDcmtkBridge::ExecuteToDicom() to LuaFunctionCall, to remove dependency of DCMTK on Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
208 void LuaFunctionCall::ExecuteToDicom(DicomMap& target)
c02a2d9efbc2 move FromDcmtkBridge::ExecuteToDicom() to LuaFunctionCall, to remove dependency of DCMTK on Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
209 {
c02a2d9efbc2 move FromDcmtkBridge::ExecuteToDicom() to LuaFunctionCall, to remove dependency of DCMTK on Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
210 Json::Value output;
c02a2d9efbc2 move FromDcmtkBridge::ExecuteToDicom() to LuaFunctionCall, to remove dependency of DCMTK on Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
211 ExecuteToJson(output, true /* keep strings */);
c02a2d9efbc2 move FromDcmtkBridge::ExecuteToDicom() to LuaFunctionCall, to remove dependency of DCMTK on Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
212
c02a2d9efbc2 move FromDcmtkBridge::ExecuteToDicom() to LuaFunctionCall, to remove dependency of DCMTK on Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
213 target.Clear();
c02a2d9efbc2 move FromDcmtkBridge::ExecuteToDicom() to LuaFunctionCall, to remove dependency of DCMTK on Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
214
c02a2d9efbc2 move FromDcmtkBridge::ExecuteToDicom() to LuaFunctionCall, to remove dependency of DCMTK on Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
215 if (output.type() == Json::arrayValue &&
c02a2d9efbc2 move FromDcmtkBridge::ExecuteToDicom() to LuaFunctionCall, to remove dependency of DCMTK on Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
216 output.size() == 0)
c02a2d9efbc2 move FromDcmtkBridge::ExecuteToDicom() to LuaFunctionCall, to remove dependency of DCMTK on Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
217 {
c02a2d9efbc2 move FromDcmtkBridge::ExecuteToDicom() to LuaFunctionCall, to remove dependency of DCMTK on Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
218 // This case happens for empty tables
c02a2d9efbc2 move FromDcmtkBridge::ExecuteToDicom() to LuaFunctionCall, to remove dependency of DCMTK on Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
219 return;
c02a2d9efbc2 move FromDcmtkBridge::ExecuteToDicom() to LuaFunctionCall, to remove dependency of DCMTK on Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
220 }
c02a2d9efbc2 move FromDcmtkBridge::ExecuteToDicom() to LuaFunctionCall, to remove dependency of DCMTK on Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
221
c02a2d9efbc2 move FromDcmtkBridge::ExecuteToDicom() to LuaFunctionCall, to remove dependency of DCMTK on Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
222 if (output.type() != Json::objectValue)
c02a2d9efbc2 move FromDcmtkBridge::ExecuteToDicom() to LuaFunctionCall, to remove dependency of DCMTK on Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
223 {
c02a2d9efbc2 move FromDcmtkBridge::ExecuteToDicom() to LuaFunctionCall, to remove dependency of DCMTK on Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
224 throw OrthancException(ErrorCode_LuaBadOutput,
c02a2d9efbc2 move FromDcmtkBridge::ExecuteToDicom() to LuaFunctionCall, to remove dependency of DCMTK on Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
225 "Lua: The script must return a table");
c02a2d9efbc2 move FromDcmtkBridge::ExecuteToDicom() to LuaFunctionCall, to remove dependency of DCMTK on Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
226 }
c02a2d9efbc2 move FromDcmtkBridge::ExecuteToDicom() to LuaFunctionCall, to remove dependency of DCMTK on Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
227
c02a2d9efbc2 move FromDcmtkBridge::ExecuteToDicom() to LuaFunctionCall, to remove dependency of DCMTK on Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
228 Json::Value::Members members = output.getMemberNames();
c02a2d9efbc2 move FromDcmtkBridge::ExecuteToDicom() to LuaFunctionCall, to remove dependency of DCMTK on Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
229
c02a2d9efbc2 move FromDcmtkBridge::ExecuteToDicom() to LuaFunctionCall, to remove dependency of DCMTK on Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
230 for (size_t i = 0; i < members.size(); i++)
c02a2d9efbc2 move FromDcmtkBridge::ExecuteToDicom() to LuaFunctionCall, to remove dependency of DCMTK on Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
231 {
c02a2d9efbc2 move FromDcmtkBridge::ExecuteToDicom() to LuaFunctionCall, to remove dependency of DCMTK on Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
232 if (output[members[i]].type() != Json::stringValue)
c02a2d9efbc2 move FromDcmtkBridge::ExecuteToDicom() to LuaFunctionCall, to remove dependency of DCMTK on Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
233 {
c02a2d9efbc2 move FromDcmtkBridge::ExecuteToDicom() to LuaFunctionCall, to remove dependency of DCMTK on Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
234 throw OrthancException(ErrorCode_LuaBadOutput,
c02a2d9efbc2 move FromDcmtkBridge::ExecuteToDicom() to LuaFunctionCall, to remove dependency of DCMTK on Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
235 "Lua: The script must return a table "
c02a2d9efbc2 move FromDcmtkBridge::ExecuteToDicom() to LuaFunctionCall, to remove dependency of DCMTK on Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
236 "mapping names of DICOM tags to strings");
c02a2d9efbc2 move FromDcmtkBridge::ExecuteToDicom() to LuaFunctionCall, to remove dependency of DCMTK on Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
237 }
c02a2d9efbc2 move FromDcmtkBridge::ExecuteToDicom() to LuaFunctionCall, to remove dependency of DCMTK on Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
238
c02a2d9efbc2 move FromDcmtkBridge::ExecuteToDicom() to LuaFunctionCall, to remove dependency of DCMTK on Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
239 DicomTag tag(FromDcmtkBridge::ParseTag(members[i]));
c02a2d9efbc2 move FromDcmtkBridge::ExecuteToDicom() to LuaFunctionCall, to remove dependency of DCMTK on Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
240 target.SetValue(tag, output[members[i]].asString(), false);
c02a2d9efbc2 move FromDcmtkBridge::ExecuteToDicom() to LuaFunctionCall, to remove dependency of DCMTK on Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
241 }
c02a2d9efbc2 move FromDcmtkBridge::ExecuteToDicom() to LuaFunctionCall, to remove dependency of DCMTK on Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
242 }
c02a2d9efbc2 move FromDcmtkBridge::ExecuteToDicom() to LuaFunctionCall, to remove dependency of DCMTK on Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
243 #endif
386
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
244 }