-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxlsxio.cpp
489 lines (403 loc) · 10.4 KB
/
xlsxio.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
#ifdef _WINDOWS
#define WIN32_MEAN_AND_LEAN
#include <windows.h>
#endif
#include <lua.hpp>
#include "xlsxio_version.h"
#include "xlsxio_read.h"
#include "xlsxio_write.h"
#include "xlsxio.h"
fn_xlsxioread_open read_open = NULL;
fn_xlsxioread_close read_close = NULL;
fn_xlsxioread_sheetlist_open read_sheetlist_open = NULL;
fn_xlsxioread_sheetlist_close read_sheetlist_close = NULL;
fn_xlsxioread_sheetlist_next read_sheetlist_next = NULL;
fn_xlsxioread_process read_process = NULL;
fn_xlsxioread_sheet_open read_sheet_open = NULL;
fn_xlsxioread_sheet_close read_sheet_close = NULL;
fn_xlsxioread_sheet_next_row read_sheet_next_row = NULL;
fn_xlsxioread_sheet_next_cell read_sheet_next_cell = NULL;
fn_xlsxiowrite_open write_open = NULL;
fn_xlsxiowrite_close write_close = NULL;
fn_xlsxiowrite_set_detection_rows write_set_detection_rows = NULL;
fn_xlsxiowrite_set_row_height write_set_row_height = NULL;
fn_xlsxiowrite_add_column write_add_column = NULL;
fn_xlsxiowrite_add_cell_string write_add_cell_string = NULL;
fn_xlsxiowrite_add_cell_int write_add_cell_int = NULL;
fn_xlsxiowrite_add_cell_float write_add_cell_float = NULL;
fn_xlsxiowrite_add_cell_datetime write_add_cell_datetime = NULL;
fn_xlsxiowrite_next_row write_next_row = NULL;
struct ReaderData
{
lua_State *L;
int tableIndex;
int currentSheetTable, currentRow;
int cellCallbackIndex, rowCallbackIndex;
int endIterator;
int nowReadCols, startReadCol, maxReadCols;
int nowReadRows, startReadRow, maxReadRows;
int numOfEmptyCells;
int dropEmptyRow;
};
struct WriterData
{
lua_State *L;
};
int xlsxioread_process_cell_callback_totbl(size_t row, size_t col, const char* value, void* callbackdata)
{
ReaderData* c = (ReaderData*)callbackdata;
if (c->maxReadCols && c->nowReadCols >= c->maxReadCols)
return 1;
if (c->currentRow && c->nowReadCols >= c->startReadCol)
{
//value = (value && value[0]) ? translateGBK(value) : NULL;
if (value && value[0])
lua_pushstring(c->L, value);
else
lua_pushnil(c->L);
lua_rawseti(c->L, c->currentRow, c->nowReadCols + 1);
}
c->nowReadCols ++;
return 0;
}
int xlsxioread_process_row_callback_totbl(size_t row, size_t maxcol, void* callbackdata)
{
ReaderData* c = (ReaderData*)callbackdata;
if (c->currentRow)
{
if (!c->dropEmptyRow || c->numOfEmptyCells < c->nowReadCols)
lua_rawseti(c->L, c->currentSheetTable, c->nowReadRows - c->startReadRow + 1);
c->currentRow = 0;
}
c->nowReadRows ++;
c->nowReadCols = 0;
c->numOfEmptyCells = 0;
if (c->maxReadRows && c->nowReadRows >= c->maxReadRows)
return 1;
if (c->nowReadRows >= c->startReadRow)
{
lua_newtable(c->L);
c->currentRow = lua_gettop(c->L);
}
return 0;
}
static int doCallback(ReaderData* c, int top, int cbIndex)
{
lua_State* L = c->L;
lua_pushvalue(L, cbIndex);
int r = lua_pcall(L, 1, 1, 0);
if (r)
{
c->endIterator = 1;
luaL_error(L, "Error when xlsreader callback: %s", lua_tostring(L, -1), 0);
return -1;
}
if (lua_gettop(L) > top)
{
if (lua_toboolean(L, -1) == 0)
{
c->endIterator = 1;
return -1;
}
lua_settop(L, top);
}
return 0;
}
int xlsxioread_process_cell_callback_cb(size_t row, size_t col, const char* value, void* callbackdata)
{
ReaderData* c = (ReaderData*)callbackdata;
if (c->maxReadCols && c->nowReadCols >= c->maxReadCols)
return 1;
if (c->currentRow && c->nowReadCols >= c->startReadCol && c->cellCallbackIndex)
{
if (value && value[0])
lua_pushstring(c->L, value);
else
lua_pushnil(c->L);
if (doCallback(c, lua_gettop(c->L) - 1, c->cellCallbackIndex))
return -1;
}
c->nowReadCols ++;
return 0;
}
int xlsxioread_process_row_callback_cb(size_t row, size_t maxcol, void* callbackdata)
{
ReaderData* c = (ReaderData*)callbackdata;
if (c->currentRow)
{
if (!c->dropEmptyRow || c->numOfEmptyCells < c->nowReadCols)
lua_rawseti(c->L, c->currentSheetTable, c->nowReadRows - c->startReadRow + 1);
c->currentRow = 0;
}
c->nowReadRows ++;
c->nowReadCols = 0;
c->numOfEmptyCells = 0;
if (c->maxReadRows && c->nowReadRows >= c->maxReadRows)
return 1;
if (c->nowReadRows >= c->startReadRow && c->rowCallbackIndex)
{
if (doCallback(c, lua_gettop(c->L), c->rowCallbackIndex))
return -1;
}
return 0;
}
static int lua_xlsx_read(lua_State* L)
{
const char* filename = luaL_checkstring(L, 1);
int nn = 2;
ReaderData ud;
memset(&ud, 0, sizeof(ud));
ud.L = L;
if (lua_istable(L, 2))
{
lua_pushliteral(L, "dropemptyrow");
lua_rawget(L, 2);
ud.dropEmptyRow = lua_toboolean(L, -1);
lua_pushliteral(L, "maxrows");
lua_rawget(L, 2);
if (lua_isnumber(L, -1))
ud.maxReadRows = lua_tointeger(L, -1);
lua_pushliteral(L, "maxcols");
lua_rawget(L, 2);
if (lua_isnumber(L, -1))
ud.maxReadCols = lua_tointeger(L, -1);
lua_pushliteral(L, "startrow");
lua_rawget(L, 2);
if (lua_isnumber(L, -1))
ud.startReadRow = lua_tointeger(L, -1);
lua_pushliteral(L, "startcol");
lua_rawget(L, 2);
if (lua_isnumber(L, -1))
ud.startReadCol = lua_tointeger(L, -1);
lua_pushliteral(L, "totable");
lua_rawget(L, 2);
if (lua_toboolean(L, -1))
{
lua_newtable(L);
ud.tableIndex = lua_gettop(L);
}
nn ++;
}
if (!ud.tableIndex)
{
// 两个回调,第一个是cell的,第二个是row的
int cb1 = lua_type(L, nn);
int cb2 = lua_type(L, nn + 1);
if (cb1 == LUA_TFUNCTION)
ud.cellCallbackIndex = nn;
else if (cb1 != LUA_TNIL && cb1 != LUA_TNONE)
luaL_error(L, "The third parameter for xlsxio.read function must be function (or nil) type", 0);
if (cb2 == LUA_TFUNCTION)
ud.rowCallbackIndex = nn;
else if (cb2 != LUA_TNIL && cb2 != LUA_TNONE)
luaL_error(L, "The forth parameter for xlsxio.read function must be function (or nil) type", 0);
if (cb1 != LUA_TFUNCTION && cb2 != LUA_TFUNCTION)
{
luaL_error(L, "Any callback function in third (or forth) parameter", 0);
return 0;
}
}
void* reader = read_open(filename);
if (!reader)
{
lua_pushboolean(L, 0);
return 1;
}
void* list = read_sheetlist_open(reader);
for (int iSheet = 0; ; ++ iSheet)
{
char* name = read_sheetlist_next(list);
if (!name)
break;
if (ud.tableIndex)
{
// 转Table模式
lua_newtable(L);
lua_pushliteral(L, "name");
lua_pushstring(L, name);
lua_rawset(L, -3);
lua_pushliteral(L, "rows");
lua_newtable(L);
ud.currentSheetTable = lua_gettop(L);
lua_newtable(L);
ud.currentRow = lua_gettop(L);
read_process(reader, name, 0, &xlsxioread_process_cell_callback_totbl, &xlsxioread_process_row_callback_totbl, &ud);
if (ud.currentRow)
{
lua_pop(L, 1);
ud.currentRow = 0;
}
lua_rawset(L, -3);
lua_rawseti(L, ud.tableIndex, iSheet + 1);
}
else
{
// 回调模式
read_process(reader, name, 0, &xlsxioread_process_cell_callback_cb, &xlsxioread_process_row_callback_cb, &ud);
if (ud.endIterator)
break;
}
ud.currentSheetTable = 0;
ud.nowReadCols = ud.nowReadRows = 0;
ud.numOfEmptyCells = 0;
}
read_close(reader);
if (!ud.tableIndex)
lua_pushboolean(L, 1);
return 1;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define D2I_ENDIALOC 0
union double2int
{
double dval;
int ivals[2];
};
static int readRow(lua_State* L, void* writer, int tableIndex)
{
int ival;
double dval;
double2int d2i;
// 读配置
lua_pushliteral(L, "height");
lua_rawget(L, tableIndex);
if (lua_isnumber(L, -1))
write_set_row_height(writer, lua_tointeger(L, -1));
// 读列
lua_pushliteral(L, "cols");
lua_rawget(L, tableIndex);
int rowsIndex = lua_gettop(L);
if (lua_istable(L, rowsIndex))
{
lua_pushnil(L);
while(lua_next(L, rowsIndex))
{
if (lua_isnumber(L, -2))
{
int tp = lua_type(L, -1);
switch (tp)
{
case LUA_TNUMBER:
dval = lua_tonumber(L, -1);
d2i.dval = dval + 6755399441055744.0;
ival = d2i.ivals[D2I_ENDIALOC];
if ((double)ival == dval && abs(ival) < INT_MAX)
write_add_cell_int(writer, d2i.ivals[D2I_ENDIALOC]);
else
write_add_cell_float(writer, dval);
break;
case LUA_TSTRING:
write_add_cell_string(writer, lua_tostring(L, -1));
break;
}
}
lua_pop(L, 1);
}
}
write_next_row(writer);
return 0;
}
static int lua_xlsx_write(lua_State* L)
{
int nn = 3, top;
const char* filename = luaL_checkstring(L, 1);
const char* sheetname = luaL_checkstring(L, 2);
void* writer = write_open(filename, sheetname);
if (!writer)
{
lua_pushboolean(L, 0);
return 1;
}
if (lua_istable(L, nn))
{
lua_pushliteral(L, "detectrows");
lua_rawget(L, nn);
if (lua_isnumber(L, -1))
write_set_detection_rows(writer, lua_tointeger(L, -1));
lua_pushliteral(L, "headers");
lua_rawget(L, nn);
if (lua_istable(L, -1))
{
// 添加表头
top = lua_gettop(L);
for(int iCol = 1; ; iCol ++)
{
lua_rawgeti(L, top, iCol);
if (lua_istable(L, -1))
{
// 表头的每一个都是一个Table,含有text+width两个成员
lua_pushliteral(L, "text");
lua_rawget(L, -2);
const char* name = lua_tostring(L, -1);
lua_pushliteral(L, "width");
lua_rawget(L, -3);
if (lua_isnumber(L, -1))
write_add_column(writer, name, lua_tointeger(L, -1));
else
write_add_column(writer, name, 100);
}
else if (lua_isstring(L, -1))
{
// 表头的每一个就是字符串,宽度用默认的
write_add_column(writer, lua_tostring(L, -1), 100);
}
else
break;
lua_settop(L, top);
}
write_next_row(writer);
}
nn ++;
}
int type = lua_type(L, nn);
if (type == LUA_TTABLE)
{
top = lua_gettop(L);
for (int iRow = 1; ; iRow ++)
{
lua_rawgeti(L, nn, iRow);
if (!lua_istable(L, top + 1))
break;
readRow(L, writer, top + 1);
lua_settop(L, top);
}
}
write_close(writer);
return 0;
}
struct InitLib
{
HLIB hReadLib, hWriteLib;
InitLib()
{
hReadLib = loadLibXLSIOReader();
hWriteLib = loadLibXLSIOWriter();
}
~InitLib()
{
if (hReadLib)
dlclose(hReadLib);
if (hWriteLib)
dlclose(hWriteLib);
}
} _g_InitLib;
#ifdef _WINDOWS
extern "C" __declspec(dllexport) int luaopen_xlsxio(lua_State* L)
#else
extern "C" __declspec(dllexport) int luaopen_libxlsxio(lua_State* L)
#endif
{
if (!_g_InitLib.hReadLib)
{
lua_pushliteral(L, "load \"xlsxio_read\" dynamic library failed");
return 1;
}
lua_newtable(L);
lua_pushliteral(L, "read");
lua_pushcfunction(L, &lua_xlsx_read);
lua_rawset(L, -3);
lua_pushliteral(L, "write");
lua_pushcfunction(L, &lua_xlsx_write);
lua_rawset(L, -3);
return 1;
}