forked from camerb/AHKs
-
Notifications
You must be signed in to change notification settings - Fork 38
/
FcnLib-Rewrites (BaustianVM's conflicted copy 2012-10-08).ahk
283 lines (238 loc) · 7.24 KB
/
FcnLib-Rewrites (BaustianVM's conflicted copy 2012-10-08).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
;#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("wrote to ini file, but it doesn't exist", file)
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)
{
;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
}
}
;}}}
;{{{ 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
}
;}}}