-
Notifications
You must be signed in to change notification settings - Fork 2
/
Utility.ahk
84 lines (74 loc) · 3.24 KB
/
Utility.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
#NoEnv
/*
Copyright 2011-2012 Anthony Zhang <azhang9@gmail.com>
This file is part of AutoHotkey.net Website Generator. Source code is available at <https://github.com/Uberi/AutoHotkey.net-Website-Generator>.
AutoHotkey.net Website Generator is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
CreateDirectory(Directory)
{
FileCreateDir, %Directory%
Return, ErrorLevel
}
;parses a given cache into a cache object
ReadCache(Cache)
{
Cache := Trim(Cache," `t`n") ;remove leading and trailing whitespace and newlines
Result := Object()
Loop, Parse, Cache, `n, %A_Space%`t ;loop through each cache entry
{
Entry := Object()
Position := InStr(A_LoopField,"`t"), URL := SubStr(A_LoopField,1,Position - 1), Field := SubStr(A_LoopField,Position + 1) ;extract the URL field
Position := InStr(Field,"`t"), Entry.Image := SubStr(Field,1,Position - 1), Field := SubStr(Field,Position + 1) ;extract the image field
Position := InStr(Field,"`t"), Entry.Source := SubStr(Field,1,Position - 1), Field := SubStr(Field,Position + 1) ;extract the source field
Entry.Description := Field ;extract the description field
Result[URL] := Entry ;add the entry to the cache object
}
Return, Result
}
;converts a cache object into the cache file format
SaveCache(Cache)
{
Result := ""
For URL, Entry In Cache
Result .= URL . "`t" . Entry.Image . "`t" . Entry.Source . "`t" . Entry.Description . "`n"
Return, SubStr(Result,1,-1)
}
;sorts an array of results by title
SortByTitle(InputObject)
{
;merge sort algorithm
MaxIndex := ObjMaxIndex(InputObject), (MaxIndex = "") ? (MaxIndex := 0) : ""
If MaxIndex < 2
Return, InputObject
Middle := MaxIndex >> 1, SortLeft := Object(), SortRight := Object()
Loop, %Middle%
ObjInsert(SortLeft,InputObject[A_Index]), ObjInsert(SortRight,InputObject[Middle + A_Index])
If (MaxIndex & 1)
ObjInsert(SortRight,InputObject[MaxIndex])
SortLeft := SortByTitle(SortLeft), SortRight := SortByTitle(SortRight), MaxRight := MaxIndex - Middle, LeftIndex := 1, RightIndex := 1, Result := Object()
Loop, %MaxIndex%
{
If (LeftIndex > Middle)
ObjInsert(Result,SortRight[RightIndex]), RightIndex ++
Else If ((RightIndex > MaxRight) || (SortLeft[LeftIndex].Title < SortRight[RightIndex].Title))
ObjInsert(Result,SortLeft[LeftIndex]), LeftIndex ++
Else
ObjInsert(Result,SortRight[RightIndex]), RightIndex ++
}
Return, Result
}
;converts a path into an absolute path
ExpandPath(Path)
{
Loop, %Path%, 2
Return, A_LoopFileLongPath
}