-
Notifications
You must be signed in to change notification settings - Fork 2
/
FoxExtends.prg
1646 lines (1458 loc) · 47.3 KB
/
FoxExtends.prg
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Function newFoxExtends(tcPrefix, tcType)
Local lcMethodPrefix, lcType, i, lcChar, llUseClass
lcMethodPrefix = ''
IF TYPE('tcPrefix') == 'C'
LOCAL llOk
lcMethodPrefix = tcPrefix
llOk = .T.
For i = 1 To Len(lcMethodPrefix)
lcChar = Substr(lcMethodPrefix, i, 1)
If !Isalpha(lcChar) And !Isdigit(lcChar) And lcChar != '_'
llOk = .F.
Exit
Endif
Endfor
If !llOk
Messagebox("Invalid name for parameter [prefix].", 16, "Fox Extends error")
Return .F.
Endif
ENDIF
lcType = 'prg'
IF TYPE('tcType') == 'C'
lcType = LOWER(tcType)
If !Inlist(lcType, 'prg', 'obj')
Messagebox("[type] parameters must be: PRG or OBJ", 16, "Fox Extends error")
Return .F.
Endif
ENDIF
llUseClass = (lcType == 'obj')
* ========================================================================= *
* PUBLIC API
* ========================================================================= *
Local lcScript As Memo
TEXT TO lcScript NOSHOW TEXTMERGE PRETEXT 7
**********************************************************************
* VFP extended methods
**********************************************************************
#IFNDEF FUNCTION_ARG_VALUE_INVALID
#Define FUNCTION_ARG_VALUE_INVALID 11
#Endif
#IFNDEF TOO_FEW_ARGUMENTS
#Define TOO_FEW_ARGUMENTS 1229
#Endif
#IFNDEF JSONFOX_NOT_FOUND
#Define JSONFOX_NOT_FOUND 'JSONFOX.APP does not exist in your PATH() directories. Please make sure JSONFOX.APP can be found by your application.'
#Endif
#IFNDEF HASHTABLE_PARAMS_MISTMATCH
#Define HASHTABLE_PARAMS_MISTMATCH 'keys does not match values'
#Endif
#IFNDEF HASHTABLE_INVALID_KEY
#Define HASHTABLE_INVALID_KEY 'Invalid key'
#Endif
If Type('_vfp.foxExtendsRegEx') != 'O'
=AddProperty(_vfp, 'foxExtendsRegEx', Createobject("VBScript.RegExp"))
_vfp.foxExtendsRegEx.IgnoreCase = .T.
_vfp.foxExtendsRegEx.Global = .T.
Endif
If Type('_vfp.fxAnyToString') = 'U'
AddProperty(_vfp, 'fxAnyToString', .Null.)
Endif
_vfp.fxAnyToString = Createobject('AnyToString')
* ========================================================================================== *
* HELPER FUNCTIONS
* ========================================================================================== *
Function CHECKJSONAPP
* ¿DOES JSONFOX.APP EXIST?
If !File("JSONFOX.APP")
Error JSONFOX_NOT_FOUND
Return .F.
Endif
If Type('_SCREEN.JSON') == 'O'
_Screen.JSON = .Null.
=Removeproperty(_Screen, 'JSON')
Endif
Do "JSONFOX.APP"
Return .T.
Endfunc
* AnyToString
Define Class AnyToString As Custom
#Define USER_DEFINED_PEMS 'U'
#Define ALL_MEMBERS "PHGNUCIBR"
lCentury = .F.
cDateAct = ''
nOrden = 0
cFlags = ''
Function Init
This.lCentury = Set("Century") == "OFF"
This.cDateAct = Set("Date")
Set Century On
Set Date Ansi
Mvcount = 60000
Endfunc
Function ToString(toRefObj, tcFlags)
lPassByRef = .T.
Try
External Array toRefObj
Catch
lPassByRef = .F.
Endtry
This.cFlags = Evl(tcFlags, ALL_MEMBERS)
If lPassByRef
Return This.ANYTOSTR(@toRefObj)
Else
Return This.ANYTOSTR(toRefObj)
Endif
Endfunc
Function ANYTOSTR As Memo
Lparameters tValue As variant
Try
External Array tValue
Catch
Endtry
Do Case
Case Type("tValue", 1) = 'A'
Local k, j, lcArray
If Alen(tValue, 2) == 0
*# Unidimensional array
lcArray = '['
For k = 1 To Alen(tValue)
lcArray = lcArray + Iif(Len(lcArray) > 1, ',', '')
Try
=Acopy(tValue[k], aLista)
lcArray = lcArray + This.ANYTOSTR(@aLista)
Catch
lcArray = lcArray + This.ANYTOSTR(tValue[k])
Endtry
Endfor
lcArray = lcArray + ']'
Else
*# Multidimensional array support
lcArray = '['
For k = 1 To Alen(tValue, 1)
lcArray = lcArray + Iif(Len(lcArray) > 1, ',', '')
* # begin of rows
lcArray = lcArray + '['
For j = 1 To Alen(tValue, 2)
If j > 1
lcArray = lcArray + ','
Endif
Try
=Acopy(tValue[k, j], aLista)
lcArray = lcArray + This.ANYTOSTR(@aLista)
Catch
lcArray = lcArray + This.ANYTOSTR(tValue[k, j])
Endtry
Endfor
lcArray = lcArray + ']'
* # end of rows
Endfor
lcArray = lcArray + ']'
Endif
Return lcArray
Case Vartype(tValue) = 'O'
Local j, lcStr, lnTot
Local Array gaMembers(1)
lcStr = '{'
lnTot = Amembers(gaMembers, tValue, 0, This.cFlags)
For j=1 To lnTot
lcProp = Lower(Alltrim(gaMembers[j]))
lcStr = lcStr + Iif(Len(lcStr) > 1, ',', '') + '"' + lcProp + '":'
Try
=Acopy(tValue. &gaMembers[j], aCopia)
lcStr = lcStr + This.ANYTOSTR(@aCopia)
Catch
Try
lcStr = lcStr + This.ANYTOSTR(tValue. &gaMembers[j])
Catch
lcStr = lcStr + "{}"
Endtry
Endtry
Endfor
*//> Collection based class object support
llIsCollection = .F.
Try
llIsCollection = (tValue.BaseClass == "Collection" And tValue.Class == "Collection" And tValue.Name == "Collection")
Catch
Endtry
If llIsCollection
lcComma = Iif(Right(lcStr, 1) != '{', ',', '')
lcStr = lcStr + lcComma + '"Collection":['
For i=1 To tValue.Count
lcStr = lcStr + Iif(i>1,',','') + This.ANYTOSTR(tValue.Item(i))
Endfor
lcStr = lcStr + ']'
Endif
*//> Collection based class object support
lcStr = lcStr + '}'
Return lcStr
Otherwise
Return This.GETVALUE(tValue, Vartype(tValue))
Endcase
Endfunc
Function Destroy
If This.lCentury
Set Century Off
Endif
lcDateAct = This.cDateAct
Set Date &lcDateAct
Endfunc
Function GETVALUE As String
Lparameters tcValue As String, tctype As Character
Do Case
Case tctype $ "CDTBGMQVWX"
Do Case
Case tctype = "D"
tcValue = '"' + Strtran(Dtoc(tcValue), ".", "-") + '"'
Case tctype = "T"
tcValue = '"' + Strtran(Ttoc(tcValue), ".", "-") + '"'
Otherwise
If tctype = "X"
tcValue = "null"
Else
tcValue = This.getstring(tcValue)
Endif
Endcase
tcValue = Alltrim(tcValue)
Case tctype $ "YFIN"
tcValue = Strtran(Transform(tcValue), ',', '.')
Case tctype = "L"
tcValue = Iif(tcValue, "true", "false")
Endcase
Return tcValue
Endfunc
Function getString As String
Lparameters tcString As String, tlParseUtf8 As Boolean
tcString = Allt(tcString)
tcString = Strtran(tcString, '\', '\\' )
tcString = Strtran(tcString, Chr(9), '\t' )
tcString = Strtran(tcString, Chr(10), '\n' )
tcString = Strtran(tcString, Chr(13), '\r' )
tcString = Strtran(tcString, '"', '\"' )
If tlParseUtf8
tcString = Strtran(tcString,"&","\u0026")
tcString = Strtran(tcString,"+","\u002b")
tcString = Strtran(tcString,"-","\u002d")
tcString = Strtran(tcString,"#","\u0023")
tcString = Strtran(tcString,"%","\u0025")
tcString = Strtran(tcString,"²","\u00b2")
tcString = Strtran(tcString,'Ã ','\u00e0')
tcString = Strtran(tcString,'á','\u00e1')
tcString = Strtran(tcString,'è','\u00e8')
tcString = Strtran(tcString,'é','\u00e9')
tcString = Strtran(tcString,'ì','\u00ec')
tcString = Strtran(tcString,'Ã','\u00ed')
tcString = Strtran(tcString,'ò','\u00f2')
tcString = Strtran(tcString,'ó','\u00f3')
tcString = Strtran(tcString,'ù','\u00f9')
tcString = Strtran(tcString,'ú','\u00fa')
tcString = Strtran(tcString,'ü','\u00fc')
tcString = Strtran(tcString,'À','\u00c0')
tcString = Strtran(tcString,'Ã','\u00c1')
tcString = Strtran(tcString,'È','\u00c8')
tcString = Strtran(tcString,'É','\u00c9')
tcString = Strtran(tcString,'Ì','\u00cc')
tcString = Strtran(tcString,'Ã','\u00cd')
tcString = Strtran(tcString,'Ã’','\u00d2')
tcString = Strtran(tcString,'Ó','\u00d3')
tcString = Strtran(tcString,'Ù','\u00d9')
tcString = Strtran(tcString,'Ú','\u00da')
tcString = Strtran(tcString,'Ü','\u00dc')
tcString = Strtran(tcString,'ñ','\u00f1')
tcString = Strtran(tcString,'Ñ','\u00d1')
tcString = Strtran(tcString,'©','\u00a9')
tcString = Strtran(tcString,'®','\u00ae')
tcString = Strtran(tcString,'ç','\u00e7')
Endif
Return '"' +tcString + '"'
Endfunc
Enddefine
Define Class TString As Custom
Value = ''
Dimension aWords[1]
Function Init(tcStartValue)
If Pcount() = 1
If Type('tcStartValue') == 'C'
This.Value = tcStartValue
Else
Error 'Invalid data type for string.'
Endif
Else
This.Value = Space(1)
Endif
Endfunc
Function Split(tcDelimiter)
Local tcWord, i
For i = 1 To Getwordcount(This.Value, tcDelimiter)
Dimension This.aWords[i]
This.aWords[i] = Getwordnum(This.Value, i, tcDelimiter)
Endfor
Return @This.aWords
Endfunc
Function LineS
Local tcWord, i, lcValue
lcValue = Strtran(This.Value, Chr(10))
For i = 1 To Getwordcount(lcValue, Chr(13))
Dimension This.aWords[i]
This.aWords[i] = Getwordnum(lcValue, i, Chr(13))
Endfor
Return @This.aWords
Endfunc
Enddefine
* ============================================================ *
* TDictionary
* ============================================================ *
Define Class TDictionary As TIterable
Hidden Items
Function Init
DoDefault()
This.Items = Createobject("Collection")
Endfunc
Function Add(tcKey As String, tvValue As variant)
If !Empty(This.Items.GetKey(tcKey))
This.Items.Remove(tcKey)
Endif
This.Items.Add(tvValue, tcKey)
Endfunc
Function Get(tcKey As String) As variant
Local lnIndex
lnIndex = This.Items.GetKey(tcKey)
Return Iif(Empty(lnIndex), .Null., This.Items.Item(lnIndex))
Endfunc
Function ContainsKey(tcKey As String) As Boolean
lnItem = This.Items.GetKey(tcKey)
Return This.Items.GetKey(tcKey) > 0
Endfunc
Function Clear
This.Items = Createobject("Collection")
Endfunc
Function GetDataByIndex
Local lvKey, lvValue
lvKey = This.Items.Item(This.nIteratorCounter)
lvValue = This.Items.GetKey(This.nIteratorCounter)
Return This.CreatePair(lvKey, lvValue)
Endfunc
Function GetLen
Return This.Items.Count
Endfunc
Hidden Function CreatePair(tvKey, tvValue)
Local loPair
loPair = Createobject('Empty')
AddProperty(loPair, 'key', tvKey)
AddProperty(loPair, 'value', tvValue)
Return loPair
Endfunc
Function ToString
If This.GetLen() > 0
Local lcStr, i
lcStr = '{'
For i = 1 To This.GetLen()
If Len(lcStr) = 1 Then
lcStr = lcStr + _vfp.fxAnyToString.ToString(This.Items.GetKey(i)) + ':' + This.ObjToString(This.Items.Item(i))
Else
lcStr = lcStr + ',' + _vfp.fxAnyToString.ToString(This.Items.GetKey(i)) + ':' + This.ObjToString(This.Items.Item(i))
Endif
Endfor
lcStr = lcStr + '}'
Return lcStr
Else
Return '{}'
Endif
Endfunc
Function ObjToString(toObj)
Local lcStr
lcStr = Space(1)
Try
lcStr = toObj.ToString()
Catch
lcStr = _vfp.fxAnyToString.ToString(toObj)
Endtry
Return lcStr
Endfunc
Enddefine
* ============================================================ *
* TArray
* ============================================================ *
Define Class tArray As TIterable
Dimension aCustomArray[1]
nIndex = 0
Function Init
DoDefault()
Endfunc
Function Push(tvItem)
This.nIndex = This.nIndex + 1
Dimension This.aCustomArray[this.nIndex]
This.aCustomArray[this.nIndex] = tvItem
Endfunc
Function Pop
This.nIndex = This.nIndex - 1
If This.nIndex <= 0
This.nIndex = 0
Dimension This.aCustomArray[1]
Return
Endif
Dimension This.aCustomArray[this.nIndex]
Endfunc
Function Get(tnIndex)
If Between(tnIndex, 1, This.GetLen())
Return This.aCustomArray[tnIndex]
Endif
Return .Null.
Endfunc
Function Set(tnIndex, tvValue)
If Between(tnIndex, 1, This.GetLen())
This.aCustomArray[tnIndex] = tvValue
Endif
Endfunc
Function GetDataByIndex
Return This.aCustomArray[this.nIteratorCounter]
Endfunc
Function GetLen
Return This.nIndex
Endfunc
Function ToString
If This.nIndex > 0
Acopy(This.aCustomArray, laData)
Return _vfp.fxAnyToString.ToString(@laData)
Else
Return '[]'
Endif
Endfunc
Enddefine
* ============================================================ *
* TStringList
* ============================================================ *
Define Class TStringList As TIterable
Dimension aCustomArray[1]
nIndex = 0
Function Init
DoDefault()
Endfunc
Function Add(tcItem)
If Type('tcItem') != 'C'
Return
Endif
This.nIndex = This.nIndex + 1
Dimension This.aCustomArray[this.nIndex]
This.aCustomArray[this.nIndex] = tcItem
Endfunc
Function GetDataByIndex
Return This.aCustomArray[this.nIteratorCounter]
Endfunc
Function GetLen
Return This.nIndex
Endfunc
Function ToString
Return This.Join()
Endfunc
Function Join(tcSep)
If This.nIndex > 0
Local lcStr, i, lcVal
lcStr = Space(1)
For i = 1 To This.GetLen()
lcVal = This.aCustomArray[i]
If i = 1 Then
lcStr = lcVal
Else
lcStr = lcStr + Iif(!Empty(tcSep), tcSep + lcVal, lcVal)
Endif
Endfor
Return lcStr
Else
Return ''
Endif
Endfunc
Enddefine
* ============================================================ *
* TIterable
* ============================================================ *
Define Class TIterable As Custom
Hidden nIteratorCounter
Len = 0
Function Init
This.nIteratorCounter = 1
Endfunc
Function hasNext
If This.nIteratorCounter > This.GetLen() Then
This.nIteratorCounter = 0
Return .F.
Endif
Return .T.
Endfunc
Function Next
Local lvValue
lvValue = This.GetDataByIndex()
This.nIteratorCounter = This.nIteratorCounter + 1
Return lvValue
Endfunc
Function GetDataByIndex
* abstract
Endfunc
Function GetLen
* abstract
Endfunc
Function Len_Access
Return This.GetLen()
Endfunc
Function ToString
Endfunc
Enddefine
* ============================================================ *
* TFoxExtendsInternalArray
* ============================================================ *
Define Class TFoxExtendsInternalArray As Custom
Dimension aCustomArray[1]
nIndex = 0
Function Push(tvItem)
This.nIndex = This.nIndex + 1
Dimension This.aCustomArray[this.nIndex]
This.aCustomArray[this.nIndex] = tvItem
Endfunc
Function GetArray
Return @This.aCustomArray
Endfunc
Enddefine
**************************************************
* Class TFoxExtendsFrmSecret
Define Class TFoxExtendsFrmSecret As Form
BorderStyle = 2
Height = 88
Width = 396
DoCreate = .T.
AutoCenter = .T.
Caption = ""
MaxButton = .F.
MinButton = .F.
WindowType = 1
cResult = ""
Name = "FrmSecret"
Add Object lbl_titulo As Label With ;
FontName = "MS Sans Serif", ;
BackStyle = 0, ;
Caption = "Label1", ;
Height = 17, ;
Left = 8, ;
Top = 12, ;
Visible = .T., ;
Width = 380, ;
TabIndex = 1, ;
Name = "LBL_TITULO"
Add Object text1 As TextBox With ;
FontName = "Wingdings", ;
ControlSource = "thisform.cResult", ;
Height = 23, ;
Left = 8, ;
TabIndex = 2, ;
Top = 30, ;
Width = 380, ;
PasswordChar = "l", ;
Name = "Text1"
Add Object btn_ok As CommandButton With ;
Top = 58, ;
Left = 242, ;
Height = 23, ;
Width = 72, ;
Caption = "OK", ;
Default = .T., ;
TabIndex = 3, ;
Name = "BTN_OK"
Add Object btn_cancel As CommandButton With ;
Top = 58, ;
Left = 316, ;
Height = 23, ;
Width = 72, ;
Cancel = .T., ;
Caption = "Cancel", ;
Default = .F., ;
TabIndex = 4, ;
Name = "BTN_CANCEL"
Procedure Init
Lparameters tcPrompt, tcCaption
If Empty(tcPrompt)
tcPrompt = ''
Endif
If Empty(tcCaption)
tcCaption = _Screen.Caption
Endif
This.Caption = tcCaption
This.lbl_titulo.Caption = tcPrompt
Endproc
Procedure btn_ok.Click
Thisform.Hide()
Endproc
Procedure btn_cancel.Click
Thisform.cResult = ''
Thisform.Hide()
Endproc
Enddefine
*------------------------------------------------------------*
* newGuid: Generate a new GUID
*------------------------------------------------------------*
Function newGuid
* Credits from: https://fox.wikis.com/wc.dll?Wiki~GUIDGenerationCode
Local lcbuffer, lnResult, lcGuid, lcResult
Declare Integer CoCreateGuid In ole32.Dll String@ pguid
Declare Integer StringFromGUID2 In ole32.Dll String pguid, String @lpszBuffer, Integer cbBuffer
lcGuid = Space(16) && 16 Byte = 128 Bit
lnResult = CoCreateGuid(@lcGuid)
lcbuffer = Space(78)
lnResult = StringFromGUID2(lcGuid, @lcbuffer, Len(lcbuffer)/2)
Clear Dlls "CoCreateGuid", "StringFromGUID2"
lcResult = Strconv((Left(lcbuffer,(lnResult-1)*2)),6)
Return Substr(lcResult, 2, Len(lcResult) - 2) && remove {}
ENDFUNC
* ========================================================================= *
* FoxExtends main class
* ========================================================================= *
<<IIF(llUseClass, 'DEFINE CLASS TFOXEXTENDS AS CUSTOM', '')>>
*------------------------------------------------------------*
* PAIR: Create a pair object (key, value)
*------------------------------------------------------------*
Function <<lcMethodPrefix>>PAIR(tvKey, tvValue)
Local loPair
loPair = Createobject('Empty')
=AddProperty(loPair, 'key', tvKey)
=AddProperty(loPair, 'value', tvValue)
Return loPair
Endfunc
*------------------------------------------------------------*
* ANYTOSTR: Convert any value to string
*------------------------------------------------------------*
Function <<lcMethodPrefix>>ANYTOSTR(tvValue)
Return _vfp.fxAnyToString.ToString(@tvValue)
Endfunc
*------------------------------------------------------------*
* ENUM: Create an enumeration object
* Max 20 values
*------------------------------------------------------------*
Function <<lcMethodPrefix>>ENUM(tvVal1, tvVal2, tvVal3, tvVal4, tvVal5, tvVal6, tvVal7, tvVal8, tvVal9, tvVal10, ;
tvVal11, tvVal12, tvVal13, tvVal14, tvVal15, tvVal16, tvVal17, tvVal18, tvVal19, tvVal20)
Local loEnum, i
loEnum = Createobject("Empty")
For i = 1 To Pcount()
=AddProperty(loEnum, Evaluate("tvVal" + Alltrim(Str(i))), i)
Endfor
Return loEnum
EndFunc
*------------------------------------------------------------*
* ARRAY FUNCTIONS
*------------------------------------------------------------*
*------------------------------------------------------------*
* ALIST: Create an array from a list of parameters
*------------------------------------------------------------*
Function <<lcMethodPrefix>>ALIST(tvVal1, tvVal2, tvVal3, tvVal4, tvVal5, tvVal6, tvVal7, tvVal8, tvVal9, tvVal10, ;
tvVal11, tvVal12, tvVal13, tvVal14, tvVal15, tvVal16, tvVal17, tvVal18, tvVal19, tvVal20)
Local laTuple, i
laTuple = Createobject("TFoxExtendsInternalArray")
For i = 1 To Pcount()
laTuple.Push(Evaluate("tvVal" + Alltrim(Str(i))))
Endfor
Return laTuple.GetArray()
Endfunc
*------------------------------------------------------------*
* APUSH: Add an item to the end of an array
*------------------------------------------------------------*
Function <<lcMethodPrefix>>APUSH(tArray, tvItem)
If Type('tArray', 1) != 'A'
Error FUNCTION_ARG_VALUE_INVALID
Endif
Local lnIndex
lnIndex = Alen(tArray, 1) + 1
Dimension tArray[lnIndex]
tArray[lnIndex] = tvItem
Endfunc
*------------------------------------------------------------*
* APOP: Remove the last item from an array
*------------------------------------------------------------*
Function <<lcMethodPrefix>>APOP(tArray)
If Type('tArray', 1) != 'A'
Error FUNCTION_ARG_VALUE_INVALID
Endif
Local lnIndex, lvOldVal
lnIndex = Alen(tArray, 1)
lnIndex = lnIndex - 1
If lnIndex <= 0
lnIndex = 1
Dimension tArray[lnIndex]
Return
Endif
lvOldVal = tArray[lnIndex+1]
Dimension tArray[lnIndex]
Return lvOldVal
Endfunc
*------------------------------------------------------------*
* ASPLIT: Split a string into an array
*------------------------------------------------------------*
Function <<lcMethodPrefix>>ASPLIT(tcString, tcDelimiter)
Local loString
loString = Createobject("TString", tcString)
Return loString.Split(tcDelimiter)
Endfunc
*------------------------------------------------------------*
* AMATCH: Return an array with all matches
*------------------------------------------------------------*
Function <<lcMethodPrefix>>AMATCH(tcString, tcPattern, tnOccurrences)
Local loResult, laItems, i
_vfp.foxExtendsRegEx.Pattern = tcPattern
loResult = _vfp.foxExtendsRegEx.Execute(tcString)
If Type('loResult') != 'O'
Return ""
Endif
If Empty(tnOccurrences) Or !Between(tnOccurrences, 1, loResult.Count)
tnOccurrences = loResult.Count
Endif
If Type('loResult.Item[tnOccurrences-1]') != 'O'
Return ""
Endif
laItems = Createobject("TFoxExtendsInternalArray")
laItems.Push(loResult.Item[tnOccurrences-1].Value)
Return laItems.GetArray()
Endfunc
*------------------------------------------------------------*
* AMAP: Apply a predicate to each element of an array
*------------------------------------------------------------*
Function <<lcMethodPrefix>>AMAP(tArray, tcPredicate)
If Type('tArray', 1) != 'A'
Error FUNCTION_ARG_VALUE_INVALID
Endif
Local laResult, i, lcExp
laResult = Createobject("TFoxExtendsInternalArray")
For i = 1 To Alen(tArray, 1)
lcExp = Strtran(tcPredicate, "$0", Transform(tArray[i]))
laResult.Push(Evaluate(lcExp))
Endfor
Return laResult.GetArray()
Endfunc
*------------------------------------------------------------*
* AEVERY: Return true if all elements of an array match a predicate
*------------------------------------------------------------*
Function <<lcMethodPrefix>>AEVERY(tArray, tcPredicate)
If Type('tArray', 1) != 'A'
Error FUNCTION_ARG_VALUE_INVALID
Endif
Local laResult, i, lcExp
laResult = Createobject("TFoxExtendsInternalArray")
For i = 1 To Alen(tArray, 1)
lcExp = Strtran(tcPredicate, "$0", Transform(tArray[i]))
If !Evaluate(lcExp)
Return .F.
Endif
Endfor
Return .T.
Endfunc
*------------------------------------------------------------*
* AFILTER: Filter an array
*------------------------------------------------------------*
Function <<lcMethodPrefix>>AFILTER(tArray, tcPredicate)
If Type('tArray', 1) != 'A'
Error FUNCTION_ARG_VALUE_INVALID
Endif
Local laResult, i, lcExp
laResult = Createobject("TFoxExtendsInternalArray")
For i = 1 To Alen(tArray, 1)
lcExp = Strtran(tcPredicate, "$0", Transform(tArray[i]))
If Evaluate(lcExp)
laResult.Push(tArray[i])
Endif
Endfor
Return laResult.GetArray()
Endfunc
*------------------------------------------------------------*
* FOREACH: Apply a function to each element of an array
*------------------------------------------------------------*
Function <<lcMethodPrefix>>FOREACH(tArray, tcFunction)
If Type('tArray', 1) != 'A'
Error FUNCTION_ARG_VALUE_INVALID
Endif
Local i, lcExp
For i = 1 To Alen(tArray, 1)
lcExp = Strtran(tcFunction, "$0", Transform(tArray[i]))
Evaluate(lcExp)
Endfor
Endfunc
*-----------------------------------------------------------*
* Array functions
*-----------------------------------------------------------*
Function <<lcMethodPrefix>>AKEYS(toDict)
Local i, laResult, lnCount
lnCount = Amembers(laKeys, toDict, 0, "PHGNUCIBR")
If lnCount > 0
laResult = Createobject("TFoxExtendsInternalArray")
For i = 1 To lnCount
laResult.Push(laKeys[i])
Endfor
Return laResult.GetArray()
Else
Return .Null.
Endif
Endfunc
*-----------------------------------------------------------*
* ASLICE: Returns a slice of an array
*-----------------------------------------------------------*
Function <<lcMethodPrefix>>ASLICE(tArray, tcRange)
If Type('tArray', 1) != 'A'
Error FUNCTION_ARG_VALUE_INVALID
Endif
Local laSlice, lnFirst, lnLast, lnLen, i
laSlice = Createobject("TFoxExtendsInternalArray")
lnLen = Alen(tArray, 1)
Do Case
Case <<lcMethodPrefix>>MATCH(tcRange, '^\w+\.\.\w+$') && DIGIT .. DIGIT
lnFirst = <<lcMethodPrefix>>AMATCH(tcRange, '\w+', 1)
lnFirst = Val(lnFirst[1])
lnLast = <<lcMethodPrefix>>AMATCH(tcRange, '\w+', 2)
lnLast = Val(lnLast[1])
Case <<lcMethodPrefix>>MATCH(tcRange, '^\.\.\w+$') && .. DIGIT
lnFirst = 1
lnLast = <<lcMethodPrefix>>AMATCH(tcRange, '\w+', 2)
lnLast = Val(lnLast[1])
Case <<lcMethodPrefix>>MATCH(tcRange, '^\w+\.\.$') && DIGIT ..
lnFirst = <<lcMethodPrefix>>AMATCH(tcRange, '\w+', 1)
lnFirst = Val(lnFirst[1])
lnLast = lnLen
Case <<lcMethodPrefix>>MATCH(tcRange, '^\w+$') && +DIGIT
lnFirst = 1
lnLast = Val(tcRange)
Case <<lcMethodPrefix>>MATCH(tcRange, '^\-\w+$') && -DIGIT
lnFirst = lnLen - Abs(Val(tcRange)) + 1
lnLast = lnLen
Otherwise
lnFirst = 1
lnLast = lnLen
Endcase
* Check out of bounds
If !Between(lnFirst, 1, lnLen) Or !Between(lnLast, 1, lnLen)
Return .Null.
Endif
* Extract elements
For i = lnFirst To lnLast
laSlice.Push(tArray[i])
Endfor
Return laSlice.GetArray()
Endfunc
*-----------------------------------------------------------*
* ALEFT: Returns the first n elements of an array
*-----------------------------------------------------------*
Function <<lcMethodPrefix>>ALEFT(tArray, tnExpression)
If Type('tArray', 1) != 'A'
Error FUNCTION_ARG_VALUE_INVALID
Endif
Local laResult, i, lnExpression, j
lnExpression = Evaluate("tnExpression")
If Type('lnExpression') != 'N'
Error FUNCTION_ARG_VALUE_INVALID
Endif
If lnExpression <= 0
Return .F.
Endif
j = Alen(tArray, 1)
If lnExpression > j
lnExpression = j
Endif
laResult = Createobject("TFoxExtendsInternalArray")
For i = 1 To lnExpression
laResult.Push(tArray[i])
Endfor
Return laResult.GetArray()
Endfunc
*-----------------------------------------------------------*
* ARIGHT: Returns the last n elements of an array
*-----------------------------------------------------------*
Function <<lcMethodPrefix>>ARIGHT(tArray, tnExpression)
If Type('tArray', 1) != 'A'
Error FUNCTION_ARG_VALUE_INVALID
Endif
Local laResult, i, lnExpression, lnArrayLen, lnTo
lnExpression = Evaluate("tnExpression")
If Type('lnExpression') != 'N'
Error FUNCTION_ARG_VALUE_INVALID
Endif
If lnExpression <= 0
lnExpression = 1
Endif
lnArrayLen = Alen(tArray, 1)
If lnExpression > lnArrayLen
lnExpression = lnArrayLen
Endif
laResult = Createobject("TFoxExtendsInternalArray")