1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- I N T E R F A C E S . C O B O L --
9 -- Copyright (C) 1992-2005, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
34 -- The body of Interfaces.COBOL is implementation independent (i.e. the same
35 -- version is used with all versions of GNAT). The specialization to a
36 -- particular COBOL format is completely contained in the private part of
39 with Interfaces
; use Interfaces
;
40 with System
; use System
;
41 with Unchecked_Conversion
;
43 package body Interfaces
.COBOL
is
45 -----------------------------------------------
46 -- Declarations for External Binary Handling --
47 -----------------------------------------------
49 subtype B1
is Byte_Array
(1 .. 1);
50 subtype B2
is Byte_Array
(1 .. 2);
51 subtype B4
is Byte_Array
(1 .. 4);
52 subtype B8
is Byte_Array
(1 .. 8);
53 -- Representations for 1,2,4,8 byte binary values
55 function To_B1
is new Unchecked_Conversion
(Integer_8
, B1
);
56 function To_B2
is new Unchecked_Conversion
(Integer_16
, B2
);
57 function To_B4
is new Unchecked_Conversion
(Integer_32
, B4
);
58 function To_B8
is new Unchecked_Conversion
(Integer_64
, B8
);
59 -- Conversions from native binary to external binary
61 function From_B1
is new Unchecked_Conversion
(B1
, Integer_8
);
62 function From_B2
is new Unchecked_Conversion
(B2
, Integer_16
);
63 function From_B4
is new Unchecked_Conversion
(B4
, Integer_32
);
64 function From_B8
is new Unchecked_Conversion
(B8
, Integer_64
);
65 -- Conversions from external binary to signed native binary
67 function From_B1U
is new Unchecked_Conversion
(B1
, Unsigned_8
);
68 function From_B2U
is new Unchecked_Conversion
(B2
, Unsigned_16
);
69 function From_B4U
is new Unchecked_Conversion
(B4
, Unsigned_32
);
70 function From_B8U
is new Unchecked_Conversion
(B8
, Unsigned_64
);
71 -- Conversions from external binary to unsigned native binary
73 -----------------------
74 -- Local Subprograms --
75 -----------------------
77 function Binary_To_Decimal
79 Format
: Binary_Format
) return Integer_64
;
80 -- This function converts a numeric value in the given format to its
81 -- corresponding integer value. This is the non-generic implementation
82 -- of Decimal_Conversions.To_Decimal. The generic routine does the
83 -- final conversion to the fixed-point format.
85 function Numeric_To_Decimal
87 Format
: Display_Format
) return Integer_64
;
88 -- This function converts a numeric value in the given format to its
89 -- corresponding integer value. This is the non-generic implementation
90 -- of Decimal_Conversions.To_Decimal. The generic routine does the
91 -- final conversion to the fixed-point format.
93 function Packed_To_Decimal
94 (Item
: Packed_Decimal
;
95 Format
: Packed_Format
) return Integer_64
;
96 -- This function converts a packed value in the given format to its
97 -- corresponding integer value. This is the non-generic implementation
98 -- of Decimal_Conversions.To_Decimal. The generic routine does the
99 -- final conversion to the fixed-point format.
101 procedure Swap
(B
: in out Byte_Array
; F
: Binary_Format
);
102 -- Swaps the bytes if required by the binary format F
106 Format
: Display_Format
;
107 Length
: Natural) return Numeric
;
108 -- This function converts the given integer value into display format,
109 -- using the given format, with the length in bytes of the result given
110 -- by the last parameter. This is the non-generic implementation of
111 -- Decimal_Conversions.To_Display. The conversion of the item from its
112 -- original decimal format to Integer_64 is done by the generic routine.
116 Format
: Packed_Format
;
117 Length
: Natural) return Packed_Decimal
;
118 -- This function converts the given integer value into packed format,
119 -- using the given format, with the length in digits of the result given
120 -- by the last parameter. This is the non-generic implementation of
121 -- Decimal_Conversions.To_Display. The conversion of the item from its
122 -- original decimal format to Integer_64 is done by the generic routine.
124 function Valid_Numeric
126 Format
: Display_Format
) return Boolean;
127 -- This is the non-generic implementation of Decimal_Conversions.Valid
128 -- for the display case.
130 function Valid_Packed
131 (Item
: Packed_Decimal
;
132 Format
: Packed_Format
) return Boolean;
133 -- This is the non-generic implementation of Decimal_Conversions.Valid
134 -- for the packed case.
136 -----------------------
137 -- Binary_To_Decimal --
138 -----------------------
140 function Binary_To_Decimal
142 Format
: Binary_Format
) return Integer_64
144 Len
: constant Natural := Item
'Length;
148 if Format
in Binary_Unsigned_Format
then
149 return Integer_64
(From_B1U
(Item
));
151 return Integer_64
(From_B1
(Item
));
161 if Format
in Binary_Unsigned_Format
then
162 return Integer_64
(From_B2U
(R
));
164 return Integer_64
(From_B2
(R
));
175 if Format
in Binary_Unsigned_Format
then
176 return Integer_64
(From_B4U
(R
));
178 return Integer_64
(From_B4
(R
));
189 if Format
in Binary_Unsigned_Format
then
190 return Integer_64
(From_B8U
(R
));
192 return Integer_64
(From_B8
(R
));
196 -- Length is not 1, 2, 4 or 8
199 raise Conversion_Error
;
201 end Binary_To_Decimal
;
203 ------------------------
204 -- Numeric_To_Decimal --
205 ------------------------
207 -- The following assumptions are made in the coding of this routine:
209 -- The range of COBOL_Digits is compact and the ten values
210 -- represent the digits 0-9 in sequence
212 -- The range of COBOL_Plus_Digits is compact and the ten values
213 -- represent the digits 0-9 in sequence with a plus sign.
215 -- The range of COBOL_Minus_Digits is compact and the ten values
216 -- represent the digits 0-9 in sequence with a minus sign.
218 -- The COBOL_Minus_Digits set is disjoint from COBOL_Digits
220 -- These assumptions are true for all COBOL representations we know of
222 function Numeric_To_Decimal
224 Format
: Display_Format
) return Integer_64
226 pragma Unsuppress
(Range_Check
);
227 Sign
: COBOL_Character
:= COBOL_Plus
;
228 Result
: Integer_64
:= 0;
231 if not Valid_Numeric
(Item
, Format
) then
232 raise Conversion_Error
;
235 for J
in Item
'Range loop
237 K
: constant COBOL_Character
:= Item
(J
);
240 if K
in COBOL_Digits
then
241 Result
:= Result
* 10 +
242 (COBOL_Character
'Pos (K
) -
243 COBOL_Character
'Pos (COBOL_Digits
'First));
245 elsif K
in COBOL_Plus_Digits
then
246 Result
:= Result
* 10 +
247 (COBOL_Character
'Pos (K
) -
248 COBOL_Character
'Pos (COBOL_Plus_Digits
'First));
250 elsif K
in COBOL_Minus_Digits
then
251 Result
:= Result
* 10 +
252 (COBOL_Character
'Pos (K
) -
253 COBOL_Character
'Pos (COBOL_Minus_Digits
'First));
256 -- Only remaining possibility is COBOL_Plus or COBOL_Minus
264 if Sign
= COBOL_Plus
then
271 when Constraint_Error
=>
272 raise Conversion_Error
;
274 end Numeric_To_Decimal
;
276 -----------------------
277 -- Packed_To_Decimal --
278 -----------------------
280 function Packed_To_Decimal
281 (Item
: Packed_Decimal
;
282 Format
: Packed_Format
) return Integer_64
284 pragma Unsuppress
(Range_Check
);
285 Result
: Integer_64
:= 0;
286 Sign
: constant Decimal_Element
:= Item
(Item
'Last);
289 if not Valid_Packed
(Item
, Format
) then
290 raise Conversion_Error
;
293 case Packed_Representation
is
295 for J
in Item
'First .. Item
'Last - 1 loop
296 Result
:= Result
* 10 + Integer_64
(Item
(J
));
299 if Sign
= 16#
0B#
or else Sign
= 16#
0D#
then
307 when Constraint_Error
=>
308 raise Conversion_Error
;
309 end Packed_To_Decimal
;
315 procedure Swap
(B
: in out Byte_Array
; F
: Binary_Format
) is
316 Little_Endian
: constant Boolean :=
317 System
.Default_Bit_Order
= System
.Low_Order_First
;
320 -- Return if no swap needed
324 if not Little_Endian
then
329 if Little_Endian
then
337 -- Here a swap is needed
340 Len
: constant Natural := B
'Length;
343 for J
in 1 .. Len
/ 2 loop
345 Temp
: constant Byte
:= B
(J
);
348 B
(J
) := B
(Len
+ 1 - J
);
349 B
(Len
+ 1 - J
) := Temp
;
355 -----------------------
356 -- To_Ada (function) --
357 -----------------------
359 function To_Ada
(Item
: Alphanumeric
) return String is
360 Result
: String (Item
'Range);
363 for J
in Item
'Range loop
364 Result
(J
) := COBOL_To_Ada
(Item
(J
));
370 ------------------------
371 -- To_Ada (procedure) --
372 ------------------------
375 (Item
: Alphanumeric
;
382 if Item
'Length > Target
'Length then
383 raise Constraint_Error
;
386 Last_Val
:= Target
'First - 1;
387 for J
in Item
'Range loop
388 Last_Val
:= Last_Val
+ 1;
389 Target
(Last_Val
) := COBOL_To_Ada
(Item
(J
));
395 -------------------------
396 -- To_COBOL (function) --
397 -------------------------
399 function To_COBOL
(Item
: String) return Alphanumeric
is
400 Result
: Alphanumeric
(Item
'Range);
403 for J
in Item
'Range loop
404 Result
(J
) := Ada_To_COBOL
(Item
(J
));
410 --------------------------
411 -- To_COBOL (procedure) --
412 --------------------------
416 Target
: out Alphanumeric
;
422 if Item
'Length > Target
'Length then
423 raise Constraint_Error
;
426 Last_Val
:= Target
'First - 1;
427 for J
in Item
'Range loop
428 Last_Val
:= Last_Val
+ 1;
429 Target
(Last_Val
) := Ada_To_COBOL
(Item
(J
));
441 Format
: Display_Format
;
442 Length
: Natural) return Numeric
444 Result
: Numeric
(1 .. Length
);
445 Val
: Integer_64
:= Item
;
447 procedure Convert
(First
, Last
: Natural);
448 -- Convert the number in Val into COBOL_Digits, storing the result
449 -- in Result (First .. Last). Raise Conversion_Error if too large.
451 procedure Embed_Sign
(Loc
: Natural);
452 -- Used for the nonseparate formats to embed the appropriate sign
453 -- at the specified location (i.e. at Result (Loc))
455 procedure Convert
(First
, Last
: Natural) is
459 while J
>= First
loop
462 (COBOL_Character
'Pos (COBOL_Digits
'First) +
463 Integer (Val
mod 10));
467 for K
in First
.. J
- 1 loop
468 Result
(J
) := COBOL_Digits
'First;
478 raise Conversion_Error
;
481 procedure Embed_Sign
(Loc
: Natural) is
482 Digit
: Natural range 0 .. 9;
485 Digit
:= COBOL_Character
'Pos (Result
(Loc
)) -
486 COBOL_Character
'Pos (COBOL_Digits
'First);
491 (COBOL_Character
'Pos (COBOL_Plus_Digits
'First) + Digit
);
495 (COBOL_Character
'Pos (COBOL_Minus_Digits
'First) + Digit
);
499 -- Start of processing for To_Display
505 raise Conversion_Error
;
510 when Leading_Separate
=>
512 Result
(1) := COBOL_Minus
;
515 Result
(1) := COBOL_Plus
;
520 when Trailing_Separate
=>
522 Result
(Length
) := COBOL_Minus
;
525 Result
(Length
) := COBOL_Plus
;
528 Convert
(1, Length
- 1);
530 when Leading_Nonseparate
=>
535 when Trailing_Nonseparate
=>
551 Format
: Packed_Format
;
552 Length
: Natural) return Packed_Decimal
554 Result
: Packed_Decimal
(1 .. Length
);
557 procedure Convert
(First
, Last
: Natural);
558 -- Convert the number in Val into a sequence of Decimal_Element values,
559 -- storing the result in Result (First .. Last). Raise Conversion_Error
560 -- if the value is too large to fit.
562 procedure Convert
(First
, Last
: Natural) is
566 while J
>= First
loop
567 Result
(J
) := Decimal_Element
(Val
mod 10);
572 for K
in First
.. J
- 1 loop
583 raise Conversion_Error
;
586 -- Start of processing for To_Packed
589 case Packed_Representation
is
591 if Format
= Packed_Unsigned
then
593 raise Conversion_Error
;
595 Result
(Length
) := 16#F#
;
600 Result
(Length
) := 16#C#
;
604 Result
(Length
) := 16#D#
;
608 Convert
(1, Length
- 1);
617 function Valid_Numeric
619 Format
: Display_Format
) return Boolean
622 if Item
'Length = 0 then
626 -- All character positions except first and last must be Digits.
627 -- This is true for all the formats.
629 for J
in Item
'First + 1 .. Item
'Last - 1 loop
630 if Item
(J
) not in COBOL_Digits
then
637 return Item
(Item
'First) in COBOL_Digits
638 and then Item
(Item
'Last) in COBOL_Digits
;
640 when Leading_Separate
=>
641 return (Item
(Item
'First) = COBOL_Plus
or else
642 Item
(Item
'First) = COBOL_Minus
)
643 and then Item
(Item
'Last) in COBOL_Digits
;
645 when Trailing_Separate
=>
646 return Item
(Item
'First) in COBOL_Digits
648 (Item
(Item
'Last) = COBOL_Plus
or else
649 Item
(Item
'Last) = COBOL_Minus
);
651 when Leading_Nonseparate
=>
652 return (Item
(Item
'First) in COBOL_Plus_Digits
or else
653 Item
(Item
'First) in COBOL_Minus_Digits
)
654 and then Item
(Item
'Last) in COBOL_Digits
;
656 when Trailing_Nonseparate
=>
657 return Item
(Item
'First) in COBOL_Digits
659 (Item
(Item
'Last) in COBOL_Plus_Digits
or else
660 Item
(Item
'Last) in COBOL_Minus_Digits
);
669 function Valid_Packed
670 (Item
: Packed_Decimal
;
671 Format
: Packed_Format
) return Boolean
674 case Packed_Representation
is
676 for J
in Item
'First .. Item
'Last - 1 loop
682 -- For unsigned, sign digit must be F
684 if Format
= Packed_Unsigned
then
685 return Item
(Item
'Last) = 16#F#
;
687 -- For signed, accept all standard and non-standard signs
690 return Item
(Item
'Last) in 16#A#
.. 16#F#
;
695 -------------------------
696 -- Decimal_Conversions --
697 -------------------------
699 package body Decimal_Conversions
is
701 ---------------------
702 -- Length (binary) --
703 ---------------------
705 -- Note that the tests here are all compile time tests
707 function Length
(Format
: Binary_Format
) return Natural is
708 pragma Unreferenced
(Format
);
710 if Num
'Digits <= 2 then
712 elsif Num
'Digits <= 4 then
714 elsif Num
'Digits <= 9 then
716 else -- Num'Digits in 10 .. 18
721 ----------------------
722 -- Length (display) --
723 ----------------------
725 function Length
(Format
: Display_Format
) return Natural is
727 if Format
= Leading_Separate
or else Format
= Trailing_Separate
then
728 return Num
'Digits + 1;
734 ---------------------
735 -- Length (packed) --
736 ---------------------
738 -- Note that the tests here are all compile time checks
741 (Format
: Packed_Format
) return Natural
743 pragma Unreferenced
(Format
);
745 case Packed_Representation
is
747 return (Num
'Digits + 2) / 2 * 2;
757 Format
: Binary_Format
) return Byte_Array
760 -- Note: all these tests are compile time tests
762 if Num
'Digits <= 2 then
763 return To_B1
(Integer_8
'Integer_Value (Item
));
765 elsif Num
'Digits <= 4 then
767 R
: B2
:= To_B2
(Integer_16
'Integer_Value (Item
));
774 elsif Num
'Digits <= 9 then
776 R
: B4
:= To_B4
(Integer_32
'Integer_Value (Item
));
783 else -- Num'Digits in 10 .. 18
785 R
: B8
:= To_B8
(Integer_64
'Integer_Value (Item
));
794 when Constraint_Error
=>
795 raise Conversion_Error
;
798 ---------------------------------
799 -- To_Binary (internal binary) --
800 ---------------------------------
802 function To_Binary
(Item
: Num
) return Binary
is
803 pragma Unsuppress
(Range_Check
);
805 return Binary
'Integer_Value (Item
);
807 when Constraint_Error
=>
808 raise Conversion_Error
;
811 -------------------------
812 -- To_Decimal (binary) --
813 -------------------------
817 Format
: Binary_Format
) return Num
819 pragma Unsuppress
(Range_Check
);
821 return Num
'Fixed_Value (Binary_To_Decimal
(Item
, Format
));
823 when Constraint_Error
=>
824 raise Conversion_Error
;
827 ----------------------------------
828 -- To_Decimal (internal binary) --
829 ----------------------------------
831 function To_Decimal
(Item
: Binary
) return Num
is
832 pragma Unsuppress
(Range_Check
);
834 return Num
'Fixed_Value (Item
);
836 when Constraint_Error
=>
837 raise Conversion_Error
;
840 --------------------------
841 -- To_Decimal (display) --
842 --------------------------
846 Format
: Display_Format
) return Num
848 pragma Unsuppress
(Range_Check
);
851 return Num
'Fixed_Value (Numeric_To_Decimal
(Item
, Format
));
853 when Constraint_Error
=>
854 raise Conversion_Error
;
857 ---------------------------------------
858 -- To_Decimal (internal long binary) --
859 ---------------------------------------
861 function To_Decimal
(Item
: Long_Binary
) return Num
is
862 pragma Unsuppress
(Range_Check
);
864 return Num
'Fixed_Value (Item
);
866 when Constraint_Error
=>
867 raise Conversion_Error
;
870 -------------------------
871 -- To_Decimal (packed) --
872 -------------------------
875 (Item
: Packed_Decimal
;
876 Format
: Packed_Format
) return Num
878 pragma Unsuppress
(Range_Check
);
880 return Num
'Fixed_Value (Packed_To_Decimal
(Item
, Format
));
882 when Constraint_Error
=>
883 raise Conversion_Error
;
892 Format
: Display_Format
) return Numeric
894 pragma Unsuppress
(Range_Check
);
898 (Integer_64
'Integer_Value (Item
),
902 when Constraint_Error
=>
903 raise Conversion_Error
;
910 function To_Long_Binary
(Item
: Num
) return Long_Binary
is
911 pragma Unsuppress
(Range_Check
);
913 return Long_Binary
'Integer_Value (Item
);
915 when Constraint_Error
=>
916 raise Conversion_Error
;
925 Format
: Packed_Format
) return Packed_Decimal
927 pragma Unsuppress
(Range_Check
);
931 (Integer_64
'Integer_Value (Item
),
935 when Constraint_Error
=>
936 raise Conversion_Error
;
945 Format
: Binary_Format
) return Boolean
948 pragma Unreferenced
(Val
);
950 Val
:= To_Decimal
(Item
, Format
);
953 when Conversion_Error
=>
957 ---------------------
958 -- Valid (display) --
959 ---------------------
963 Format
: Display_Format
) return Boolean
966 return Valid_Numeric
(Item
, Format
);
974 (Item
: Packed_Decimal
;
975 Format
: Packed_Format
) return Boolean
978 return Valid_Packed
(Item
, Format
);
981 end Decimal_Conversions
;
983 end Interfaces
.COBOL
;