2020-08-09 09:54:42 -06:00
|
|
|
/*
|
|
|
|
* Wrapper for xlsxio_read library functions.
|
|
|
|
*/
|
|
|
|
|
2020-08-09 22:22:17 -06:00
|
|
|
#define BUILD_XLSXIO_STATIC
|
2020-08-09 09:54:42 -06:00
|
|
|
#include <xlsxio_read.h>
|
|
|
|
|
|
|
|
#include "DataFuncs.h"
|
|
|
|
#include "LuaTools.h"
|
|
|
|
#include "PluginManager.h"
|
2020-08-11 15:51:17 -06:00
|
|
|
#include "PluginStatics.h"
|
2020-08-09 09:54:42 -06:00
|
|
|
|
|
|
|
using namespace DFHack;
|
|
|
|
|
|
|
|
DFHACK_PLUGIN("xlsxreader");
|
|
|
|
|
2020-08-15 23:35:20 -06:00
|
|
|
// returns NULL if the file failed to open
|
2020-08-10 02:13:16 -06:00
|
|
|
xlsx_file_handle* open_xlsx_file(std::string filename) {
|
2020-08-15 23:35:20 -06:00
|
|
|
xlsxioreader opaque_file_handle = xlsxioread_open(filename.c_str());
|
|
|
|
if (!opaque_file_handle) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return new xlsx_file_handle(opaque_file_handle);
|
2020-08-09 09:54:42 -06:00
|
|
|
}
|
|
|
|
|
2020-08-10 02:13:16 -06:00
|
|
|
void close_xlsx_file(xlsx_file_handle *file_handle) {
|
2020-08-16 00:37:21 -06:00
|
|
|
if (!file_handle) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (file_handle->handle) {
|
|
|
|
xlsxioread_close(file_handle->handle);
|
|
|
|
}
|
2020-08-10 02:13:16 -06:00
|
|
|
delete(file_handle);
|
2020-08-09 09:54:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// returns XLSXIOReaderSheet object or NULL on error
|
2020-08-10 02:13:16 -06:00
|
|
|
xlsx_sheet_handle* open_sheet(xlsx_file_handle *file_handle,
|
|
|
|
std::string sheet_name) {
|
|
|
|
CHECK_NULL_POINTER(file_handle);
|
2020-08-15 23:35:20 -06:00
|
|
|
CHECK_NULL_POINTER(file_handle->handle);
|
|
|
|
xlsxioreadersheet opaque_sheet_handle = xlsxioread_sheet_open(
|
|
|
|
file_handle->handle, sheet_name.c_str(), XLSXIOREAD_SKIP_NONE);
|
|
|
|
if (!opaque_sheet_handle) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return new xlsx_sheet_handle(opaque_sheet_handle);
|
2020-08-09 09:54:42 -06:00
|
|
|
}
|
|
|
|
|
2020-08-10 02:13:16 -06:00
|
|
|
void close_sheet(xlsx_sheet_handle *sheet_handle) {
|
2020-08-16 00:37:21 -06:00
|
|
|
if (!sheet_handle) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (sheet_handle->handle) {
|
|
|
|
xlsxioread_sheet_close(sheet_handle->handle);
|
|
|
|
}
|
2020-08-10 02:13:16 -06:00
|
|
|
delete(sheet_handle);
|
2020-08-09 09:54:42 -06:00
|
|
|
}
|
|
|
|
|
2020-08-10 02:13:16 -06:00
|
|
|
static int list_callback(const XLSXIOCHAR* name, void* cbdata) {
|
2020-08-09 09:54:42 -06:00
|
|
|
auto sheetNames = (std::vector<std::string> *)cbdata;
|
|
|
|
sheetNames->push_back(name);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// start reading the next row of data; must be called before get_next_cell.
|
|
|
|
// returns false if there is no next row to get.
|
2020-08-10 02:13:16 -06:00
|
|
|
static bool get_next_row(xlsx_sheet_handle *sheet_handle) {
|
|
|
|
return xlsxioread_sheet_next_row(sheet_handle->handle) != 0;
|
2020-08-09 09:54:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// fills the value param with the contents of the cell in the next column cell
|
|
|
|
// in the current row.
|
|
|
|
// returns false if there are no more cells in this row.
|
2020-08-10 02:13:16 -06:00
|
|
|
static bool get_next_cell(xlsx_sheet_handle *sheet_handle, std::string& value) {
|
2020-08-09 09:54:42 -06:00
|
|
|
char* result;
|
2020-08-10 02:13:16 -06:00
|
|
|
if (!xlsxioread_sheet_next_cell_string(sheet_handle->handle, &result)) {
|
2020-08-09 09:54:42 -06:00
|
|
|
value.clear();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
value.assign(result);
|
|
|
|
free(result);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-08-10 02:13:16 -06:00
|
|
|
// internal function to extract a handle from the first stack param
|
|
|
|
static void * get_xlsxreader_handle(lua_State *L) {
|
|
|
|
if (lua_gettop(L) < 1 || lua_isnil(L, 1)) {
|
2020-08-09 09:54:42 -06:00
|
|
|
luaL_error(L, "invalid xlsxreader handle");
|
|
|
|
}
|
2020-08-10 02:13:16 -06:00
|
|
|
luaL_checktype(L, 1, LUA_TUSERDATA);
|
|
|
|
return LuaWrapper::get_object_ref(L, 1);
|
2020-08-09 09:54:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// takes a file handle and returns a table-list of sheet names
|
2020-08-10 02:13:16 -06:00
|
|
|
int list_sheets(lua_State *L) {
|
|
|
|
auto file_handle = (xlsx_file_handle *)get_xlsxreader_handle(L);
|
2020-08-15 23:35:20 -06:00
|
|
|
CHECK_NULL_POINTER(file_handle->handle);
|
2020-08-09 09:54:42 -06:00
|
|
|
auto sheetNames = std::vector<std::string>();
|
2020-08-10 02:13:16 -06:00
|
|
|
xlsxioread_list_sheets(file_handle->handle, list_callback, &sheetNames);
|
2020-08-09 09:54:42 -06:00
|
|
|
Lua::PushVector(L, sheetNames, true);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-03-08 16:04:50 -07:00
|
|
|
// takes the sheet handle and returns a table-list of strings, limited to the
|
|
|
|
// requested number of tokens if specified and > 0, or nil if we already
|
|
|
|
// processed the last row in the file.
|
2020-08-10 02:13:16 -06:00
|
|
|
int get_row(lua_State *L) {
|
|
|
|
auto sheet_handle = (xlsx_sheet_handle *)get_xlsxreader_handle(L);
|
2020-08-15 23:35:20 -06:00
|
|
|
CHECK_NULL_POINTER(sheet_handle->handle);
|
2021-03-08 16:04:50 -07:00
|
|
|
lua_Integer max_tokens = luaL_optinteger(L, 2, 0);
|
2020-08-09 09:54:42 -06:00
|
|
|
bool ok = get_next_row(sheet_handle);
|
2020-08-10 02:13:16 -06:00
|
|
|
if (!ok) {
|
2020-08-09 09:54:42 -06:00
|
|
|
lua_pushnil(L);
|
2020-08-10 02:13:16 -06:00
|
|
|
} else {
|
2020-08-09 09:54:42 -06:00
|
|
|
std::string value;
|
|
|
|
auto cells = std::vector<std::string>();
|
2020-08-10 02:13:16 -06:00
|
|
|
while (get_next_cell(sheet_handle, value)) {
|
2021-03-26 20:43:15 -06:00
|
|
|
// read all cells in the row, even if we don't need to;
|
|
|
|
// otherwise xlsxio will return a spurious empty row on
|
|
|
|
// next call
|
2021-03-26 22:11:50 -06:00
|
|
|
if (max_tokens <= 0 || int(cells.size()) < max_tokens) {
|
2021-03-26 20:43:15 -06:00
|
|
|
cells.push_back(value);
|
|
|
|
}
|
2020-08-09 09:54:42 -06:00
|
|
|
}
|
|
|
|
Lua::PushVector(L, cells, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
DFHACK_PLUGIN_LUA_FUNCTIONS {
|
|
|
|
DFHACK_LUA_FUNCTION(open_xlsx_file),
|
|
|
|
DFHACK_LUA_FUNCTION(close_xlsx_file),
|
|
|
|
DFHACK_LUA_FUNCTION(open_sheet),
|
|
|
|
DFHACK_LUA_FUNCTION(close_sheet),
|
|
|
|
DFHACK_LUA_END
|
|
|
|
};
|
|
|
|
|
|
|
|
DFHACK_PLUGIN_LUA_COMMANDS{
|
|
|
|
DFHACK_LUA_COMMAND(list_sheets),
|
|
|
|
DFHACK_LUA_COMMAND(get_row),
|
|
|
|
DFHACK_LUA_END
|
|
|
|
};
|
|
|
|
|
2020-08-10 02:13:16 -06:00
|
|
|
DFhackCExport command_result plugin_init(color_ostream &,
|
|
|
|
std::vector<PluginCommand> &) {
|
2020-08-09 09:54:42 -06:00
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
2020-08-10 02:13:16 -06:00
|
|
|
DFhackCExport command_result plugin_shutdown(color_ostream &) {
|
2020-08-09 09:54:42 -06:00
|
|
|
return CR_OK;
|
|
|
|
}
|