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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308 | --------------------------------------------------------------------
--
-- Unit Name: Tash.Arrays body
--
-- File Name: tash-arrays.adb
--
-- Purpose: Defines the Tash array type which is
-- an associative array whose indices are
-- strings and contents may be any Tash data type.
--
-- Copyright (c) 1999-2000 Terry J. Westley
--
-- Tash is free software; you can redistribute it and/or modify it under
-- terms of the GNU General Public License as published by the Free
-- Software Foundation; either version 2, or (at your option) any later
-- version. Tash 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 General Public License
-- for more details. You should have received a copy of the GNU General
-- Public License distributed with Tash; see file COPYING. If not, write to
--
-- Free Software Foundation
-- 59 Temple Place - Suite 330
-- Boston, MA 02111-1307, USA
--
-- As a special exception, if other files instantiate generics from this
-- unit, or you link this unit with other files to produce an executable,
-- this unit does not by itself cause the resulting executable to be
-- covered by the GNU General Public License. This exception does not
-- however invalidate any other reasons why the executable file might be
-- covered by the GNU Public License.
--
-- Tash is maintained at http://tcladashell.sourceforge.net/.
--
--------------------------------------------------------------------
--
-- We have chosen to implement Tash arrays by calling
-- Tcl_ArrayObjCmd. This will provide a complete implementation
-- and is easier to develop than other possible implementation methods:
--
-- 1) Call Tcl hash facilities directly (see tcl.ads), or
-- 2) Create Tash version of Tcl hash facilities and use it.
--
--------------------------------------------------------------------
with Ada.Strings.Fixed;
with Ada.Strings.Unbounded;
with Ada.Text_IO;
with CHelper;
with Tcl.Ada;
with System;
package body Tash.Arrays is
function Get_Element (Interp : in Tcl.Tcl_Interp;
TArray : in Tcl.Tcl_Obj;
Index : in String) return Tcl.Tcl_Obj;
function Type_Of_Array_Element (ObjPtr : in Tcl.Tcl_Obj) return String;
use type Interfaces.C.int;
Array_Cmd : constant Tcl.Tcl_Obj := Tash.To_Tcl_Obj ("array");
Get_Option : constant Tcl.Tcl_Obj := Tash.To_Tcl_Obj ("get");
Names_Option : constant Tcl.Tcl_Obj := Tash.To_Tcl_Obj ("names");
Set_Option : constant Tcl.Tcl_Obj := Tash.To_Tcl_Obj ("set");
Size_Option : constant Tcl.Tcl_Obj := Tash.To_Tcl_Obj ("size");
Unset_Option : constant Tcl.Tcl_Obj := Tash.To_Tcl_Obj ("unset");
function Tcl_ArrayObjCmd (
dummy : in Tcl.ClientData;
interp : in Tcl.Tcl_Interp;
objc : in Interfaces.C.int;
objv : in Tcl.Tcl_Obj_Array
) return Interfaces.C.int;
pragma Import (C, Tcl_ArrayObjCmd, "Tcl_ArrayObjCmd");
function TclInfoExistsCmd (
dummy : in Tcl.ClientData;
interp : in Tcl.Tcl_Interp;
objc : in Interfaces.C.int;
objv : in Tcl.Tcl_Obj_Array
) return Interfaces.C.int;
pragma Import (C, TclInfoExistsCmd, "TclInfoExistsCmd");
protected Variable_Count is
procedure Next (Value : out Natural);
private
Count : Natural := 0;
end Variable_Count;
protected body Variable_Count is
procedure Next (Value : out Natural) is
begin -- Next
Value := Count;
Count := Count + 1;
end Next;
end Variable_Count;
function Is_Empty (
TArray : in Tash_Array) return Boolean is
begin -- Is_Empty
return Is_Null (TArray) or else Length (TArray) = 0;
end Is_Empty;
pragma Inline (Is_Empty);
function To_Tash_Array (
Str : in String) return Tash_Array is
Count : Natural;
New_Obj : Tcl.Tcl_Obj;
Objc : constant Interfaces.C.int := 4;
Objv : Tcl.Tcl_Obj_Array (1 .. Objc);
TArray : Tash_Array;
Interp : Tcl.Tcl_Interp;
Result : Interfaces.C.int;
begin -- To_Tash_Array
-- A Tash array is merely a unique variable name for a
-- Tcl array. We will now create a Tcl object and a
-- unique variable name string to store in it.
------------------------------------------------------
Variable_Count.Next (Count);
New_Obj := Tash.To_Tcl_Obj ("array" &
Ada.Strings.Fixed.Trim (Natural'Image (Count), Ada.Strings.Left));
TArray := (Ada.Finalization.Controlled with
Obj => New_Obj);
-- Create object parameters for Tcl_ArrayObjCmd call
----------------------------------------------------
Objv (1) := Array_Cmd;
Objv (2) := Set_Option;
Objv (3) := TArray.Obj;
Objv (4) := Tash.To_Tcl_Obj (Str);
-- Use Tcl_ArrayObjCmd to create a new Tcl array
------------------------------------------------
Tash_Interp.Get (Interp);
Tcl.Tcl_ResetResult (Interp);
Result := Tcl_ArrayObjCmd (
dummy => System.Null_Address,
interp => Interp,
objc => Objc,
objv => Objv);
Tcl.Tcl_DecrRefCount (Objv (4));
Tash_Interp.Assert (Interp, Result, Array_Error'Identity);
Tcl.Tcl_ResetResult (Interp);
Tash_Interp.Release (Interp);
PArray (TArray);
return TArray;
end To_Tash_Array;
function "+" (
Str : in String) return Tash_Array is
begin -- "+"
return To_Tash_Array (Str);
end "+";
pragma Inline ("+");
-- function Copy (
-- TArray : in Tash_Array) return Tash_Array;
-- function Duplicate (
-- TArray : in Tash_Array) return Tash_Array;
function To_String (
TArray : in Tash_Array) return String is
Objc : constant Interfaces.C.int := 3;
Objv : Tcl.Tcl_Obj_Array (1 .. Objc);
Interp : Tcl.Tcl_Interp;
Result : Interfaces.C.int;
InterpObj : Tcl.Tcl_Obj;
Length : aliased Interfaces.C.int;
begin -- To_String
if Is_Empty (TArray) then
return "";
end if;
-- Create object parameters for Tcl_ArrayObjCmd call
----------------------------------------------------
Objv (1) := Array_Cmd;
Objv (2) := Get_Option;
Objv (3) := TArray.Obj;
-- Get the array value as a string into
-- Interp's result object.
---------------------------------------
Tash_Interp.Get (Interp);
Tcl.Tcl_ResetResult (Interp);
Result := Tcl_ArrayObjCmd (
dummy => System.Null_Address,
interp => Interp,
objc => Objc,
objv => Objv);
Tash_Interp.Assert (Interp, Result, Array_Error'Identity);
InterpObj := Tcl.Tcl_GetObjResult (Interp);
-- Get and return the string in Interp's result object
------------------------------------------------------
declare
Image : constant String := CHelper.Value (
Tcl.Tcl_GetStringFromObj (InterpObj, Length'Access));
begin
Tcl.Tcl_ResetResult (Interp);
Tash_Interp.Release (Interp);
return Image;
end;
end To_String;
procedure PArray (
TArray : in Tash_Array) is
--
Interp : Tcl.Tcl_Interp;
begin -- PArray
Tash_Interp.Get (Interp);
Tcl.Ada.Tcl_Eval (Interp, "parray " & Tash.Image (TArray.Obj));
Tash_Interp.Release (Interp);
end PArray;
function Length (
TArray : in Tash_Array) return Natural is
--
Objc : constant Interfaces.C.int := 3;
Objv : Tcl.Tcl_Obj_Array (1 .. Objc);
Interp : Tcl.Tcl_Interp;
Result : Interfaces.C.int;
InterpObj : Tcl.Tcl_Obj;
C_Length : aliased Interfaces.C.long;
begin -- Length
Objv (1) := Array_Cmd;
Objv (2) := Size_Option;
Objv (3) := TArray.Obj;
Tash_Interp.Get (Interp);
Tcl.Tcl_ResetResult (Interp);
Result := Tcl_ArrayObjCmd (
dummy => System.Null_Address,
interp => Interp,
objc => Objc,
objv => Objv);
Tash_Interp.Assert (Interp, Result, Array_Error'Identity);
InterpObj := Tcl.Tcl_GetObjResult (Interp);
Result := Tcl.Tcl_GetLongFromObj (
Interp, InterpObj, C_Length'Access);
Tcl.Tcl_ResetResult (Interp);
Tash_Interp.Release (Interp);
return Integer (C_Length);
end Length;
function Get_Element (
Interp : in Tcl.Tcl_Interp;
TArray : in Tcl.Tcl_Obj;
Index : in String) return Tcl.Tcl_Obj is
Objc : constant Interfaces.C.int := 4;
Objv : Tcl.Tcl_Obj_Array (1 .. Objc);
Result : Interfaces.C.int;
InterpObj : Tcl.Tcl_Obj;
ElementObj : aliased Tcl.Tcl_Obj;
use type Tcl.Tcl_Obj;
begin -- Get_Element
if TArray = null then
Tash_Interp.Raise_Exception
(Interp => Interp,
E => Array_Error'Identity,
Message =>
"Element """ & Index & """ does not exist: array is null");
end if;
-- Create object parameters for Tcl_ArrayObjCmd call
----------------------------------------------------
Objv (1) := Array_Cmd;
Objv (2) := Get_Option;
Objv (3) := TArray;
Objv (4) := Tash.To_Tcl_Obj (Index);
-- Get the array element in Interp's result object
--------------------------------------------------
Tcl.Tcl_ResetResult (Interp);
Result := Tcl_ArrayObjCmd (
dummy => System.Null_Address,
interp => Interp,
objc => Objc,
objv => Objv);
Tcl.Tcl_DecrRefCount (Objv (4));
if Result = Tcl.TCL_ERROR then
Tash_Interp.Raise_Exception (
Interp => Interp,
E => Array_Error'Identity,
Message => CHelper.Value (Tcl.Tcl_GetStringResult (Interp)));
end if;
-- The 2nd element of the list in the Interp's result object
-- is the array element value we're looking for.
------------------------------------------------------------
InterpObj := Tcl.Tcl_GetObjResult (Interp);
Result := Tcl.Tcl_ListObjIndex (
interp => Interp,
listPtr => InterpObj,
index => 1,
objPtrPtr => ElementObj'Unchecked_Access);
if Result = Tcl.TCL_ERROR then
Tash_Interp.Raise_Exception (
Interp => Interp,
E => Array_Error'Identity,
Message => CHelper.Value (Tcl.Tcl_GetStringResult (Interp)));
end if;
if ElementObj = null then
Tash_Interp.Raise_Exception (
Interp => Interp,
E => Array_Error'Identity,
Message => "Element """ & Index & """ does not exist");
end if;
return ElementObj;
end Get_Element;
function Get_Element (
TArray : in Tash_Array;
Index : in String) return String is
--
Interp : Tcl.Tcl_Interp;
begin -- Get_Element
Tash_Interp.Get (Interp);
declare
Obj : constant Tcl.Tcl_Obj :=
Get_Element (Interp, TArray.Obj, Index);
Len : aliased Interfaces.C.int;
begin
Tash_Interp.Release (Interp);
return Tcl.Ada.Tcl_GetStringFromObj (Obj, Len'Access);
end;
end Get_Element;
function Get_Element (
TArray : in Tash_Array;
Index : in String) return Tash.Lists.Tash_List is
--
Interp : Tcl.Tcl_Interp;
begin -- Get_Element
Tash_Interp.Get (Interp);
declare
Obj : constant Tcl.Tcl_Obj :=
Get_Element (Interp, TArray.Obj, Index);
begin
Tash_Interp.Release (Interp);
Tcl.Tcl_IncrRefCount (Obj);
return (Ada.Finalization.Controlled with Obj => Obj);
end;
end Get_Element;
function Exists (
TArray : in Tash_Array;
Index : in String) return Boolean is
Objc : constant Interfaces.C.int := 2;
Objv : Tcl.Tcl_Obj_Array (1 .. Objc);
Interp : Tcl.Tcl_Interp;
Result : Interfaces.C.int;
InterpObj : Tcl.Tcl_Obj;
Length : aliased Interfaces.C.int;
begin -- Exists
if Is_Empty (TArray) then
return False;
end if;
-- Create object parameters for TclInfoExistsCmd call
----------------------------------------------------
Objv (1) := Tash.To_Tcl_Obj ("exists");
Objv (2) := Tash.To_Tcl_Obj (Tash.Image (TArray.Obj)
& "(" & Index & ")");
-- Execute the info command
---------------------------
Tash_Interp.Get (Interp);
Result := TclInfoExistsCmd (
dummy => System.Null_Address,
interp => Interp,
objc => Objc,
objv => Objv);
Tash_Interp.Assert (Interp, Result, Array_Error'Identity);
InterpObj := Tcl.Tcl_GetObjResult (Interp);
Tash_Interp.Release (Interp);
-- Get the string in Interp's result object and
-- return whether it is equal to "1" or not
-----------------------------------------------
declare
Image : constant String := CHelper.Value (
Tcl.Tcl_GetStringFromObj (InterpObj, Length'Access));
begin
return Image = "1";
end;
end Exists;
-- Determine type of array element
----------------------------------
function Type_Of_Array_Element (
ObjPtr : in Tcl.Tcl_Obj) return String is
--
use type Tcl.Tcl_Obj;
begin -- Type_Of_Array_Element (
if ObjPtr = null then
return "null";
end if;
return CHelper.Value (Tcl.Tcl_GetObjTypeName (ObjPtr));
end Type_Of_Array_Element;
function Element_Is_String (
TArray : in Tash_Array;
Index : in String) return Boolean is
--
Interp : Tcl.Tcl_Interp;
Result : Boolean;
begin -- Element_Is_String
Tash_Interp.Get (Interp);
Result := Type_Of_Array_Element (
Get_Element (Interp, TArray.Obj, Index)) /= "list";
Tash_Interp.Release (Interp);
return Result;
exception
when others => return False;
end Element_Is_String;
function Element_Is_List (
TArray : in Tash_Array;
Index : in String) return Boolean is
--
Interp : Tcl.Tcl_Interp;
Result : Boolean;
begin -- Element_Is_List
Tash_Interp.Get (Interp);
Result := Type_Of_Array_Element (
Get_Element (Interp, TArray.Obj, Index)) = "list";
Tash_Interp.Release (Interp);
return Result;
exception
when others => return False;
end Element_Is_List;
function Get_Elements (
TArray : in Tash_Array;
Pattern : in String := "") return Tash.Lists.Tash_List is
Objc : Interfaces.C.int := 4;
Objv : Tcl.Tcl_Obj_Array (1 .. Objc);
Interp : Tcl.Tcl_Interp;
Result : Interfaces.C.int;
InterpObj : Tcl.Tcl_Obj;
begin -- Get_Elements
if Tash.Arrays.Is_Empty (TArray) then
raise Array_Error;
end if;
-- Create object parameters for Tcl_ArrayObjCmd call
----------------------------------------------------
Objv (1) := Array_Cmd;
Objv (2) := Get_Option;
Objv (3) := TArray.Obj;
if Pattern = "" then
Objc := 3;
else
Objv (4) := Tash.To_Tcl_Obj (Pattern);
end if;
-- Get the array elements in Interp's result object
--------------------------------------------------
Tash_Interp.Get (Interp);
Tcl.Tcl_ResetResult (Interp);
Result := Tcl_ArrayObjCmd (
dummy => System.Null_Address,
interp => Interp,
objc => Objc,
objv => Objv);
if Objc = 4 then
Tcl.Tcl_DecrRefCount (Objv (4));
end if;
Tash_Interp.Assert (Interp, Result, Array_Error'Identity);
-- Return the list in the Interp's result object.
-------------------------------------------------
InterpObj := Tcl.Tcl_GetObjResult (Interp);
Tcl.Tcl_IncrRefCount (InterpObj);
Tcl.Tcl_ResetResult (Interp);
Tash_Interp.Release (Interp);
return Tash.Lists.Tash_List'(
(Ada.Finalization.Controlled with
Obj => InterpObj));
end Get_Elements;
function Get_Sorted_Elements (
TArray : in Tash_Array;
Pattern : in String := "";
Mode : in Tash.Lists.Sort_Mode := Tash.Lists.SM_ASCII;
Order : in Tash.Lists.Ordering := Tash.Lists.Increasing)
return Tash.Lists.Tash_List is
Return_List : Tash.Lists.Tash_List;
begin -- Get_Sorted_Elements
if Tash.Arrays.Is_Empty (TArray) then
return Tash.Lists.Null_Tash_List;
end if;
-- Get and sort the indices, then get the
-- array elements in order of the sorted
-- indices.
-----------------------------------------
declare
Sorted_Indices : constant Tash.Lists.Tash_List :=
Tash.Lists.Sort (
List => Get_Indices (TArray, Pattern),
Mode => Mode,
Order => Order);
begin
for I in 1 .. Tash.Lists.Length (Sorted_Indices) loop
declare
Index : constant String :=
Tash.Lists.Get_Element (Sorted_Indices, I);
Element : constant String :=
Get_Element (TArray, Index);
begin
Tash.Lists.Append (Return_List, Index);
Tash.Lists.Append (Return_List, Element);
end;
end loop;
end;
-- return the sorted indices for now for testing
------------------------------------------------
return Return_List;
end Get_Sorted_Elements;
function Get_Indices (
TArray : in Tash_Array;
Pattern : in String := "") return String is
Objc : Interfaces.C.int := 4;
Objv : Tcl.Tcl_Obj_Array (1 .. Objc);
Interp : Tcl.Tcl_Interp;
Result : Interfaces.C.int;
InterpObj : Tcl.Tcl_Obj;
Length : aliased Interfaces.C.int;
begin -- Get_Indices
-- Create object parameters for Tcl_ArrayObjCmd call
----------------------------------------------------
Objv (1) := Array_Cmd;
Objv (2) := Names_Option;
Objv (3) := TArray.Obj;
if Pattern = "" then
Objc := 3;
else
Objv (4) := Tash.To_Tcl_Obj (Pattern);
end if;
-- Get array indices
-------------------------
Tash_Interp.Get (Interp);
Tcl.Tcl_ResetResult (Interp);
Result := Tcl_ArrayObjCmd (
dummy => System.Null_Address,
interp => Interp,
objc => Objc,
objv => Objv);
if Objc = 4 then
Tcl.Tcl_DecrRefCount (Objv (4));
end if;
Tash_Interp.Assert (Interp, Result, Array_Error'Identity);
InterpObj := Tcl.Tcl_GetObjResult (Interp);
-- Convert indices string to a Tash list
----------------------------------------
declare
Indices : constant String := CHelper.Value (
Tcl.Tcl_GetStringFromObj (InterpObj, Length'Access));
begin
Tcl.Tcl_ResetResult (Interp);
Tash_Interp.Release (Interp);
return Indices;
end;
end Get_Indices;
function Get_Indices (
TArray : in Tash_Array;
Pattern : in String := "") return Tash.Lists.Tash_List is
--
Indices : constant String := Get_Indices (TArray, Pattern);
begin -- Get_Indices
return Tash.Lists.To_Tash_List (Indices);
end Get_Indices;
procedure Set_Element (
TArray : in out Tash_Array;
Index : in String;
Value : in String) is
Count : Natural;
New_Obj : Tcl.Tcl_Obj;
Objc : constant Interfaces.C.int := 4;
Objv : Tcl.Tcl_Obj_Array (1 .. Objc);
Listc : constant Interfaces.C.int := 2;
Listv : Tcl.Tcl_Obj_Array (1 .. Listc);
ListObj : Tcl.Tcl_Obj;
Result : Interfaces.C.int;
Interp : Tcl.Tcl_Interp;
use type Tcl.Tcl_Obj;
begin -- Set_Element
if TArray.Obj = null then
-- A Tash array is merely a unique variable name for a
-- Tcl array. We will now create a Tcl object and a
-- unique variable name string to store in it.
------------------------------------------------------
Variable_Count.Next (Count);
New_Obj := Tash.To_Tcl_Obj ("array" &
Ada.Strings.Fixed.Trim (Natural'Image (Count), Ada.Strings.Left));
TArray := (Ada.Finalization.Controlled with
Obj => New_Obj);
end if;
-- Create a Tcl list to hold the index and value
------------------------------------------------
Listv (1) := Tash.To_Tcl_Obj (Index);
Listv (2) := Tash.To_Tcl_Obj (Value);
ListObj := Tcl.Tcl_NewListObj (objc => Listc, objv => Listv);
Tcl.Tcl_IncrRefCount (ListObj);
Tcl.Tcl_DecrRefCount (Listv (1));
-- Create object parameters for Tcl_ArrayObjCmd call
----------------------------------------------------
Objv (1) := Array_Cmd;
Objv (2) := Set_Option;
Objv (3) := TArray.Obj;
Objv (4) := ListObj;
-- Set the array element
------------------------
Tash_Interp.Get (Interp);
Tcl.Tcl_ResetResult (Interp);
Result := Tcl_ArrayObjCmd (
dummy => System.Null_Address,
interp => Interp,
objc => Objc,
objv => Objv);
Tcl.Tcl_DecrRefCount (ListObj);
Tash_Interp.Assert (Interp, Result, Array_Error'Identity);
Tcl.Tcl_ResetResult (Interp);
Tash_Interp.Release (Interp);
end Set_Element;
procedure Set_Element (
TArray : in out Tash_Array;
Index : in String;
Value : in Tash.Lists.Tash_List) is
Count : Natural;
New_Obj : Tcl.Tcl_Obj;
Objc : constant Interfaces.C.int := 4;
Objv : Tcl.Tcl_Obj_Array (1 .. Objc);
Listc : constant Interfaces.C.int := 2;
Listv : Tcl.Tcl_Obj_Array (1 .. Listc);
ListObj : Tcl.Tcl_Obj;
Result : Interfaces.C.int;
Interp : Tcl.Tcl_Interp;
use type Tcl.Tcl_Obj;
begin -- Set_Element
if TArray.Obj = null then
-- A Tash array is merely a unique variable name for a
-- Tcl array. We will now create a Tcl object and a
-- unique variable name string to store in it.
------------------------------------------------------
Variable_Count.Next (Count);
New_Obj := Tash.To_Tcl_Obj ("array" &
Ada.Strings.Fixed.Trim (Natural'Image (Count), Ada.Strings.Left));
TArray := (Ada.Finalization.Controlled with
Obj => New_Obj);
end if;
-- Create a Tcl list to hold the index and value
------------------------------------------------
Listv (1) := Tash.To_Tcl_Obj (Index);
Listv (2) := Tash.Tash_Object (Value).Obj;
ListObj := Tcl.Tcl_NewListObj (objc => Listc, objv => Listv);
Tcl.Tcl_IncrRefCount (ListObj);
Tcl.Tcl_DecrRefCount (Listv (1));
-- Create object parameters for Tcl_ArrayObjCmd call
----------------------------------------------------
Objv (1) := Array_Cmd;
Objv (2) := Set_Option;
Objv (3) := TArray.Obj;
Objv (4) := ListObj;
-- Set the array element
------------------------
Tash_Interp.Get (Interp);
Tcl.Tcl_ResetResult (Interp);
Result := Tcl_ArrayObjCmd (
dummy => System.Null_Address,
interp => Interp,
objc => Objc,
objv => Objv);
Tcl.Tcl_DecrRefCount (ListObj);
Tash_Interp.Assert (Interp, Result, Array_Error'Identity);
Tcl.Tcl_ResetResult (Interp);
Tash_Interp.Release (Interp);
end Set_Element;
procedure Set_Elements (
TArray : in out Tash_Array;
List : in Tash.Lists.Tash_List) is
Count : Natural;
New_Obj : Tcl.Tcl_Obj;
Objc : constant Interfaces.C.int := 4;
Objv : Tcl.Tcl_Obj_Array (1 .. Objc);
Result : Interfaces.C.int;
Interp : Tcl.Tcl_Interp;
use type Tcl.Tcl_Obj;
begin -- Set_Elements
if TArray.Obj = null then
-- A Tash array is merely a unique variable name for a
-- Tcl array. We will now create a Tcl object and a
-- unique variable name string to store in it.
------------------------------------------------------
Variable_Count.Next (Count);
New_Obj := Tash.To_Tcl_Obj ("array" &
Ada.Strings.Fixed.Trim (Natural'Image (Count), Ada.Strings.Left));
TArray := (Ada.Finalization.Controlled with
Obj => New_Obj);
end if;
-- Create object parameters for Tcl_ArrayObjCmd call
----------------------------------------------------
Objv (1) := Array_Cmd;
Objv (2) := Set_Option;
Objv (3) := TArray.Obj;
Objv (4) := Tash.Tash_Object (List).Obj;
-- Set the array elements
-------------------------
Tash_Interp.Get (Interp);
Tcl.Tcl_ResetResult (Interp);
Result := Tcl_ArrayObjCmd (
dummy => System.Null_Address,
interp => Interp,
objc => Objc,
objv => Objv);
Tash_Interp.Assert (Interp, Result, Array_Error'Identity);
Tcl.Tcl_ResetResult (Interp);
Tash_Interp.Release (Interp);
end Set_Elements;
procedure Delete_Element (
TArray : in Tash_Array;
Index : in String) is
Objc : constant Interfaces.C.int := 4;
Objv : Tcl.Tcl_Obj_Array (1 .. Objc);
Interp : Tcl.Tcl_Interp;
Result : Interfaces.C.int;
use type Tcl.Tcl_Obj;
begin -- Delete_Element
if TArray.Obj = null then
return;
end if;
-- Create object parameters for Tcl_ArrayObjCmd call
----------------------------------------------------
Objv (1) := Array_Cmd;
Objv (2) := Unset_Option;
Objv (3) := TArray.Obj;
Objv (4) := Tash.To_Tcl_Obj (Index);
-- Use Tcl_ArrayObjCmd to delete the
-- element indexed by Index
------------------------------------
Tash_Interp.Get (Interp);
Tcl.Tcl_ResetResult (Interp);
Result := Tcl_ArrayObjCmd (
dummy => System.Null_Address,
interp => Interp,
objc => Objc,
objv => Objv);
Tcl.Tcl_DecrRefCount (Objv (4));
Tash_Interp.Assert (Interp, Result, Array_Error'Identity);
Tcl.Tcl_ResetResult (Interp);
Tash_Interp.Release (Interp);
end Delete_Element;
package body Generic_Integer_Arrays is
function To_Tcl_Obj (Num : in Item) return Tcl.Tcl_Obj;
function To_Tcl_Obj (Num : in Item) return Tcl.Tcl_Obj is
New_Obj : Tcl.Tcl_Obj;
begin -- To_Tcl_Obj
New_Obj := Tcl.Tcl_NewIntObj (Interfaces.C.int (Num));
Tcl.Tcl_IncrRefCount (New_Obj);
return New_Obj;
end To_Tcl_Obj;
function To_Tash_Array (
Index : in String;
Num : in Item) return Tash_Array is
Count : Natural;
New_Obj : Tcl.Tcl_Obj;
TArray : Tash_Array;
Objc : constant Interfaces.C.int := 4;
Objv : Tcl.Tcl_Obj_Array (1 .. Objc);
Listc : constant Interfaces.C.int := 2;
Listv : Tcl.Tcl_Obj_Array (1 .. Listc);
ListObj : Tcl.Tcl_Obj;
Result : Interfaces.C.int;
Interp : Tcl.Tcl_Interp;
begin -- To_Tash_Array
-- A Tash array is merely a unique variable name for a
-- Tcl array. We will now create a Tcl object and a
-- unique variable name string to store in it.
------------------------------------------------------
Variable_Count.Next (Count);
New_Obj := Tash.To_Tcl_Obj ("array" &
Ada.Strings.Fixed.Trim (Natural'Image (Count), Ada.Strings.Left));
TArray := (Ada.Finalization.Controlled with
Obj => New_Obj);
-- Create a Tcl list to hold the index and value
------------------------------------------------
Listv (1) := Tash.To_Tcl_Obj (Index);
Listv (2) := To_Tcl_Obj (Num);
ListObj := Tcl.Tcl_NewListObj (objc => Listc, objv => Listv);
Tcl.Tcl_IncrRefCount (ListObj);
Tcl.Tcl_DecrRefCount (Listv (1));
-- Create object parameters for Tcl_ArrayObjCmd call
----------------------------------------------------
Objv (1) := Array_Cmd;
Objv (2) := Set_Option;
Objv (3) := TArray.Obj;
Objv (4) := ListObj;
-- Set the array element
------------------------
Tash_Interp.Get (Interp);
Tcl.Tcl_ResetResult (Interp);
Result := Tcl_ArrayObjCmd (
dummy => System.Null_Address,
interp => Interp,
objc => Objc,
objv => Objv);
Tcl.Tcl_DecrRefCount (ListObj);
Tash_Interp.Assert (Interp, Result, Array_Error'Identity);
Tcl.Tcl_ResetResult (Interp);
Tash_Interp.Release (Interp);
return TArray;
end To_Tash_Array;
function Get_Element (
TArray : in Tash_Array;
Index : in String) return Item is
--
Interp : Tcl.Tcl_Interp;
begin -- Get_Element
Tash_Interp.Get (Interp);
declare
Obj : constant Tcl.Tcl_Obj :=
Get_Element (Interp, TArray.Obj, Index);
Value : aliased Interfaces.C.int;
Result : Interfaces.C.int;
pragma Unreferenced (Result); -- XXX this doesn't seem right!
begin
if Type_Of_Array_Element (Obj) /= "int" then
Tash_Interp.Raise_Exception (
Interp => Interp,
E => Constraint_Error'Identity,
Message => "Element is not an integer");
end if;
Result := Tcl.Tcl_GetIntFromObj (
interp => Interp,
objPtr => Obj,
intPtr => Value'Access);
Tash_Interp.Release (Interp);
return Item (Value);
end;
end Get_Element;
function Element_Is_Integer (
TArray : in Tash_Array;
Index : in String) return Boolean is
--
Interp : Tcl.Tcl_Interp;
Result : Boolean;
begin -- Element_Is_Integer
Tash_Interp.Get (Interp);
Result := Type_Of_Array_Element (
Get_Element (Interp, TArray.Obj, Index)) = "int";
Tash_Interp.Release (Interp);
return Result;
exception
when others => return False;
end Element_Is_Integer;
procedure Set_Element (
TArray : in out Tash_Array;
Index : in String;
Value : in Item) is
Count : Natural;
New_Obj : Tcl.Tcl_Obj;
Objc : constant Interfaces.C.int := 4;
Objv : Tcl.Tcl_Obj_Array (1 .. Objc);
Listc : constant Interfaces.C.int := 2;
Listv : Tcl.Tcl_Obj_Array (1 .. Listc);
ListObj : Tcl.Tcl_Obj;
Result : Interfaces.C.int;
Interp : Tcl.Tcl_Interp;
use type Tcl.Tcl_Obj;
begin -- Set_Element
if TArray.Obj = null then
-- A Tash array is merely a unique variable name for a
-- Tcl array. We will now create a Tcl object and a
-- unique variable name string to store in it.
------------------------------------------------------
Variable_Count.Next (Count);
New_Obj := Tash.To_Tcl_Obj
("array" &
Ada.Strings.Fixed.Trim (Natural'Image (Count),
Ada.Strings.Left));
TArray := (Ada.Finalization.Controlled with
Obj => New_Obj);
end if;
-- Create a Tcl list to hold the index and value
------------------------------------------------
Listv (1) := Tash.To_Tcl_Obj (Index);
Listv (2) := To_Tcl_Obj (Value);
ListObj := Tcl.Tcl_NewListObj (objc => Listc, objv => Listv);
Tcl.Tcl_IncrRefCount (ListObj);
Tcl.Tcl_DecrRefCount (Listv (1));
-- Create object parameters for Tcl_ArrayObjCmd call
----------------------------------------------------
Objv (1) := Array_Cmd;
Objv (2) := Set_Option;
Objv (3) := TArray.Obj;
Objv (4) := ListObj;
-- Set the array element
------------------------
Tash_Interp.Get (Interp);
Tcl.Tcl_ResetResult (Interp);
Result := Tcl_ArrayObjCmd (
dummy => System.Null_Address,
interp => Interp,
objc => Objc,
objv => Objv);
Tcl.Tcl_DecrRefCount (ListObj);
Tash_Interp.Assert (Interp, Result, Array_Error'Identity);
Tcl.Tcl_ResetResult (Interp);
Tash_Interp.Release (Interp);
end Set_Element;
end Generic_Integer_Arrays;
package body Generic_Float_Arrays is
function To_Tcl_Obj (Num : in Item) return Tcl.Tcl_Obj;
function To_Tcl_Obj (Num : in Item) return Tcl.Tcl_Obj is
New_Obj : Tcl.Tcl_Obj;
begin -- To_Tcl_Obj
New_Obj := Tcl.Tcl_NewDoubleObj (Interfaces.C.double (Num));
Tcl.Tcl_IncrRefCount (New_Obj);
return New_Obj;
end To_Tcl_Obj;
function To_Tash_Array (
Index : in String;
Num : in Item) return Tash_Array is
Count : Natural;
New_Obj : Tcl.Tcl_Obj;
TArray : Tash_Array;
Objc : constant Interfaces.C.int := 4;
Objv : Tcl.Tcl_Obj_Array (1 .. Objc);
Listc : constant Interfaces.C.int := 2;
Listv : Tcl.Tcl_Obj_Array (1 .. Listc);
ListObj : Tcl.Tcl_Obj;
Result : Interfaces.C.int;
Interp : Tcl.Tcl_Interp;
begin -- To_Tash_Array
-- A Tash array is merely a unique variable name for a
-- Tcl array. We will now create a Tcl object and a
-- unique variable name string to store in it.
------------------------------------------------------
Variable_Count.Next (Count);
New_Obj := Tash.To_Tcl_Obj ("array" &
Ada.Strings.Fixed.Trim (Natural'Image (Count), Ada.Strings.Left));
TArray := (Ada.Finalization.Controlled with
Obj => New_Obj);
-- Create a Tcl list to hold the index and value
------------------------------------------------
Listv (1) := Tash.To_Tcl_Obj (Index);
Listv (2) := To_Tcl_Obj (Num);
ListObj := Tcl.Tcl_NewListObj (objc => Listc, objv => Listv);
Tcl.Tcl_IncrRefCount (ListObj);
Tcl.Tcl_DecrRefCount (Listv (1));
-- Create object parameters for Tcl_ArrayObjCmd call
----------------------------------------------------
Objv (1) := Array_Cmd;
Objv (2) := Set_Option;
Objv (3) := TArray.Obj;
Objv (4) := ListObj;
-- Set the array element
------------------------
Tash_Interp.Get (Interp);
Tcl.Tcl_ResetResult (Interp);
Result := Tcl_ArrayObjCmd (
dummy => System.Null_Address,
interp => Interp,
objc => Objc,
objv => Objv);
Tcl.Tcl_DecrRefCount (ListObj);
Tash_Interp.Assert (Interp, Result, Array_Error'Identity);
Tcl.Tcl_ResetResult (Interp);
Tash_Interp.Release (Interp);
return TArray;
end To_Tash_Array;
function Get_Element (
TArray : in Tash_Array;
Index : in String) return Item is
--
Interp : Tcl.Tcl_Interp;
begin -- Get_Element
Tash_Interp.Get (Interp);
declare
Obj : constant Tcl.Tcl_Obj :=
Get_Element (Interp, TArray.Obj, Index);
Value : aliased Interfaces.C.double;
Result : Interfaces.C.int;
pragma Unreferenced (Result); -- XXX this doesn't seem right!
begin
if Type_Of_Array_Element (Obj) /= "double" then
Tash_Interp.Raise_Exception (
Interp => Interp,
E => Constraint_Error'Identity,
Message => "Element is not an float");
end if;
Result := Tcl.Tcl_GetDoubleFromObj (
interp => Interp,
objPtr => Obj,
doublePtr => Value'Access);
Tash_Interp.Release (Interp);
return Item (Value);
end;
end Get_Element;
function Element_Is_Float (
TArray : in Tash_Array;
Index : in String) return Boolean is
--
Interp : Tcl.Tcl_Interp;
Result : Boolean;
begin -- Element_Is_Float
Tash_Interp.Get (Interp);
Result := Type_Of_Array_Element (
Get_Element (Interp, TArray.Obj, Index)) = "double";
Tash_Interp.Release (Interp);
return Result;
exception
when others => return False;
end Element_Is_Float;
procedure Set_Element (
TArray : in out Tash_Array;
Index : in String;
Value : in Item) is
Count : Natural;
New_Obj : Tcl.Tcl_Obj;
Objc : constant Interfaces.C.int := 4;
Objv : Tcl.Tcl_Obj_Array (1 .. Objc);
Listc : constant Interfaces.C.int := 2;
Listv : Tcl.Tcl_Obj_Array (1 .. Listc);
ListObj : Tcl.Tcl_Obj;
Result : Interfaces.C.int;
Interp : Tcl.Tcl_Interp;
use type Tcl.Tcl_Obj;
begin -- Set_Element
if TArray.Obj = null then
-- A Tash array is merely a unique variable name for a
-- Tcl array. We will now create a Tcl object and a
-- unique variable name string to store in it.
------------------------------------------------------
Variable_Count.Next (Count);
New_Obj := Tash.To_Tcl_Obj
("array" &
Ada.Strings.Fixed.Trim (Natural'Image (Count),
Ada.Strings.Left));
TArray := (Ada.Finalization.Controlled with
Obj => New_Obj);
end if;
-- Create a Tcl list to hold the index and value
------------------------------------------------
Listv (1) := Tash.To_Tcl_Obj (Index);
Listv (2) := To_Tcl_Obj (Value);
ListObj := Tcl.Tcl_NewListObj (objc => Listc, objv => Listv);
Tcl.Tcl_IncrRefCount (ListObj);
Tcl.Tcl_DecrRefCount (Listv (1));
-- Create object parameters for Tcl_ArrayObjCmd call
----------------------------------------------------
Objv (1) := Array_Cmd;
Objv (2) := Set_Option;
Objv (3) := TArray.Obj;
Objv (4) := ListObj;
-- Set the array element
------------------------
Tash_Interp.Get (Interp);
Tcl.Tcl_ResetResult (Interp);
Result := Tcl_ArrayObjCmd (
dummy => System.Null_Address,
interp => Interp,
objc => Objc,
objv => Objv);
Tcl.Tcl_DecrRefCount (ListObj);
Tash_Interp.Assert (Interp, Result, Array_Error'Identity);
Tcl.Tcl_ResetResult (Interp);
Tash_Interp.Release (Interp);
end Set_Element;
end Generic_Float_Arrays;
function Internal_Rep (
TArray : in Tash_Array) return String is
Name : constant String := Internal_Name (TArray);
Num_Elements : Integer;
Indices : Tash.Lists.Tash_List;
Max_Index_Len : Natural := 0;
Image : Ada.Strings.Unbounded.Unbounded_String;
Interp : Tcl.Tcl_Interp;
Elem : Tcl.Tcl_Obj;
Elements : Tash.Lists.Tash_List;
pragma Unreferenced (Elements); -- XXX why do we get it then?
begin -- Internal_Rep
-- Get the internal representation of the array itself
-----------------------------------------------------
Image := Ada.Strings.Unbounded.To_Unbounded_String (
Tash.Internal_Rep (Tash_Object (TArray)));
Elements := Get_Elements (TArray);
if True then
return Ada.Strings.Unbounded.To_String (Image);
end if;
-- Find max length of the indices
---------------------------------
Num_Elements := Tash.Lists.Length (Indices);
for I in 1 .. Num_Elements loop
declare
Index : constant String := Tash.Lists.Get_Element (Indices, I);
begin
if Index'Length > Max_Index_Len then
Max_Index_Len := Index'Length;
end if;
end;
end loop;
-- Append index and internal rep of each element.
-- Note that we use the low-level internal rep of the
-- Tcl object, not the Tash Object. This is because
-- if we declare Elem as Tash_Object'Class, we'll
-- increment the reference count and show a confusingly
-- high reference count.
------------------------------------------------
for I in 1 .. Num_Elements loop
declare
Index : constant String := Tash.Lists.Get_Element (Indices, I);
begin
Tash_Interp.Get (Interp);
Elem := Get_Element (Interp, TArray.Obj, Index);
Tash_Interp.Release (Interp);
Ada.Text_IO.Put_Line
("TA.Internal_Rep: i=" & Integer'Image (I) &
" index=" & Index &
" value=" & Tash.Image (Elem) &
" count=" & Interfaces.C.int'Image (Tcl.Tcl_GetRefCount
(Elem)));
Ada.Strings.Unbounded.Append (Image,
ASCII.LF & " " & Name & "(" & Index & ")" &
(Index'Length + 1 .. Max_Index_Len => ' ') &
"=" & Tash.Internal_Rep (Elem));
-- Tcl.Tcl_DecrRefCount (Elem);
end;
end loop;
return Ada.Strings.Unbounded.To_String (Image);
end Internal_Rep;
function Internal_Name (
TArray : in Tash_Array) return String is
--
Length : aliased Interfaces.C.int;
begin -- Internal_Name
if Is_Null (TArray) then
return "";
else
return CHelper.Value (Tcl.Tcl_GetStringFromObj (
TArray.Obj, Length'Access));
end if;
end Internal_Name;
procedure Finalize (
TArray : in out Tash_Array) is
Ref_Count : Interfaces.C.int;
Indices : Tash.Lists.Tash_List;
use type Tcl.Tcl_Obj;
begin -- Finalize
if TArray.Obj = null then
return;
end if;
Ref_Count := Tcl.Tcl_GetRefCount (TArray.Obj);
if Ref_Count = 1 then
-- array is now being de-referenced;
-- decrement count of each element
------------------------------------
Indices := Get_Indices (TArray);
for I in 1 .. Tash.Lists.Length (Indices) loop
declare
Index : constant String :=
Tash.Lists.Get_Element (Indices, I);
Element : Tash.Tash_Object'Class :=
Get_Element (TArray, Index);
begin
Tash.Finalize (Element);
end;
end loop;
Tcl.Tcl_DecrRefCount (TArray.Obj);
else
Tcl.Tcl_DecrRefCount (TArray.Obj);
end if;
end Finalize;
end Tash.Arrays;
|