fix line endings in XlsxReader.cpp

develop
Myk Taylor 2020-08-08 20:07:54 -07:00
parent 28eaf0ae8b
commit cffac8ed93
1 changed files with 115 additions and 115 deletions

@ -1,115 +1,115 @@
/* /*
* Wrapper for xlsxio_read library functions. * Wrapper for xlsxio_read library functions.
* *
* Sample usage: * Sample usage:
* *
* std::string filename = "sample_file.xlsx"; * std::string filename = "sample_file.xlsx";
* xlsxioreader xlsxfile = XlsxReader::open_xlsx_file(filename); * xlsxioreader xlsxfile = XlsxReader::open_xlsx_file(filename);
* if (xlsxfile == NULL) { * if (xlsxfile == NULL) {
* printf("cannot open file: '%s'", filename.c_str()); * printf("cannot open file: '%s'", filename.c_str());
* return false; * return false;
* } * }
* auto sheetNames = XlsxReader::list_sheets(xlsxfile); * auto sheetNames = XlsxReader::list_sheets(xlsxfile);
* for (auto sheetName = sheetNames.begin(); * for (auto sheetName = sheetNames.begin();
* sheetName != sheetNames.end(); * sheetName != sheetNames.end();
* ++sheetName) { * ++sheetName) {
* printf("reading sheet: %s\n", sheetName->c_str()); * printf("reading sheet: %s\n", sheetName->c_str());
* xlsxioreadersheet xlsxsheet = * xlsxioreadersheet xlsxsheet =
* XlsxReader::open_sheet(xlsxfile, *sheetName); * XlsxReader::open_sheet(xlsxfile, *sheetName);
* if (xlsxsheet == NULL) { * if (xlsxsheet == NULL) {
* printf("cannot open sheet: '%s'", sheetName->c_str()); * printf("cannot open sheet: '%s'", sheetName->c_str());
* continue; * continue;
* } * }
* std::string value; * std::string value;
* int row_num = 1; * int row_num = 1;
* while (XlsxReader::GetNextRow(xlsxsheet)) { * while (XlsxReader::GetNextRow(xlsxsheet)) {
* std::string s; * std::string s;
* printf("%d:\t", row_num); * printf("%d:\t", row_num);
* while (XlsxReader::GetNextCell(xlsxsheet, s)) { * while (XlsxReader::GetNextCell(xlsxsheet, s)) {
* printf("%s\t", s.c_str()); * printf("%s\t", s.c_str());
* } * }
* printf("\n"); * printf("\n");
* ++row_num; * ++row_num;
* } * }
* XlsxReader::close_sheet(xlsxsheet); * XlsxReader::close_sheet(xlsxsheet);
* } * }
* XlsxReader::close_xlsx_file(xlsxfile); * XlsxReader::close_xlsx_file(xlsxfile);
* return true; * return true;
*/ */
#include <xlsxio_read.h> #include <xlsxio_read.h>
#include "modules/XlsxReader.h" #include "modules/XlsxReader.h"
using namespace DFHack; using namespace DFHack;
// returns NULL on error // returns NULL on error
DFHACK_EXPORT XlsxReader::xlsx_file_handle XlsxReader::open_xlsx_file( DFHACK_EXPORT XlsxReader::xlsx_file_handle XlsxReader::open_xlsx_file(
std::string filename) std::string filename)
{ {
return xlsxioread_open(filename.c_str()); return xlsxioread_open(filename.c_str());
} }
DFHACK_EXPORT void XlsxReader::close_xlsx_file( DFHACK_EXPORT void XlsxReader::close_xlsx_file(
XlsxReader::xlsx_file_handle file_handle) XlsxReader::xlsx_file_handle file_handle)
{ {
xlsxioread_close((xlsxioreader)file_handle); xlsxioread_close((xlsxioreader)file_handle);
} }
static int list_callback(const XLSXIOCHAR* name, void* cbdata) static int list_callback(const XLSXIOCHAR* name, void* cbdata)
{ {
auto sheetNames = (std::vector<std::string> *)cbdata; auto sheetNames = (std::vector<std::string> *)cbdata;
sheetNames->push_back(name); sheetNames->push_back(name);
return 0; return 0;
} }
DFHACK_EXPORT std::vector<std::string> XlsxReader::list_sheets( DFHACK_EXPORT std::vector<std::string> XlsxReader::list_sheets(
XlsxReader::xlsx_file_handle file_handle) XlsxReader::xlsx_file_handle file_handle)
{ {
auto sheetNames = std::vector<std::string>(); auto sheetNames = std::vector<std::string>();
xlsxioread_list_sheets( xlsxioread_list_sheets(
(xlsxioreader)file_handle, list_callback, &sheetNames); (xlsxioreader)file_handle, list_callback, &sheetNames);
return sheetNames; return sheetNames;
} }
// returns XLSXIOReaderSheet object or NULL on error // returns XLSXIOReaderSheet object or NULL on error
DFHACK_EXPORT XlsxReader::xlsx_sheet_handle XlsxReader::open_sheet( DFHACK_EXPORT XlsxReader::xlsx_sheet_handle XlsxReader::open_sheet(
XlsxReader::xlsx_file_handle file_handle, std::string sheet_name) XlsxReader::xlsx_file_handle file_handle, std::string sheet_name)
{ {
if (file_handle == NULL) if (file_handle == NULL)
return NULL; return NULL;
return xlsxioread_sheet_open( return xlsxioread_sheet_open(
(xlsxioreader)file_handle, sheet_name.c_str(), XLSXIOREAD_SKIP_NONE); (xlsxioreader)file_handle, sheet_name.c_str(), XLSXIOREAD_SKIP_NONE);
} }
DFHACK_EXPORT void XlsxReader::close_sheet( DFHACK_EXPORT void XlsxReader::close_sheet(
XlsxReader::xlsx_sheet_handle sheet_handle) XlsxReader::xlsx_sheet_handle sheet_handle)
{ {
xlsxioread_sheet_close((xlsxioreadersheet)sheet_handle); xlsxioread_sheet_close((xlsxioreadersheet)sheet_handle);
} }
// start reading the next row of data; must be called before GetNextCell . // start reading the next row of data; must be called before GetNextCell .
// returns false if there is no next row to get. // returns false if there is no next row to get.
DFHACK_EXPORT bool XlsxReader::get_next_row( DFHACK_EXPORT bool XlsxReader::get_next_row(
XlsxReader::xlsx_sheet_handle sheet_handle) XlsxReader::xlsx_sheet_handle sheet_handle)
{ {
return xlsxioread_sheet_next_row((xlsxioreadersheet)sheet_handle) != 0; return xlsxioread_sheet_next_row((xlsxioreadersheet)sheet_handle) != 0;
} }
// fills the value param with the contents of the cell in the next column cell // fills the value param with the contents of the cell in the next column cell
// in the current row. // in the current row.
// returns false if there are no more cells in this row. // returns false if there are no more cells in this row.
DFHACK_EXPORT bool XlsxReader::get_next_cell( DFHACK_EXPORT bool XlsxReader::get_next_cell(
XlsxReader::xlsx_sheet_handle sheet_handle, std::string& value) XlsxReader::xlsx_sheet_handle sheet_handle, std::string& value)
{ {
char* result; char* result;
if (!xlsxioread_sheet_next_cell_string((xlsxioreadersheet)sheet_handle, if (!xlsxioread_sheet_next_cell_string((xlsxioreadersheet)sheet_handle,
&result)) { &result)) {
value.clear(); value.clear();
return false; return false;
} }
value.assign(result); value.assign(result);
free(result); free(result);
return true; return true;
} }