File: x_strings.adb

package info (click to toggle)
libadabindx 0.7.2-4
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,136 kB
  • ctags: 32
  • sloc: ada: 35,597; makefile: 556; sh: 10
file content (397 lines) | stat: -rw-r--r-- 13,489 bytes parent folder | download
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
-------------------------------------------------------------------------------
--                                                                           --
--  Ada Interface to the X Window System and Motif(tm)/Lesstif               --
--  Copyright (c) 1996-2000 Hans-Frieder Vogt                                --
--                                                                           --
--  Adabindx is free software; you can redistribute it and/or modify it      --
--  under the terms of the GNU General Public License as published by the    --
--  Free Software Foundation; either version 2 of the License, or (at your   --
--  option) any later version.                                               --
--                                                                           --
--  This program 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        --
--  along with this program; if not, write to the                            --
--  Free Software Foundation, Inc.,                                          --
--  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 General Public License.                               --
--                                                                           --
--  X Window System is copyrighted by the X Consortium                       --
--  Motif(tm)       is copyrighted by the Open Software Foundation, Inc.     --
--                  and by The Open Group                                    --
--                                                                           --
--                                                                           --
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
--
-- HISTORY:
--          June 20, 1998 begin of history
--
-------------------------------------------------------------------------------

with Interfaces.C,
     Unchecked_Conversion,
     Unchecked_Deallocation;

