-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathAHKDb.ahk
5609 lines (5014 loc) · 394 KB
/
AHKDb.ahk
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
; ##############################################################################################################################################################################################
; # #
; # AHKDb version 0.1 alpha #
; # #
; # Description: A database library for AutoHotkey. #
; # Dependencies: None. #
; # #
; # Original author: Elias #
; # Released: January 20, 2020 #
; # Lisence: GNU AGPLv3 #
; # Encoding: UTF-8 #
; # Tab length: 4 spaces #
; # #
; ##############################################################################################################################################################################################
; ##############################################################################################################################################################################################
; # #
; # FUNCTIONS: #
; # #
; ##############################################################################################################################################################################################
DatabaseAbsoluteValue( db_DatabaseName, db_Row:="", db_Column:="", db_SkipFirstRow:=FALSE ) { ; Turn negative values (from a specified part of the database) into positive
; Example: DatabaseAbsoluteValue( "database.txt" ) ; turn all negative values in database into positive values
; Example: DatabaseAbsoluteValue( "database.txt", 2 ) ; turn all negative values in the second database row into positive values
; Example: DatabaseAbsoluteValue( "database.txt", , 2 ) ; turn all negative values in the second database column into positive values
; Example: DatabaseAbsoluteValue( "database.txt", 1, 2 ) ; turn the cell value (if negative) in the first row and second column into a positive value
; Example: DatabaseAbsoluteValue( "database.txt", , , TRUE ) ; turn all negative values in database into positive values, but skip the first row
; Rename database to unused filename:
db_TemporaryFilename := db_RenameDatabaseToUnusedFilename( db_DatabaseName ) ; rename database file (and store new filename)
; If neither row nor column is specified:
if ( db_Row=="" AND db_Column=="" ) { ; if neither row nor column is specified
Loop, read, %db_TemporaryFilename%, %db_DatabaseName% ; loop through database (row by row)
{ ; {
if ( A_Index==1 AND db_SkipFirstRow==TRUE ) { ; if first database row and it shall be skipped
FileAppend, %A_LoopReadLine%`r`n ; append the current content of the first row
continue ; continue loop (next iteration)
}
db_LineNumber := A_Index ; store current database row
db_NumberOfRows := DatabaseGetNumberOfRows( db_TemporaryFilename ) ; store (total) number of database rows
db_NumberOfColumns := DatabaseGetNumberOfColumns( db_TemporaryFilename ) ; store (total) number of database columns
Loop, parse, A_LoopReadLine, %A_Tab% ; loop through cells on row (cell by cell)
{ ; {
if ( A_Index<db_NumberOfColumns ) ; if not the last database column
FileAppend, % Abs(A_LoopField) . A_Tab ; save updated cell content followed by a tab to output file
else if ( db_LineNumber<db_NumberOfRows ) ; else if not the last row
FileAppend, % Abs(A_LoopField) . "`r`n" ; save updated cell content followed by linebreak to output file
else ; else (last cell of last row)
FileAppend, % Abs(A_LoopField) ; save updated cell content to output file
} ; }
} ; }
}
; If column is specified but row is not specified:
else if ( db_Row=="" AND !(db_Column=="") ) { ; else if column is specified but row is not specified
db_NumberOfRows := DatabaseGetNumberOfRows( db_TemporaryFilename ) ; store number of database rows
Loop, read, %db_TemporaryFilename%, %db_DatabaseName% ; loop through database rows
{ ; {
if ( A_Index==1 AND db_SkipFirstRow==TRUE ) { ; if first database row and it shall be skipped
FileAppend, %A_LoopReadLine%`r`n ; append the current content of the first row
continue ; continue loop (next iteration)
}
db_DatabaseRow := A_LoopReadLine ; read (current) line of database
db_TabBeforeCellContent := InStr(db_DatabaseRow, A_Tab, , 1, db_Column-1) ; store position of tab before cell content on database row
db_TabAfterCellContent := InStr(db_DatabaseRow, A_Tab, , 1, db_Column) ; store position of tab after cell content on database row
db_LeftPartOfRow := SubStr(db_DatabaseRow, 1, db_TabBeforeCellContent) ; store part of database row before cell content to update
if !( db_TabAfterCellContent==0 ) { ; if a column other than the last database column shall be updated
db_CellContent := SubStr(db_DatabaseRow, db_TabBeforeCellContent+1 ; store content of cell to update
, db_TabAfterCellContent-db_TabBeforeCellContent-1)
db_RightPartOfRow := SubStr(db_DatabaseRow, db_TabAfterCellContent) ; store content of cell to the right of cell to update
} else { ; else (last database column shall be updated)
db_CellContent := SubStr(db_DatabaseRow, db_TabBeforeCellContent+1) ; store cell content
db_RightPartOfRow := ; store nothing in output variable for content to the right of cell content
}
db_CellContent := Abs(db_CellContent) ; update cell content as specified
if ( A_Index<db_NumberOfRows ) ; if not the last row of database
FileAppend, %db_LeftPartOfRow%%db_CellContent%%db_RightPartOfRow%`r`n ; save updated row and a linebreak in output file
else ; else (the last row of database)
FileAppend, %db_LeftPartOfRow%%db_CellContent%%db_RightPartOfRow% ; store updated row (but no linebreak) in output file
} ; }
}
; If row is specified but column is not specified:
else if ( !(db_Row=="") AND db_Column=="" ) { ; else if row is specified but column is not specified
db_NumberOfRows := DatabaseGetNumberOfRows( db_TemporaryFilename ) ; store number of database rows
Loop, read, %db_TemporaryFilename%, %db_DatabaseName% ; loop through database rows
{ ; {
if ( A_Index==db_Row ) { ; if row to modify
db_Cells := StrSplit( A_LoopReadLine, A_Tab ) ; split row into cells
db_Output := Abs(db_Cells[1]) ; store first cell of row (with updated cell content)
Loop, % db_Cells.MaxIndex()-1 ; loop through cells of row (all except the first cell)
db_Output .= A_Tab . Abs(db_Cells[A_Index+1]) ; attach tab and updated cell content
} else ; else (not the database row to modify)
db_Output := A_LoopReadLine ; add (unmodified) database row to output string
if ( A_Index<db_NumberOfRows ) ; if not the last row of database
FileAppend, %db_Output%`r`n ; save output string and linebreak to output file
else ; else (not the last row of database
FileAppend, %db_Output% ; save output string (without linebreak) to output file
} ; }
}
; If row and column are specified:
else { ; else (if both row and column are specified)
db_NumberOfRows := DatabaseGetNumberOfRows( db_TemporaryFilename ) ; store number of database rows
Loop, read, %db_TemporaryFilename%, %db_DatabaseName% ; loop through database rows
{ ; {
if ( A_Index==db_Row ) { ; if database row to modify
db_Cells := StrSplit( A_LoopReadLine, A_Tab ) ; split row into cells
db_Cells[db_Column] := Abs(db_Cells[db_Column]) ; update database cell as specified
db_Output := db_Cells[1] ; store first cell of database row in output string
Loop, % db_Cells.MaxIndex()-1 ; loop through cells of row (all except the first cell)
db_Output .= A_Tab . db_Cells[A_Index+1] ; attach tab and cell content to output string
} else ; else (not the database row to modify)
db_Output := A_LoopReadLine ; add (unmodified) database row to output string
if ( A_Index<db_NumberOfRows ) ; if not the last row of database
FileAppend, %db_Output%`r`n ; save output string and linebreak to output file
else ; else (not the last row of database
FileAppend, %db_Output% ; save output string (without linebreak) to output file
} ; }
}
; Delete temporary database file:
FileDelete, %db_TemporaryFilename% ; delete (temporary) file
return ; return
}
DatabaseAddColumn( db_DatabaseName
, db_Row1:="" , db_Row2:="" , db_Row3:="" , db_Row4:="" , db_Row5:=""
, db_Row6:="" , db_Row7:="" , db_Row8:="" , db_Row9:="" , db_Row10:=""
, db_Row11:="" , db_Row12:="" , db_Row13:="" , db_Row14:="" , db_Row15:=""
, db_Row16:="" , db_Row17:="" , db_Row18:="" , db_Row19:="" , db_Row20:=""
, db_Row21:="" , db_Row22:="" , db_Row23:="" , db_Row24:="" , db_Row25:=""
, db_Row26:="" , db_Row27:="" , db_Row28:="" , db_Row29:="" , db_Row30:=""
, db_Row31:="" , db_Row32:="" , db_Row33:="" , db_Row34:="" , db_Row35:=""
, db_Row36:="" , db_Row37:="" , db_Row38:="" , db_Row39:="" , db_Row40:=""
, db_Row41:="" , db_Row42:="" , db_Row43:="" , db_Row44:="" , db_Row45:=""
, db_Row46:="" , db_Row47:="" , db_Row48:="" , db_Row49:="" , db_Row50:="" ) { ; Add database column (to right end of database)
; Example: DatabaseAddColumn( "database.txt" ) ; add a new database column (with empty cells)
; Example: DatabaseAddColumn( "database.txt", "First cell", "Second", "Third" ) ; add a new database column with first three cells specified
; Rename database to unused filename:
db_TemporaryFilename := db_RenameDatabaseToUnusedFilename( db_DatabaseName ) ; rename database file (and store new filename)
; Save database with new column:
db_NumberOfRows := DatabaseGetNumberOfRows( db_TemporaryFilename ) ; store number of database rows
Loop, Read, %db_TemporaryFilename%, %db_DatabaseName% ; loop through each row of database
{ ; {
db_RowOutput := A_LoopReadLine . A_Tab . db_Row%A_Index% ; prepare output row with new column
if ( A_Index<db_NumberOfRows ) ; if not the last row of database
db_RowOutput .= "`r`n" ; add linebreak at the end of the row
FileAppend, %db_RowOutput% ; add modified database row (with new column) to output file
} ; }
; Delete temporary database file:
FileDelete, %db_TemporaryFilename% ; delete (temporary) file
return ; return
}
DatabaseAddition( db_DatabaseName, db_Constant, db_Row:="", db_Column:="", db_SkipFirstRow:=FALSE ) { ; Add constant to value in each cell (assume cells contain numbers)
; Example: DatabaseAddition( "database.txt", 10 ) ; add 10 to each cell in database
; Example: DatabaseAddition( "database.txt", 3, 2 ) ; add 3 to each cell of the second row
; Example: DatabaseAddition( "database.txt", 3, , 4 ) ; add 3 to each cell of the fourth column
; Example: DatabaseAddition( "database.txt", 3, 5, 4 ) ; add 3 to the cell on the fifth row and fourth column
; Rename database to unused filename:
db_TemporaryFilename := db_RenameDatabaseToUnusedFilename( db_DatabaseName ) ; rename database file (and store new filename)
; If neither row nor column is specified:
if ( db_Row=="" AND db_Column=="" ) { ; if neither row nor column is specified
Loop, read, %db_TemporaryFilename%, %db_DatabaseName% ; loop through database (row by row)
{ ; {
if ( A_Index==1 AND db_SkipFirstRow==TRUE ) { ; if first database row and it shall be skipped
FileAppend, %A_LoopReadLine%`r`n ; append the current content of the first row
continue ; continue loop (next iteration)
}
db_LineNumber := A_Index ; store current database row
db_NumberOfRows := DatabaseGetNumberOfRows( db_TemporaryFilename ) ; store (total) number of database rows
db_NumberOfColumns := DatabaseGetNumberOfColumns( db_TemporaryFilename ) ; store (total) number of database columns
Loop, parse, A_LoopReadLine, %A_Tab% ; loop through cells on row (cell by cell)
if ( A_Index<db_NumberOfColumns ) ; if not the last database column
FileAppend, % A_LoopField+db_Constant . A_Tab ; save updated cell content followed by a tab to output file
else if ( db_LineNumber<db_NumberOfRows ) ; else if not the last row
FileAppend, % A_LoopField+db_Constant . "`r`n" ; save updated cell content followed by linebreak to output file
else ; else (last cell of last row)
FileAppend, % A_LoopField+db_Constant ; save updated cell content to output file
} ; }
}
; If column is specified but row is not specified:
else if ( db_Row=="" AND !(db_Column=="") ) { ; else if column is specified but row is not specified
db_NumberOfRows := DatabaseGetNumberOfRows( db_TemporaryFilename ) ; store number of database rows
Loop, read, %db_TemporaryFilename%, %db_DatabaseName% ; loop through database rows
{ ; {
if ( A_Index==1 AND db_SkipFirstRow==TRUE ) { ; if first database row and it shall be skipped
FileAppend, %A_LoopReadLine%`r`n ; append the current content of the first row
continue ; continue loop (next iteration)
}
db_DatabaseRow := A_LoopReadLine ; read (current) line of database
db_TabBeforeCellContent := InStr(db_DatabaseRow, A_Tab, , 1, db_Column-1) ; store position of tab before cell content on database row
db_TabAfterCellContent := InStr(db_DatabaseRow, A_Tab, , 1, db_Column) ; store position of tab after cell content on database row
db_LeftPartOfRow := SubStr(db_DatabaseRow, 1, db_TabBeforeCellContent) ; store part of database row before cell content to update
if !( db_TabAfterCellContent==0 ) { ; if a column other than the last database column shall be updated
db_CellContent := SubStr(db_DatabaseRow, db_TabBeforeCellContent+1 ; store content of cell to update
, db_TabAfterCellContent-db_TabBeforeCellContent-1)
db_RightPartOfRow := SubStr(db_DatabaseRow, db_TabAfterCellContent) ; store content of cell to the right of cell to update
} else { ; else (last database column shall be updated)
db_CellContent := SubStr(db_DatabaseRow, db_TabBeforeCellContent+1) ; store cell content
db_RightPartOfRow := ; store nothing in output variable for content to the right of cell content
}
db_CellContent := db_CellContent+db_Constant ; update cell content as specified
if ( A_Index<db_NumberOfRows ) ; if not the last row of database
FileAppend, %db_LeftPartOfRow%%db_CellContent%%db_RightPartOfRow%`r`n ; save updated row and a linebreak in output file
else ; else (the last row of database)
FileAppend, %db_LeftPartOfRow%%db_CellContent%%db_RightPartOfRow% ; store updated row (but no linebreak) in output file
} ; }
}
; If row is specified but column is not specified:
else if ( !(db_Row=="") AND db_Column=="" ) { ; else if row is specified but column is not specified
db_NumberOfRows := DatabaseGetNumberOfRows( db_TemporaryFilename ) ; store number of database rows
Loop, read, %db_TemporaryFilename%, %db_DatabaseName% ; loop through database rows
{ ; {
if ( A_Index==db_Row ) { ; if row to modify
db_Cells := StrSplit( A_LoopReadLine, A_Tab ) ; split row into cells
db_Output := db_Cells[1]+db_Constant ; store first cell of row (with updated cell content)
Loop, % db_Cells.MaxIndex()-1 ; loop through cells of row (all except the first cell)
db_Output .= A_Tab . db_Cells[A_Index+1]+db_Constant ; attach tab and updated cell content
} else ; else (not the database row to modify)
db_Output := A_LoopReadLine ; add (unmodified) database row to output string
if ( A_Index<db_NumberOfRows ) ; if not the last row of database
FileAppend, %db_Output%`r`n ; save output string and linebreak to output file
else ; else (not the last row of database
FileAppend, %db_Output% ; save output string (without linebreak) to output file
} ; }
}
; If row and column are specified:
else { ; else (if both row and column are specified)
db_NumberOfRows := DatabaseGetNumberOfRows( db_TemporaryFilename ) ; store number of database rows
Loop, read, %db_TemporaryFilename%, %db_DatabaseName% ; loop through database rows
{ ; {
if ( A_Index==db_Row ) { ; if database row to modify
db_Cells := StrSplit( A_LoopReadLine, A_Tab ) ; split row into cells
db_Cells[db_Column] := db_Cells[db_Column]+db_Constant ; update database cell as specified
db_Output := db_Cells[1] ; store first cell of database row in output string
Loop, % db_Cells.MaxIndex()-1 ; loop through cells of row (all except the first cell)
db_Output .= A_Tab . db_Cells[A_Index+1] ; attach tab and cell content to output string
} else ; else (not the database row to modify)
db_Output := A_LoopReadLine ; add (unmodified) database row to output string
if ( A_Index<db_NumberOfRows ) ; if not the last row of database
FileAppend, %db_Output%`r`n ; save output string and linebreak to output file
else ; else (not the last row of database
FileAppend, %db_Output% ; save output string (without linebreak) to output file
} ; }
}
; Delete temporary database file:
FileDelete, %db_TemporaryFilename% ; delete (temporary) file
return ; return
}
DatabaseAddNA( db_DatabaseName, db_SkipFirstRow:=FALSE ) { ; Replace empty cell content with "NA" and return number of such cases
; Example: AddedNA := DatabaseAddNA( "database.txt" ) ; fill all empty cells with "NA", and present number of such cases
; MsgBox, Number of NAs added: %AddedNA% ;
; Example: DatabaseAddNA( "database.txt", TRUE) ; fill all empty cells with "NA", but skip the first row
return DatabaseReplace( db_DatabaseName, "", "NA", , , , db_SkipFirstRow ) ; fill empty cells with "NA" as specified and return number of such cases
}
DatabaseAddNumerationColumn( db_DatabaseName, db_SkipFirstRow:=FALSE ) { ; Add a new database column containing increasing integers (starting from 1)
; Example: DatabaseAddNumerationColumn( "database.txt" ) ; add a new database column containing increasing integers (starting from 1)
; Rename database to unused filename:
db_TemporaryFilename := db_RenameDatabaseToUnusedFilename( db_DatabaseName ) ; rename database file (and store new filename)
; Save database with numeration column:
Numeration := 1 ; initiate numeration variable
Loop, Read, %db_TemporaryFilename%, %db_DatabaseName% ; loop through each row of database
{ ; {
IfNotExist, %db_DatabaseName% ; if database output file does not exist
{ ; {
if ( db_SkipFirstRow==TRUE ) ; if the first row shall be skipped
FileAppend, %A_LoopReadLine%%A_Tab% ; append first database row without numeration
else { ; else (if the first row shall not be skipped)
FileAppend, %A_LoopReadLine%%A_Tab%%Numeration% ; append first database row with numeration
Numeration++ ; increase numeration variable by 1
}
continue ; continue loop (next iteration)
} ; }
FileAppend, `r`n%A_LoopReadLine%%A_Tab%%Numeration% ; append database row with numeration
Numeration++ ; increase numeration variable by 1
} ; }
; Delete temporary database file:
FileDelete, %db_TemporaryFilename% ; delete (temporary) file
return ; return
}
DatabaseAddRandomColumn( db_DatabaseName, db_RandomMin:=0, db_RandomMax:=9, db_SkipFirstRow:=FALSE ) { ; Add a new database column containing (pseudo) random numbers
; Example: DatabaseAddRandomColumn( "database.txt" ) ; add a database column containing random integers (from 0 to 9)
; Example: DatabaseAddRandomColumn( "database.txt", -5, 3 ) ; add a database column containing random integers from -5 to 3
; Example: DatabaseAddRandomColumn( "database.txt", -5.0, 3.0 ) ; add a database column containing random rational numbers from -5.0 to 3.0
; Example: DatabaseAddRandomColumn( "database.txt", , , TRUE ) ; add a database column containing random integers (from 0 to 9), but skip the first row
; Rename database to unused filename:
db_TemporaryFilename := db_RenameDatabaseToUnusedFilename( db_DatabaseName ) ; rename database file (and store new filename)
; Save database with numeration column:
Loop, Read, %db_TemporaryFilename%, %db_DatabaseName% ; loop through each row of database
{ ; {
Random, db_RandomCellContent, db_RandomMin, db_RandomMax ; store a generated (pseudo) random number
IfNotExist, %db_DatabaseName% ; if database output file does not exist
{ ; {
if ( db_SkipFirstRow==TRUE ) ; if the first row shall be skipped
FileAppend, %A_LoopReadLine%%A_Tab% ; append first database row without random number
else ; else (if the first row shall not be skipped)
FileAppend, %A_LoopReadLine%%A_Tab%%db_RandomCellContent% ; append first database row with random number
continue ; continue loop (next iteration)
} ; }
FileAppend, `r`n%A_LoopReadLine%%A_Tab%%db_RandomCellContent% ; append database row with random number
} ; }
; Delete temporary database file:
FileDelete, %db_TemporaryFilename% ; delete (temporary) file
return ; return
}
DatabaseAddRow( db_DatabaseName
, db_Column1:="" , db_Column2:="" , db_Column3:="" , db_Column4:="" , db_Column5:=""
, db_Column6:="" , db_Column7:="" , db_Column8:="" , db_Column9:="" , db_Column10:=""
, db_Column11:="" , db_Column12:="" , db_Column13:="" , db_Column14:="" , db_Column15:=""
, db_Column16:="" , db_Column17:="" , db_Column18:="" , db_Column19:="" , db_Column20:=""
, db_Column21:="" , db_Column22:="" , db_Column23:="" , db_Column24:="" , db_Column25:=""
, db_Column26:="" , db_Column27:="" , db_Column28:="" , db_Column29:="" , db_Column30:=""
, db_Column31:="" , db_Column32:="" , db_Column33:="" , db_Column34:="" , db_Column35:=""
, db_Column36:="" , db_Column37:="" , db_Column38:="" , db_Column39:="" , db_Column40:=""
, db_Column41:="" , db_Column42:="" , db_Column43:="" , db_Column44:="" , db_Column45:=""
, db_Column46:="" , db_Column47:="" , db_Column48:="" , db_Column49:="" , db_Column50:="" ) { ; Add database row (to bottom of database)
; Example: DatabaseAddRow( "database.txt", "First", "Second", "Third" ) ; add a new database row with first three cells specified
; Example: DatabaseAddRow( "database.txt", "", , "Third" ) ; add a new database row with only third cell specified
; Prepare new database row:
IfExist %db_DatabaseName% ; if database exists
{
db_NewDatabaseRow = `r`n ; add linebreak to row output
db_NumberOfColumns := DatabaseGetNumberOfColumns( db_DatabaseName ) ; store number of database columns
} else { ; else (if database file does not exist)
db_NewDatabaseRow = ; prepare empty row output
Loop, 50 ; loop through all possible cells of row
if ( db_Column%A_Index%!="" ) ; if cell content is specified
db_NumberOfColumns := A_Index ; store loop iteration as number of columns of databae
}
while A_Index<db_NumberOfColumns { ; loop (number of database columns minus 1)
db_NewDatabaseRow .= db_Column%A_Index% . A_Tab ; add cell content followed by tab to string
}
db_NewDatabaseRow .= db_Column%db_NumberOfColumns% ; add last cell content without tab to string
; Add new row to database:
FileAppend, %db_NewDatabaseRow%, %db_DatabaseName%, UTF-8 ; add new database row to file
return ; return
}
DatabaseArcCos( db_DatabaseName, db_Row:="", db_Column:="", db_SkipFirstRow:=FALSE ) { ; Take arccos() of each cell value in specified part of database
; Example: DatabaseArcCos( "database.txt" ) ; take arccos() of each value in database
; Example: DatabaseArcCos( "database.txt", 2 ) ; take arccos() of each value in second row of database
; Example: DatabaseArcCos( "database.txt", , 2 ) ; take arccos() of each value in second column of database
; Example: DatabaseArcCos( "database.txt", 1, 2 ) ; take arccos() of cell value in the first row and second column of database
; Example: DatabaseArcCos( "database.txt", , , TRUE ) ; take arccos() of each value in database, but skip the first row
; Rename database to unused filename:
db_TemporaryFilename := db_RenameDatabaseToUnusedFilename( db_DatabaseName ) ; rename database file (and store new filename)
; If neither row nor column is specified:
if ( db_Row=="" AND db_Column=="" ) { ; if neither row nor column is specified
Loop, read, %db_TemporaryFilename%, %db_DatabaseName% ; loop through database (row by row)
{ ; {
if ( A_Index==1 AND db_SkipFirstRow==TRUE ) { ; if first database row and it shall be skipped
FileAppend, %A_LoopReadLine%`r`n ; append the current content of the first row
continue ; continue loop (next iteration)
}
db_LineNumber := A_Index ; store current database row
db_NumberOfRows := DatabaseGetNumberOfRows( db_TemporaryFilename ) ; store (total) number of database rows
db_NumberOfColumns := DatabaseGetNumberOfColumns( db_TemporaryFilename ) ; store (total) number of database columns
Loop, parse, A_LoopReadLine, %A_Tab% ; loop through cells on row (cell by cell)
{ ; {
if ( A_Index<db_NumberOfColumns ) ; if not the last database column
FileAppend, % ACos(A_LoopField) . A_Tab ; save updated cell content followed by a tab to output file
else if ( db_LineNumber<db_NumberOfRows ) ; else if not the last row
FileAppend, % ACos(A_LoopField) . "`r`n" ; save updated cell content followed by linebreak to output file
else ; else (last cell of last row)
FileAppend, % ACos(A_LoopField) ; save updated cell content to output file
} ; }
} ; }
}
; If column is specified but row is not specified:
else if ( db_Row=="" AND !(db_Column=="") ) { ; else if column is specified but row is not specified
db_NumberOfRows := DatabaseGetNumberOfRows( db_TemporaryFilename ) ; store number of database rows
Loop, read, %db_TemporaryFilename%, %db_DatabaseName% ; loop through database rows
{ ; {
if ( A_Index==1 AND db_SkipFirstRow==TRUE ) { ; if first database row and it shall be skipped
FileAppend, %A_LoopReadLine%`r`n ; append the current content of the first row
continue ; continue loop (next iteration)
}
db_DatabaseRow := A_LoopReadLine ; read (current) line of database
db_TabBeforeCellContent := InStr(db_DatabaseRow, A_Tab, , 1, db_Column-1) ; store position of tab before cell content on database row
db_TabAfterCellContent := InStr(db_DatabaseRow, A_Tab, , 1, db_Column) ; store position of tab after cell content on database row
db_LeftPartOfRow := SubStr(db_DatabaseRow, 1, db_TabBeforeCellContent) ; store part of database row before cell content to update
if !( db_TabAfterCellContent==0 ) { ; if a column other than the last database column shall be updated
db_CellContent := SubStr(db_DatabaseRow, db_TabBeforeCellContent+1 ; store content of cell to update
, db_TabAfterCellContent-db_TabBeforeCellContent-1)
db_RightPartOfRow := SubStr(db_DatabaseRow, db_TabAfterCellContent) ; store content of cell to the right of cell to update
} else { ; else (last database column shall be updated)
db_CellContent := SubStr(db_DatabaseRow, db_TabBeforeCellContent+1) ; store cell content
db_RightPartOfRow := ; store nothing in output variable for content to the right of cell content
}
db_CellContent := ACos(db_CellContent) ; update cell content as specified
if ( A_Index<db_NumberOfRows ) ; if not the last row of database
FileAppend, %db_LeftPartOfRow%%db_CellContent%%db_RightPartOfRow%`r`n ; save updated row and a linebreak in output file
else ; else (the last row of database)
FileAppend, %db_LeftPartOfRow%%db_CellContent%%db_RightPartOfRow% ; store updated row (but no linebreak) in output file
} ; }
}
; If row is specified but column is not specified:
else if ( !(db_Row=="") AND db_Column=="" ) { ; else if row is specified but column is not specified
db_NumberOfRows := DatabaseGetNumberOfRows( db_TemporaryFilename ) ; store number of database rows
Loop, read, %db_TemporaryFilename%, %db_DatabaseName% ; loop through database rows
{ ; {
if ( A_Index==db_Row ) { ; if row to modify
db_Cells := StrSplit( A_LoopReadLine, A_Tab ) ; split row into cells
db_Output := ACos(db_Cells[1]) ; store first cell of row (with updated cell content)
Loop, % db_Cells.MaxIndex()-1 ; loop through cells of row (all except the first cell)
db_Output .= A_Tab . ACos(db_Cells[A_Index+1]) ; attach tab and updated cell content
} else ; else (not the database row to modify)
db_Output := A_LoopReadLine ; add (unmodified) database row to output string
if ( A_Index<db_NumberOfRows ) ; if not the last row of database
FileAppend, %db_Output%`r`n ; save output string and linebreak to output file
else ; else (not the last row of database
FileAppend, %db_Output% ; save output string (without linebreak) to output file
} ; }
}
; If row and column are specified:
else { ; else (if both row and column are specified)
db_NumberOfRows := DatabaseGetNumberOfRows( db_TemporaryFilename ) ; store number of database rows
Loop, read, %db_TemporaryFilename%, %db_DatabaseName% ; loop through database rows
{ ; {
if ( A_Index==db_Row ) { ; if database row to modify
db_Cells := StrSplit( A_LoopReadLine, A_Tab ) ; split row into cells
db_Cells[db_Column] := ACos(db_Cells[db_Column]) ; update database cell as specified
db_Output := db_Cells[1] ; store first cell of database row in output string
Loop, % db_Cells.MaxIndex()-1 ; loop through cells of row (all except the first cell)
db_Output .= A_Tab . db_Cells[A_Index+1] ; attach tab and cell content to output string
} else ; else (not the database row to modify)
db_Output := A_LoopReadLine ; add (unmodified) database row to output string
if ( A_Index<db_NumberOfRows ) ; if not the last row of database
FileAppend, %db_Output%`r`n ; save output string and linebreak to output file
else ; else (not the last row of database
FileAppend, %db_Output% ; save output string (without linebreak) to output file
} ; }
}
; Delete temporary database file:
FileDelete, %db_TemporaryFilename% ; delete (temporary) file
return ; return
}
DatabaseArcSin( db_DatabaseName, db_Row:="", db_Column:="", db_SkipFirstRow:=FALSE ) { ; Take arcsin() of each cell value in specified part of database
; Example: DatabaseArcSin( "database.txt" ) ; take arcsin() of each value in database
; Example: DatabaseArcSin( "database.txt", 2 ) ; take arcsin() of each value in second row of database
; Example: DatabaseArcSin( "database.txt", , 2 ) ; take arcsin() of each value in second column of database
; Example: DatabaseArcSin( "database.txt", 1, 2 ) ; take arcsin() of cell value in the first row and second column of database
; Example: DatabaseArcSin( "database.txt", , , TRUE ) ; take arcsin() of each value in database, but skip the first row
; Rename database to unused filename:
db_TemporaryFilename := db_RenameDatabaseToUnusedFilename( db_DatabaseName ) ; rename database file (and store new filename)
; If neither row nor column is specified:
if ( db_Row=="" AND db_Column=="" ) { ; if neither row nor column is specified
Loop, read, %db_TemporaryFilename%, %db_DatabaseName% ; loop through database (row by row)
{ ; {
if ( A_Index==1 AND db_SkipFirstRow==TRUE ) { ; if first database row and it shall be skipped
FileAppend, %A_LoopReadLine%`r`n ; append the current content of the first row
continue ; continue loop (next iteration)
}
db_LineNumber := A_Index ; store current database row
db_NumberOfRows := DatabaseGetNumberOfRows( db_TemporaryFilename ) ; store (total) number of database rows
db_NumberOfColumns := DatabaseGetNumberOfColumns( db_TemporaryFilename ) ; store (total) number of database columns
Loop, parse, A_LoopReadLine, %A_Tab% ; loop through cells on row (cell by cell)
{ ; {
if ( A_Index<db_NumberOfColumns ) ; if not the last database column
FileAppend, % ASin(A_LoopField) . A_Tab ; save updated cell content followed by a tab to output file
else if ( db_LineNumber<db_NumberOfRows ) ; else if not the last row
FileAppend, % ASin(A_LoopField) . "`r`n" ; save updated cell content followed by linebreak to output file
else ; else (last cell of last row)
FileAppend, % ASin(A_LoopField) ; save updated cell content to output file
} ; }
} ; }
}
; If column is specified but row is not specified:
else if ( db_Row=="" AND !(db_Column=="") ) { ; else if column is specified but row is not specified
db_NumberOfRows := DatabaseGetNumberOfRows( db_TemporaryFilename ) ; store number of database rows
Loop, read, %db_TemporaryFilename%, %db_DatabaseName% ; loop through database rows
{ ; {
if ( A_Index==1 AND db_SkipFirstRow==TRUE ) { ; if first database row and it shall be skipped
FileAppend, %A_LoopReadLine%`r`n ; append the current content of the first row
continue ; continue loop (next iteration)
}
db_DatabaseRow := A_LoopReadLine ; read (current) line of database
db_TabBeforeCellContent := InStr(db_DatabaseRow, A_Tab, , 1, db_Column-1) ; store position of tab before cell content on database row
db_TabAfterCellContent := InStr(db_DatabaseRow, A_Tab, , 1, db_Column) ; store position of tab after cell content on database row
db_LeftPartOfRow := SubStr(db_DatabaseRow, 1, db_TabBeforeCellContent) ; store part of database row before cell content to update
if !( db_TabAfterCellContent==0 ) { ; if a column other than the last database column shall be updated
db_CellContent := SubStr(db_DatabaseRow, db_TabBeforeCellContent+1 ; store content of cell to update
, db_TabAfterCellContent-db_TabBeforeCellContent-1)
db_RightPartOfRow := SubStr(db_DatabaseRow, db_TabAfterCellContent) ; store content of cell to the right of cell to update
} else { ; else (last database column shall be updated)
db_CellContent := SubStr(db_DatabaseRow, db_TabBeforeCellContent+1) ; store cell content
db_RightPartOfRow := ; store nothing in output variable for content to the right of cell content
}
db_CellContent := ASin(db_CellContent) ; update cell content as specified
if ( A_Index<db_NumberOfRows ) ; if not the last row of database
FileAppend, %db_LeftPartOfRow%%db_CellContent%%db_RightPartOfRow%`r`n ; save updated row and a linebreak in output file
else ; else (the last row of database)
FileAppend, %db_LeftPartOfRow%%db_CellContent%%db_RightPartOfRow% ; store updated row (but no linebreak) in output file
} ; }
}
; If row is specified but column is not specified:
else if ( !(db_Row=="") AND db_Column=="" ) { ; else if row is specified but column is not specified
db_NumberOfRows := DatabaseGetNumberOfRows( db_TemporaryFilename ) ; store number of database rows
Loop, read, %db_TemporaryFilename%, %db_DatabaseName% ; loop through database rows
{ ; {
if ( A_Index==db_Row ) { ; if row to modify
db_Cells := StrSplit( A_LoopReadLine, A_Tab ) ; split row into cells
db_Output := ASin(db_Cells[1]) ; store first cell of row (with updated cell content)
Loop, % db_Cells.MaxIndex()-1 ; loop through cells of row (all except the first cell)
db_Output .= A_Tab . ASin(db_Cells[A_Index+1]) ; attach tab and updated cell content
} else ; else (not the database row to modify)
db_Output := A_LoopReadLine ; add (unmodified) database row to output string
if ( A_Index<db_NumberOfRows ) ; if not the last row of database
FileAppend, %db_Output%`r`n ; save output string and linebreak to output file
else ; else (not the last row of database
FileAppend, %db_Output% ; save output string (without linebreak) to output file
} ; }
}
; If row and column are specified:
else { ; else (if both row and column are specified)
db_NumberOfRows := DatabaseGetNumberOfRows( db_TemporaryFilename ) ; store number of database rows
Loop, read, %db_TemporaryFilename%, %db_DatabaseName% ; loop through database rows
{ ; {
if ( A_Index==db_Row ) { ; if database row to modify
db_Cells := StrSplit( A_LoopReadLine, A_Tab ) ; split row into cells
db_Cells[db_Column] := ASin(db_Cells[db_Column]) ; update database cell as specified
db_Output := db_Cells[1] ; store first cell of database row in output string
Loop, % db_Cells.MaxIndex()-1 ; loop through cells of row (all except the first cell)
db_Output .= A_Tab . db_Cells[A_Index+1] ; attach tab and cell content to output string
} else ; else (not the database row to modify)
db_Output := A_LoopReadLine ; add (unmodified) database row to output string
if ( A_Index<db_NumberOfRows ) ; if not the last row of database
FileAppend, %db_Output%`r`n ; save output string and linebreak to output file
else ; else (not the last row of database
FileAppend, %db_Output% ; save output string (without linebreak) to output file
} ; }
}
; Delete temporary database file:
FileDelete, %db_TemporaryFilename% ; delete (temporary) file
return ; return
}
DatabaseArcTan( db_DatabaseName, db_Row:="", db_Column:="", db_SkipFirstRow:=FALSE ) { ; Take arctan() of each cell value in specified part of database
; Example: DatabaseArcTan( "database.txt" ) ; take arctan() of each value in database
; Example: DatabaseArcTan( "database.txt", 2 ) ; take arctan() of each value in second row of database
; Example: DatabaseArcTan( "database.txt", , 2 ) ; take arctan() of each value in second column of database
; Example: DatabaseArcTan( "database.txt", 1, 2 ) ; take arctan() of cell value in the first row and second column of database
; Example: DatabaseArcTan( "database.txt", , , TRUE ) ; take arctan() of each value in database, but skip the first row
; Rename database to unused filename:
db_TemporaryFilename := db_RenameDatabaseToUnusedFilename( db_DatabaseName ) ; rename database file (and store new filename)
; If neither row nor column is specified:
if ( db_Row=="" AND db_Column=="" ) { ; if neither row nor column is specified
Loop, read, %db_TemporaryFilename%, %db_DatabaseName% ; loop through database (row by row)
{ ; {
if ( A_Index==1 AND db_SkipFirstRow==TRUE ) { ; if first database row and it shall be skipped
FileAppend, %A_LoopReadLine%`r`n ; append the current content of the first row
continue ; continue loop (next iteration)
}
db_LineNumber := A_Index ; store current database row
db_NumberOfRows := DatabaseGetNumberOfRows( db_TemporaryFilename ) ; store (total) number of database rows
db_NumberOfColumns := DatabaseGetNumberOfColumns( db_TemporaryFilename ) ; store (total) number of database columns
Loop, parse, A_LoopReadLine, %A_Tab% ; loop through cells on row (cell by cell)
{ ; {
if ( A_Index<db_NumberOfColumns ) ; if not the last database column
FileAppend, % ATan(A_LoopField) . A_Tab ; save updated cell content followed by a tab to output file
else if ( db_LineNumber<db_NumberOfRows ) ; else if not the last row
FileAppend, % ATan(A_LoopField) . "`r`n" ; save updated cell content followed by linebreak to output file
else ; else (last cell of last row)
FileAppend, % ATan(A_LoopField) ; save updated cell content to output file
} ; }
} ; }
}
; If column is specified but row is not specified:
else if ( db_Row=="" AND !(db_Column=="") ) { ; else if column is specified but row is not specified
db_NumberOfRows := DatabaseGetNumberOfRows( db_TemporaryFilename ) ; store number of database rows
Loop, read, %db_TemporaryFilename%, %db_DatabaseName% ; loop through database rows
{ ; {
if ( A_Index==1 AND db_SkipFirstRow==TRUE ) { ; if first database row and it shall be skipped
FileAppend, %A_LoopReadLine%`r`n ; append the current content of the first row
continue ; continue loop (next iteration)
}
db_DatabaseRow := A_LoopReadLine ; read (current) line of database
db_TabBeforeCellContent := InStr(db_DatabaseRow, A_Tab, , 1, db_Column-1) ; store position of tab before cell content on database row
db_TabAfterCellContent := InStr(db_DatabaseRow, A_Tab, , 1, db_Column) ; store position of tab after cell content on database row
db_LeftPartOfRow := SubStr(db_DatabaseRow, 1, db_TabBeforeCellContent) ; store part of database row before cell content to update
if !( db_TabAfterCellContent==0 ) { ; if a column other than the last database column shall be updated
db_CellContent := SubStr(db_DatabaseRow, db_TabBeforeCellContent+1 ; store content of cell to update
, db_TabAfterCellContent-db_TabBeforeCellContent-1)
db_RightPartOfRow := SubStr(db_DatabaseRow, db_TabAfterCellContent) ; store content of cell to the right of cell to update
} else { ; else (last database column shall be updated)
db_CellContent := SubStr(db_DatabaseRow, db_TabBeforeCellContent+1) ; store cell content
db_RightPartOfRow := ; store nothing in output variable for content to the right of cell content
}
db_CellContent := ATan(db_CellContent) ; update cell content as specified
if ( A_Index<db_NumberOfRows ) ; if not the last row of database
FileAppend, %db_LeftPartOfRow%%db_CellContent%%db_RightPartOfRow%`r`n ; save updated row and a linebreak in output file
else ; else (the last row of database)
FileAppend, %db_LeftPartOfRow%%db_CellContent%%db_RightPartOfRow% ; store updated row (but no linebreak) in output file
} ; }
}
; If row is specified but column is not specified:
else if ( !(db_Row=="") AND db_Column=="" ) { ; else if row is specified but column is not specified
db_NumberOfRows := DatabaseGetNumberOfRows( db_TemporaryFilename ) ; store number of database rows
Loop, read, %db_TemporaryFilename%, %db_DatabaseName% ; loop through database rows
{ ; {
if ( A_Index==db_Row ) { ; if row to modify
db_Cells := StrSplit( A_LoopReadLine, A_Tab ) ; split row into cells
db_Output := ATan(db_Cells[1]) ; store first cell of row (with updated cell content)
Loop, % db_Cells.MaxIndex()-1 ; loop through cells of row (all except the first cell)
db_Output .= A_Tab . ATan(db_Cells[A_Index+1]) ; attach tab and updated cell content
} else ; else (not the database row to modify)
db_Output := A_LoopReadLine ; add (unmodified) database row to output string
if ( A_Index<db_NumberOfRows ) ; if not the last row of database
FileAppend, %db_Output%`r`n ; save output string and linebreak to output file
else ; else (not the last row of database
FileAppend, %db_Output% ; save output string (without linebreak) to output file
} ; }
}
; If row and column are specified:
else { ; else (if both row and column are specified)
db_NumberOfRows := DatabaseGetNumberOfRows( db_TemporaryFilename ) ; store number of database rows
Loop, read, %db_TemporaryFilename%, %db_DatabaseName% ; loop through database rows
{ ; {
if ( A_Index==db_Row ) { ; if database row to modify
db_Cells := StrSplit( A_LoopReadLine, A_Tab ) ; split row into cells
db_Cells[db_Column] := ATan(db_Cells[db_Column]) ; update database cell as specified
db_Output := db_Cells[1] ; store first cell of database row in output string
Loop, % db_Cells.MaxIndex()-1 ; loop through cells of row (all except the first cell)
db_Output .= A_Tab . db_Cells[A_Index+1] ; attach tab and cell content to output string
} else ; else (not the database row to modify)
db_Output := A_LoopReadLine ; add (unmodified) database row to output string
if ( A_Index<db_NumberOfRows ) ; if not the last row of database
FileAppend, %db_Output%`r`n ; save output string and linebreak to output file
else ; else (not the last row of database
FileAppend, %db_Output% ; save output string (without linebreak) to output file
} ; }
}
; Delete temporary database file:
FileDelete, %db_TemporaryFilename% ; delete (temporary) file
return ; return
}
DatabaseBackup( db_DatabaseName, db_Location:="", db_BackupName:="NUMBER" ) { ; Create a backup of database by a built-in naming system (NUMBER, DATE, DATETIME or ORIGINAL)
; Example: DatabaseBackup( "database.txt" ) ; store a database backup (filename with number) in working directory
; Example: DatabaseBackup( "database.txt", , "NUMBER" ) ; store a database backup (filename with number) in working directory
; Example: DatabaseBackup( "database.txt", , "DATE" ) ; store a database backup (filename with date)
; Example: DatabaseBackup( "database.txt", , "DATETIME" ) ; store a database backup (filename with date and time)
; Example: DatabaseBackup( "database.txt", "C:\Users\Username\Desktop\Backup", "ORIGINAL" ) ; store a backup (with same name as original) in a backup folder on desktop
; Example: BackupName := DatabaseBackup( "database.txt" ) ; create a backup, and present backup filename (if no problem was detected)
; if ( BackupName==0 ) ;
; MsgBox, Backup failed. ;
; else ;
; MsgBox, Backup named %BackupName% created. ;
; Prepare location (folder):
if ( db_Location=="" ) ; if no location is specified
db_Location := A_WorkingDir ; set location to the working directory
; Prepare filename:
SplitPath, db_DatabaseName, , , , db_DatabaseNameNoExtension ; get database filename without extension
if ( db_BackupName=="NUMBER" ) { ; if naming system based on numbering
Loop, ; loop
IfNotExist, %db_DatabaseNameNoExtension%_%A_Index%.txt ; if filename is available
{ ; {
db_BackupFile = %db_DatabaseNameNoExtension%_%A_Index%.txt ; store filename
break ; break
} ; }
} else if ( db_BackupName=="DATE" ) ; else if naming system based on date
db_BackupFile = %db_DatabaseNameNoExtension%_%A_YYYY%_%A_MM%_%A_DD%.txt ; store filename
else if ( db_BackupName=="DATETIME" ) ; else if naming system based on date and time
db_BackupFile = %db_DatabaseNameNoExtension%_%A_YYYY%_%A_MM%_%A_DD%_%A_Hour%_%A_Min%_%A_Sec%.txt ; store filename
else if ( db_BackupName=="ORIGINAL" ) ; else if naming backup same as original
db_BackupFile = %db_DatabaseNameNoExtension%.txt ; store filename
else ; else
return 0 ; return 0 (incorrect naming system declared)
; Save database file:
FileCopy, %db_DatabaseName%, %db_Location%\%db_BackupFile%, 1 ; save a backup of database (overwrite if needed)
; Return 1 if backup file is found:
IfNotExist, %db_Location%\%db_BackupFile% ; if database backup file is not found
return 0 ; return 0
return db_BackupFile ; return 1 (no problem was detected)
}
DatabaseCeiling( db_DatabaseName, db_Row:="", db_Column:="", db_SkipFirstRow:=FALSE ) { ; Round up values from specified part of database
; Example: DatabaseCeiling( "database.txt" ) ; round up all database values
; Example: DatabaseCeiling( "database.txt", 2 ) ; round up all database values in the second row
; Example: DatabaseCeiling( "database.txt", , 2 ) ; round up all database values in the second column
; Example: DatabaseCeiling( "database.txt", 1, 2 ) ; round up the value in the first row and second column
; Example: DatabaseCeiling( "database.txt", , , TRUE ) ; round up all database values, but skip the first row
; Rename database to unused filename:
db_TemporaryFilename := db_RenameDatabaseToUnusedFilename( db_DatabaseName ) ; rename database file (and store new filename)
; If neither row nor column is specified:
if ( db_Row=="" AND db_Column=="" ) { ; if neither row nor column is specified
Loop, read, %db_TemporaryFilename%, %db_DatabaseName% ; loop through database (row by row)
{ ; {
if ( A_Index==1 AND db_SkipFirstRow==TRUE ) { ; if first database row and it shall be skipped
FileAppend, %A_LoopReadLine%`r`n ; append the current content of the first row
continue ; continue loop (next iteration)
}
db_LineNumber := A_Index ; store current database row
db_NumberOfRows := DatabaseGetNumberOfRows( db_TemporaryFilename ) ; store (total) number of database rows
db_NumberOfColumns := DatabaseGetNumberOfColumns( db_TemporaryFilename ) ; store (total) number of database columns
Loop, parse, A_LoopReadLine, %A_Tab% ; loop through cells on row (cell by cell)
{ ; {
if ( A_Index<db_NumberOfColumns ) ; if not the last database column
FileAppend, % Ceil(A_LoopField) . A_Tab ; save updated cell content followed by a tab to output file
else if ( db_LineNumber<db_NumberOfRows ) ; else if not the last row
FileAppend, % Ceil(A_LoopField) . "`r`n" ; save updated cell content followed by linebreak to output file
else ; else (last cell of last row)
FileAppend, % Ceil(A_LoopField) ; save updated cell content to output file
} ; }
} ; }
}
; If column is specified but row is not specified:
else if ( db_Row=="" AND !(db_Column=="") ) { ; else if column is specified but row is not specified
db_NumberOfRows := DatabaseGetNumberOfRows( db_TemporaryFilename ) ; store number of database rows
Loop, read, %db_TemporaryFilename%, %db_DatabaseName% ; loop through database rows
{ ; {
if ( A_Index==1 AND db_SkipFirstRow==TRUE ) { ; if first database row and it shall be skipped
FileAppend, %A_LoopReadLine%`r`n ; append the current content of the first row
continue ; continue loop (next iteration)
}
db_DatabaseRow := A_LoopReadLine ; read (current) line of database
db_TabBeforeCellContent := InStr(db_DatabaseRow, A_Tab, , 1, db_Column-1) ; store position of tab before cell content on database row
db_TabAfterCellContent := InStr(db_DatabaseRow, A_Tab, , 1, db_Column) ; store position of tab after cell content on database row
db_LeftPartOfRow := SubStr(db_DatabaseRow, 1, db_TabBeforeCellContent) ; store part of database row before cell content to update
if !( db_TabAfterCellContent==0 ) { ; if a column other than the last database column shall be updated
db_CellContent := SubStr(db_DatabaseRow, db_TabBeforeCellContent+1 ; store content of cell to update
, db_TabAfterCellContent-db_TabBeforeCellContent-1)
db_RightPartOfRow := SubStr(db_DatabaseRow, db_TabAfterCellContent) ; store content of cell to the right of cell to update
} else { ; else (last database column shall be updated)
db_CellContent := SubStr(db_DatabaseRow, db_TabBeforeCellContent+1) ; store cell content
db_RightPartOfRow := ; store nothing in output variable for content to the right of cell content
}
db_CellContent := Ceil(db_CellContent) ; update cell content as specified
if ( A_Index<db_NumberOfRows ) ; if not the last row of database
FileAppend, %db_LeftPartOfRow%%db_CellContent%%db_RightPartOfRow%`r`n ; save updated row and a linebreak in output file
else ; else (the last row of database)
FileAppend, %db_LeftPartOfRow%%db_CellContent%%db_RightPartOfRow% ; store updated row (but no linebreak) in output file
} ; }
}
; If row is specified but column is not specified:
else if ( !(db_Row=="") AND db_Column=="" ) { ; else if row is specified but column is not specified
db_NumberOfRows := DatabaseGetNumberOfRows( db_TemporaryFilename ) ; store number of database rows
Loop, read, %db_TemporaryFilename%, %db_DatabaseName% ; loop through database rows
{ ; {
if ( A_Index==db_Row ) { ; if row to modify
db_Cells := StrSplit( A_LoopReadLine, A_Tab ) ; split row into cells
db_Output := Ceil(db_Cells[1]) ; store first cell of row (with updated cell content)
Loop, % db_Cells.MaxIndex()-1 ; loop through cells of row (all except the first cell)
db_Output .= A_Tab . Ceil(db_Cells[A_Index+1]) ; attach tab and updated cell content
} else ; else (not the database row to modify)
db_Output := A_LoopReadLine ; add (unmodified) database row to output string
if ( A_Index<db_NumberOfRows ) ; if not the last row of database
FileAppend, %db_Output%`r`n ; save output string and linebreak to output file
else ; else (not the last row of database
FileAppend, %db_Output% ; save output string (without linebreak) to output file
} ; }
}
; If row and column are specified:
else { ; else (if both row and column are specified)
db_NumberOfRows := DatabaseGetNumberOfRows( db_TemporaryFilename ) ; store number of database rows
Loop, read, %db_TemporaryFilename%, %db_DatabaseName% ; loop through database rows
{ ; {
if ( A_Index==db_Row ) { ; if database row to modify
db_Cells := StrSplit( A_LoopReadLine, A_Tab ) ; split row into cells
db_Cells[db_Column] := Ceil(db_Cells[db_Column]) ; update database cell as specified
db_Output := db_Cells[1] ; store first cell of database row in output string
Loop, % db_Cells.MaxIndex()-1 ; loop through cells of row (all except the first cell)
db_Output .= A_Tab . db_Cells[A_Index+1] ; attach tab and cell content to output string
} else ; else (not the database row to modify)
db_Output := A_LoopReadLine ; add (unmodified) database row to output string
if ( A_Index<db_NumberOfRows ) ; if not the last row of database
FileAppend, %db_Output%`r`n ; save output string and linebreak to output file
else ; else (not the last row of database
FileAppend, %db_Output% ; save output string (without linebreak) to output file
} ; }
}
; Delete temporary database file:
FileDelete, %db_TemporaryFilename% ; delete (temporary) file
return ; return
}
DatabaseCheck( db_DatabaseName ) { ; Check if database contain errors (rows with different number of cells, and if last row with cells end with linebreak), and return 1 if no problems found
; Example: if ( DatabaseCheck( "database.txt" ) ) ; check if database contain error, and present result
; MsgBox, No problem detected. ;
; else ;
; MsgBox, Problem detected. ;
; Ensure all rows have the same number of cells:
db_NumberOfColumns := DatabaseGetNumberOfColumns( db_DatabaseName ) ; store number of columns
Loop, read, %db_DatabaseName% ; loop through database rows
if !( db_NumberOfColumns==StrSplit( A_LoopReadLine, A_Tab ).MaxIndex() ) ; if row does not have correct number of cells
return 0 ; return 0
; Ensure last row with cells does not end with linebreak:
db_DatabaseOpen := FileOpen( db_DatabaseName, "r" ) ; open file (for reading)
Loop, read, %db_DatabaseName% ; loop through database rows
if !( A_Index==1 ) ; if not the first row
db_DatabaseOpen.ReadLine() ; read one line of database (move pointer to next row)
db_DatabaseRow := db_DatabaseOpen.ReadLine() ; read one line of database (the last row containing cells)
StringRight, db_EndOfRow, db_DatabaseRow, 1 ; store last character of row
if !( db_EndOfRow=="`n" ) ; if row does not end with linebreak
return 1 ; return 1
db_DatabaseOpen.Close() ; close file
return 0 ; return 0 (the last row with cells end with a linebreak)
}
DatabaseColumnSplitDelimiter( db_DatabaseName, db_Column, db_Delimiter, db_SkipFirstRow:=FALSE ) { ; Split a column at a delimiter
; Example: DatabaseColumnSplitDelimiter( "database.txt", 4, "," ) ; split the fourth column at (the first) comma (the comma is removed)
; Example: DatabaseColumnSplitDelimiter( "database.txt", 4, ",", TRUE ) ; split the fourth column at (the first) comma, but skip the first row
; Rename database to unused filename:
db_TemporaryFilename := db_RenameDatabaseToUnusedFilename( db_DatabaseName ) ; rename database file (and store new filename)
; Split column:
Loop, read, %db_TemporaryFilename%, %db_DatabaseName% ; loop through database rows
{ ; {
db_DatabaseRow := A_LoopReadLine ; read (current) line of database
db_TabBeforeCellContent := InStr(db_DatabaseRow, A_Tab, , 1, db_Column-1) ; store position of tab before cell content on database row
db_TabAfterCellContent := InStr(db_DatabaseRow, A_Tab, , 1, db_Column) ; store position of tab after cell content on database row
db_LeftPartOfRow := SubStr(db_DatabaseRow, 1, db_TabBeforeCellContent) ; store part of database row before cell content to split
if !( db_TabAfterCellContent==0 ) { ; if a column other than the last database column shall be splitted
db_CellContent := SubStr(db_DatabaseRow, db_TabBeforeCellContent+1 ; store content of cell to splitted
, db_TabAfterCellContent-db_TabBeforeCellContent-1)
db_RightPartOfRow := SubStr(db_DatabaseRow, db_TabAfterCellContent) ; store content of cell to the right of cell to split (no tab included)
} else { ; else (last database column shall be splitted)
db_CellContent := SubStr(db_DatabaseRow, db_TabBeforeCellContent+1) ; store cell content
db_RightPartOfRow := ; store nothing in output variable for content to the right of cell content
}
db_SplittedCells := StrSplit( db_CellContent, db_Delimiter, , 2 ) ; store splitted cell in array
IfExist, %db_DatabaseName% ; if output file exists
FileAppend, `r`n ; append linebreak
FileAppend, %db_LeftPartOfRow% ; append part before colummn to split
if ( A_Index==1 AND db_SkipFirstRow==TRUE ) ; if the first row, and the first row shall be skipped
FileAppend, %db_Cellcontent%`t ; append the unsplitted cell content and a tab
else ; else (if the row shall be splitted)
FileAppend, % db_SplittedCells[1] . "`t" . db_SplittedCells[2] ; append splitted cell content in two cells
FileAppend, %db_RightPartOfRow% ; append part after the splitted cell
} ; }
; Delete temporary database file:
FileDelete, %db_TemporaryFilename% ; delete (temporary) file
return ; return
}
DatabaseColumnSplitLeft( db_DatabaseName, db_Column, db_SplitPosition, db_SkipFirstRow:=FALSE ) { ; Split a column after a specified number of characters from the left
; Example: DatabaseColumnSplitLeft( "database.txt", 5, 2 ) ; split the fifth column after the second character (from left)
; Example: NewColumn := DatabaseColumnSplitLeft( "database.txt", 3, 2 ) ; split the third column after the second character and show new column number
; MsgBox, The new column has column number %NewColumn%. ;
; Example: DatabaseColumnSplitLeft( "database.txt", 4, 2, TRUE ) ; split the fourth column after the second character, but skip the first row
; Rename database to unused filename:
db_TemporaryFilename := db_RenameDatabaseToUnusedFilename( db_DatabaseName ) ; rename database file (and store new filename)
; Split column:
db_NumberOfRows := DatabaseGetNumberOfRows( db_TemporaryFilename ) ; store number of database rows
Loop, read, %db_TemporaryFilename%, %db_DatabaseName% ; loop through database rows
{ ; {
db_DatabaseRow := A_LoopReadLine ; read (current) line of database
db_TabBeforeCellContent := InStr(db_DatabaseRow, A_Tab, , 1, db_Column-1) ; store position of tab before cell content on database row
db_TabAfterCellContent := InStr(db_DatabaseRow, A_Tab, , 1, db_Column) ; store position of tab after cell content on database row
db_LeftPartOfRow := SubStr(db_DatabaseRow, 1, db_TabBeforeCellContent) ; store part of database row before cell content to split
if !( db_TabAfterCellContent==0 ) { ; if a column other than the last database column shall be splitted
db_CellContent := SubStr(db_DatabaseRow, db_TabBeforeCellContent+1 ; store content of cell to split
, db_TabAfterCellContent-db_TabBeforeCellContent-1)
db_RightPartOfRow := SubStr(db_DatabaseRow, db_TabAfterCellContent) ; store content of cell to the right of cell to split
} else { ; else (last database column shall be splitted)
db_CellContent := SubStr(db_DatabaseRow, db_TabBeforeCellContent+1) ; store cell content
db_RightPartOfRow := ; store nothing in output variable for content to the right of cell content
}
IfExist, %db_DatabaseName% ; if output file exists
FileAppend, `r`n ; append linebreak
FileAppend, %db_LeftPartOfRow% ; append part to the left of column to split
if ( A_Index==1 AND db_SkipFirstRow==TRUE ) ; if first database row, and it shall be skipped
FileAppend, %db_CellContent%`t ; output row content (with first cell of new column empty)
else ; else (if the row shall be splitted)
FileAppend, % SubStr(db_CellContent, 1, db_SplitPosition) . "`t" ; append splitted columns (separated by a tab)
. SubStr(db_CellContent, db_SplitPosition+1 )
FileAppend, %db_RightPartOfRow% ; output part of row to the right of splitted column
} ; }
; Delete temporary database file:
FileDelete, %db_TemporaryFilename% ; delete (temporary) file
return % db_Column+1 ; return column number of new column (right part of splitted column)
}
DatabaseColumnSplitRight( db_DatabaseName, db_Column, db_SplitPosition, db_SkipFirstRow:=FALSE ) { ; Split a column after a specified number of characters from the right
; Example: DatabaseColumnSplitRight( "database.txt", 5, 2 ) ; split the fifth column after the second character (from right)
; Example: NewColumn := DatabaseColumnSplitRight( "database.txt", 3, 2 ) ; split the third column after the second character and show new column number
; MsgBox, The new column has column number %NewColumn%. ;
; Example: DatabaseColumnSplitRight( "database.txt", 4, 2, TRUE ) ; split the fourth column after the second character, but skip the first row
; Rename database to unused filename:
db_TemporaryFilename := db_RenameDatabaseToUnusedFilename( db_DatabaseName ) ; rename database file (and store new filename)
; Split column:
Loop, read, %db_TemporaryFilename%, %db_DatabaseName% ; loop through database rows
{ ; {
db_DatabaseRow := A_LoopReadLine ; read (current) line of database
db_TabBeforeCellContent := InStr(db_DatabaseRow, A_Tab, , 1, db_Column-1) ; store position of tab before cell content on database row
db_TabAfterCellContent := InStr(db_DatabaseRow, A_Tab, , 1, db_Column) ; store position of tab after cell content on database row
db_LeftPartOfRow := SubStr(db_DatabaseRow, 1, db_TabBeforeCellContent) ; store part of database row before cell content to split
if !( db_TabAfterCellContent==0 ) { ; if a column other than the last database column shall be splitted
db_CellContent := SubStr(db_DatabaseRow, db_TabBeforeCellContent+1 ; store content of cell to split
, db_TabAfterCellContent-db_TabBeforeCellContent-1)
db_RightPartOfRow := SubStr(db_DatabaseRow, db_TabAfterCellContent) ; store content of cell to the right of cell to split
} else { ; else (last database column shall be splitted)
db_CellContent := SubStr(db_DatabaseRow, db_TabBeforeCellContent+1) ; store cell content
db_RightPartOfRow := ; store nothing in output variable for content to the right of cell content
}
IfExist, %db_DatabaseName% ; if output file exists
FileAppend, `r`n ; append linebreak
FileAppend, %db_LeftPartOfRow% ; append part to the left of column to split
if ( A_Index==1 AND db_SkipFirstRow==TRUE ) ; if first database row, and it shall be skipped
FileAppend, %db_CellContent%`t ; output row content (with first cell of new column empty)
else { ; else (if the row shall be splitted)
db_SplitPositionFromLeft := StrLen(db_CellContent)-db_SplitPosition ; store last character to store in left column
FileAppend, % SubStr(db_CellContent, 1, db_SplitPositionFromLeft) . "`t" ; append splitted columns (separated by a tab)
. SubStr(db_CellContent, db_SplitPositionFromLeft+1 )
}
FileAppend, %db_RightPartOfRow% ; output part of row to the right of splitted column
} ; }
; Delete temporary database file:
FileDelete, %db_TemporaryFilename% ; delete (temporary) file
return % db_Column+1 ; return column number of new column (right part of splitted column)
}
DatabaseCompare( db_DatabaseName1, db_DatabaseName2, db_Row:="", db_Column:="", db_CaseSensitive:=FALSE, db_SkipFirstRow:=FALSE) { ; Compare content of two databases (return 1 if identical)
; Example: Comparison := DatabaseCompare( "database1.txt", "database2.txt" ) ; compare content of two databases (not case-sensitive) and present result
; if (Comparison==1) ;
; MsgBox, Databases are identical. ;
; else ;
; MsgBox, Databases are different. ;
; Example: Comparison := DatabaseCompare( "database1.txt", "database2.txt", 2 ) ; compare content of the second row of two databases and store result
; Example: Comparison := DatabaseCompare( "database1.txt", "database2.txt", , 3 ) ; compare content of the third row of two databases and store result