forked from TLMcode/AHKs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FcnLib-Rewrites.ahk
504 lines (423 loc) · 13.8 KB
/
FcnLib-Rewrites.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
;#include FcnLib.ahk
;FcnLib-Rewrites.ahk by camerb
;This library contains those functions that reproduce functionality in AHK_basic, but which have significant differences in usage and/or side effects. The differences in functionality in these functions were found to be preferable over the functionality of the commands as they are in plain AHK (that's just my opinion, though)
;{{{ File Manipulation Functions
FileAppend(text, file)
{
EnsureDirExists(file)
FileAppend, %text%, %file%
;TODO should we ensure that the file exists?
}
FileAppendLine(text, file)
{
text.="`r`n"
return FileAppend(text, file)
}
FileCopy(source, dest, options="")
{
if InStr(options, "overwrite")
overwrite=1
if NOT FileExist(source)
fatalErrord("file doesn't exist", source, A_ThisFunc, A_LineNumber)
EnsureDirExists(dest)
FileCopy, %source%, %dest%, %overwrite%
}
FileDelete(file)
{
;nothing is wrong if the file is already gone
if NOT FileExist(file)
return
FileDelete, %file%
}
FileMove(source, dest, options="")
{
if InStr(options, "overwrite")
overwrite=1
if NOT FileExist(source)
fatalErrord("file doesn't exist", source, A_ThisFunc, A_LineNumber)
EnsureDirExists(dest)
FileMove, %source%, %dest%, %overwrite%
}
FileCreate(text, file)
{
;if rewriting the file wouldn't change anything on the hard drive, don't do it
if FileExist(file)
{
currentText := FileRead(file)
if (currentText == text)
return
}
;Create the sucker
FileDelete(file)
FileAppend(text, file)
}
;A simple little function, probably isn't performed as quickly as it could be, but what's the harm?
FileLineCount(file)
{
Loop, read, %file%
returned:=A_Index
return returned
}
;}}}
;{{{Folder Manipulation Functions
;TESTME
FileCopyDir(source, dest, options="")
{
if InStr(options, "overwrite")
overwrite=1
if NOT DirExist(source)
return false
EnsureDirExists(dest)
FileCopyDir, %source%, %dest%, %overwrite%
}
;TODO consider a rename to FileDeleteDirForceful (or forceful option)
;Delete folder very forcefully
FileDeleteDirForceful(dir)
{
;this will delete as much as possible from the target folder
;depending upon file locks, some items may not get deleted
if NOT DirExist(dir)
return
dir:=EnsureEndsWith(dir, "\")
dir:=EnsureEndsWith(dir, "*")
;delete as many files as we possibly can (typically difficult in windows)
Loop, %dir%, , 1
{
FileDelete, %A_LoopFileFullPath%
}
;delete all the folders that we can
Loop, %dir%, 2, 1
{
FileRemoveDir, %A_LoopFileFullPath%, 1
}
}
;Returns if the directory exists
;FIXME hmm, perhaps I should have named this starting with the word "file"
;TODO rename all instances of DirExist to FileDirExist
FileDirExist(dirPath)
{
return InStr(FileExist(dirPath), "D") ? 1 : 0
}
DirExist(dirPath)
{
return FileDirExist(dirPath)
}
;Creates the parent dir if necessary
;TODO if path is a directory, this ensures that that dir exists
;simply: this ensures that the entire specified dir structure exists
EnsureDirExists(path)
{
;if path is a file, this ensures that the parent dir exists
dir:=ParentDir(path)
;figure out if it is a file or dir
;split off filename if applicable
FileCreateDir, %dir%
}
;TESTME
;Gets the parent directory of the specified directory
ParentDir(fileOrFolder)
{
fileOrFolder := RegExReplace(fileOrFolder, "(\\|/)", "\")
if (StringRight(fileOrFolder, 1) == "\")
fileOrFolder:= StringTrimRight(fileOrFolder, 1)
RegexMatch(fileOrFolder, "^.*\\", returned)
return returned
}
;}}}
;{{{ INI Functions
;TESTME
IniWrite(file, section, key, value)
{
;sanitize key and value (replace with NAK to indicate an error)
key := RegExReplace(key, "(\r|\n)", "?")
value := RegExReplace(value, "(\r|\n)", "?")
EnsureDirExists(file)
;TODO put this in the read write and delete fcns
if (file == "")
fatalErrord(A_ThisFunc, A_ThisLine, A_ScriptName, "no filename was provided for writing the ini to")
if (key == "")
fatalErrord(A_ThisFunc, A_ThisLine, A_ScriptName, "no key was provided for writing the ini value to")
if (section == "")
section:="default"
IniWrite, %value%, %file%, %section%, %key%
;TODO test if the file is there
if NOT FileExist(file)
{
errord("silent", "wrote to ini file, but it doesn't exist", file)
Sleep, 500
return "ERROR"
}
}
IniDelete(file, section, key="")
{
;sanitize key and value (replace with NAK to indicate an error)
key := RegExReplace(key, "(\r|\n)", "?")
;EnsureDirExists(file)
if (file == "")
fatalErrord(A_ThisFunc, A_ThisLine, A_ScriptName, "no filename was provided for deleting the ini value from")
;if (key == "")
;fatalErrord(A_ThisFunc, A_ThisLine, A_ScriptName, "no key was provided for deleting the ini value from")
if (section == "")
section:="default"
if (key == "")
IniDelete, %file%, %section%
else
IniDelete, %file%, %section%, %key%
;TODO perhaps we should return what the old value was?
;or maybe that would be stupid
}
IniRead(file, section, key, Default = "ERROR")
{
;sanitize key and value (replace with NAK to indicate an error)
;TODO get a better value!!! NAK was bad, but ? would probably be used...
key := RegExReplace(key, "(\r|\n)", "?")
;EnsureDirExists(file)
if (file == "")
fatalErrord(A_ThisFunc, A_ThisLine, A_ScriptName, "no filename was provided for reading the ini value from")
if (key == "")
fatalErrord(A_ThisFunc, A_ThisLine, A_ScriptName, "no key was provided for reading the ini value from")
if (section == "")
section:="default"
IniRead, value, %file%, %section%, %key%, %Default%
Return, value
}
;ok, these two aren't actually rewrites of things that are core
; but these functions probably should have been core
#include thirdParty/ini.ahk
IniListAllSections(file)
{
content := FileRead(file)
return ini_getAllSectionNames(content)
}
IniListAllKeys(file, section="") ;defaults to all sections
{
content := FileRead(file)
return ini_getAllKeyNames(content, section)
}
;}}}
;{{{ Process Manipulation
GetPID(exeName)
{
Process, Exist, %exeName%
return ERRORLEVEL
}
ProcessExist(exeName)
{
Process, Exist, %exeName%
return !!ERRORLEVEL
}
ProcessClose(exeName)
{
Process, Close, %exeName%
}
ProcessCloseAll(exeName)
{
cmd=taskkill /im %exeName% /f
CmdRet_RunReturn(cmd)
;FIXME
;this waits for all processes to get closed...
;something like "taskkill /f /im perl.exe" would be best, but that would require external libs
;another solution is to just get a list of all processes with that name and ProcessClose() each one
; that would not wait for each one to close
while ProcessExist(exeName)
{
ProcessClose(exeName)
Sleep, 100
}
}
;sometimes ProcessCloseAll will fail miserably and just hang. If you use this, it will try to close everything many times, but won't hang
ProcessCloseFifty(exeName)
{
Loop, 50
ProcessClose(exeName)
cmd=taskkill /im %exeName% /f
CmdRet_RunReturn(cmd)
}
;}}}
;{{{ String Manipulation
StringReplace(ByRef InputVar, SearchText, ReplaceText = "", All = "A") {
StringReplace, v, InputVar, %SearchText%, %ReplaceText%, %All%
Return, v
}
;}}}
;{{{ Misc
Reload()
{
Reload
Sleep, 60000 ;noticed that reload doesn't reload instantly
}
;}}}
;iniF below this point ========
;TODO this does not belong in the rewrites lib... this needs to be in a lib by itself, and posted on the AHK site
;{{{ viewable iniFolder in one INI
ViewableIniFolder(iniFolder, viewableIniDestination)
{
;TODO, make this read all into memory, then output the sucker, rather than making tons of R/Ws on the HD
;FileRead, ini, %ConfigFilePath%
;value := ini_getValue(ini, "Config", "Started")
;ini_replaceValue(ini, "Config", "Started", value)
;updateConfigFile(ConfigFilePath, ini)
;fix up the incoming params
if NOT FileDirExist(iniFolder)
fatalErrord("", "you did not specify an iniFolder that exists already", iniFolder)
iniFolder := EnsureEndsWith(iniFolder, "\")
destIni := viewableIniDestination
;TESTME if we don't need to recreate the INI, then don't bother doing the regen
;note that if some of the IniF files are removed, the ViewableIni will not be regenerated
if NOT FileExist(destIni)
needToRegen := true
lastTimeGenerated := FileGetTime(destIni)
Loop, %iniFolder%*.ini
{
thisIniFile := A_LoopFileFullPath
if ( FileGetTime(thisIniFile) > lastTimeGenerated)
needToRegen := true
}
if NOT needToRegen
return
FileDelete(destIni)
sections:=IniFolderListAllSections(iniFolder)
Loop, parse, sections, CSV
{
thisSection := A_LoopField
keys:=IniFolderListAllKeys(iniFolder, thisSection)
Loop, parse, keys, CSV
{
thisKey := A_LoopField
if NOT thisKey
{
;FIXME what the heck!? the key should never be null... make this less ghetto
;not exactly sure why this is null so often
;errord("notimeout", iniFolder, thisSection, thisKey)
continue
}
value := IniFolderRead(iniFolder, thisSection, thisKey)
IniWrite(destIni, thisSection, thisKey, value)
}
}
;addtotrace("finished making viewable inif")
}
;}}}
;{{{ iniFolder Library
IniFolderRead(iniFolder, section, key)
{
iniFolder := EnsureEndsWith(iniFolder, "\")
timestamp := CurrentTime("hyphenated")
datestamp := CurrentTime("hyphendate")
keyValue=%key%-value
keyDate=%key%-timestamp
;search through all applicable files to get the most recently inserted value
maxValue=ERROR
maxDate=0
Loop, %iniFolder%*.ini
{
thisIniFile := A_LoopFileFullPath
thisValue := IniRead(thisIniFile, section, keyValue)
thisTimestamp := IniRead(thisIniFile, section, keyDate)
thisTimestamp := DeFormatTime(thisTimestamp)
if (thisTimestamp > maxDate)
{
maxValue := thisValue
maxDate := thisTimestamp
}
}
return maxValue
}
IniFolderWrite(iniFolder, section, key, value)
{
iniFolder := EnsureEndsWith(iniFolder, "\")
timestamp := CurrentTime("hyphenated")
datestamp := CurrentTime("hyphendate")
modMinute := "ModMin" . Mod(A_Min, 5)
;myIniFile=%iniFolder%%datestamp%-%A_ComputerName%.ini
myIniFile=%iniFolder%%datestamp%-%modMinute%-%A_ComputerName%.ini
keyValue=%key%-value
keyDate=%key%-timestamp
IniWrite(myIniFile, section, keyValue, value)
IniWrite(myIniFile, section, keyDate, timestamp)
}
IniFolderListAllSections(iniFolder)
{
iniFolder := EnsureEndsWith(iniFolder, "\")
Loop, %iniFolder%*.ini
{
thisIniFile := A_LoopFileFullPath
if (strlen(returned) != 0)
returned .= ","
returned .= IniListAllSections(thisIniFile)
}
;dedup
Sort MyVar, N U D, ; Sort numerically, dedup, use comma as delimiter.
return returned
}
;TESTME
IniFolderListAllKeys(iniFolder, section="") ;defaults to all sections
{
;deleting keys or sections is not allowed, because that will be a lot of work (you'd have to mark as inactive instead)
iniFolder := EnsureEndsWith(iniFolder, "\")
Loop, %iniFolder%*.ini
{
thisIniFile := A_LoopFileFullPath
if (strlen(returned) != 0)
returned .= ","
returned .= IniListAllKeys(thisIniFile, section)
}
;np=[^()]
;haystack=\((%notParen%+)\)\-\(%notParen%+\)
;returned := RegExReplace(returned, haystack, "$1")
;returned := RegExReplace(returned, "\-(value|timestamp)$")
;FIXME make this a little less ghetto, so that I can use "-value" and "-timestamp" in my keys
returned := RegExReplace(returned, "\-(value|timestamp)")
;dedup
Sort MyVar, N U D, ; Sort numerically, dedup, use comma as delimiter.
return returned
}
;}}}
;{{{ archive iniFolder old parts
ArchiveOldInifParts(IniFolder)
{
;THIS PART IS AN OLD HOLDOVER FROM THE FIREFLY DAYS
; back when we needed to make sure that all old fees were added before we archived
;if NOT IsBot()
;fatalErrord("This function is only designed for the Bot", A_LineNumber, A_ScriptName, A_ThisFunc, A_ThisLabel)
;fatalIfNotThisPC("BAUSTIANVM")
datestamp := CurrentTime("hyphendate")
needle=^%datestamp%
iniFolder := EnsureEndsWith(iniFolder, "\")
Loop, %IniFolder%*
{
deout .= "`n"
shouldArchive := true
destination=%IniFolder%archive\%A_LoopFileName%
if RegExMatch(A_LoopFileName, needle)
shouldArchive := false
if shouldArchive
{
deout .= "ARCHIVE "
FileMove(A_LoopFileFullPath, destination)
archivedFile := true
}
deout .= A_LoopFileFullPath
deout .= " moved to "
deout .= destination
}
;debug("errord nolog", deout)
if archivedFile
{
AddToTrace("yellow line - archived old fees files")
QuickFileOutput("archived files:`n" . deout)
}
}
;}}}
;{{{ former Firefly Check-Ins (monitoring the bot)
ScriptCheckin(CurrentStatus)
{
iniFolder:=GetPath("FireflyCheckinIniFolder")
;doing the checkin with fewer arguments
whoIsCheckingIn := A_ComputerName . "_" . A_ScriptName
iniPP("FireflyCheckin, yellow line - " . whoIsCheckingIn)
iniFolderWrite(iniFolder, "ReadableCheckin", whoIsCheckingIn, CurrentTime("hyphenated"))
iniFolderWrite(iniFolder, "TickCheckin", whoIsCheckingIn, A_TickCount)
iniFolderWrite(iniFolder, "Status", whoIsCheckingIn, CurrentStatus)
}
;}}}