| 1 |
(* Conversions.mod provides a Logitech-3.0 compatible library. |
| 2 |
|
| 3 |
Copyright (C) 2004-2025 Free Software Foundation, Inc. |
| 4 |
Contributed by Gaius Mulley <[email protected]>. |
| 5 |
|
| 6 |
This file is part of GNU Modula-2. |
| 7 |
|
| 8 |
GNU Modula-2 is free software; you can redistribute it and/or modify |
| 9 |
it under the terms of the GNU General Public License as published by |
| 10 |
the Free Software Foundation; either version 3, or (at your option) |
| 11 |
any later version. |
| 12 |
|
| 13 |
GNU Modula-2 is distributed in the hope that it will be useful, but |
| 14 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
General Public License for more details. |
| 17 |
|
| 18 |
Under Section 7 of GPL version 3, you are granted additional |
| 19 |
permissions described in the GCC Runtime Library Exception, version |
| 20 |
3.1, as published by the Free Software Foundation. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License and |
| 23 |
a copy of the GCC Runtime Library Exception along with this program; |
| 24 |
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 25 |
<http://www.gnu.org/licenses/>. *) |
| 26 |
|
| 27 |
IMPLEMENTATION MODULE Conversions ; |
| 28 |
|
| 29 |
|
| 30 |
FROM DynamicStrings IMPORT String, InitString, KillString, CopyOut ; |
| 31 |
FROM StringConvert IMPORT IntegerToString, StringToInteger, |
| 32 |
StringToLongInteger, LongIntegerToString, |
| 33 |
StringToCardinal, CardinalToString ; |
| 34 |
|
| 35 |
|
| 36 |
(* |
| 37 |
ConvertOctal - converts a CARDINAL, num, into an octal string |
| 38 |
and right justifies the string. It adds |
| 39 |
spaces rather than '0' to pad out the string |
| 40 |
to len characters. |
| 41 |
|
| 42 |
If the length of str is < num then the number is |
| 43 |
truncated on the right. |
| 44 |
*) |
| 45 |
|
| 46 |
PROCEDURE ConvertOctal (num, len: CARDINAL; VAR str: ARRAY OF CHAR) ; |
| 47 |
VAR |
| 48 |
s: String ; |
| 49 |
BEGIN |
| 50 |
s := CardinalToString(num, len, ' ', 8, FALSE) ; |
| 51 |
CopyOut(str, s) ; |
| 52 |
s := KillString(s) |
| 53 |
END ConvertOctal ; |
| 54 |
|
| 55 |
|
| 56 |
PROCEDURE ConvertHex (num, len: CARDINAL; VAR str: ARRAY OF CHAR) ; |
| 57 |
VAR |
| 58 |
s: String ; |
| 59 |
BEGIN |
| 60 |
s := CardinalToString(num, len, ' ', 16, TRUE) ; |
| 61 |
CopyOut(str, s) ; |
| 62 |
s := KillString(s) |
| 63 |
END ConvertHex ; |
| 64 |
|
| 65 |
|
| 66 |
PROCEDURE ConvertCardinal (num, len: CARDINAL; VAR str: ARRAY OF CHAR) ; |
| 67 |
VAR |
| 68 |
s: String ; |
| 69 |
BEGIN |
| 70 |
s := CardinalToString(num, len, ' ', 10, FALSE) ; |
| 71 |
CopyOut(str, s) ; |
| 72 |
s := KillString(s) |
| 73 |
END ConvertCardinal ; |
| 74 |
|
| 75 |
|
| 76 |
(* |
| 77 |
The INTEGER counterparts will add a '-' if, num, is <0 |
| 78 |
*) |
| 79 |
|
| 80 |
PROCEDURE ConvertInteger (num: INTEGER; len: CARDINAL; |
| 81 |
VAR str: ARRAY OF CHAR) ; |
| 82 |
VAR |
| 83 |
s: String ; |
| 84 |
BEGIN |
| 85 |
IF num<0 |
| 86 |
THEN |
| 87 |
s := IntegerToString(num, len, ' ', TRUE, 10, FALSE) |
| 88 |
ELSE |
| 89 |
s := IntegerToString(num, len, ' ', FALSE, 10, FALSE) |
| 90 |
END ; |
| 91 |
CopyOut(str, s) ; |
| 92 |
s := KillString(s) |
| 93 |
END ConvertInteger ; |
| 94 |
|
| 95 |
|
| 96 |
PROCEDURE ConvertLongInt (num: LONGINT; len: CARDINAL; VAR str: ARRAY OF CHAR) ; |
| 97 |
VAR |
| 98 |
s: String ; |
| 99 |
BEGIN |
| 100 |
IF num<0 |
| 101 |
THEN |
| 102 |
s := LongIntegerToString(num, len, ' ', TRUE, 10, FALSE) |
| 103 |
ELSE |
| 104 |
s := LongIntegerToString(num, len, ' ', FALSE, 10, FALSE) |
| 105 |
END ; |
| 106 |
CopyOut(str, s) ; |
| 107 |
s := KillString(s) |
| 108 |
END ConvertLongInt ; |
| 109 |
|
| 110 |
|
| 111 |
PROCEDURE ConvertShortInt (num: SHORTINT; len: CARDINAL; VAR str: ARRAY OF CHAR) ; |
| 112 |
VAR |
| 113 |
s: String ; |
| 114 |
BEGIN |
| 115 |
IF num<0 |
| 116 |
THEN |
| 117 |
s := IntegerToString(VAL(INTEGER, num), len, ' ', TRUE, 10, FALSE) |
| 118 |
ELSE |
| 119 |
s := IntegerToString(VAL(INTEGER, num), len, ' ', FALSE, 10, FALSE) |
| 120 |
END ; |
| 121 |
CopyOut(str, s) ; |
| 122 |
s := KillString(s) |
| 123 |
END ConvertShortInt ; |
| 124 |
|
| 125 |
|
| 126 |
END Conversions. |