A binding to libqalculate for lua
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.

87 lines
2.9 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. #include <lua.hpp>
  2. #include <libqalculate/qalculate.h>
  3. #include <vector>
  4. #include <string>
  5. EvaluationOptions user_evaluation_options = default_evaluation_options;
  6. PrintOptions user_print_options = default_print_options;
  7. struct Output_struct {
  8. char * result;
  9. std::vector<char *> messages;
  10. };
  11. Output_struct qalculate(const char* input, bool exact_mode, bool interval_mode, bool structuring_mode) {
  12. // Create and prepare calculator
  13. Calculator* c = new Calculator();
  14. c->loadExchangeRates();
  15. c->loadGlobalDefinitions();
  16. c->loadLocalDefinitions();
  17. // Create input and output string buffers
  18. std::string Input = input;
  19. std::string Output;
  20. // Check evaluation options
  21. user_evaluation_options.structuring = (structuring_mode ?
  22. STRUCTURING_FACTORIZE :
  23. STRUCTURING_EXPAND);
  24. user_evaluation_options.interval_calculation = (interval_mode ?
  25. INTERVAL_CALCULATION_VARIANCE_FORMULA :
  26. INTERVAL_CALCULATION_NONE);
  27. user_evaluation_options.approximation = (exact_mode ?
  28. APPROXIMATION_EXACT :
  29. APPROXIMATION_APPROXIMATE);
  30. user_print_options.number_fraction_format = (exact_mode ?
  31. FRACTION_DECIMAL_EXACT :
  32. FRACTION_DECIMAL);
  33. user_print_options.interval_display = (interval_mode ?
  34. INTERVAL_DISPLAY_INTERVAL :
  35. INTERVAL_DISPLAY_MIDPOINT);
  36. // Prepare output struct
  37. struct Output_struct output;
  38. memset(&output,0,sizeof(Output_struct));
  39. // Evaluate and record result in Output
  40. Output = c->calculateAndPrint(Input,2000,user_evaluation_options,user_print_options);
  41. // record messages
  42. std::string current_msg;
  43. while (c->message() != NULL) {
  44. current_msg = c->message()->message();
  45. output.messages.push_back(new char[current_msg.size()+1]);
  46. current_msg.copy(output.messages.back(),current_msg.size());
  47. output.messages.back()[current_msg.size()] = "\0"[0];
  48. c->nextMessage();
  49. }
  50. delete c;
  51. // Copy result to the output structure
  52. output.result = new char [Output.length()+1];
  53. Output.copy(output.result,Output.length());
  54. output.result[Output.length()] = "\0"[0];
  55. return output;
  56. }
  57. extern "C"
  58. int l_qalc(lua_State* L) {
  59. const char * input = luaL_checkstring(L, 1);
  60. bool exact_mode = lua_toboolean(L, 2);
  61. bool interval_mode = lua_toboolean(L, 3);
  62. bool struct_mode = lua_toboolean(L, 4);
  63. Output_struct output = qalculate(input, exact_mode, interval_mode, struct_mode);
  64. lua_pushstring(L, output.result);
  65. lua_newtable(L);
  66. for (int i = 0; i < output.messages.size(); i++) {
  67. lua_pushstring(L, output.messages[i]);
  68. lua_rawseti(L, -2, i+1);
  69. }
  70. return 2;
  71. }
  72. extern "C"
  73. int luaopen_libqalculator(lua_State* L) {
  74. static const struct luaL_Reg library [] =
  75. {
  76. {"qalc", l_qalc},
  77. {NULL, NULL}
  78. };
  79. luaL_register(L, "qalc", library);
  80. return 1;
  81. }