aboutsummaryrefslogtreecommitdiff
path: root/sys/fmtio/gltoc.x
diff options
context:
space:
mode:
authorJoe Hunkeler <jhunkeler@gmail.com>2015-08-11 16:51:37 -0400
committerJoe Hunkeler <jhunkeler@gmail.com>2015-08-11 16:51:37 -0400
commit40e5a5811c6ffce9b0974e93cdd927cbcf60c157 (patch)
tree4464880c571602d54f6ae114729bf62a89518057 /sys/fmtio/gltoc.x
downloadiraf-osx-40e5a5811c6ffce9b0974e93cdd927cbcf60c157.tar.gz
Repatch (from linux) of OSX IRAF
Diffstat (limited to 'sys/fmtio/gltoc.x')
-rw-r--r--sys/fmtio/gltoc.x82
1 files changed, 82 insertions, 0 deletions
diff --git a/sys/fmtio/gltoc.x b/sys/fmtio/gltoc.x
new file mode 100644
index 00000000..eaf47405
--- /dev/null
+++ b/sys/fmtio/gltoc.x
@@ -0,0 +1,82 @@
+# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.
+
+include <mach.h>
+include <ctype.h>
+
+define OCTAL 8
+define DECIMAL 10
+define HEX 16
+define MAX_RADIX 'Z' - 'A' + 11
+
+# GLTOC -- Convert long integer to any radix string. Returns the
+# number of characters generated.
+
+int procedure gltoc (lval, outstr, maxch, base)
+
+long lval # long integer to be encoded
+char outstr[maxch] # output buffer
+int maxch, base # numeric base (2..16)
+
+int carry, d, op, radix, n, size, nchars, gstrcpy()
+long andl(), orl()
+bool unsigned
+
+begin
+ if (IS_INDEFL(lval) && base > 0)
+ return (gstrcpy ("INDEF", outstr, maxch))
+ size = maxch
+
+ # Digit string is generated backwards, then reversed. Unsigned
+ # conversion used if radix negative.
+
+ radix = max(2, min(MAX_RADIX, abs(base)))
+
+ unsigned = (base < 0) # get raw number
+ if (unsigned) {
+ n = andl (lval, MAX_LONG) / 2
+ if (lval < 0)
+ n = orl (n, (MAX_LONG / 2 + 1))
+ carry = andl (lval, 1) # get initial carry
+ } else
+ n = lval
+
+ op = 0
+ repeat {
+ d = abs (mod (n, radix)) # generate next digit
+ if (unsigned) {
+ d = 2 * d + carry # get actual digit value
+ if (d >= radix) { # check for generated carry
+ d = d - radix
+ carry = 1
+ } else
+ carry = 0
+ }
+ op = op + 1
+ if (d < 10) # convert to char and store
+ outstr[op] = TO_DIGIT (d)
+ else
+ outstr[op] = d - 10 + 'A'
+ n = n / radix
+ } until (n == 0 || op >= size)
+
+ if (unsigned) {
+ if (carry != 0 && op < size) { # check for final carry
+ op = op + 1
+ outstr[op] = '1'
+ }
+ } else if (lval < 0 && op < size) { # add sign if needed
+ op = op + 1
+ outstr[op] = '-'
+ }
+ nchars = op # return length of string
+
+ for (d=1; d < op; d=d+1) { # reverse digits
+ carry = outstr[d]
+ outstr[d] = outstr[op]
+ outstr[op] = carry
+ op = op - 1
+ }
+
+ outstr[nchars+1] = EOS
+ return (nchars)
+end