-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilfile.bas
1844 lines (1636 loc) · 71.5 KB
/
utilfile.bas
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
' used for app launcher
#include once "crt/process.bi"
' dir function and provides constants to use for the attrib_mask parameter
#include once "vbcompat.bi"
#include once "dir.bi"
' disable filename globbing otherwise g:\* list files
' when using command how ever conflicts with dir()
' also odd this is used for 64bits but works with 32bits
' Extern _dowildcard Alias "_dowildcard" As Long
' Dim Shared _dowildcard As Long = 0
' setup log
dim shared logfile as string
dim shared logtype as string
dim shared appname as string
dim shared appfile as string
dim shared usecons as string
dim shared exeversion as string
dim shared taginfo(1 to 5) as string
' note command(0) can arbitraly add the path so strip it
appname = mid(command(0), instrrev(command(0), "\") + 1)
' without file extension
if instr(appname, ".exe") > 0 then
appname = left(appname, len(appname) - 4)
end if
' options logtype verbose, full
logtype = "verbose"
' options usecons true, false
usecons = "false"
' generic check for true or false
dim chk as boolean
' get version exe for log
declare function getfileversion(versinfo() as string, versdesc() as string) as integer
declare function replace(byref haystack as string, byref needle as string, byref substitute as string) as string
declare Function explode(haystack As String = "", delimiter as string, ordinance() As String) As UInteger
declare function getmp3baseinfo(fx1File as string) as boolean
dim as integer c, resp
dim as string versinfo(8)
dim as string versdesc(7) =>_
{"CompanyName",_
"FileDescription",_
"FileVersion",_
"InternalName",_
"LegalCopyright",_
"OriginalFilename",_
"ProductName",_
"ProductVersion"}
versinfo(8) = appname + ".exe"
resp = getfileversion(versinfo(),versdesc())
exeversion = replace(trim(versinfo(2)), ", ", ".")
' get metric os
dim shared os as string
os = "unknown"
#ifdef __FB_WIN32__
os = "windows"
#endif
#ifdef __FB_UNIX__
os = "unix"
#endif
' metric functions
' ______________________________________________________________________________'
' used for logging
' entrytypes: error, fatal, notice, warning, terminate
Function logentry(entrytype As String, logmsg As String) As Boolean
' validate logentry
If InStr(logmsg, "|") > 0 Then
logmsg = "entry contained delimeter -> | <-"
End If
' output to console
if usecons = "true" then
print time & " " + entrytype + " | " + logmsg
end if
' setup logfile
dim f as long
f = FreeFile
logfile = exepath + "\" + appname + ".log"
if FileExists(logfile) = false then
Open logfile For output As #f
print #f, format(now, "dd/mm/yyyy") + " - " + time + "|" + "notice" + "|" + appname + "|" + logfile + " created"
print #f, format(now, "dd/mm/yyyy") + " - " + time + "|" + "notice" + "|" + appname + "|" + "version " + exeversion
print #f, format(now, "dd/mm/yyyy") + " - " + time + "|" + "notice" + "|" + appname + "|" + "platform " + os
close #f
end if
if entrytype = "warning" and logtype = "verbose" then
exit function
end if
' write to logfile
Open logfile For append As #f
print #f, format(now, "dd/mm/yyyy") + " - " + time + "|" + entrytype + "|" + appname + "|" + logmsg
close #f
' normal termination or fatal error
select case entrytype
case "fatal"
print logmsg
end
case "terminate"
end
end select
return true
End function
' get fileversion executable or dll windows only
function getfileversion(versinfo() as string, versdesc() as string) as integer
dim as integer bytesread,c,dwHandle,res,verSize
dim as string buffer,ls,qs,tfn
dim as ushort ptr b1,b2
dim as ubyte ptr bptr
tfn=versinfo(8)
if dir(tfn)="" then return -1
verSize=GetFileVersionInfoSize(tfn,@dwHandle)
if verSize=0 then return -2
dim as any ptr verdat=callocate(verSize*2)
res=GetFileVersionInfo(strptr(tfn),dwHandle,verSize*2,verdat)
res=_
VerQueryValue(_
verdat,_
"\VarFileInfo\Translation",_
@bptr,_
@bytesread)
if bytesread=0 then deallocate(verdat):return -3
b1=cast(ushort ptr,bptr)
b2=cast(ushort ptr,bptr+2)
ls=hex(*b1,4)& hex(*b2,4)
for c=0 to 7
qs="\StringFileInfo\" & ls & "\" & versdesc(c)
res=_
VerQueryValue(_
verdat,_
strptr(qs),_
@bptr,_
@bytesread)
if bytesread>0 then
buffer=space(bytesread)
CopyMemory(strptr(buffer),bptr,bytesread)
versinfo(c)=buffer
else
versinfo(c)="N/A"
end if
next c
deallocate(verdat)
return 1
end function
' process timer
type execduration
as double beginexectime
as double endexectime
end type
dim exectimer as execduration
function exectime(exectimer as execduration, state as string) as string
select case state
case "set"
exectimer.beginexectime = timer
return "true"
case "stop"
exectimer.endexectime = timer - exectimer.beginexectime
return format((exectimer.endexectime) * 1000, "00:000") + " (ms)"
end select
end function
' generic file functions
' ______________________________________________________________________________'
' list files in folder
function getfilesfromfolder (filespec As String) as boolean
Dim As String filename = Dir(filespec, 1)
if len(filename) = 0 then print "path not found..." end if
Do While Len(filename) > 0
filename = Dir()
Loop
return true
end function
' list folders
function getfolders (filespec As String) as boolean
Dim As String filename = Dir(filespec, fbDirectory)
if len(filename) = 0 then print "path not found..." end if
Do While Len(filename) > 0
filename = Dir()
Loop
return true
end function
' create a new file
Function newfile(filename As String) As boolean
Dim f As long
if FileExists(filename) then
logentry("fatal", "creating " + filename + " file excists")
return false
end if
f = FreeFile
Open filename For output As #f
logentry("notice", filename + " created")
close(f)
return true
End Function
' create a temp file
Function tmp2file(filename As String) As boolean
Dim f As long
if FileExists(filename) = true then
If Kill(filename) <> 0 Then
logentry("warning", "could not delete " + filename )
end if
end if
f = FreeFile
Open filename For output As #f
logentry("notice", filename + " created")
close(f)
return true
End Function
' append to an excisiting file
Function appendfile(filename As String, msg as string) As boolean
Dim f As long
if FileExists(filename) = false then
logentry("fatal", "appending " + filename + " file does not excist")
return false
end if
f = FreeFile
Open filename For append As #f
print #f, msg
close(f)
return true
End Function
' read a file
Function readfromfile(filename As String) As long
Dim f As long
if FileExists(filename) = false then
logentry("fatal", "reading " + filename + " file does not excist")
end if
f = FreeFile
Open filename For input As #f
return f
End Function
' delete a file
Function delfile(filename As String) As boolean
if FileExists(filename) = true then
If Kill(filename) <> 0 Then
logentry("warning", "could not delete " + filename)
return false
end if
end if
return true
End Function
' check path
Function checkpath(chkpath As String) As boolean
dim dummy as string
dummy = curdir
if chdir(chkpath) <> 0 then
logentry("warning", "path " + chkpath + " not found")
chdir(dummy)
return false
end if
chdir(dummy)
return true
End Function
' based on recursive dir code of coderjeff https://www.freebasic.net/forum/viewtopic.php?t=5758
function createlist(folder as string, filterext as string, listname as string) as integer
' setup filelist
dim chk as boolean
redim path(1 to 1) As string
dim as integer i = 1, n = 1, attrib
dim as long f, g
dim file as string
dim fileext as string
dim maxfiles as integer
f = freefile
dim filelist as string = exepath + "\" + listname + ".tmp"
open filelist for output as #f
g = freefile
dim filelistb as string = exepath + "\" + listname + ".lst"
open filelistb for output as #g
#ifdef __FB_LINUX__
const pathchar = "/"
#else
const pathchar = "\"
#endif
' read dir recursive starting directory
path(1) = folder
if( right(path(1), 1) <> pathchar) then
file = dir(path(1), fbNormal or fbDirectory, @attrib)
if( attrib and fbDirectory ) then
path(1) += pathchar
end if
end if
while i <= n
file = dir(path(i) + "*" , fbNormal or fbDirectory, @attrib)
while file > ""
if (attrib and fbDirectory) then
if file <> "." and file <> ".." then
' todo evaluate limit recursive if starting folder is root
if len(path(1)) > 3 then
n += 1
redim preserve path(1 to n)
path(n) = path(i) + file + pathchar
else
logentry("error", "scanning from root dir not supported! " + path(i))
end if
end if
else
fileext = lcase(mid(file, instrrev(file, ".")))
if instr(1, filterext, fileext) > 0 and len(fileext) > 3 then
print #f, path(i) & file
print #g, path(i) & file
maxfiles += 1
else
logentry("warning", "file format not supported - " + path(i) & file)
end if
end if
file = dir(@attrib)
wend
i += 1
wend
close(f)
close(g)
' chk if filelist is created
if FileExists(filelist) = false then
logentry("warning", "could not create filelist: " + filelist)
end if
return maxfiles
end function
' localization file functions
' ______________________________________________________________________________'
' localization can be applied by getting a locale or other method
dim locale as string = "en"
sub displayhelp(locale as string)
dim dummy as string
dim f as long
f = freefile
' get text
if FileExists(exepath + "\conf\" + locale + "\help.ini") then
'nop
else
logentry("error", "open " + exepath + "\conf\" + locale + "\help.ini" + " file does not excist")
locale = "en"
end if
Open exepath + "\conf\" + locale + "\help.ini" For input As #f
Do Until EOF(f)
Line Input #f, dummy
print wstr(dummy)
Loop
close f
end sub
' setup ui labels aka data spindel
type tbrec
as string fieldname(any)
as string fieldvalue(any)
end type
dim shared record as tbrec
common shared recnr as integer
recnr = 0
' get key value pair cheap localization via ini file
Function readuilabel(filename as string) as boolean
dim itm as string
dim inikey as string
dim inival as string
dim f as long
if FileExists(filename) = false then
logentry("error", filename + " does not excist switching to default language")
filename = exepath + "\conf\en\menu.ini"
end if
f = readfromfile(filename)
Do Until EOF(f)
Line Input #f, itm
if instr(1, itm, "=") > 1 then
inikey = trim(mid(itm, 1, instr(1, itm, "=") - 2))
inival = trim(mid(itm, instr(1, itm, "=") + 2, len(itm)))
if inival = "" then
logentry("error", inikey + " has empty value in " + filename)
inival = "null"
end if
'print inikey + " - " + inival
recnr += 1
redim preserve record.fieldname(0 to recnr)
redim preserve record.fieldvalue(0 to recnr)
record.fieldname(recnr) = inikey
record.fieldvalue(recnr) = inival
end if
loop
close f
return true
end function
' display ui lable with unicode and semi automatic spacing via offset
function getuilabelvalue(needle as string, suffix as string = "", offset as integer = 10) as boolean
dim fieldname as string = ""
dim fieldvalue as string = ""
for i as integer = 0 to recnr
with record
if record.fieldname(i) = needle then
fieldname = record.fieldname(i)
fieldvalue = record.fieldvalue(i)
end if
end with
next i
if fieldname = "" or fieldvalue = "" then
print needle + " not found or empty " + suffix
return false
else
print wstr(fieldvalue + space(offset - Len(fieldvalue)) + suffix)
return true
end if
end function
' file type specific functions
' ______________________________________________________________________________'
Dim Shared As String B64
B64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" & _
"abcdefghijklmnopqrstuvwxyz" & _
"0123456789+/"
#define E0(v1) v1 Shr 2
#define E1(v1, v2) ((v1 And 3) Shl 4) + (v2 Shr 4)
#define E2(v2, v3) ((v2 And &H0F) Shl 2) + (v3 Shr 6)
#define E3(v3) (v3 And &H3F)
' via https://rosettacode.org/wiki/Base64_encode_data?section=1#FreeBASIC
Function base64encode(S As String) As String
Dim As Integer j, k, l = Len(S)
Dim As String mE
If l = 0 Then Return mE
mE = String(((l+2)\3)*4,"=")
For j = 0 To l - ((l Mod 3)+1) Step 3
mE[k+0]=B64[e0(S[j+0])]
mE[k+1]=B64[e1(S[j+0],S[j+1])]
mE[k+2]=B64[e2(S[j+1],S[j+2])]
mE[k+3]=B64[e3(S[j+2])]:k+=4
Next j
If (l Mod 3) = 2 Then
mE[k+0]=B64[e0(S[j+0])]
mE[k+1]=B64[e1(S[j+0],S[j+1])]
mE[k+2]=B64[e2(S[j+1],S[j+2])]
mE[k+3]=61
Elseif (l Mod 3) = 1 Then
mE[k+0]=B64[e0(S[j+0])]
mE[k+1]=B64[e1(S[j+0],S[j+1])]
mE[k+2]=61
mE[k+3]=61
End If
Return mE
End Function
Function MIMEDecode(s As String ) As Integer
If Len(s) Then
MIMEdecode = Instr(B64,s) - 1
Else
MIMEdecode = -1
End If
End Function
' via https://rosettacode.org/wiki/Base64_decode_data#FreeBASIC
Function base64decode(s As String) As String
Dim As Integer w1, w2, w3, w4
Dim As String mD
For n As Integer = 1 To Len(s) Step 4
w1 = MIMEdecode(Mid(s,n+0,1))
w2 = MIMEdecode(Mid(s,n+1,1))
w3 = MIMEdecode(Mid(s,n+2,1))
w4 = MIMEdecode(Mid(s,n+3,1))
If w2 >-1 Then mD+= Chr(((w1* 4 + Int(w2/16)) And 255))
If w3 >-1 Then mD+= Chr(((w2*16 + Int(w3/ 4)) And 255))
If w4 >-1 Then mD+= Chr(((w3*64 + w4 ) And 255))
Next n
Return mD
End Function
' cheap csv to sql export
Function csv2sql(filename as string, tbname as string = "") As boolean
Dim f As long
Dim cnt As integer = 0
Dim fieldnr As integer = 0
dim chk as boolean = false
dim dbchk as boolean = false
Dim text As String
dim dummy as string = ""
' used for "... , ..." structure
dim i as integer = 1
dim b as integer = 0
dim e as integer = 0
dim p as string = ""
dim temp as string
' filter out ext
tbname = left(filename, instrrev(filename, ".") - 1)
' filter out preceding path if present
tbname = lcase(mid(tbname, instrrev(tbname, "\") + 1))
if FileExists(filename) = false then
logentry("fatal", "file not found or missing..'" & filename & "'")
end if
f = FreeFile
Open filename For input As #f
print "begin transaction;"
print "create table if not exists '" + tbname + "' ("
Do Until EOF( f )
Line Input #f, text
' create table defintion
if cnt = 0 then
ReDim As String ordinance(0)
' use strict cleaining for now
text = replace(text, "'", "")
text = replace(text, chr$(34), "")
explode(text, ",", ordinance())
For x As Integer = 1 To UBound(ordinance)
if x <> UBound(ordinance) then
Print "'" + lcase(trim(ordinance(x))) + "'" + space(20 - len(trim(ordinance(x)))) + "text" + ","
else
Print "'" + lcase(trim(ordinance(x))) + "'" + space(20 - len(trim(ordinance(x)))) + "text"
end if
Next
fieldnr = UBound(ordinance)
print ");"
else
' create inserts
dummy = ""
ReDim As String ordinance(0)
' work around data "... , ..." structure still if-y...
if instr(text, chr$(34)) > 0 then
temp = text
do
p = mid(text,i,1)
if p = chr$(34) and b > 0 and e = 0 then
e = i
dummy = mid(text, b, e - (b - 1))
dummy = replace(dummy, ",", "|\|")
temp = replace(temp, mid(text, b, e - (b - 1)) , dummy)
dummy = ""
b = 0
p = ""
end if
if p = chr$(34) and b = 0 then
b = i
e = 0
p = ""
end if
i += 1
loop until i > len(text)
text = temp
' reset workaround
b = 0 : e = 0 : i = 0 : p = "" : dummy = ""
end if
' remove trailing comma at end of record
if mid(text, len(text) - 1) = chr$(34) + "," then
text = mid(text, 1, len(text) - 1)
end if
explode(text, ",", ordinance())
if UBound(ordinance) <> fieldnr then
logentry("fatal", "error unequal amount of fields at line " & cnt + 1 & " " + text)
end if
For x As Integer = 1 To UBound(ordinance)
' restore data "... , ..." structure
ordinance(x) = replace(trim(ordinance(x)), chr$(34), "")
ordinance(x) = replace(trim(ordinance(x)), "|\|", ",")
' check validty data
if instr(trim(ordinance(x)), "'") > 0 then
logentry("warning", "found unescaped ' modified with '' at line " & cnt + 1 & " " + text)
end if
if x <> UBound(ordinance) then
dummy += "'" + replace(trim(ordinance(x)), "'", "''") + "',"
else
dummy += "'" + replace(trim(ordinance(x)), "'", "''") + "'"
end if
Next
Print "insert into '" + tbname + "' values (" + dummy + ");"
end if
cnt += 1
Loop
print "commit;"
close(f)
logentry("notice", "exported csv " + filename + " to sql with tablename " + tbname + " #recs " & cnt)
return true
end function
' cheap json to sql export
Function json2sql(filename as string, tbname as string = "") As boolean
Dim f As long
dim g as long
Dim cnt As integer = 0
Dim fieldnr As integer = 0
dim chk as boolean = false
dim dbchk as boolean = false
Dim text As String
dim dummy as string = ""
' filter out ext
tbname = left(filename, instrrev(filename, ".") - 1)
' filter out preceding path if present
tbname = lcase(mid(tbname, instrrev(tbname, "\") + 1))
if FileExists(filename) = false then
logentry("fatal", "file not found or missing..'" & filename & "'")
end if
f = FreeFile
Open filename For input As #f
' handle one liner json
Do Until EOF( f )
Line Input #f, text
cnt +=1
loop
if cnt = 1 then
g = freefile
open exepath + "\temp.json" for output as #g
text = replace(text, "[", "[" + chr$(13) + chr$(10))
text = replace(text, "},", "}," + chr$(13) + chr$(10))
text = replace(text, "]", chr$(13) + chr$(10) + "]" + chr$(13) + chr$(10))
print #g, text
close(g)
logentry ("notice", "converted one line json " + filename)
filename = exepath + "\temp.json"
end if
close(f)
f = FreeFile
Open filename For input As #f
cnt = 0
print "begin transaction;"
print "create table if not exists '" + tbname + "' ("
' create table defintion
Do Until EOF( f )
Line Input #f, text
' replace null values json
if instr(text, chr$(34) + ":null") > 0 then
text = replace(text, chr$(34) + ":null", chr$(34) + ":" + chr$(34) + "null" + chr$(34))
logentry ("warning", "replaced null value" + text)
end if
if cnt = 1 then
ReDim As String ordinance(0)
explode(text, chr$(34) + "," + chr$(34), ordinance())
For x As Integer = 1 To UBound(ordinance)
dummy = lcase(mid(trim(ordinance(x)), 3, instr(trim(ordinance(x)), chr$(34) + ":" + chr$(34)) - 3))
if x <> UBound(ordinance) then
Print "'" + dummy + "'" + space(20 - len(dummy)) + "text" + ","
else
Print "'" + dummy + "'" + space(20 - len(dummy)) + "text"
end if
Next
fieldnr = UBound(ordinance)
print ");"
end if
cnt += 1
loop
close(f)
' create inserts
f = FreeFile
Open filename For input As #f
cnt = 0
Do Until EOF( f )
Line Input #f, text
' replace null values json
if instr(text, chr$(34) + ":null") > 0 then
text = replace(text, chr$(34) + ":null", chr$(34) + ":" + chr$(34) + "null" + chr$(34))
logentry ("warning", "replaced null value" + text)
end if
if cnt > 0 then
dummy = "'"
ReDim As String ordinance(0)
explode(text, chr$(34) + "," + chr$(34), ordinance())
' validate data
if UBound(ordinance) <> fieldnr then
logentry ("warning", "number of field(s) and value(s) do not match " + text)
end if
For x As Integer = 1 To UBound(ordinance)
' unescape double quote if needed
ordinance(x) = replace(ordinance(x), "\" + chr$(34), chr$(34))
ordinance(x) = replace(ordinance(x), "'", "''")
dummy += mid(trim(ordinance(x)), instr(trim(ordinance(x)), chr$(34) + ":" + chr$(34)) + 3)
if x <> UBound(ordinance) then
dummy += "','"
else
if instrrev(dummy, "},") > 0 then
dummy = mid(dummy, 1, len(dummy) - 3) + "'"
else
dummy = mid(dummy, 1, len(dummy) - 2) + "'"
END IF
end if
Next
IF dummy <> "''" then
' work around te restore null value
' todo find better solution see listjson
dummy = replace(dummy, "'null'", "null")
Print "insert into '" + tbname + "' values (" + dummy + ");"
end if
end if
cnt += 1
Loop
print "commit;"
close(f)
delfile(exepath + "\temp.json")
logentry("notice", "exported json " + filename + " to sql with tablename " + tbname + " #recs " & cnt)
return true
end function
' cheap xml to sql export
Function xml2sql(filename as string, tbname as string = "", element as string = "") As boolean
Dim f As long
Dim cnt As integer = 0
dim chk as boolean = false
dim dbchk as boolean = false
dim tbchk as boolean = false
Dim text As String
dim fname as string
dim fvalue as string
dim dummy as string = ""
dim dummy2 as string = ""
dim dbname as string = ""
' filter out ext
tbname = left(filename, instrrev(filename, ".") - 1)
' filter out preceding path if present
tbname = lcase(mid(tbname, instrrev(tbname, "\") + 1))
if FileExists(filename) = false then
logentry("fatal", "file not found or missing..'" & filename & "'")
end if
f = FreeFile
Open filename For input As #f
print "begin transaction;"
'print "create table if not exists '" + tbname + "' ("
Do Until EOF( f )
Line Input #f, text
select case true
' get node meta, version etc
case instr(text, "<?") > 0
' print "meta ";text
' get node dbname
case instr(text, "<") > 0 and instr(text, "/") = 0 and dbchk = false
if instr(text, " ") > 0 then
dbname = mid(text, 2, instr(trim(text), " ") - 2)
else
dbname = mid(trim(text), 2, len(trim(text)) - 2)
end if
dbchk = true
' get node tbname or record
case instr(text, ">") > 0 and instr(text, "/") = 0 and dbchk
if instr(trim(text), " ") > 0 then
tbname = mid(text, 2, instr(trim(text), " ") - 2)
else
tbname = mid(trim(text), 2, len(trim(text)) - 2)
end if
dummy += "insert into '" + tbname + "' values ('"
cnt += 1
' get nodes field and value
' todo cleanup loops too much truncation is needed
case else
if element = "" then
if instr(text, "</" + tbname + ">") = 0 and text <> "</" + dbname + ">" then
fname = mid(text, instr(text, "<") + 1, instr(trim(text), ">") - 2)
' create table defintion
if tbchk = false then
dummy2 += "'" + lcase(fname) + "'" + space(20 - len(fname)) + tbname + "," + chr(13) + chr$(10)
end if
' create inserts
fvalue = mid(text, instr(text, ">") + 1, instr(trim(text), "</") - (len(fname) + 3))
if fvalue = "" then fvalue = "null" end if
' reverse xml sanitazion
fvalue = replace(fvalue, " & ", " & ")
fvalue = replace(fvalue, ">", ">")
fvalue = replace(fvalue, "<", "<")
fvalue = replace(fvalue, "'", "''")
dummy += fvalue + "','"
else
' work around te restore null value
' todo find better solution see listjson
dummy = replace(dummy, "'null'", "null")
dummy = mid(dummy, 1, len(dummy) - 2) + ");" + chr(13) + chr$(10)
tbchk = true
end if
end if
end select
Loop
close(f)
dummy2 = "create table if not exists '" + tbname + "' (" + chr#(13) + chr$(10) + dummy2
print mid(dummy2, 1, len(dummy2) - 3) + chr(13) + chr$(10) + ");"
print mid(dummy, 1, len(dummy) - 4)
print "commit;"
if element <> "" then
if chk = false then
logentry("warning", filename + " searching for '" + element + "' element not found")
else
logentry("notice", filename + " searching for '" + element + "' element found " & cnt & " recs")
end if
else
logentry("notice", filename + " found " & cnt & " recs")
end if
logentry("notice", "exported xml " + filename + " to sql with tablename " + tbname + " #recs " & cnt)
return true
end function
' export folder filespec to supported file types
' current csv, json, html, sql and xml
' based on recursive dir code of coderjeff https://www.freebasic.net/forum/viewtopic.php?t=5758
function dir2file(folder as string, filterext as string, listtype as string = "sql", htmloutput as string = "default") as integer
' setup filelist
dim as integer i = 1, j=1, n = 1, attrib, itemnr, maxfiles
dim as long tmp, f
dim dummy as string
dim dummy2 as string
dim file as string
dim fileext as string
dim fsize as long
dim fdate as string
dim fattr as string
dim argc(0 to 5) as string
dim argv(0 to 5) as string
redim path(1 to 1) As string
f = freefile
#ifdef __FB_LINUX__
const pathchar = "/"
#else
const pathchar = "\"
#endif
select case listtype
case "html"
' get template for body, css, and javacript
tmp = readfromfile(exepath + "\templates\head.html")
Do Until EOF(tmp)
Line Input #tmp, dummy
print dummy
itemnr += 1
Loop
close(tmp)
dummy = ""
if instr(filterext, ".mp3") > 0 and htmloutput = "exif" then
' table header todo needs to be refactored and compressed
print "<table class='sortable' id='datatable'>"
print " <thead><tr>"
print " <th width=20px;>"
print " <div class='trdropdown'><button class='trdropbtn'></button><div class='trdropdown-content'>"
print " <a href='' onclick=" + chr$(34) + "localStorage.setItem('tdelement', '1')" + chr$(34) + ";>artist</a>"
print " <a href='' onclick=" + chr$(34) + "localStorage.setItem('tdelement', '2')" + chr$(34) + ";>title</a>"
print " <a href='' onclick=" + chr$(34) + "localStorage.setItem('tdelement', '3')" + chr$(34) + ";>album</a>"
print " <a href='' onclick=" + chr$(34) + "localStorage.setItem('tdelement', '4')" + chr$(34) + ";>genre</a>"
print " <a href='' onclick=" + chr$(34) + "localStorage.setItem('tdelement', '5')" + chr$(34) + ";>year</a>"
print " </div></div>"
print " </th>"
print " <th>artist</th>"
print " <th>title</th>"
print " <th>album</th>"
print " <th>genre</th>"
print " <th>year</th>"
print " </tr></thead>"
else
if (instr(filterext, ".jpg") > 0 or instr(filterext, ".png") > 0) and htmloutput = "exif" then
' table header
print "<table class='sortable' id='datatable'>"
print " <thead><tr>"
print " <th width=20px;>"
print " <div class='trdropdown'><button class='trdropbtn'></button><div class='trdropdown-content'>"
print " <a href='' onclick=" + chr$(34) + "localStorage.setItem('tdelement', '1')" + chr$(34) + ";>filename</a>"
print " <a href='' onclick=" + chr$(34) + "localStorage.setItem('tdelement', '2')" + chr$(34) + ";>coverwidth</a>"
print " <a href='' onclick=" + chr$(34) + "localStorage.setItem('tdelement', '3')" + chr$(34) + ";>coverheight</a>"
print " <a href='' onclick=" + chr$(34) + "localStorage.setItem('tdelement', '4')" + chr$(34) + ";>orientation</a>"
print " <a href='' onclick=" + chr$(34) + "localStorage.setItem('tdelement', '5')" + chr$(34) + ";>filesize</a>"
print " <a href='' onclick=" + chr$(34) + "localStorage.setItem('tdelement', '5')" + chr$(34) + ";>thumbnail</a>"
print " </div></div>"
print " </th>"
print " <th>filename</th>"
print " <th>coverwidth</th>"
print " <th>coverheight</th>"
print " <th>orientation</th>"
print " <th>filesize</th>"
print " <th>thumbnail</th>"
print " </tr></thead>"
else
' table header todo drop down filter needs to alter javascript tdelement ui name
print "<table class='sortable' id='datatable'>"
print " <thead><tr>"
print " <th width=20px;>"
print " <div class='trdropdown'><button class='trdropbtn'></button><div class='trdropdown-content'>"
print " <a href='' onclick=" + chr$(34) + "localStorage.setItem('tdelement', '1')" + chr$(34) + ";>path</a>"
print " <a href='' onclick=" + chr$(34) + "localStorage.setItem('tdelement', '2')" + chr$(34) + ";>name</a>"
print " <a href='' onclick=" + chr$(34) + "localStorage.setItem('tdelement', '3')" + chr$(34) + ";>ext</a>"
print " <a href='' onclick=" + chr$(34) + "localStorage.setItem('tdelement', '4')" + chr$(34) + ";>size</a>"
print " <a href='' onclick=" + chr$(34) + "localStorage.setItem('tdelement', '5')" + chr$(34) + ";>date</a>"
print " </div></div>"
print " </th>"
print " <th>path</th>"
print " <th>name</th>"
print " <th>ext</th>"
print " <th>size</th>"
print " <th>date</th>"
print " <th>attr</th>"
print " </tr></thead>"
end if
end if
end select
' read dir recursive starting directory
path(1) = folder
if( right(path(1), 1) <> pathchar) then
file = dir(path(1), fbNormal or fbDirectory, @attrib)
if( attrib and fbDirectory ) then
path(1) += pathchar
end if
end if
while i <= n
file = dir(path(i) + "*" , fbNormal or fbDirectory, @attrib)
while file > ""
if (attrib and fbDirectory) then
if file <> "." and file <> ".." then
n += 1
redim preserve path(1 to n)
path(n) = path(i) + file + pathchar
end if
else
fileext = lcase(mid(file, instrrev(file, ".")))
if instr(1, filterext, fileext) > 0 and len(fileext) > 3 then
' get specific file information
fsize = filelen(path(i) + file)
fdate = Format(FileDateTime(path(i) + file), "yyyy-mm-dd hh:mm:ss" )
If (attrib And fbReadOnly) <> 0 Then fattr = "read-only"
If (attrib And fbHidden ) <> 0 Then fattr = "hidden"
If (attrib And fbSystem ) <> 0 Then fattr = "system"
If (attrib And fbArchive ) <> 0 Then fattr = "archived"
select case listtype
case "csv", "xml", "json", "sql"
if instr(filterext, ".mp3") > 0 and htmloutput = "exif" then
' path(i) folder and drive
getmp3baseinfo(path(i) + file)