A set of portable routines to perform tasks associated with character handling is available within ADAM. These routines have the prefix CHR, and a complete description of the specifications is given in SUN/40. Appendix E gives a list of all the routines together with their argument lists and a short description of their functions. The appendix also indicates whether each routine is implemented as a subroutine or a function. (Functions must be declared with the appropriate type in the programs using them.) The CHR library is automatically linked during the ALINK procedure. Several of the most useful CHR routines are considered below.
One set of routines can be characterised as having the form CHR_xTOy
where x
and y
are each one of C,
D, I, L & R
corresponding to type CHARACTER, DOUBLE, INTEGER, LOGICAL & REAL
respectively. For
example the call below will encode the real number X as a string:
NCHAR is the number of characters in the returned STRING. Thus if X=1237.4
, STRING
is returned as
’1237.4’ and NCHAR
becomes 6
.
The example program REPDIM1 in Section 5 reported the dimensions of the input data array
one after another. A tidier result can be achieved by building a string using a succession
of CHR_PUTx calls, where as above, x
can be any of C, D, I, L or R. These calls have the
form:
where VALUE is a value of appropriate type, STRING is a character string which has the encoded value appended, and NCHAR on entry contains the position in the STRING at which the encoded VALUE is inserted and contains the length of the new string on return. The code to report the array dimensions can be changed to:
ADAM_EXAMPLES:REPDIM2.FOR contains this modification. Running this program on the datafile IMAGE.SDF produces the output below:
Another common use for the CHR routines is in the ‘cleaning’ and comparison of strings. The fragment of code below is used to remove blanks and determine if the units given in the two strings are the same.
A more ambitious example taken from ADAM_EXAMPLES:GETEBV.FOR follows. The extract below compares an input object name, OBJECT, with star names in a text file (EBV.DAT) and if a match is found, the value (the E) associated with the star is reported. The text file format is:
The code first tidies the input string OBJECT by removing blanks. The first character of OBJECT is then
tested to see if it is a number. If only the number has been given, the string ’HD’ is added at the beginning.
Each line in the text file is now read; blank lines and lines beginning with ‘*
’ are ignored. Other lines are
decoded into words. On each line the first word is the star name and the second is a string containing
the E
associated with that star. The star name on each line is compared with OBJECT. If a match is found the
second word on that line is decoded into a real value and reported.