forked from Ixiko/AHK-libs-and-classes-collection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjtostring.ahk
142 lines (122 loc) · 4.68 KB
/
objtostring.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
;===Description=========================================================================
/*
Object from/to file or string - another way to set/get/edit/store data structures. By Learning one.
http://www.autohotkey.com/board/topic/66496-object-fromto-file-or-string-data-structures/
Very easy for both humans and machines to read and write. Fast, simple, flexible.
Hierarchy in data structure string is defined by using indentation, more precisely; tab character. Can be changed.
Functions, obligatory parameters, and return values:
ObjFromFile(FilePath) ; creates object from file. Returns: object
ObjToFile(Object, FilePath) ; saves object to file. Returns: 1 if there was a problem or 0 otherwise
ObjFromStr(String) ; creates object from string. Returns: object
ObjToStr(Object) ; converts object to string. Returns: string
*/
/*
;===How to use example==================================================================
DataPath := A_ScriptDir "\AppData.txt"
if !FileExist(DataPath)
Gosub, CreateDemoFile ; first time creation
oData := ObjFromFile(DataPath) ; creates (loads) object from file
MsgBox, % oData.Version "`n" oData.Menu.Item1.Action "`n" oData["Tip of the day?"] ; get some data
MsgBox, % ObjToStr(oData) ; get and see all data - converts whole object to string
oData.Version := "3.33" ; modify
;oData.Menu.Remove("Item2") ; remove (delete)
oData.Sounds := "Yes" ; add
MsgBox, % ObjToStr(oData) ; see all after modifications
ObjToFile(oData, DataPath) ; save modified object (data structure) back to file. Ready to load next time.
ExitApp
;===Subroutines=========================================================================
CreateDemoFile:
SomeData = ; demo data structure
(
Date modified = 05.05.2011.
Hotkeys
Win + C = Calculator
Win + P = Paint
Menu
Item1
Action = C:\
Text = C disk
Item2
Action = C:\Script.ahk
Icon = AHK.png
Skin = Black
Tip of the day? = No
Version = 1.00
)
FileAppend, %SomeData%, %DataPath%, UTF-8 ; save to file
SomeData := "" ; not needed any more
return
;===Hotkeys=============================================================================
Escape::ExitApp
*/
;===Functions===========================================================================
ObjFromStr(String, Indent="`t", Rows="`n", Equal="=") { ; creates object from string which represents data structure.
obj := Object(), kn := Object() ; kn means "key names" - simple array object
IndentLen := StrLen(Indent)
Loop, parse, String, %Rows%
{
if A_LoopField is space
continue
Field := RTrim(A_LoopField, " `t`r")
CurLevel := 1, k := "", v := "" ; clear, reset
While (SubStr(Field,1,IndentLen) = Indent) {
StringTrimLeft, Field, Field, %IndentLen%
CurLevel++
}
EqualPos := InStr(Field, Equal)
if (EqualPos = 0)
k := Field
else
k := SubStr(Field, 1, EqualPos-1), v := SubStr(Field, EqualPos+1)
k := Trim(k, " `t`r"), v := Trim(v, " `t`r")
kn[CurLevel] := k
if !(EqualPos = 0) { ; key-value
if (CurLevel = 1)
obj[kn.1] := v
else if (CurLevel = 2)
obj[kn.1][k] := v
else if (CurLevel = 3)
obj[kn.1][kn.2][k] := v
else if (CurLevel = 4)
obj[kn.1][kn.2][kn.3][k] := v
else if (CurLevel = 5)
obj[kn.1][kn.2][kn.3][kn.4][k] := v
else if (CurLevel = 6)
obj[kn.1][kn.2][kn.3][kn.4][kn.5][k] := v
else if (CurLevel = 7)
obj[kn.1][kn.2][kn.3][kn.4][kn.5][kn.6][k] := v ; etc...
}
else { ; sub-object
if (CurLevel = 1)
obj.Insert(kn.1,Object())
else if (CurLevel = 2)
obj[kn.1].Insert(kn.2,Object())
else if (CurLevel = 3)
obj[kn.1][kn.2].Insert(kn.3,Object())
else if (CurLevel = 4)
obj[kn.1][kn.2][kn.3].Insert(kn.4,Object())
else if (CurLevel = 5)
obj[kn.1][kn.2][kn.3][kn.4].Insert(kn.5,Object())
else if (CurLevel = 6)
obj[kn.1][kn.2][kn.3][kn.4][kn.5].Insert(kn.6,Object()) ; etc...
}
}
return obj
}
ObjToStr(Obj, Indent="`t", Rows="`n", Equal=" = ", Depth=7, CurIndent="") { ; converts object to string
For k,v in Obj
ToReturn .= CurIndent . k . (IsObject(v) && depth>1 ? Rows . ObjToStr(v, Indent, Rows, Equal, Depth-1, CurIndent . Indent) : Equal . v) . Rows
return RTrim(ToReturn, Rows)
} ; http://www.autohotkey.com/forum/post-426623.html#426623
ObjFromFile(FilePath, Indent="`t", Rows="`n", Equal="=") { ; creates object from file
if !FileExist(FilePath)
return
FileRead, String, %FilePath%
return ObjFromStr(String, Indent, Rows, Equal) ; creates and returns object from string
}
ObjToFile(Obj, FilePath, Indent="`t", Rows="`n", Equal=" = ", Depth=7) { ; saves object to file
if FileExist(FilePath)
FileDelete, %FilePath% ; delete old
FileAppend, % ObjToStr(Obj, Indent, Rows, Equal, Depth), %FilePath%, UTF-8 ; store new
return ErrorLevel
}