simple_components_4.42.0_21c00530/strings_edit.ads

  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
--                                                                    --
--  package Strings_Edit            Copyright (c)  Dmitry A. Kazakov  --
--  Interface                                      Luebeck            --
--                                                 Spring, 2000       --
--                                                                    --
--                                Last revision :  22:14 08 May 2009  --
--                                                                    --
--  This  library  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 library  --
--  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 library; 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 Public License.       --
--____________________________________________________________________--
--
-- The following I/O items are supported by the package:
--
-- (o) Integer numbers (generic, package Integer_Edit)
-- (o) Floating-point numbers (generic, package Float_Edit)
-- (o) Roman numbers (the child package Roman_Edit)
-- (o) Strings
--
-- The major differences to  the  standard  Image/Value  attributes  and
-- Text_IO procedures.
--
-- 1. For  numeric  types,  the  base  is  neither written nor read. For
--    instance, output of 23 as hexadecimal gives 17, not 16#17#.
--
-- 2. Get procedures do not skip blank characters around  input  tokens,
--    except  the  cases  when  the  blank  charactes is required by the
--    syntax.
--
-- 3. Get  procedures  use  the current string position pointer, so that
--    they can be consequently called advancing the pointer as the tokes
--    are recognized.
--
-- 4. Numeric  get procedures allow to specify the expected value range.
--    When  the  actual  value  is  out  of  the range then depending on
--    procedure  parameters, either Constrain_Error is propagated or the
--    value is forced to the nearest range boundary.
--
-- 5. Put procedures also use the current string position pointer, which
--    allows to call them consequently.
--
-- 6. The format used for floating-number output is based on the  number
--    precision, instead of rather typographic approach of Text_IO.  The
--    precision can be specified either as the number of valid digits of
--    the  current  base  (i.e. relative) or as the position of the last
--    valid digit (i.e. absolute). For instance, 12.345678 with relative
--    precision 3 gives 12.3.  With  absolute  precision  -3,  it  gives
--    12.346.
--
-- G E T   P R O C E D U R E S
--
-- Get procedures are used to scan strings. The first two parameters are
-- always Source and Pointer. Source is the string to be scaned. Pointer
-- indicates  the  current  position.  After successful completion it is
-- advanced  to the first string position following the recognized item.
-- The  value  of  Pointer  shall  be  in  the  range  Source'First   ..
-- Source'Last+1. The Layout_Error exception  is  propagated  when  this
-- check fails. The third  parameter  usually  accepts  the  value.  The
-- following example shows how to use get procedures:
--
--       package Edit_Float is new Strings_Edit.Float_Edit (Float);
--       use Edit_Float;
--
--       Line        : String (1..512);   -- A line
--       Pointer     : Integer;
--       Value       : Float;
--       TabAndSpace : Ada.Strings.Maps.Character_Set :=
--                        To_Set (" " & Ada.Characters.Latin_1.HT);
--    begin
--       ...
--       Pointer := Line'First;
--       Get (Line, Pointer, TabAndSpace);  -- Skip tabs and spaces
--       Get (Line, Pointer, Value);        -- Get number
--       Get (Line, Pointer, TabAndSpace);  -- Skip tabs and spaces
--       ...
--
-- The numeric Get procedures have additional parameters controlling the
-- range  of  the  input value. The parameters First and Last define the
-- range of  the  expected  value.  The  exception  Constraint_Error  is
-- propagated  when  the value is not in the range. The exception can be
-- suppressed using the parameters ToFirst and ToLast, which  cause  the
-- input value to be substituted by the corresponding  margin  when  the
-- parameter is True.
--
-- V A L U E   F U N C T I O N S
--
-- Each  get procedure returning some value has a corresponding function
-- Value.  The  function  Value  has the same parameter profile with the
-- exception  that  the  parameter  Pointer  is  absent and the value is
-- returned via result. Unlike Get the function Value  tolerates  spaces
-- and  tabs  around  the  converted  value.  The whole string should be
-- matched, otherwise, the exception Data_Error is propagated.
--
-- P U T   P R O C E D U R E S
--
-- Put procedures place something into the  output  string  Destination.
-- The string  is  written  starting  from  Destination  (Pointer).  The
-- parameter Field defines the output size. When it has the  value  zero
-- then the output size is defined by the  output  item.  Otherwise  the
-- output  is  justified  within  the  field  and  the parameter Justify
-- specifies output alignment and  the  parameter  Fill  gives  the  pad
-- character. When Field is greater than Destination'Last - Pointer + 1,
-- the later is used instead. After  successful  completion  Pointer  is
-- advanced   to   the  first  character  following  the  output  or  to
-- Destination'Last + 1.
--
-- I M A G E   F U N C T I O N S
--
-- Image  functions convert a value into string. Unlike standard S'Image
-- they do not place an extra space character.
--
with Ada.Strings.Maps;  use Ada.Strings.Maps;

with Ada.Strings;
with Ada.Characters.Latin_1;

package Strings_Edit is
   pragma Elaborate_Body (Strings_Edit);
   MaxSmall    : constant := 250;  -- Bigger than any possible
   Figures     : constant String := "0123456789ABCDEF";
   Blanks      : constant String := ' ' & Ada.Characters.Latin_1.HT;
   SpaceAndTab : constant Character_Set := To_Set (Blanks);

   subtype Alignment is Ada.Strings.Alignment;

   Center : Alignment renames Ada.Strings.Center;
   Left   : Alignment renames Ada.Strings.Left;
   Right  : Alignment renames Ada.Strings.Right;

   subtype NumberBase is Integer range 2..16;
