Merge remote-tracking branch 'myk002/quickfort_xlsxio' into develop
commit
6755233887
@ -0,0 +1 @@
|
|||||||
|
Subproject commit a7bc26b69768f7fb24f0c7976fae24b157b85b13
|
@ -0,0 +1 @@
|
|||||||
|
Subproject commit 29b881d286f43189ff7392f609da78daa80c0606
|
@ -0,0 +1 @@
|
|||||||
|
Subproject commit 261d56815b29908fc960fecb9cb3143db4b485ad
|
@ -0,0 +1,9 @@
|
|||||||
|
#include "PluginStatics.h"
|
||||||
|
|
||||||
|
namespace DFHack {
|
||||||
|
|
||||||
|
// xlsxreader statics
|
||||||
|
DFHACK_EXPORT xlsx_file_handle_identity xlsx_file_handle::_identity;
|
||||||
|
DFHACK_EXPORT xlsx_sheet_handle_identity xlsx_sheet_handle::_identity;
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,42 @@
|
|||||||
|
/*
|
||||||
|
* This file and the companion PluginStatics.cpp contain static structures used
|
||||||
|
* by DFHack plugins. Linking them here, into the dfhack library, instead of
|
||||||
|
* into the plugins themselves allows the plugins to be freely unloaded and
|
||||||
|
* reloaded without fear of causing cached references to static data becoming
|
||||||
|
* corrupted.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "DataIdentity.h"
|
||||||
|
|
||||||
|
namespace DFHack {
|
||||||
|
|
||||||
|
// xlsxreader definitions
|
||||||
|
struct DFHACK_EXPORT xlsx_file_handle_identity : public compound_identity {
|
||||||
|
xlsx_file_handle_identity()
|
||||||
|
:compound_identity(0, nullptr, nullptr, "xlsx_file_handle") {};
|
||||||
|
DFHack::identity_type type() override { return IDTYPE_OPAQUE; }
|
||||||
|
};
|
||||||
|
|
||||||
|
struct DFHACK_EXPORT xlsx_sheet_handle_identity : public compound_identity {
|
||||||
|
xlsx_sheet_handle_identity()
|
||||||
|
:compound_identity(0, nullptr, nullptr, "xlsx_sheet_handle") {};
|
||||||
|
DFHack::identity_type type() override { return IDTYPE_OPAQUE; }
|
||||||
|
};
|
||||||
|
|
||||||
|
struct DFHACK_EXPORT xlsx_file_handle {
|
||||||
|
typedef struct xlsxio_read_struct* xlsxioreader;
|
||||||
|
const xlsxioreader handle;
|
||||||
|
xlsx_file_handle(xlsxioreader handle): handle(handle) {}
|
||||||
|
static xlsx_file_handle_identity _identity;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct DFHACK_EXPORT xlsx_sheet_handle {
|
||||||
|
typedef struct xlsxio_read_sheet_struct* xlsxioreadersheet;
|
||||||
|
const xlsxioreadersheet handle;
|
||||||
|
xlsx_sheet_handle(xlsxioreadersheet handle): handle(handle) {}
|
||||||
|
static xlsx_sheet_handle_identity _identity;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,47 @@
|
|||||||
|
local _ENV = mkmodule('plugins.xlsxreader')
|
||||||
|
|
||||||
|
--[[
|
||||||
|
|
||||||
|
Native functions:
|
||||||
|
|
||||||
|
* file_handle open_xlsx_file(filename)
|
||||||
|
* close_xlsx_file(file_handle)
|
||||||
|
* sheet_names list_sheets(file_handle)
|
||||||
|
|
||||||
|
* sheet_handle open_sheet(file_handle, sheet_name)
|
||||||
|
* close_sheet(sheet_handle)
|
||||||
|
* cell_strings get_row(sheet_handle)
|
||||||
|
|
||||||
|
Sample usage:
|
||||||
|
|
||||||
|
local xlsxreader = require('plugins.xlsxreader')
|
||||||
|
|
||||||
|
local function dump_sheet(xlsx_file, sheet_name)
|
||||||
|
print('reading sheet: '..sheet_name)
|
||||||
|
local xlsx_sheet = xlsxreader.open_sheet(xlsx_file, sheet_name)
|
||||||
|
dfhack.with_finalize(
|
||||||
|
function () xlsxreader.close_sheet(xlsx_sheet) end,
|
||||||
|
function ()
|
||||||
|
local row_cells = xlsxreader.get_row(xlsx_sheet)
|
||||||
|
while row_cells do
|
||||||
|
printall(row_cells)
|
||||||
|
row_cells = xlsxreader.get_row(xlsx_sheet)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
local filepath = "path/to/some_file.xlsx"
|
||||||
|
local xlsx_file = xlsxreader.open_xlsx_file(filepath)
|
||||||
|
dfhack.with_finalize(
|
||||||
|
function () xlsxreader.close_xlsx_file(xlsx_file) end,
|
||||||
|
function ()
|
||||||
|
for _, sheet_name in ipairs(xlsxreader.list_sheets(xlsx_file)) do
|
||||||
|
dump_sheet(xlsx_file, sheet_name)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
)
|
||||||
|
|
||||||
|
--]]
|
||||||
|
|
||||||
|
return _ENV
|
@ -0,0 +1,145 @@
|
|||||||
|
/*
|
||||||
|
* Wrapper for xlsxio_read library functions.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define BUILD_XLSXIO_STATIC
|
||||||
|
#include <xlsxio_read.h>
|
||||||
|
|
||||||
|
#include "DataFuncs.h"
|
||||||
|
#include "LuaTools.h"
|
||||||
|
#include "PluginManager.h"
|
||||||
|
#include "PluginStatics.h"
|
||||||
|
|
||||||
|
using namespace DFHack;
|
||||||
|
|
||||||
|
DFHACK_PLUGIN("xlsxreader");
|
||||||
|
|
||||||
|
// returns NULL if the file failed to open
|
||||||
|
xlsx_file_handle* open_xlsx_file(std::string filename) {
|
||||||
|
xlsxioreader opaque_file_handle = xlsxioread_open(filename.c_str());
|
||||||
|
if (!opaque_file_handle) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
return new xlsx_file_handle(opaque_file_handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
void close_xlsx_file(xlsx_file_handle *file_handle) {
|
||||||
|
if (!file_handle) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (file_handle->handle) {
|
||||||
|
xlsxioread_close(file_handle->handle);
|
||||||
|
}
|
||||||
|
delete(file_handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
// returns XLSXIOReaderSheet object or NULL on error
|
||||||
|
xlsx_sheet_handle* open_sheet(xlsx_file_handle *file_handle,
|
||||||
|
std::string sheet_name) {
|
||||||
|
CHECK_NULL_POINTER(file_handle);
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
void close_sheet(xlsx_sheet_handle *sheet_handle) {
|
||||||
|
if (!sheet_handle) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (sheet_handle->handle) {
|
||||||
|
xlsxioread_sheet_close(sheet_handle->handle);
|
||||||
|
}
|
||||||
|
delete(sheet_handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int list_callback(const XLSXIOCHAR* name, void* cbdata) {
|
||||||
|
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.
|
||||||
|
static bool get_next_row(xlsx_sheet_handle *sheet_handle) {
|
||||||
|
return xlsxioread_sheet_next_row(sheet_handle->handle) != 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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.
|
||||||
|
static bool get_next_cell(xlsx_sheet_handle *sheet_handle, std::string& value) {
|
||||||
|
char* result;
|
||||||
|
if (!xlsxioread_sheet_next_cell_string(sheet_handle->handle, &result)) {
|
||||||
|
value.clear();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
value.assign(result);
|
||||||
|
free(result);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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)) {
|
||||||
|
luaL_error(L, "invalid xlsxreader handle");
|
||||||
|
}
|
||||||
|
luaL_checktype(L, 1, LUA_TUSERDATA);
|
||||||
|
return LuaWrapper::get_object_ref(L, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// takes a file handle and returns a table-list of sheet names
|
||||||
|
int list_sheets(lua_State *L) {
|
||||||
|
auto file_handle = (xlsx_file_handle *)get_xlsxreader_handle(L);
|
||||||
|
CHECK_NULL_POINTER(file_handle->handle);
|
||||||
|
auto sheetNames = std::vector<std::string>();
|
||||||
|
xlsxioread_list_sheets(file_handle->handle, list_callback, &sheetNames);
|
||||||
|
Lua::PushVector(L, sheetNames, true);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// takes the sheet handle and returns a table-list of strings, or nil if we
|
||||||
|
// already processed the last row in the file.
|
||||||
|
int get_row(lua_State *L) {
|
||||||
|
auto sheet_handle = (xlsx_sheet_handle *)get_xlsxreader_handle(L);
|
||||||
|
CHECK_NULL_POINTER(sheet_handle->handle);
|
||||||
|
bool ok = get_next_row(sheet_handle);
|
||||||
|
if (!ok) {
|
||||||
|
lua_pushnil(L);
|
||||||
|
} else {
|
||||||
|
std::string value;
|
||||||
|
auto cells = std::vector<std::string>();
|
||||||
|
while (get_next_cell(sheet_handle, value)) {
|
||||||
|
cells.push_back(value);
|
||||||
|
}
|
||||||
|
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
|
||||||
|
};
|
||||||
|
|
||||||
|
DFhackCExport command_result plugin_init(color_ostream &,
|
||||||
|
std::vector<PluginCommand> &) {
|
||||||
|
return CR_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
DFhackCExport command_result plugin_shutdown(color_ostream &) {
|
||||||
|
return CR_OK;
|
||||||
|
}
|
Loading…
Reference in New Issue