forked from kimvais/bat-tf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.tf
More file actions
293 lines (279 loc) · 9.06 KB
/
array.tf
File metadata and controls
293 lines (279 loc) · 9.06 KB
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
/loauyded array.tf
;========================================================================
; get_array() / get_2array()
;
; Retrieves data from a virtual array.
; USAGE:
; x := get_array("array_name", index)
; x := get_2array("array_name", index, index2)
;
; x : Value returned.
; "array_name" : Name of the array.
; index : Element.
; index2 : Element of index (get_2array only).
;========================================================================
/def get_array = \
/let pedantic_tmp=%pedantic%;\
/set pedantic=off%;\
/return _array_%1_%2%;\
/set pedantic=%pedantic_tmp
/def get_2array = \
/let pedantic_tmp=%pedantic%;\
/set pedantic=off%;\
/return _array_%1_%2_%3%;\
/set pedantic=%pedantic_tmp
;========================================================================
; put_array() / put_2array()
;
; Stores data into a virtual array
; USAGE:
; put_array("array_name", index, value)
; put_2array("array_name2", index, index2, value)
;
; "array_name" : Name of the array.
; index : Element.
; index2 : Element of index (put_2array only).
; value : Data you're placing into the array. Can be anything you want.
;========================================================================
/def put_array = \
/eval /test _array_%1_%2 := strcat({3})
/def put_2array = \
/eval /test _array_%1_%2_%3 := strcat({4})
;========================================================================
; tfwrite_array() / tfwrite_2array()
;
; Writes a virtual array to a disk file.
; USAGE:
; tfwrite_array(file_variable, "array_name", start_index, size)
; tfwrite_2array(file_variable, "array_name", index, start_index, size)
;
; file_variable : File variable of a tfopened file.
; "array_name" : Name of the array.
; start_index : The element you want to start writing from.
; index : Start_index of index [first dimension] (tfwrite_2array only)
; Size : Number of elements to write.
;
; NOTES: tfwrite_2array() can't write the whole array to disk. You must
; write each dimension at a time.
;========================================================================
/def tfwrite_array = \
/let file_ %{1} %;\
/let array_ %{2} %;\
/let start_ %{3} %;\
/let size_ %{4} %;\
/let count_ 0 %;\
/WHILE (++count_ <= size_) \
/test tfwrite(file_, get_array(array_, start_)) %;\
/test ++start_ %;\
/DONE
/def tfwrite_2array = \
/let file_ %{1} %;\
/let array_ %{2} %;\
/let index_ %{3} %;\
/let start_ %{4} %;\
/let size_ %{5} %;\
/let count_ 0 %;\
/WHILE (++count_ <= size_) \
/test tfwrite(file_, get_2array(array_, index_, start_)) %;\
/test ++start_ %;\
/DONE
;========================================================================
; tfread_array() / tfread_2array()
;
; reads an array file from disk
; USAGE:
; x := tfread_array(file_variable, "array_name", start_index, size)
; x := tfread_2array(file_variable, "array_name", index, start_index, size)
;
; x : Number of records read.
; file_variable : File variable of a tfopened file.
; "array_name" : Name of the array.
; start_index : Starting element you want to read into.
; index : Start_index of index [first dimension] (tfread_2array only)
; size : Number of elements to read.
;========================================================================
/def tfread_array = \
/let file_ %{1} %;\
/let array_ %{2} %;\
/let start_ %{3} %;\
/let size_ %{4} %;\
/let count_ 0 %;\
/let done_ 0 %;\
/let err_ 0 %;\
/let st_ 0 %;\
/test st_ := '' %;\
/WHILE (!done_) \
/test err_ := tfread(file_, st_) %;\
/IF ( (err_ != -1) & (count_ < size_) ) \
/test put_array(array_, start_, st_) %;\
/test ++count_ %;\
/test ++start_ %;\
/ELSE \
/test done_ := 1 %;\
/ENDIF %;\
/DONE %;\
/RETURN count_
/def tfread_2array = \
/let file_ %{1} %;\
/let array_ %{2} %;\
/let index_ %{3} %;\
/let start_ %{4} %;\
/let size_ %{5} %;\
/let count_ 0 %;\
/let done_ 0 %;\
/let err_ 0 %;\
/let st_ 0 %;\
/test st_ := '' %;\
/WHILE (!done_) \
/test err_ := tfread(file_, st_) %;\
/IF ( (err_ != -1) & (count_ < size_) ) \
/test put_2array(array_, index_, start_, st_) %;\
/test ++count_ %;\
/test ++start_ %;\
/ELSE \
/test done_ := 1 %;\
/ENDIF %;\
/DONE %;\
/RETURN count_
;========================================================================
; strstr_array() / strstr_2array()
;
; Searches for a value in a virtual array and returns what element its found in.
; USAGE:
; x := strstr_array("array_name", start_index, size, value)
; x := strstr_2array("array_name", index, start_index, size, value)
;
; x : Element of "array_name" that value was found in.
; -1 is returned if value was not found.
; "array_name" : Name of the array.
; start_index : Element to start searching at
; index : Start_index of index [first dimension] (strstr_2arrat only).
; size : Number of elements to search.
; value : The item your searching for.
;
; NOTES: Strstr_2array can't search all dimensions, you must search each dimension
; at a time.
; If value = "" then it will return the first element that is blank.
;========================================================================
/def strstr_array = \
/let array_ %{1} %;\
/let start_ %{2} %;\
/let size_ %{3} %;\
/let value_ 0 %;\
/test value_ := {4} %;\
/let count_ 0 %;\
/let pos_ 0 %;\
/let st_ 0 %;\
/test st_ := '' %;\
/let element_ -1 %;\
/WHILE ( (++count_ <= size_) & (element_ = -1) ) \
/test st_ := get_array(array_, start_) %;\
/IF (value_ =~ '') \
/IF (st_ =~ '') \
/test element_ := start_ %;\
/ENDIF %;\
/ELSE \
/test pos_ := strstr(st_, value_) %;\
/IF (pos_ > -1) \
/test element_ := start_ %;\
/ENDIF %;\
/ENDIF %;\
/test ++start_ %;\
/DONE %;\
/RETURN element_
/def strstr_2array = \
/let array_ %{1} %;\
/let index_ %{2} %;\
/let start_ %{3} %;\
/let size_ %{4} %;\
/let value_ 0 %;\
/test value_ := {5} %;\
/let count_ 0 %;\
/let pos_ 0 %;\
/let st_ 0 %;\
/let element_ -1 %;\
/test st_ := '' %;\
/WHILE ( (++count_ <= size_) & (element_ = -1) ) \
/test st_ := get_2array(array_, index_, start_) %;\
/IF (value_ =~ '') \
/IF (st_ =~ '') \
/test element_ := start_ %;\
/ENDIF %;\
/ELSE \
/test pos_ := strstr(st_, value_) %;\
/IF (pos_ > -1) \
/test element_ := start_ %;\
/ENDIF %;\
/ENDIF %;\
/test ++start_ %;\
/DONE %;\
/RETURN element_
;========================================================================
; purge_array()
;
; Purges a virtual array made by get_array() and put_array()
; USAGE:
; purge_array("array_name")
;
; "array_name" : Name of array you want to delete from memory.
;
; NOTES: Purge_array() deletes the whole entire virtual array. It
; can be a double dimensioned array as well.
;========================================================================
/def purge_array = \
/IF ({1} !~ '') \
/quote -S /unset `/listvar -mglob -s _array_%1* %;\
/ENDIF %;\
;========================================================================
; list_array() / list_2array()
;
; List an array in an easy to read format.
; USAGE:
; list_array("array_name", startindex)
; list_2array("array_name", startindex1, startindex2)
; /list_array <array_name> <startindex>
; /list_2array <array_name> <startindex1> <startindex2>
;
; "array_name" : Name of the array you want to list. (function form only)
; startindex : Starting element you want to list from.
; startindex1 : Starting element of startindex2 you want to list from.
; startindex2 : Starting element of startindex1.
;========================================================================
/def list_array = \
/let name_ %{1} %;\
/let startindex_ 0 %;\
/test startindex_ := {2} %;\
/let index_ 0 %;\
/def list_array2 = \
/let rawname_ %%{1} %%;\
/test index_ := substr(rawname_, strlen(name_) + 8, 256) %%;\
/IF (index_ >= startindex_) \
/test echo(strcat(name_, '[', index_, '] :=', get_array(name_, index_))) %%;\
/ENDIF %;\
/quote -S /test list_array2("`"/listvar -mglob -s _array_%1_*"") %;\
/undef list_array2
/def list_2array = \
/let i2F_ 0 %;\
/let name_ %{1} %;\
/let startindex1_ %{2} %;\
/let startindex2_ %{3} %;\
/let pos_ 0 %;\
/let index1_ 0 %;\
/let index2_ 0 %;\
/let rawindex_ 0 %;\
/def list_2array2 = \
/let rawname_ %%{1} %%;\
/test rawindex_ := substr(rawname_, strlen(name_) + 8, 256) %%;\
/test pos_ := strstr(rawindex_, '_') %%;\
/test index1_ := substr(rawindex_, 0, pos_) %%;\
/test index2_ := substr(rawindex_, pos_ + 1, 255) %%;\
/IF (index1_ >= startindex1_) \
/IF ( (i2F_ = 0) & (index2_ >= startindex2_) )\
/test i2F_ := 1 %%;\
/ENDIF %%;\
/IF (i2F_ = 1) \
/test echo(strcat(name_, '[', index1_, '][', index2_, '] :=', get_2array(name_, index1_, index2_))) %%;\
/ENDIF %%;\
/ENDIF %;\
/quote -S /test list_2array2("`"/listvar -mglob -s _array_%1_*"") %;\
/undef list_2array2