forked from Ixiko/AHK-libs-and-classes-collection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDBase.ahk
516 lines (470 loc) · 22.9 KB
/
DBase.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
505
506
507
508
509
510
511
512
513
; #FUNCTION# ====================================================================================================================
; Name .......... : dBaseDll 1.0
; Description ... : create, read, write dBaseIII & dBaseIV dbf files
; : create, read, write DBaseIII & dBaseIV dbt files
; Author ........ : Jpam
; Modified ...... : Modified by just me for AHK
; Email ......... : [email protected]
; Lastupdate .... : 31-12-2015
; ====================================================================================================================
; #include-once
; OnAutoItExitRegister("Close_DLL")
; #Globals# =====================================================================================================================
; Version const
; ====================================================================================================================
Global DBASEIII := 0x03 ; dBaseIII w/o memo
Global DBASEIII_MEMO:= 0x83 ; dBaseIII memo
Global DBASEIV := 0x04 ; dBaseIV w/o memo
Global DBASEIV_MEMO:= 0x8B ; dBaseIV memo
dbasedll:= "\include\dbasedll.dll"
DBase_CreateDBF(pFileName, bVersion) { ;-- creates new database file
/*
; #FUNCTION# ====================================================================================================================
; Name ..........: CreateDBF
; Description ...: creates new database file
; Syntax ........: CreateDBF(pFileName, bVersion)
; Parameters ....: pFileName - filename for dbf
; bVersion - version info $DBASEIII_MEMO / $DBASEIV_MEMO
; Return values .: handle to database
; Author ........: Jpam
; Modified ......:
; Remarks .......: CreateDBF creates automatic the dbt file if $DBASEIII_MEMO / $DBASEIV_MEMO is detected
; Related .......:
; Link ..........:
; Example .......: $hBase = CreateDBF("Sample.dbf", $DBASEIII_MEMO)
; ====================================================================================================================
*/
Return DllCall("dbasedll.dll\CreateDBF", "Str", pFileName, "UChar", bVersion, "UPtr")
}
DBase_OpenDBF(pFileName) {
/*
; #FUNCTION# ====================================================================================================================
; Name ..........: OpenDBF
; Description ...:
; Syntax ........: OpenDBF(pFileName)
; Parameters ....: pFileName - filename dbf file
; Return values .: Handle dbf file
; Author ........: Jpam
; Modified ......:
; Remarks .......: OpenDBF opens automatic the related dbt file if the file exist
; Related .......:
; Link ..........:
; Example .......: $hBase = OpenDBF("sample.dbf")
; ====================================================================================================================
*/
Return DllCall(dbasedll . "\OpenDBF", "Str", pFileName, "UPtr")
}
DBase_AddField(hBase, fldName, fldType, fldLen, fldDecimal, fldWorkAreaId, fldFlag) { ;-- add new field to dbf file
/*
; #FUNCTION# ====================================================================================================================
; Name ..........: AddField
; Description ...: add new field to dbf file
; Syntax ........: AddField(hBase, fldName, fldType, fldLen, fldDecimal, fldWorkAreaId, fldFlag)
; Parameters ....: hBase - handle returned from CreateDBF
; fldName - name for new field
; fldType - Field type in ASCII (C, D, L, M, or N) (dBaseIII)
; - Field type in ASCII (C, D, F, L, M, or N) (dBaseIV)
; - Field type in ASCII (B, C, D, F, G, L, M, or N) (dBaseV for dos)
; - Field type in ASCII (B, C, D, F, G, L, M, or N) (dBaseV for windows)
; fldLen - field lenght in bytes
; fldDecimal - decimal point location
; fldWorkAreaId - mostly not used
; fldFlag - mostly not used
; Return values .: None
; Author ........: Jpam
; Modified ......:
; Remarks .......: warning ! don't use AddField() if your dbf file allready has records, your database become corupted !
; Related .......:
; Link ..........:
; Example .......: AddField($hBase, "city", "C", 25, 0, 0, 0)
; ====================================================================================================================
*/
DllCall("dbasedll.dll\AddField", "Ptr", hBase
, "Str", fldName
, "UChar", Asc(fldType) ; (StringToBinary(fldType),
, "UChar", fldLen
, "UChar", fldDecimal
, "UChar", fldWorkAreaId
, "UChar", fldFlag)
}
DBase_GetFieldName(hBase, fldNr) { ;-- get the fieldname from fieldnumber
/*
; #FUNCTION# =======================================================================================================
; Name ..........: GetFieldName
; Description ...: get the fieldname from fieldnumber
; Syntax ........: GetFieldName(hBase, fldNr)
; Parameters ....: hBase - handle returned from OpenDBF
; fldNr - field number
; Return values .: pointer to buffer containing fieldname
; Author ........: Jpam
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: $fldName = GetFieldName($hBase, 5)
; ================================================================================================================
*/
pFldname := DllCall("dbasedll.dll\GetFieldName", "Ptr", hBase, "UInt", fldNr, "Str")
Return pFldname
}
DBase_GetFieldType(hBase, fldNr) { ;-- get the fieldtype from fieldnumber
/*
; #FUNCTION# ====================================================================================================================
; Name ..........: GetFieldType
; Description ...: get the fieldtype from fieldnumber
; Syntax ........: GetFieldType(hBase, fldNr)
; Parameters ....: hBase - handle returned from OpenDBF
; fldNr - field number
; Return values .: pointer to buffer containg the fieldtype
; Author ........: Jpam
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: $fldType = GetFieldType($hBase, 3)
; ===================================================================================================================
*/
pFldType := DllCall("dbasedll.dll\GetFieldType", "Ptr", hBase, "UInt", fldNr, "Str")
Return pFldType
}
DBase_GetFieldLenght(hBase, fldNr) { ;-- GetFieldLenght returns byte lenght from field
/*
; #FUNCTION# ====================================================================================================================
; Name ..........: GetFieldLenght
; Description ...: GetFieldLenght returns byte lenght from field
; Syntax ........: GetFieldLenght(hBase, fldNr)
; Parameters ....: hBase - handle returned from OpenDBF
; fldNr - field number
; Return values .: dword containing fieldlenght
; Author ........: Jpam
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: $fldLen = GetFieldLenght($hBase, 1)
; =================================================================================================================
*/
pFldLen := DllCall("dbasedll.dll\GetFieldLenght", "Ptr", hBase, "UInt", fldNr, "UInt")
Return pFldLen
}
DBase_GetFieldDecimal(hBase, fldNr) { ;-- get decimal point location
/*
; #FUNCTION# ====================================================================================================================
; Name ..........: GetFieldDecimal
; Description ...: get decimal point location
; Syntax ........: GetFieldDecimal(hBase, fldNr)
; Parameters ....: hBase - handle returned from OpenDBF
; fldNr - field number
; Return values .: dword containing decimal point number
; Author ........: Jpam
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: $fldDec = GetFieldDecimal($hBase, 3)
; =====================================================================================================================
*/
pFldDec := DllCall("dbasedll.dll\GetFieldDecimal", "Ptr", hBase, "UInt", fldNr, "UInt")
Return pFldDec
}
DBase_GetRecordCount(hBase) { ;-- get the number of records in dbf file
/*
; #FUNCTION# ====================================================================================================================
; Name ..........: GetRecordCount
; Description ...: get the number of records in dbf file
; Syntax ........: GetRecordCount(hBase)
; Parameters ....: hBase - handle returned from OpenDBF
; Return values .: dword containing number of records in dbf file
; Author ........: Jpam
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: $recCnt = GetRecordCount($hBase)
; ================================================================================================================
*/
pRecCnt := DllCall("dbasedll.dll\GetRecordCount", "Ptr", hBase, "UInt")
Return pRecCnt
}
DBase_GetFieldCount(hBase) { ;-- get number of fields in dbf file
/*
; #FUNCTION# ====================================================================================================================
; Name ..........: GetFieldCount
; Description ...: get number of fields in dbf file
; Syntax ........: GetFieldCount(hBase)
; Parameters ....: hBase - handle returned from OpenDBF
; Return values .: dword containing number of fields in dbf file
; Author ........: Jpam
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: #fldCnt = GetFieldCount($hBase)
; ==================================================================================================================
*/
pFldCnt := DllCall("dbasedll.dll\GetFieldCount", "Ptr", hBase, "UInt")
Return pFldCnt
}
DBase_AddRecord(hBase) { ;-- write new blank record to dbf file
/*
; #FUNCTION# ====================================================================================================================
; Name ..........: AddRecord
; Description ...: write new blank record to dbf file
; Syntax ........: AddRecord(hBase)
; Parameters ....: hBase - handle returned from CreateDBF or OpenDBF
; Return values .: None
; Author ........: Jpam
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: AddRecord($hBase)
; ====================================================================================================================
*/
DllCall("dbasedll.dll\AddRecord", "Ptr", hBase)
}
DBase_GetSubRecord(hBase, recNr, fldNr) { ;-- get record data
/*
; #FUNCTION# ====================================================================================================================
; Name ..........: GetSubRecord
; Description ...: get record data
; Syntax ........: GetSubRecord(hBase, recNr, fldNr)
; Parameters ....: hBase - handle returned from OpenDBF
; recNr - record number
; fldNr - fieldnumber
; Return values .: pointer buffer containing record data (failure returns -1)
; Author ........: Jpam
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: $rec = GetSubRecord($hBase, 0, 2)
; ==========================================================================================================
*/
pRec := DllCall("dbasedll.dll\GetSubRecord", "Ptr", hBase, "UInt", recNr, "UInt", fldNr, "Str")
Return pRec
}
DBase_PutSubRecord(hBase, pValue, recNr, fldNr) { ;-- write sub record to database
/*
; #FUNCTION# ====================================================================================================================
; Name ..........: PutSubRecord
; Description ...: write sub record to database
; Syntax ........: PutSubRecord(hBase, pValue, recNr, fldNr)
; Parameters ....: hBase - handle returned from CreateDBF or OpenDBF
; pValue - value to write
; recNr - record number
; fldNr - fieldnumber
; Return values .: None
; Author ........: Jpam
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: PutSubRecord($hBase, "Amsterdam", 1, 2)
; =================================================================================================
*/
DllCall("dbasedll.dll\PutSubRecord", "Ptr", hBase, "Str", pValue, "UInt", recNr, "UInt", fldNr)
}
DBase_DeleteRecord(hBase, recNr) { ;-- mark record as deleted
/*
; #FUNCTION# ====================================================================================================================
; Name ..........: DeleteRecord
; Description ...: mark record as deleted
; Syntax ........: DeleteRecord(hBase, recNr)
; Parameters ....: hBase - handle returned from OpenDBF
; recNr - record number
; Return values .: None
; Author ........: Jpam
; Modified ......:
; Remarks .......: use func Pack() to cleanup all deleted records
; Related .......:
; Link ..........:
; Example .......: DeleteRecord($hBase, 15)
; =====================================================================================================================
*/
DllCall("dbasedll.dll\DeleteRecord", "Ptr", hBase, "UInt", recNr)
}
DBase_UnDeleteRecord(hBase, recNr) { ;-- unMark the marked record
/*
; #FUNCTION# ====================================================================================================================
; Name ..........: UnDeleteRecord
; Description ...: unMark the marked record
; Syntax ........: UnDeleteRecord(hBase, recNr)
; Parameters ....: hBase - handle returned from OpenDBF
; recNr - record number
; Return values .: None
; Author ........: Jpam
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: UnDeleteRecord($hBase, 15)
; ===========================================================================================================
*/
DllCall("dbasedll.dll\UnDeleteRecord", "Ptr", hBase, "UInt", recNr)
}
DBase_Search(hBase, pStr, fld, pBuf, len) { ;-- searches the database for an specified value
/*
; #FUNCTION# ====================================================================================================================
; Name ..........: Search
; Description ...: searches the database for an specified value
; Syntax ........: Search(hBase, pStr, fld, pBuf, len)
; Parameters ....: hBase - handle returned from OpenDBF
; pStr - pointer zero terminated string
; fld - field number or -1
; pBuf - pointer buffer created with DllStructCreate("DWORD[100]")
; len - buffer length in bytes
; Return values .: number of records found in database
; Author ........: Jpam
; Modified ......:
; Remarks .......: pStr - can be number or alpha case-insensitive
; the dll supports wildcards * between chars or at the end of the string
; fld - need a field number to search specific field records or -1 to search the entire database
; fld - is protected in the dll for a valid field number , Search() will fail with error -1
; len - determine the amount of records you get returned from the dll
; Related .......:
; Link ..........:
; Example .......: $Array = DllStructCreate("DWORD[100]") *this buffer gives you a max of 100 results*
; $recCnt = Search($hDbf, "New York*", -1, DllStructGetPtr($Array), DllStructGetSize($Array))
;
; $Array = DllStructCreate("DWORD[400]") *the len para is set to 40, only 10 results are returned*
; $recCnt = Search($hDbf, "New York*", -1, DllStructGetPtr($Array), 40)
; ===============================================================================================================================
*/
pRecArray := DllCall("dbasedll.dll\Search", "Ptr", hBase
, "Str", pStr
, "UInt", fld
, "Ptr", pBuf
, "UInt", len
, "Ptr")
Return pRecArray
}
DBase_Pack(hBase) { ;-- deletes all marked records
/*
; #FUNCTION# ====================================================================================================================
; Name ..........: Pack
; Description ...: deletes all marked records
; Syntax ........: Pack(hBase)
; Parameters ....: hBase - handle returned from OpenDBF
; Return values .: None
; Author ........: Jpam
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: Pack($hBase)
; ===============================================================================================================================
*/
DllCall("dbasedll.dll\Pack", "Ptr", hBase)
}
DBase_Zap(hBase) { ;-- cleanup database, deletes all records
/*
; #FUNCTION# ====================================================================================================================
; Name ..........: Zap
; Description ...: cleanup database, deletes all records
; Syntax ........: Zap(hBase)
; Parameters ....: hBase - handle returned from OpenDBF
; Return values .: None
; Author ........: Jpam
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: Zap($hBase)
; ===============================================================================================================================
*/
DllCall("dbasedll.dll\Zap", "Ptr", hBase)
}
DBase_LoadMemo(hBase, recNr, fldNr) { ;-- load memo from dbt or fpt file
/*
; #FUNCTION# ====================================================================================================================
; Name ..........: LoadMemo
; Description ...: load memo from dbt or fpt file
; Syntax ........: LoadMemo(hBase, recNr, fldNr)
; Parameters ....: hBase - handle returned from OpenDBF
; recNr - record number
; fldNr - field number
; Return values .: pointer buffer containing memo data
; Author ........: Jpam
; Modified ......:
; Remarks .......: internal code figures out by looking at the first byte of the dbf header if it's dealing with dbt or fpt memo files
; Related .......:
; Link ..........:
; Example .......: $memo = LoadMemo($hBase, $rec, $fld)
; ===============================================================================================================================
*/
pBuf = DllCall("dbasedll.dll\LoadMemo", "Ptr", hBase, "UInt", recNr, "UInt", fldNr, "Str")
Return pBuf
}
DBase_WriteMemo(hBase, pMemo, recNr, fldNr) { ;-- write memo to dbt file and updates the dbf memo record with blockNumber
/*
; #FUNCTION# ====================================================================================================================
; Name ..........: WriteMemo
; Description ...: write memo to dbt file and updates the dbf memo record with blockNumber
; Syntax ........: WriteMemo(hBase, pMemo, recNr, fldNr)
; Parameters ....: hBase - handle returned from OpenDBF
; pMemo - data to write
; recNr - record number
; fldNr - field number
; Return values .: None
; Author ........: Jpam
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: WriteMemo($hBase, $Memo, 5, 3)
; ===============================================================================================================================
*/
DllCall("dbasedll.dll\WriteMemo", "Ptr", hBase, "Str", pMemo, "UInt", recNr, "UInt", fldNr)
}
DBase_CloseDBF(hBase) { ;-- closes the database
/*
; #FUNCTION# ====================================================================================================================
; Name ..........: CloseDBF
; Description ...: closes the database
; Syntax ........: CloseDBF(hBase)
; Parameters ....: hBase - handle returned from OpenDBF
; Return values .: None
; Author ........: Jpam
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: CloseDBF($hBase)
; ===============================================================================================================================
*/
DllCall("dbasedll.dll\CloseDBF", "Ptr", hBase)
}
DBase_LoadDLL(P*) { ;-- Loads the database.dll on start-up and unloads it automatically when the script exits.
/*
; DBase_Close_DLL() {
;
; DllClose("dbasedll.dll\")
;
; }
; ===============================================================================================================================
; Name ..........: LoadDLL
; Description ...: Loads the database.dll on start-up and unloads it automatically when the script exits.
; Syntax ........: Do not call!!
; Parameters ....: None
; Return values .: None
; Author ........: just me
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: None
; ===============================================================================================================================
*/
Static DBaseMod := 0
Static Load := DBase_LoadDLL()
If (DBaseMod = 0) {
If !(DBaseMod := DllCall("LoadLibrary", "Str", "dbasedll.dll", "UPtr")) {
MsgBox, 262160, %A_ThisFunc%, The dbasedll.dll could not be loaded!`n`nThe program will exit!
ExitApp
}
OnExit("DBase_LoadDLL")
}
Else
DllCall("FreeLibrary", "Ptr", DBaseMod)
}