--
-- S T R I N G S
--
   -- Get -- Skip blank characters
   --
   --    Source  - The string to be processed
   --    Pointer - The current position in the string
   --    Blank   - The blank character
   --
   -- This procedure skips the  character  Blank  starting  from  Source
   -- (Pointer). Pointer is advanced to the first non-Blank character or
   -- to Source'Last + 1.
   --
   -- Exceptions :
   --
   --    Layout_Error - Pointer is not in Source'First..Source'Last + 1
   --
   procedure Get
             (  Source  : String;
                Pointer : in out Integer;
                Blank   : Character := ' '
             );
   --
   -- Get -- Skip blank characters
   --
   --    Source  - The string to be processed
   --    Pointer - The current position in the string
   --    Blanks  - The set characters to be considered as blank ones
   --
   -- This procedure skips all the characters of the set Blanks starting
   -- from  Source (Pointer). Pointer is advanced to the first non-blank
   -- character or to Source'Last + 1.
   --
   -- Exceptions :
   --
   --    Layout_Error - Pointer is not in Source'First..Source'Last + 1
   --
   procedure Get
             (  Source  : String;
                Pointer : in out Integer;
                Blanks  : Character_Set
             );
   --
   -- Put -- Put a character into a string
   --
   --    Destination - The string that accepts the output
   --    Pointer     - The current position in the string
   --    Value       - The character to be put
   --    Field       - The output field
   --    Justify     - Alignment within the field
   --    Fill        - The fill character
   --
   -- This  procedure  places  the specified character (Value parameter)
   -- into the output string Destination. The string is written starting
   -- from the Destination (Pointer).
   --
   -- Exceptions:
   --
   --    Layout_Error - Pointer  is not in Destination'Range or there is
   --                   no room for the output.
   --
   procedure Put
             (  Destination : in out String;
                Pointer     : in out Integer;
                Value       : Character;
                Field       : Natural   := 0;
                Justify     : Alignment := Left;
                Fill        : Character := ' '
             );
   --
   -- Put -- Put a string into another string
   --
   --    Destination - The string that accepts the output
   --    Pointer     - The current position in the string
   --    Value       - The string to be put
   --    Field       - The output field
   --    Justify     - Alignment within the field
   --    Fill        - The fill character
   --
   -- This  procedure places the specified by the Value parameter string
   -- into the output string Destination. The string is written starting
   -- from the Destination (Pointer).
   --
   -- Exceptions:
   --
   --    Layout_Error - Pointer  is not in Destination'Range or there is
   --                   no room for the output.
   --
   procedure Put
             (  Destination : in out String;
                Pointer     : in out Integer;
                Value       : String;
                Field       : Natural   := 0;
                Justify     : Alignment := Left;
                Fill        : Character := ' '
             );
--
-- R O M A N   N U M B E R S
--
   -- Roman_Edit -- Child package for roman numbers
--
-- I N T E G E R   N U M B E R S
--
   -- Integer_Edit -- Generic child package for integer numbers
--
-- F L O A T I N G - P O I N T   N U M B E R S
--
   -- Float_Edit -- Generic child package for floating-point numbers
--
-- P R E F I X   T E S T
--
   --
   -- Is_Prefix -- Test if Prefix is a prefix of Text
   --
   --    Prefix    - To check
   --    Source    - The string
   --  [ Pointer ] - To start at
   --  [ Map     ] - Used to convert characters before comparison
   --
   -- Returns :
   --
   --   True if Prefix is a prefix of Source
   --
   function Is_Prefix (Prefix, Source : String) return Boolean;
   function Is_Prefix
            (  Prefix, Source : String;
               Pointer        : Integer
            )  return Boolean;
   function Is_Prefix
            (  Prefix, Source : String;
               Map            : Character_Mapping
            )  return Boolean;
   function Is_Prefix
            (  Prefix, Source : String;
               Pointer        : Integer;
               Map            : Character_Mapping
            )  return Boolean;
--
-- T R I M   F U N C T I O N S
--
   -- Trim -- Delete blank characters form string ends
   --
   --    Source - The string to be processed
   --    Blank  - The blank character
   --
   -- This function removes the Blank character from both  ends  of  the
   -- string and returns the result.
   --
   -- Returns :
   --
   --    The result string
   --
   function Trim
            (  Source : String;
               Blank  : Character := ' '
            )  return String;
   --
   -- Trim -- Delete blank characters form string ends
   --
   --    Source - The string to be processed
   --    Blanks - The set of blank characters
   --
   -- This  function  removes any characters of the Blanks set from both
   -- ends of the string and returns the result.
   --
   -- Returns :
   --
   --    The result string
   --
   function Trim
            (  Source : String;
               Blanks : Character_Set
            )  return String;

private
   pragma Inline (Is_Prefix);
   --
   -- GetDigit -- Get one digit
   --
   --	   Symbol - To be decoded
   --
   -- Returns :
   --
   --	 [0..15]  The decoded digit value
   --    [16]   The Symbol is not a digit
   --
   function GetDigit (Symbol : Character) return Natural;
   pragma Inline (GetDigit);

end Strings_Edit;