-
Notifications
You must be signed in to change notification settings - Fork 1
/
utilfile.bas
1563 lines (1388 loc) · 61.1 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 integer
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 integer
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 integer
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 integer
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 integer
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
' 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 integer
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 integer
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
' ______________________________________________________________________________'
' cheap csv to sql export
Function csv2sql(filename as string, tbname as string = "") As boolean
Dim f As integer
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
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)
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)
explode(text, ",", ordinance())
if UBound(ordinance) <> fieldnr then
if instr(text, chr$(34)) > 0 then
' nop could be "..., ..." construct
else
logentry("fatal", "error unequal amount of fields at line " & cnt + 1 & " " + text)
end if
end if
For x As Integer = 1 To UBound(ordinance)
' check validty data "... , ..." structure
' todo improve very funky code
if instr(ordinance(x), chr$(34)) > 0 then
dim dummy2 as integer = 0
for y as integer = x to UBound(ordinance)
' check validty data
if instr(trim(ordinance(x)), "'") > 0 then
logentry("warning", "found unescaped ' modified with '' at line " & cnt + 1 & " " + text)
end if
if instr(ordinance(y), chr$(34)) > 0 then
dummy2 = y
exit for
end if
next
if x + 1 <> UBound(ordinance) then
dummy += ordinance(x) + "," + ordinance(x + 1) + ","
else
dummy += ordinance(x) + "," + ordinance(x + 1)
END IF
x += 1
if dummy2 > 3 then
if instr(len(ordinance(dummy2)), ordinance(dummy2), chr$(34)) > 0 then
dummy = mid(dummy, 1, len(dummy) - len(ordinance(dummy2+1)) -1) + "'" + ordinance(dummy2+1) + "',"
end if
end if
logentry("warning", "found 'double quote comma double quote' in field concatenated two fields at line " & cnt + 1 & " " + text)
else
' 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
end if
Next
Print "insert into '" + tbname + "' values (" + dummy + ");"
end if
cnt += 1
Loop
print "commit;"
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 integer
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
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)) + tbname + ","
else
Print "'" + dummy + "'" + space(20 - len(dummy)) + tbname
end if
Next
fieldnr = UBound(ordinance)
print ");"
end if
cnt += 1
loop
close(f)
' create inserts
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))
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;"
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 integer
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 = ""
' 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
' print "dbname ";mid(text, 2, instr(trim(text), " ") - 2)
else
' print "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 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
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
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, 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 = "extra" 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) + ";>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
' 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 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 = "extra" then
' path(i) folder and drive
getmp3baseinfo(path(i) + file)
argc(0) = "artist"
argc(1) = "title"
argc(2) = "album"
argc(3) = "year"
argc(4) = "genre"
argc(5) = "nop"
argv(0) = taginfo(1)
argv(1) = taginfo(2)
argv(2) = taginfo(3)
argv(3) = taginfo(4)
argv(4) = taginfo(5)
argv(5) = "nop"
else
argc(0) = "path"
argc(1) = "file"
argc(2) = "fileext"
argc(3) = "fsize"
argc(4) = "fdate"
argc(5) = "fattr"
argv(0) = path(i)
argv(1) = file
argv(2) = fileext
argv(3) = str(fsize)
argv(4) = fdate
argv(5) = fattr
end if
For j As Integer = 0 To 5
redim preserve record.fieldname(0 to recnr + 5)
redim preserve record.fieldvalue(0 to recnr + 5)
record.fieldname(recnr) = argc(j)
record.fieldvalue(recnr) = argv(j)
recnr += 1
Next j
case "html"
' create html5 audioplayer
if instr(filterext, ".mp3") > 0 and htmloutput = "extra" then
' path(i) folder and drive
getmp3baseinfo(path(i) + file)
print "<tr class='trlight' onclick=" + chr$(34) + "audioplay('file://" + replace(path(i), "\", "/") + file + "', this);" + chr$(34) + ">" + _
"<td><div class='audiobutton'></div></td>" _
+ "<td>" + taginfo(1) + "</td>" _
+ "<td>" + taginfo(2) + "</td>" _
+ "<td>" + taginfo(3) + "</td>" _
+ "<td>" + taginfo(5) + "</td>" _
+ "<td>" + taginfo(4) + "</td>" _
+ "</tr>"
' print ".. adding " + taginfo(1) + " - " + taginfo(2)
else
if instr(filterext, ".jpg") > 0 and htmloutput = "extra" then
print " <tr onclick=" + chr$(34) + "document.getElementById('myModal').style.display='block'; currentDiv(" _
& maxfiles + 1 & ");" + chr$(34) + ">"
print " <td><img class=" + chr$(34) + "tdthumb" + chr$(34) + " src=" + chr$(34) _
+ "file://" + replace(path(i), "\", "/") + file + chr$(34) + "></td>"
else
print " <tr class='trlight'>"
print " <td></td>"
end if
print " <td>" + path(i) + "</td>"
print " <td>" + file + "</td>"
print " <td>" + fileext + "</td>"
print " <td>" & fsize & "</td>"
print " <td>" + fdate + "</td>"
print " <td>" + fattr + "</td>"
print " </tr>"
end if
' create imageviewer A
if instr(filterext, ".jpg") > 0 and htmloutput = "extra" then
dummy += " <div class=" + chr$(34) + "w3-display-container mySlides" + chr$(34) _
+ ">" + chr$(13) + chr$(10)
dummy += " <img class=" + chr$(34) + "w3-animate-left ovimage" + chr$(34) + " src=" + chr$(34) _
+ "file://" + replace(path(i), "\", "/") + file + chr$(34) + ">" + chr$(13) + chr$(10)
dummy += " <div class=" + chr$(34) + "w3-display-bottomleft-stretch w3-container w3-padding-8 w3-black" _
+ chr$(34) + ">" + chr$(13) + chr$(10)
dummy += " " + replace(replace(file, "_", " "), ".jpg", "") + chr$(13) + chr$(10)
dummy += " </div>" + chr$(13) + chr$(10)
dummy += " </div>" + chr$(13) + chr$(10)
dummy2 += " <img class=" + chr$(34) + "demo w3-opacity w3-hover-opacity-off ovthumb" + chr$(34) _
+ " src=" + chr$(34) + "file://" + replace(path(i), "\", "/") + file + chr$(34) _
+ " onclick=" + chr$(34) + "currentDiv(" & maxfiles + 1 & ")" + chr$(34) + ">" + chr$(13) + chr$(10)
end if
end select
maxfiles += 1
else
'logentry("warning", "file format not supported - " + path(i) & file)
end if
end if
file = dir(@attrib)
wend
i += 1
wend
select case listtype
case "html"
' table footer
print "</table>"
' create imageviewer B
if instr(filterext, ".jpg") > 0 then
print "</div>"
print "<!-- overlay for image navigation -->
print "<div id=" + chr$(34) + "myModal" + chr$(34) + " class=" + chr$(34) + "modal" + chr$(34) + ">"
print " <span class='playslide'>"
print " <a href='templates/slide.html' style='text-decoration: none;' target='_blank'>"
print " <svg class='svglight' viewBox='0 0 32 3' height='16px' width='16px' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink'>"
print " <path d='M1,14c0,0.547,0.461,1,1,1c0.336,0,0.672-0.227,1-0.375L14.258,9C14.531,8.867,15,8.594,15,8s-0.469-0.867-0.742-1L3,1.375 C2.672,1.227,2.336,1,2,1C1.461,1,1,1.453,1,2V14z'/>"
print " </svg>"
print " </a> "
print " </span>"
print " <span class=" + chr$(34) + "close" + chr$(34) + ">×</span>"
print " <p id=" + chr$(34) + "time" + chr$(34) + "></p>"
print " <p id=" + chr$(34) + "date" + chr$(34) + "></p>"
print " <div class=" + chr$(34) + "w3-content w3-display-container" + chr$(34) + ">"
print dummy
print " <!-- image navigation left and right -->"
print " <div class=" + chr$(34) + "w3-text-white w3-display-middle" + chr$(34) _
+ " style=" + chr$(34) + "top:350px;width:90%" + chr$(34) + ">"
print " <div class=" + chr$(34) + "w3-left w3-hover-text-khaki" + chr$(34) _
+ " onclick=" + chr$(34) + "plusDivs(-1)" + chr$(34) + ">❮</div>"
print " <div class=" + chr$(34) + "w3-right w3-hover-text-khaki" + chr$(34) _
+ " onclick=" + chr$(34) + "plusDivs(1)" + chr$(34) + ">❯</div>"
print " </div>"
print " <!-- <div class=" + chr$(34) + "w3-row-padding w3-section" + chr$(34) + "> -->"
print " <div class=" + chr$(34) + "ovthumbbox" + chr$(34) + ">"
print mid(dummy2, 1, len(dummy2) - 2)
print " </div>"
print " </div>"
dummy = ""
dummy2 = ""
end if
' get template for footer, css, and javacript
tmp = readfromfile(exepath + "\templates\footer.html")
Do Until EOF(tmp)
Line Input #tmp, dummy
print dummy
itemnr += 1
Loop
close(tmp)
close(f)
end select
' todo find out why last record is empty
recnr = recnr - 1
logentry("notice", "found in " + folder + " with filespec " + filterext + " " & maxfiles & " files")
return maxfiles
end function
' cheap ini file searcher
Function readinikeyvalue( filename as string, section as string, inikey as string ) as boolean
if FileExists(filename) = false then
logentry("error", "reading " + filename + " file does not excist")
end if
Dim f As integer
Dim text As String
f = FreeFile
Open filename For input As #f
logentry("notice", filename + " searching" + " with section " + section + " for key " + inikey)
Do Until EOF(f)
Line Input #f, text
' check if section is found in the current line
If LCase( text ) = "[" & LCase( section ) & "]" Then
' parse lines until the next section is reached
Do until eof(f)
Line Input #f, text
if instr(text, inikey + "=") > 0 then
if mid(text, instr(text, "=") + 1, 1) = "" then
logentry("warning", filename + " searching" + " with section " + section + " with key " + inikey + " key value is blank")
else
print text
end if
end if
if Left( text, 1 ) = "[" then
exit do
end if
'logentry("warning", filename + " searching" + " with section " + section + "key not found")
Loop
end if
Loop
'logentry("notice", filename + " searching" + " with section " + section + " not found")
return true
End Function
' cheap ini file reader