You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
1.2 KiB
44 lines
1.2 KiB
#include <lua.hpp>
|
|
#include <libqalculate/qalculate.h>
|
|
#include <string>
|
|
|
|
const char* qalculate(const char* input, bool exact_mode) {
|
|
Calculator* c = new Calculator();
|
|
c->loadExchangeRates();
|
|
c->loadGlobalDefinitions();
|
|
c->loadLocalDefinitions();
|
|
std::string Input = input;
|
|
std::string Output;
|
|
if (exact_mode) {
|
|
default_user_evaluation_options.approximation = APPROXIMATION_EXACT;
|
|
} else {
|
|
default_user_evaluation_options.approximation = APPROXIMATION_APPROXIMATE;
|
|
}
|
|
Output = c->calculateAndPrint(Input,2000,default_user_evaluation_options);
|
|
delete c;
|
|
char * output = new char [Output.length()+1];
|
|
Output.copy(output,Output.length());
|
|
output[Output.length()] = "\0"[0];
|
|
return output;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
extern "C"
|
|
int luaopen_libqalculator(lua_State* L) {
|
|
static const struct luaL_Reg library [] =
|
|
{
|
|
{"qalc", l_qalc},
|
|
{NULL, NULL}
|
|
};
|
|
luaL_register(L, "qalc", library);
|
|
return 1;
|
|
}
|
|
|