-
Notifications
You must be signed in to change notification settings - Fork 1
/
lua-tokyocabinet.c
7272 lines (6793 loc) · 191 KB
/
lua-tokyocabinet.c
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*************************************************************************************************
* Lua binding of Tokyo Cabinet
* Copyright (C) 2006-2010 FAL Labs
* This file is part of Tokyo Cabinet.
* Tokyo Cabinet is free software; you can redistribute it and/or modify it under the terms of
* the GNU Lesser General Public License as published by the Free Software Foundation; either
* version 2.1 of the License or any later version. Tokyo Cabinet is distributed in the hope
* that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
* You should have received a copy of the GNU Lesser General Public License along with Tokyo
* Cabinet; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA.
*************************************************************************************************/
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
#if LUA_VERSION_NUM > 501
#include <lauxlib.h>
#define lua_objlen(L, index) luaL_len(L, index)
#endif
#include "tcutil.h"
#include "tchdb.h"
#include "tcbdb.h"
#include "tcfdb.h"
#include "tctdb.h"
#include "tcadb.h"
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <limits.h>
#include <math.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#define SWAB16(TC_num) \
( \
((TC_num & 0x00ffU) << 8) | \
((TC_num & 0xff00U) >> 8) \
)
#define SWAB32(TC_num) \
( \
((TC_num & 0x000000ffUL) << 24) | \
((TC_num & 0x0000ff00UL) << 8) | \
((TC_num & 0x00ff0000UL) >> 8) | \
((TC_num & 0xff000000UL) >> 24) \
)
#define SWAB64(TC_num) \
( \
((TC_num & 0x00000000000000ffULL) << 56) | \
((TC_num & 0x000000000000ff00ULL) << 40) | \
((TC_num & 0x0000000000ff0000ULL) << 24) | \
((TC_num & 0x00000000ff000000ULL) << 8) | \
((TC_num & 0x000000ff00000000ULL) >> 8) | \
((TC_num & 0x0000ff0000000000ULL) >> 24) | \
((TC_num & 0x00ff000000000000ULL) >> 40) | \
((TC_num & 0xff00000000000000ULL) >> 56) \
)
#if defined(_MYBIGEND) || defined(_MYSWAB)
#define HTONS(TC_num) (TC_num)
#define HTONL(TC_num) (TC_num)
#define HTONLL(TC_num) (TC_num)
#define NTOHS(TC_num) (TC_num)
#define NTOHL(TC_num) (TC_num)
#define NTOHLL(TC_num) (TC_num)
#else
#define HTONS(TC_num) SWAB16(TC_num)
#define HTONL(TC_num) SWAB32(TC_num)
#define HTONLL(TC_num) SWAB64(TC_num)
#define NTOHS(TC_num) SWAB16(TC_num)
#define NTOHL(TC_num) SWAB32(TC_num)
#define NTOHLL(TC_num) SWAB64(TC_num)
#endif
#define NUMBUFSIZ 32
#define HDBDATAVAR "_hdbdata_"
#define BDBDATAVAR "_bdbdata_"
#define BDBCURDATAVAR "_bdbcurdata_"
#define FDBDATAVAR "_fdbdata_"
#define TDBDATAVAR "_tdbdata_"
#define TDBQRYDATAVAR "_tdbqrydata_"
#define ADBDATAVAR "_adbdata_"
typedef struct {
lua_State *lua;
char *fname;
} FUNCOP;
typedef struct {
TCHDB *hdb;
} HDBDATA;
typedef struct {
TCBDB *bdb;
} BDBDATA;
typedef struct {
BDBCUR *cur;
} BDBCURDATA;
typedef struct {
TCFDB *fdb;
} FDBDATA;
typedef struct {
TCTDB *tdb;
} TDBDATA;
typedef struct {
TDBQRY *qry;
} TDBQRYDATA;
typedef struct {
TCADB *adb;
} ADBDATA;
/* function prototypes */
int luaopen_tokyocabinet(lua_State *lua);
static TCLIST *tabletotclist(lua_State *lua, int index);
static void tclisttotable(lua_State *lua, TCLIST *list);
static TCMAP *tabletotcmap(lua_State *lua, int index);
static void tcmaptotable(lua_State *lua, TCMAP *map);
static void util_init(lua_State *lua);
static int util_tablenew(lua_State *lua);
static int util_pack(lua_State *lua);
static int util_unpack(lua_State *lua);
static int util_split(lua_State *lua);
static int util_codec(lua_State *lua);
static int util_hash(lua_State *lua);
static int util_bit(lua_State *lua);
static int util_strstr(lua_State *lua);
static int util_regex(lua_State *lua);
static int util_ucs(lua_State *lua);
static int util_dist(lua_State *lua);
static int util_isect(lua_State *lua);
static int util_union(lua_State *lua);
static int util_time(lua_State *lua);
static int util_sleep(lua_State *lua);
static int util_stat(lua_State *lua);
static int util_glob(lua_State *lua);
static int util_remove(lua_State *lua);
static int util_mkdir(lua_State *lua);
static int util_chdir(lua_State *lua);
static bool util_iterrec(const void *kbuf, int ksiz, const void *vbuf, int vsiz, FUNCOP *funcop);
static int util_cmpobj(const char *aptr, int asiz, const char *bptr, int bsiz, FUNCOP *funcop);
static void hdb_init(lua_State *lua);
static int hdb_new(lua_State *lua);
static int hdb_del(lua_State *lua);
static int hdb_errmsg(lua_State *lua);
static int hdb_ecode(lua_State *lua);
static int hdb_tune(lua_State *lua);
static int hdb_setcache(lua_State *lua);
static int hdb_setxmsiz(lua_State *lua);
static int hdb_setdfunit(lua_State *lua);
static int hdb_open(lua_State *lua);
static int hdb_close(lua_State *lua);
static int hdb_put(lua_State *lua);
static int hdb_putkeep(lua_State *lua);
static int hdb_putcat(lua_State *lua);
static int hdb_putasync(lua_State *lua);
static int hdb_out(lua_State *lua);
static int hdb_get(lua_State *lua);
static int hdb_vsiz(lua_State *lua);
static int hdb_iterinit(lua_State *lua);
static int hdb_iternext(lua_State *lua);
static int hdb_fwmkeys(lua_State *lua);
static int hdb_addint(lua_State *lua);
static int hdb_adddouble(lua_State *lua);
static int hdb_sync(lua_State *lua);
static int hdb_optimize(lua_State *lua);
static int hdb_vanish(lua_State *lua);
static int hdb_copy(lua_State *lua);
static int hdb_tranbegin(lua_State *lua);
static int hdb_trancommit(lua_State *lua);
static int hdb_tranabort(lua_State *lua);
static int hdb_path(lua_State *lua);
static int hdb_rnum(lua_State *lua);
static int hdb_fsiz(lua_State *lua);
static int hdb_foreach(lua_State *lua);
static int hdb_pairs(lua_State *lua);
static int hdb_next(lua_State *lua);
static void bdb_init(lua_State *lua);
static int bdb_new(lua_State *lua);
static int bdb_del(lua_State *lua);
static int bdb_errmsg(lua_State *lua);
static int bdb_ecode(lua_State *lua);
static int bdb_setcmpfunc(lua_State *lua);
static int bdb_tune(lua_State *lua);
static int bdb_setcache(lua_State *lua);
static int bdb_setxmsiz(lua_State *lua);
static int bdb_setdfunit(lua_State *lua);
static int bdb_open(lua_State *lua);
static int bdb_close(lua_State *lua);
static int bdb_put(lua_State *lua);
static int bdb_putkeep(lua_State *lua);
static int bdb_putcat(lua_State *lua);
static int bdb_putdup(lua_State *lua);
static int bdb_putlist(lua_State *lua);
static int bdb_out(lua_State *lua);
static int bdb_outlist(lua_State *lua);
static int bdb_get(lua_State *lua);
static int bdb_getlist(lua_State *lua);
static int bdb_vnum(lua_State *lua);
static int bdb_vsiz(lua_State *lua);
static int bdb_range(lua_State *lua);
static int bdb_fwmkeys(lua_State *lua);
static int bdb_addint(lua_State *lua);
static int bdb_adddouble(lua_State *lua);
static int bdb_sync(lua_State *lua);
static int bdb_optimize(lua_State *lua);
static int bdb_vanish(lua_State *lua);
static int bdb_copy(lua_State *lua);
static int bdb_tranbegin(lua_State *lua);
static int bdb_trancommit(lua_State *lua);
static int bdb_tranabort(lua_State *lua);
static int bdb_path(lua_State *lua);
static int bdb_rnum(lua_State *lua);
static int bdb_fsiz(lua_State *lua);
static int bdb_foreach(lua_State *lua);
static int bdb_pairs(lua_State *lua);
static int bdb_next(lua_State *lua);
static void bdbcur_init(lua_State *lua);
static int bdbcur_new(lua_State *lua);
static int bdbcur_del(lua_State *lua);
static int bdbcur_first(lua_State *lua);
static int bdbcur_last(lua_State *lua);
static int bdbcur_jump(lua_State *lua);
static int bdbcur_prev(lua_State *lua);
static int bdbcur_next(lua_State *lua);
static int bdbcur_put(lua_State *lua);
static int bdbcur_out(lua_State *lua);
static int bdbcur_key(lua_State *lua);
static int bdbcur_val(lua_State *lua);
static void fdb_init(lua_State *lua);
static int fdb_new(lua_State *lua);
static int fdb_del(lua_State *lua);
static int fdb_errmsg(lua_State *lua);
static int fdb_ecode(lua_State *lua);
static int fdb_tune(lua_State *lua);
static int fdb_open(lua_State *lua);
static int fdb_close(lua_State *lua);
static int fdb_put(lua_State *lua);
static int fdb_putkeep(lua_State *lua);
static int fdb_putcat(lua_State *lua);
static int fdb_out(lua_State *lua);
static int fdb_get(lua_State *lua);
static int fdb_vsiz(lua_State *lua);
static int fdb_iterinit(lua_State *lua);
static int fdb_iternext(lua_State *lua);
static int fdb_range(lua_State *lua);
static int fdb_addint(lua_State *lua);
static int fdb_adddouble(lua_State *lua);
static int fdb_sync(lua_State *lua);
static int fdb_optimize(lua_State *lua);
static int fdb_vanish(lua_State *lua);
static int fdb_copy(lua_State *lua);
static int fdb_tranbegin(lua_State *lua);
static int fdb_trancommit(lua_State *lua);
static int fdb_tranabort(lua_State *lua);
static int fdb_path(lua_State *lua);
static int fdb_rnum(lua_State *lua);
static int fdb_fsiz(lua_State *lua);
static int fdb_foreach(lua_State *lua);
static int fdb_pairs(lua_State *lua);
static int fdb_next(lua_State *lua);
static void tdb_init(lua_State *lua);
static int tdb_new(lua_State *lua);
static int tdb_del(lua_State *lua);
static int tdb_errmsg(lua_State *lua);
static int tdb_ecode(lua_State *lua);
static int tdb_tune(lua_State *lua);
static int tdb_setcache(lua_State *lua);
static int tdb_setxmsiz(lua_State *lua);
static int tdb_setdfunit(lua_State *lua);
static int tdb_open(lua_State *lua);
static int tdb_close(lua_State *lua);
static int tdb_put(lua_State *lua);
static int tdb_putkeep(lua_State *lua);
static int tdb_putcat(lua_State *lua);
static int tdb_out(lua_State *lua);
static int tdb_get(lua_State *lua);
static int tdb_vsiz(lua_State *lua);
static int tdb_iterinit(lua_State *lua);
static int tdb_iternext(lua_State *lua);
static int tdb_fwmkeys(lua_State *lua);
static int tdb_addint(lua_State *lua);
static int tdb_adddouble(lua_State *lua);
static int tdb_sync(lua_State *lua);
static int tdb_optimize(lua_State *lua);
static int tdb_vanish(lua_State *lua);
static int tdb_copy(lua_State *lua);
static int tdb_tranbegin(lua_State *lua);
static int tdb_trancommit(lua_State *lua);
static int tdb_tranabort(lua_State *lua);
static int tdb_path(lua_State *lua);
static int tdb_rnum(lua_State *lua);
static int tdb_fsiz(lua_State *lua);
static int tdb_setindex(lua_State *lua);
static int tdb_genuid(lua_State *lua);
static int tdb_foreach(lua_State *lua);
static int tdb_pairs(lua_State *lua);
static int tdb_next(lua_State *lua);
static void tdbqry_init(lua_State *lua);
static int tdbqry_procrec(const void *pkbuf, int pksiz, TCMAP *cols, FUNCOP *funcop);
static int tdbqry_new(lua_State *lua);
static int tdbqry_del(lua_State *lua);
static int tdbqry_addcond(lua_State *lua);
static int tdbqry_setorder(lua_State *lua);
static int tdbqry_setlimit(lua_State *lua);
static int tdbqry_search(lua_State *lua);
static int tdbqry_searchout(lua_State *lua);
static int tdbqry_proc(lua_State *lua);
static int tdbqry_hint(lua_State *lua);
static int tdbqry_metasearch(lua_State *lua);
static int tdbqry_kwic(lua_State *lua);
static void adb_init(lua_State *lua);
static int adb_new(lua_State *lua);
static int adb_del(lua_State *lua);
static int adb_open(lua_State *lua);
static int adb_close(lua_State *lua);
static int adb_put(lua_State *lua);
static int adb_putkeep(lua_State *lua);
static int adb_putcat(lua_State *lua);
static int adb_out(lua_State *lua);
static int adb_get(lua_State *lua);
static int adb_vsiz(lua_State *lua);
static int adb_iterinit(lua_State *lua);
static int adb_iternext(lua_State *lua);
static int adb_fwmkeys(lua_State *lua);
static int adb_addint(lua_State *lua);
static int adb_adddouble(lua_State *lua);
static int adb_sync(lua_State *lua);
static int adb_optimize(lua_State *lua);
static int adb_vanish(lua_State *lua);
static int adb_copy(lua_State *lua);
static int adb_tranbegin(lua_State *lua);
static int adb_trancommit(lua_State *lua);
static int adb_tranabort(lua_State *lua);
static int adb_path(lua_State *lua);
static int adb_rnum(lua_State *lua);
static int adb_size(lua_State *lua);
static int adb_foreach(lua_State *lua);
static int adb_pairs(lua_State *lua);
static int adb_next(lua_State *lua);
static int tc_version(lua_State *lua);
static const luaL_Reg tokyo_cabinet_lib[] = {
// util
{"version", tc_version},
{"tablenew", util_tablenew},
{"pack", util_pack},
{"unpack", util_unpack},
{"split", util_split},
{"codec", util_codec},
{"hash", util_hash},
{"bit", util_bit},
{"strstr", util_strstr},
{"regex", util_regex},
{"ucs", util_ucs},
{"dist", util_dist},
{"isect", util_isect},
{"union", util_union},
{"time", util_time},
{"sleep", util_sleep},
{"stat", util_stat},
{"glob", util_glob},
{"remove", util_remove},
{"mkdir", util_mkdir},
{"chdir", util_chdir},
// hdb
{"hdbnew", hdb_new},
// bdb
{"bdbnew", bdb_new},
// bdbcur
{"bdbcurnew", bdbcur_new},
// fdb
{"fdbnew", fdb_new},
// tdb
{"tdbnew", tdb_new},
// tdbqry
{"tdbqrynew", tdbqry_new},
// adb
{"adbnew", adb_new},
// end
{NULL, NULL}
};
/* initialization when the library is loaded */
int luaopen_cabinet_tokyo(lua_State *lua){
#if 0
lua_settop(lua, 0);
lua_newtable(lua);
lua_setglobal(lua, "tokyocabinet");
util_init(lua);
hdb_init(lua);
bdb_init(lua);
bdbcur_init(lua);
fdb_init(lua);
tdb_init(lua);
tdbqry_init(lua);
adb_init(lua);
lua_settop(lua, 0);
return 0;
#else
luaL_newlib(lua, tokyo_cabinet_lib);
return 1;
#endif
}
/* convert a table of Lua into a list object of TC */
static TCLIST *tabletotclist(lua_State *lua, int index){
int len = lua_objlen(lua, index);
TCLIST *list = tclistnew2(len);
for(int i = 1; i <= len; i++){
lua_rawgeti(lua, index, i);
size_t vsiz;
const char *vbuf = lua_tolstring(lua, -1, &vsiz);
if(vbuf) tclistpush(list, vbuf, vsiz);
lua_pop(lua, 1);
}
return list;
}
/* convert a list object of TC into a table of Lua */
static void tclisttotable(lua_State *lua, TCLIST *list){
int num = tclistnum(list);
lua_createtable(lua, num, 0);
for(int i = 0; i < num; i++){
int size;
const char *buf = tclistval(list, i, &size);
lua_pushlstring(lua, buf, size);
lua_rawseti(lua, -2, i + 1);
}
}
/* convert a table of Lua into a map object of TC */
static TCMAP *tabletotcmap(lua_State *lua, int index){
TCMAP *map = tcmapnew2(31);
char knbuf[NUMBUFSIZ], vnbuf[NUMBUFSIZ];
lua_pushnil(lua);
while(lua_next(lua, index) != 0){
const char *kbuf = NULL;
size_t ksiz = 0;
switch(lua_type(lua, -2)){
case LUA_TNUMBER:
ksiz = sprintf(knbuf, "%lld", (long long)lua_tonumber(lua, -2));
kbuf = knbuf;
break;
case LUA_TSTRING:
kbuf = lua_tolstring(lua, -2, &ksiz);
break;
}
if(kbuf){
const char *vbuf = NULL;
size_t vsiz = 0;
switch(lua_type(lua, -1)){
case LUA_TNUMBER:
vsiz = sprintf(vnbuf, "%lld", (long long)lua_tonumber(lua, -1));
if(vsiz > sizeof(vnbuf)) vsiz = sizeof(vnbuf);
vbuf = vnbuf;
break;
case LUA_TSTRING:
vbuf = lua_tolstring(lua, -1, &vsiz);
break;
}
if(vbuf) tcmapput(map, kbuf, ksiz, vbuf, vsiz);
}
lua_pop(lua, 1);
}
lua_pop(lua, 1);
return map;
}
/* convert a map object of TC into a table of Lua */
static void tcmaptotable(lua_State *lua, TCMAP *map){
int num = tcmaprnum(map);
lua_createtable(lua, 0, num);
tcmapiterinit(map);
const char *kbuf;
int ksiz;
while((kbuf = tcmapiternext(map, &ksiz)) != NULL){
int vsiz;
const char *vbuf = tcmapiterval(kbuf, &vsiz);
lua_pushlstring(lua, vbuf, vsiz);
lua_setfield(lua, -2, kbuf);
}
}
/* initialization of utility */
static void util_init(lua_State *lua){
lua_settop(lua, 0);
lua_getglobal(lua, "tokyocabinet");
lua_pushstring(lua, tcversion);
lua_setfield(lua, -2, "version");
lua_pushcfunction(lua, util_tablenew);
lua_setfield(lua, -2, "tablenew");
lua_pushcfunction(lua, util_pack);
lua_setfield(lua, -2, "pack");
lua_pushcfunction(lua, util_unpack);
lua_setfield(lua, -2, "unpack");
lua_pushcfunction(lua, util_split);
lua_setfield(lua, -2, "split");
lua_pushcfunction(lua, util_codec);
lua_setfield(lua, -2, "codec");
lua_pushcfunction(lua, util_hash);
lua_setfield(lua, -2, "hash");
lua_pushcfunction(lua, util_bit);
lua_setfield(lua, -2, "bit");
lua_pushcfunction(lua, util_strstr);
lua_setfield(lua, -2, "strstr");
lua_pushcfunction(lua, util_regex);
lua_setfield(lua, -2, "regex");
lua_pushcfunction(lua, util_ucs);
lua_setfield(lua, -2, "ucs");
lua_pushcfunction(lua, util_dist);
lua_setfield(lua, -2, "dist");
lua_pushcfunction(lua, util_isect);
lua_setfield(lua, -2, "isect");
lua_pushcfunction(lua, util_union);
lua_setfield(lua, -2, "union");
lua_pushcfunction(lua, util_time);
lua_setfield(lua, -2, "time");
lua_pushcfunction(lua, util_sleep);
lua_setfield(lua, -2, "sleep");
lua_pushcfunction(lua, util_stat);
lua_setfield(lua, -2, "stat");
lua_pushcfunction(lua, util_glob);
lua_setfield(lua, -2, "glob");
lua_pushcfunction(lua, util_remove);
lua_setfield(lua, -2, "remove");
lua_pushcfunction(lua, util_mkdir);
lua_setfield(lua, -2, "mkdir");
lua_pushcfunction(lua, util_chdir);
lua_setfield(lua, -2, "chdir");
}
/* for tablenew function */
static int util_tablenew(lua_State *lua){
int argc = lua_gettop(lua);
int anum = argc > 0 ? lua_tointeger(lua, 1) : 0;
int rnum = argc > 1 ? lua_tointeger(lua, 2) : 0;
if(anum < 0) anum = 0;
if(rnum < 0) rnum = 0;
lua_settop(lua, 0);
lua_createtable(lua, anum, rnum);
return 1;
}
/* for pack function */
static int util_pack(lua_State *lua){
int argc = lua_gettop(lua);
if(argc < 1){
lua_pushstring(lua, "pack: invalid arguments");
lua_error(lua);
}
const char *format = lua_tostring(lua, 1);
if(!format){
lua_pushstring(lua, "pack: invalid arguments");
lua_error(lua);
}
lua_newtable(lua);
int aidx = argc + 1;
int eidx = 1;
for(int i = 2; i <= argc; i++){
int len;
switch(lua_type(lua, i)){
case LUA_TNUMBER:
case LUA_TSTRING:
lua_pushvalue(lua, i);
lua_rawseti(lua, aidx, eidx++);
break;
case LUA_TTABLE:
len = lua_objlen(lua, i);
for(int j = 1; j <= len; j++){
lua_rawgeti(lua, i, j);
lua_rawseti(lua, aidx, eidx++);
}
break;
default:
lua_pushnumber(lua, 0);
lua_rawseti(lua, aidx, eidx++);
break;
}
}
lua_replace(lua, 2);
lua_settop(lua, 2);
TCXSTR *xstr = tcxstrnew();
int emax = eidx - 1;
eidx = 1;
while(*format != '\0'){
int c = *format;
int loop = 1;
if(format[1] == '*'){
loop = INT_MAX;
format++;
} else if(format[1] >= '0' && format[1] <= '9'){
char *suffix;
loop = strtol(format + 1, &suffix, 10);
format = suffix - 1;
}
loop = tclmin(loop, emax);
int end = tclmin(eidx + loop - 1, emax);
while(eidx <= end){
lua_rawgeti(lua, 2, eidx);
double num = lua_tonumber(lua, 3);
lua_pop(lua, 1);
uint8_t cnum;
uint16_t snum;
uint32_t inum;
uint64_t lnum;
double dnum;
float fnum;
uint64_t wnum;
char wbuf[NUMBUFSIZ], *wp;
switch(c){
case 'c':
case 'C':
cnum = num;
tcxstrcat(xstr, &cnum, sizeof(cnum));
break;
case 's':
case 'S':
snum = num;
tcxstrcat(xstr, &snum, sizeof(snum));
break;
case 'i':
case 'I':
inum = num;
tcxstrcat(xstr, &inum, sizeof(inum));
break;
case 'l':
case 'L':
lnum = num;
tcxstrcat(xstr, &lnum, sizeof(lnum));
break;
case 'f':
case 'F':
fnum = num;
tcxstrcat(xstr, &fnum, sizeof(fnum));
break;
case 'd':
case 'D':
dnum = num;
tcxstrcat(xstr, &dnum, sizeof(dnum));
break;
case 'n':
snum = num;
snum = HTONS(snum);
tcxstrcat(xstr, &snum, sizeof(snum));
break;
case 'N':
inum = num;
inum = HTONL(inum);
tcxstrcat(xstr, &inum, sizeof(inum));
break;
case 'M':
lnum = num;
lnum = HTONLL(lnum);
tcxstrcat(xstr, &lnum, sizeof(lnum));
break;
case 'w':
case 'W':
wnum = num;
wp = wbuf;
if(wnum < (1ULL << 7)){
*(wp++) = wnum;
} else if(wnum < (1ULL << 14)){
*(wp++) = (wnum >> 7) | 0x80;
*(wp++) = wnum & 0x7f;
} else if(wnum < (1ULL << 21)){
*(wp++) = (wnum >> 14) | 0x80;
*(wp++) = ((wnum >> 7) & 0x7f) | 0x80;
*(wp++) = wnum & 0x7f;
} else if(wnum < (1ULL << 28)){
*(wp++) = (wnum >> 21) | 0x80;
*(wp++) = ((wnum >> 14) & 0x7f) | 0x80;
*(wp++) = ((wnum >> 7) & 0x7f) | 0x80;
*(wp++) = wnum & 0x7f;
} else if(wnum < (1ULL << 35)){
*(wp++) = (wnum >> 28) | 0x80;
*(wp++) = ((wnum >> 21) & 0x7f) | 0x80;
*(wp++) = ((wnum >> 14) & 0x7f) | 0x80;
*(wp++) = ((wnum >> 7) & 0x7f) | 0x80;
*(wp++) = wnum & 0x7f;
} else if(wnum < (1ULL << 42)){
*(wp++) = (wnum >> 35) | 0x80;
*(wp++) = ((wnum >> 28) & 0x7f) | 0x80;
*(wp++) = ((wnum >> 21) & 0x7f) | 0x80;
*(wp++) = ((wnum >> 14) & 0x7f) | 0x80;
*(wp++) = ((wnum >> 7) & 0x7f) | 0x80;
*(wp++) = wnum & 0x7f;
} else if(wnum < (1ULL << 49)){
*(wp++) = (wnum >> 42) | 0x80;
*(wp++) = ((wnum >> 35) & 0x7f) | 0x80;
*(wp++) = ((wnum >> 28) & 0x7f) | 0x80;
*(wp++) = ((wnum >> 21) & 0x7f) | 0x80;
*(wp++) = ((wnum >> 14) & 0x7f) | 0x80;
*(wp++) = ((wnum >> 7) & 0x7f) | 0x80;
*(wp++) = wnum & 0x7f;
} else if(wnum < (1ULL << 56)){
*(wp++) = (wnum >> 49) | 0x80;
*(wp++) = ((wnum >> 42) & 0x7f) | 0x80;
*(wp++) = ((wnum >> 35) & 0x7f) | 0x80;
*(wp++) = ((wnum >> 28) & 0x7f) | 0x80;
*(wp++) = ((wnum >> 21) & 0x7f) | 0x80;
*(wp++) = ((wnum >> 14) & 0x7f) | 0x80;
*(wp++) = ((wnum >> 7) & 0x7f) | 0x80;
*(wp++) = wnum & 0x7f;
} else {
*(wp++) = (wnum >> 63) | 0x80;
*(wp++) = ((wnum >> 49) & 0x7f) | 0x80;
*(wp++) = ((wnum >> 42) & 0x7f) | 0x80;
*(wp++) = ((wnum >> 35) & 0x7f) | 0x80;
*(wp++) = ((wnum >> 28) & 0x7f) | 0x80;
*(wp++) = ((wnum >> 21) & 0x7f) | 0x80;
*(wp++) = ((wnum >> 14) & 0x7f) | 0x80;
*(wp++) = ((wnum >> 7) & 0x7f) | 0x80;
*(wp++) = wnum & 0x7f;
}
tcxstrcat(xstr, wbuf, wp - wbuf);
break;
}
eidx++;
}
format++;
if(eidx > emax) break;
}
lua_settop(lua, 0);
lua_pushlstring(lua, tcxstrptr(xstr), tcxstrsize(xstr));
tcxstrdel(xstr);
return 1;
}
/* for unpack function */
static int util_unpack(lua_State *lua){
int argc = lua_gettop(lua);
if(argc != 2){
lua_pushstring(lua, "unpack: invalid arguments");
lua_error(lua);
}
const char *format = lua_tostring(lua, 1);
size_t size;
const char *buf = lua_tolstring(lua, 2, &size);
if(!format){
lua_pushstring(lua, "unpack: invalid arguments");
lua_error(lua);
}
if(!buf){
buf = "";
size = 0;
}
lua_newtable(lua);
const char *rp = buf;
int eidx = 1;
while(*format != '\0'){
int c = *format;
int loop = 1;
if(format[1] == '*'){
loop = INT_MAX;
format++;
} else if(format[1] >= '0' && format[1] <= '9'){
char *suffix;
loop = strtol(format + 1, &suffix, 10);
format = suffix - 1;
}
loop = tclmin(loop, size);
for(int i = 0; i < loop && size > 0; i++){
uint8_t cnum;
uint16_t snum;
uint32_t inum;
uint64_t lnum;
float fnum;
double dnum;
uint64_t wnum;
switch(c){
case 'c':
if(size >= sizeof(cnum)){
memcpy(&cnum, rp, sizeof(cnum));
lua_pushnumber(lua, (int8_t)cnum);
lua_rawseti(lua, 3, eidx++);
rp += sizeof(cnum);
size -= sizeof(cnum);
} else {
size = 0;
}
break;
case 'C':
if(size >= sizeof(cnum)){
memcpy(&cnum, rp, sizeof(cnum));
lua_pushnumber(lua, (uint8_t)cnum);
lua_rawseti(lua, 3, eidx++);
rp += sizeof(cnum);
size -= sizeof(cnum);
} else {
size = 0;
}
break;
case 's':
if(size >= sizeof(snum)){
memcpy(&snum, rp, sizeof(snum));
lua_pushnumber(lua, (int16_t)snum);
lua_rawseti(lua, 3, eidx++);
rp += sizeof(snum);
size -= sizeof(snum);
} else {
size = 0;
}
break;
case 'S':
if(size >= sizeof(snum)){
memcpy(&snum, rp, sizeof(snum));
lua_pushnumber(lua, (uint16_t)snum);
lua_rawseti(lua, 3, eidx++);
rp += sizeof(snum);
size -= sizeof(snum);
} else {
size = 0;
}
break;
case 'i':
if(size >= sizeof(inum)){
memcpy(&inum, rp, sizeof(inum));
lua_pushnumber(lua, (int32_t)inum);
lua_rawseti(lua, 3, eidx++);
rp += sizeof(inum);
size -= sizeof(inum);
} else {
size = 0;
}
break;
case 'I':
if(size >= sizeof(inum)){
memcpy(&inum, rp, sizeof(inum));
lua_pushnumber(lua, (uint32_t)inum);
lua_rawseti(lua, 3, eidx++);
rp += sizeof(inum);
size -= sizeof(inum);
} else {
size = 0;
}
break;
case 'l':
if(size >= sizeof(lnum)){
memcpy(&lnum, rp, sizeof(lnum));
lua_pushnumber(lua, (int64_t)lnum);
lua_rawseti(lua, 3, eidx++);
rp += sizeof(lnum);
size -= sizeof(lnum);
} else {
size = 0;
}
break;
case 'L':
if(size >= sizeof(lnum)){
memcpy(&lnum, rp, sizeof(lnum));
lua_pushnumber(lua, (uint64_t)lnum);
lua_rawseti(lua, 3, eidx++);
rp += sizeof(lnum);
size -= sizeof(lnum);
} else {
size = 0;
}
break;
case 'f':
case 'F':
if(size >= sizeof(fnum)){
memcpy(&fnum, rp, sizeof(fnum));
lua_pushnumber(lua, (float)fnum);
lua_rawseti(lua, 3, eidx++);
rp += sizeof(fnum);
size -= sizeof(fnum);
} else {
size = 0;
}
break;
case 'd':
case 'D':
if(size >= sizeof(dnum)){
memcpy(&dnum, rp, sizeof(dnum));
lua_pushnumber(lua, (double)dnum);
lua_rawseti(lua, 3, eidx++);
rp += sizeof(dnum);
size -= sizeof(dnum);
} else {
size = 0;
}
break;
case 'n':
if(size >= sizeof(snum)){
memcpy(&snum, rp, sizeof(snum));
snum = NTOHS(snum);
lua_pushnumber(lua, (uint16_t)snum);
lua_rawseti(lua, 3, eidx++);
rp += sizeof(snum);
size -= sizeof(snum);
} else {
size = 0;
}
break;
case 'N':
if(size >= sizeof(inum)){
memcpy(&inum, rp, sizeof(inum));
inum = NTOHL(inum);
lua_pushnumber(lua, (uint32_t)inum);
lua_rawseti(lua, 3, eidx++);
rp += sizeof(inum);
size -= sizeof(inum);
} else {
size = 0;
}
break;
case 'M':
if(size >= sizeof(lnum)){
memcpy(&lnum, rp, sizeof(lnum));
lnum = NTOHLL(lnum);
lua_pushnumber(lua, (uint64_t)lnum);
lua_rawseti(lua, 3, eidx++);
rp += sizeof(lnum);
size -= sizeof(lnum);
} else {
size = 0;
}
break;
case 'w':
case 'W':
wnum = 0;
do {
inum = *(unsigned char *)rp;
wnum = wnum * 0x80 + (inum & 0x7f);
rp++;
size--;
} while(inum >= 0x80 && size > 0);
lua_pushnumber(lua, (uint64_t)wnum);
lua_rawseti(lua, 3, eidx++);
break;
}
}
format++;
if(size < 1) break;
}
lua_replace(lua, 1);
lua_settop(lua, 1);
return 1;
}
/* for split function */
static int util_split(lua_State *lua){
int argc = lua_gettop(lua);
if(argc < 1){
lua_pushstring(lua, "split: invalid arguments");
lua_error(lua);
}
size_t isiz;
const char *ibuf = lua_tolstring(lua, 1, &isiz);
if(!ibuf){
lua_pushstring(lua, "split: invalid arguments");
lua_error(lua);
}
const char *delims = argc > 1 ? lua_tostring(lua, 2) : NULL;
lua_newtable(lua);
int lnum = 1;
if(delims){
const char *str = ibuf;
while(true){
const char *sp = str;
while(*str != '\0' && !strchr(delims, *str)){
str++;
}
lua_pushlstring(lua, sp, str - sp);
lua_rawseti(lua, -2, lnum++);
if(*str == '\0') break;
str++;
}
} else {
const char *ptr = ibuf;
int size = isiz;
while(size >= 0){
const char *rp = ptr;
const char *ep = ptr + size;
while(rp < ep){