|
|
@ -1,26 +1,50 @@ |
|
|
|
#include <lua.hpp>
|
|
|
|
#include <libqalculate/qalculate.h>
|
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
EvaluationOptions user_evaluation_options = default_evaluation_options; |
|
|
|
|
|
|
|
const char* qalculate(const char* input, bool exact_mode) { |
|
|
|
struct Output_struct { |
|
|
|
char * result; |
|
|
|
std::vector<char *> messages; |
|
|
|
}; |
|
|
|
|
|
|
|
Output_struct qalculate(const char* input, bool exact_mode, bool interval_mode) { |
|
|
|
// Create and prepare calculator
|
|
|
|
Calculator* c = new Calculator(); |
|
|
|
c->loadExchangeRates(); |
|
|
|
c->loadGlobalDefinitions(); |
|
|
|
c->loadLocalDefinitions(); |
|
|
|
// Create input and output string buffers
|
|
|
|
std::string Input = input; |
|
|
|
std::string Output; |
|
|
|
if (exact_mode) { |
|
|
|
user_evaluation_options.approximation = APPROXIMATION_EXACT; |
|
|
|
} else { |
|
|
|
user_evaluation_options.approximation = APPROXIMATION_APPROXIMATE; |
|
|
|
} |
|
|
|
// Check evaluation options
|
|
|
|
user_evaluation_options.interval_calculation = (interval_mode ? |
|
|
|
INTERVAL_CALCULATION_NONE : |
|
|
|
INTERVAL_CALCULATION_VARIANCE_FORMULA); |
|
|
|
user_evaluation_options.approximation = (exact_mode ? |
|
|
|
APPROXIMATION_EXACT : |
|
|
|
APPROXIMATION_APPROXIMATE); |
|
|
|
// Prepare output struct
|
|
|
|
struct Output_struct output; |
|
|
|
memset(&output,0,sizeof(Output_struct)); |
|
|
|
// Evaluate and record result in Output
|
|
|
|
Output = c->calculateAndPrint(Input,2000,user_evaluation_options); |
|
|
|
// record messages
|
|
|
|
std::string current_msg; |
|
|
|
while (c->message() != NULL) { |
|
|
|
current_msg = c->message()->message(); |
|
|
|
output.messages.push_back(new char[current_msg.size()+1]); |
|
|
|
current_msg.copy(output.messages.back(),current_msg.size()); |
|
|
|
output.messages.back()[current_msg.size()] = "\0"[0]; |
|
|
|
c->nextMessage(); |
|
|
|
} |
|
|
|
delete c; |
|
|
|
char * output = new char [Output.length()+1]; |
|
|
|
Output.copy(output,Output.length()); |
|
|
|
output[Output.length()] = "\0"[0]; |
|
|
|
// Copy result to the output structure
|
|
|
|
output.result = new char [Output.length()+1]; |
|
|
|
Output.copy(output.result,Output.length()); |
|
|
|
output.result[Output.length()] = "\0"[0]; |
|
|
|
return output; |
|
|
|
} |
|
|
|
|
|
|
@ -28,9 +52,15 @@ extern "C" |
|
|
|
int l_qalc(lua_State* L) { |
|
|
|
const char * input = luaL_checkstring(L, 1); |
|
|
|
bool exact_mode = lua_toboolean(L, 2); |
|
|
|
const char * output = qalculate(input, exact_mode); |
|
|
|
lua_pushstring(L, output); |
|
|
|
return 1; |
|
|
|
bool interval_mode = lua_toboolean(L, 3); |
|
|
|
Output_struct output = qalculate(input, exact_mode, !interval_mode); |
|
|
|
lua_pushstring(L, output.result); |
|
|
|
lua_newtable(L); |
|
|
|
for (int i = 0; i < output.messages.size(); i++) { |
|
|
|
lua_pushstring(L, output.messages[i]); |
|
|
|
lua_rawseti(L, -2, i+1); |
|
|
|
} |
|
|
|
return 2; |
|
|
|
} |
|
|
|
|
|
|
|
extern "C" |
|
|
|