package body X_Strings is

   --  for address operation may be used Interfaces.C.Pointers

   use type Interfaces.C.unsigned_long;

   -- some conversion routines
   function To_Long_Uns is
      new Unchecked_Conversion (X_String, Interfaces.C.unsigned_long);
   function To_X_String is
      new Unchecked_Conversion (Interfaces.C.unsigned_long, X_String);


   procedure Next_Char (Pointer : in out X_String) is
   begin
      Pointer := To_X_String (To_Long_Uns (Pointer) + 1);
   end Next_Char;
   pragma Inline (Next_Char);


   procedure Prev_Char (Pointer : in out X_String) is
   begin
      Pointer := To_X_String (To_Long_Uns (Pointer) - 1);
   end Prev_Char;
   pragma Inline (Prev_Char);



   procedure Free (What : in out X_String) is
   begin
      if What /= Null_X_String then
         declare
            subtype Specific_String is String (1 .. Length(What) + 1);
            type Specific_String_Access is access Specific_String;
            pragma Controlled (Specific_String_Access);
            procedure Deallocate_X_String is new Unchecked_Deallocation (Specific_String, Specific_String_Access);
            function To_String is new Unchecked_Conversion (X_String, Specific_String_Access);
            Temp_String : Specific_String_Access := To_String (What);
         begin
            Deallocate_X_String (Temp_String);
            What := Null_X_String;
         end;
      end if;
   end Free;



   function Is_Equal (Left, Right : in X_String) return Boolean is
      Left_Len  : constant Natural := Length (Left);
      Right_Len : constant Natural := Length (Right);
   begin
      if Left_Len /= Right_Len then
         return False;
      elsif Left_Len = 0 then  -- both arguments have no length
         return True;
      else
         declare
            Left_Acc  : X_String := Left;
            Right_Acc : X_String := Right;
         begin
            for I in 1 .. Left_Len loop
               if Left_Acc.all = Right_Acc.all then
                  Next_Char (Left_Acc);
                  Next_Char (Right_Acc);
               else
                  return False;
               end if;
            end loop;
            return True;
         end;
      end if;
   end Is_Equal;


   function Length (Item : in X_String) return Natural is
      Indx    : Natural := 0;
      Ch      : Character;
      Mom_Acc : X_String := Item;
   begin
      if Item = Null_X_String then
         return 0;
      else
         -- look for the terminating Null
         loop
            Ch := Mom_Acc.all;
            if Ch /= Character'Val (0) then
               Indx := Indx + 1;
               Next_Char (Mom_Acc);
            else
               exit;
            end if;
         end loop;
         return Indx;
      end if;
   end Length;


   function "&" (Left, Right : in X_String) return X_String is
      Left_Len  : constant Natural := Length (Left);
      Right_Len : constant Natural := Length (Right);
   begin
      if Left_Len+Right_Len > 0 then
         declare
            Str       : String (1 .. Left_Len+Right_Len);
            Mom_Acc   : X_String;
            Arry_Indx : Natural := 1;
         begin
            Mom_Acc := Left;
            for I in 1 .. Left_Len loop
               Str (Arry_Indx) := Mom_Acc.all;
               Arry_Indx       := Arry_Indx + 1;
               Next_Char (Mom_Acc);
            end loop;
            Mom_Acc := Right;
            for I in 1 .. Right_Len loop
               Str (Arry_Indx) := Mom_Acc.all;
               Arry_Indx       := Arry_Indx + 1;
               Next_Char (Mom_Acc);
            end loop;
            return To_X_String (Str);
         end;
      else
         return Null_X_String;
      end if;
   end "&";


   function "&" (Left : in X_String; Right : in String) return X_String is
      Left_Len  : constant Natural := Length (Left);
      Right_Len : constant Natural := Right'Length;
   begin
      if Left_Len+Right_Len > 0 then
         declare
            Str        : String (1 .. Left_Len+Right_Len);
            Mom_Acc    : X_String;
            Arry_Indx  : Natural := 1;
         begin
            Mom_Acc := Left;
            for I in 1 .. Left_Len loop
               Str (Arry_Indx) := Mom_Acc.all;
               Arry_Indx       := Arry_Indx + 1;
               Next_Char (Mom_Acc);
            end loop;
            for I in Right'Range loop
               Str (Arry_Indx) := Right (I);
               Arry_Indx       := Arry_Indx + 1;
            end loop;
            return To_X_String (Str);
         end;
      else
         return Null_X_String;
      end if;
   end "&";


   function "&" (Left : in X_String; Right : in Character) return X_String is
      Left_Len  : constant Natural := Length (Left);
   begin
      declare
         Str        : String (1 .. Left_Len+1);
         Mom_Acc    : X_String;
         Arry_Indx  : Natural := 1;
      begin
         Mom_Acc := Left;
         for I in 1 .. Left_Len loop
            Str (Arry_Indx) := Mom_Acc.all;
            Arry_Indx       := Arry_Indx + 1;
            Next_Char (Mom_Acc);
         end loop;
         Str (Arry_Indx)   := Right;
         return To_X_String (Str);
      end;
   end "&";


   function "&" (Left : in String;    Right : in X_String) return X_String is
      Left_Len  : constant Natural := Left'Length;
      Right_Len : constant Natural := Length (Right);
   begin
      if Left_Len+Right_Len > 0 then
         declare
            Str        : String (1 .. Left_Len+Right_Len);
            Mom_Acc    : X_String;
            Arry_Indx  : Natural := 1;
         begin
            for I in Left'Range loop
               Str (Arry_Indx) := Left (I);
               Arry_Indx       := Arry_Indx + 1;
            end loop;
            Mom_Acc := Right;
            for I in 1 .. Right_Len loop
               Str (Arry_Indx) := Mom_Acc.all;
               Arry_Indx       := Arry_Indx + 1;
               Next_Char (Mom_Acc);
            end loop;
            return To_X_String (Str);
         end;
      else
         return Null_X_String;
      end if;
   end "&";


   function "&" (Left : in Character; Right : in X_String) return X_String is
      Right_Len : constant Natural := Length (Right);
   begin
      declare
         Str        : String (1 .. Right_Len+1);
         Mom_Acc    : X_String;
         Arry_Indx  : Natural := 1;
      begin
         Str (Arry_Indx) := Left;
         Arry_Indx       := Arry_Indx + 1;
         Mom_Acc := Right;
         for I in 1 .. Right_Len loop
            Str (Arry_Indx) := Mom_Acc.all;
            Arry_Indx       := Arry_Indx + 1;
            Next_Char (Mom_Acc);
         end loop;
         return To_X_String (Str);
      end;
   end "&";





   procedure Append (To : in out X_String; Item : in X_String) is
      Left_Len  : constant Natural := Length (To);
      Right_Len : constant Natural := Length (Item);
   begin
      if Right_Len > 0 then
         declare
            Str       : String (1 .. Left_Len+Right_Len);
            Mom_Acc   : X_String;
            Arry_Indx : Natural := 1;
         begin
            Mom_Acc := To;
            for I in 1 .. Left_Len loop
               Str (Arry_Indx) := Mom_Acc.all;
               Arry_Indx       := Arry_Indx + 1;
               Next_Char (Mom_Acc);
            end loop;
            Mom_Acc := Item;
            for I in 1 .. Right_Len loop
               Str (Arry_Indx) := Mom_Acc.all;
               Arry_Indx       := Arry_Indx + 1;
               Next_Char (Mom_Acc);
            end loop;
            Free (To);
            To              := To_X_String (Str);
         end;
      end if;
   end Append;


   procedure Append (To : in out X_String; Item : in String) is
      Left_Len  : constant Natural := Length (To);
      Right_Len : constant Natural := Item'Length;
   begin
      if Right_Len > 0 then
         declare
            Str       : String (1 .. Left_Len+Right_Len);
            Mom_Acc   : X_String;
            Arry_Indx : Natural := 1;
         begin
            Mom_Acc := To;
            for I in 1 .. Left_Len loop
               Str (Arry_Indx) := Mom_Acc.all;
               Arry_Indx       := Arry_Indx + 1;
               Next_Char (Mom_Acc);
            end loop;
            for I in Item'Range loop
               Str (Arry_Indx) := Item (I);
               Arry_Indx       := Arry_Indx + 1;
            end loop;
            Free (To);
            To              := To_X_String (Str);
         end;
      end if;
   end Append;



   procedure Append (To : in out X_String; Item : in Character) is
      Left_Len  : constant Natural := Length (To);
   begin
      declare
         Str       : String (1 .. Left_Len+1);
         Mom_Acc   : X_String;
         Arry_Indx : Natural := 1;
      begin
         Mom_Acc := To;
         for I in 1 .. Left_Len loop
            Str (Arry_Indx) := Mom_Acc.all;
            Arry_Indx       := Arry_Indx + 1;
            Next_Char (Mom_Acc);
         end loop;
         Str (Arry_Indx)   := Item;
         Free (To);
         To              := To_X_String (Str);
      end;
   end Append;



   function To_X_String (Item : in String) return X_String is
      subtype Specific_String is String (1 .. Item'Length + 1);
      type Specific_String_Access is access Specific_String;
      pragma Controlled (Specific_String_Access);
      function New_String is new Unchecked_Conversion (Specific_String_Access,
                                                        X_String);
   begin
      if Item'Length > 0 then
         return New_String (new Specific_String'(Item & Character'Val (0)));
      else
         return Null_X_String;
      end if;
   end To_X_String;


   function To_String (Item : in X_String) return String is
      Indx    : Natural := 0;
      Ch      : Character;
      Mom_Acc : X_String := Item;
   begin
      if Item = Null_X_String then
         return String'("");
      else
         loop
            Ch := Mom_Acc.all;
            exit when Ch = Character'Val (0);
               Indx := Indx + 1;
               Next_Char (Mom_Acc);
         end loop;
         declare
            Buffer : String (1 .. Indx);
         begin
            -- Mom_Acc shows to a Character'Val (0) now!
            for I in reverse 1 .. Indx loop
               Prev_Char (Mom_Acc);
               Buffer (I) := Mom_Acc.all;
            end loop;
            return Buffer;
         end;
      end if;
   end To_String;



end X_Strings;