Debian Codegraph @

prim/tc3/libsrc/edpic.c

raw
1 /*===========================================================================
2 Copyright (C) 1986-2009 European Southern Observatory (ESO)
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of
7 the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public
15 License along with this program; if not, write to the Free
16 Software Foundation, Inc., 675 Massachusetss Ave, Cambridge,
17 MA 02139, USA.
18
19 Corresponding concerning ESO-MIDAS should be addressed as follows:
20 Internet e-mail: [email protected]
21 Postal address: European Southern Observatory
22 Data Management Division
23 Karl-Schwarzschild-Strasse 2
24 D 85748 Garching bei Muenchen
25 GERMANY
26 ===========================================================================*/
27
28 /*+++++++++
29 .MODULE edpic.c
30 .AUTHOR Francois Ochsenbein [ESO]
31 .LANGUAGE C
32 .CATEGORY Edition of integer data
33
34 .COMMENTS
35 Functions in this module edit numbers using a "picture" or template.
36 \begin{TeX}
37 The picture is a character string specifying how to edit a number.
38 All characters but the following ones are simply copied to the output:
39 \begin{itemize}
40 \item \& ends the picture (starting the next number)
41 \item 0 is replaced either by a digit or by the fill character
42 \item digits in the range 1\dots9 indicate an edition in base $n+1$, \ie
43 9 for decimal representation, 7 for octal, etc\dots
44 \item X for an hexadecimal edition, performed in uppercase
45 \item $+$ is replaced by the sign, with repetition for floating position.
46 \item $-$ is replaced by a minus sign if the number is negative,
47 or is equivalent to 0 in case of a positive number;
48 the repetition indicates a floating position for the sign.
49 \end{itemize}
50 If neither $+$ nor $-$ character is specified in the picture,
51 the value to be edited is assumed to be {\em unsigned}.
52 Some illustrations:
53 \begin{itemize}
54 \item {\tt "+99 59 59"} (edition of a declination expressed in arc seconds)
55 applied to 86400 results in \fbox{{\tt +24 00 00}}
56 \item {\tt "+++999.9"} applied on the number 256 results in
57 \fbox{{\tt\ \ +025.6"}}
58 \item {\tt "-9.9"} applied on 123 results in {\tt "12.3"}, and
59 applied on -1 results in \fbox{{\tt -0.1}}
60 \item {\tt "77 77 77 77 77 77"} applied on the number -1 results in
61 \fbox{{\tt 03 77 77 77 77 77}}, since the number -1 is in fact $2^{32}-1$.
62 \item {\tt "-XXXX XXXX"} applied on the number -12 results in
63 \fbox{{\tt -0000 000C}}
64 \end{itemize}
65 \end{TeX}
66 To copy the ``picture'' characters to the output, use the \ espace char,
67 i.e. '\+' for +, '\-' for --, etc, and \\ to issue the backslash.
68
69 .VERSION 1.0 18-Apr-1986: Creation
70 .VERSION 1.1 23-May-1986: Added hexa possibility
71 .VERSION 1.2 03-Mar-1987: Cosmetic modifications.
72 .VERSION 2.0 25-Jan-1988: Integrated for Midas
73 .VERSION 2.1 12-Jun-1989: Sign after digit or dots is taken as `copy char'
74
75 090831 last modif
76 ----------------------------------------------------------------------------*/
77
78 #include <osdefos.h> /* Required for shareable images... */
79 #include <atype.h> /* Required for isdigit */
80
81 #define ABSOLUTE(x) ((x) < 0 ? (-x) : (x)) /* Absolute*/
82 #define EOS '\0' /* End Of String */
83
84 #define _ERR_CHAR_ '~' /* Character used if edition error occurs */
85
86 MID_STATIC unsigned int base = 10; /* Base for the edition of digits */
87 MID_STATIC unsigned int sign = 0; /* Sign of the number from 0 to 2,
88 used as an index in signs[] */
89 MID_STATIC int pic_len; /* Where picture stops, used by ed_mpic */
90
91 MID_STATIC unsigned char signs[] = { ' ', '-', '+'};
92 MID_STATIC char fill_char = ' ';
93 MID_RSTATIC char xdigit[] = "0123456789ABCDEF";
94
95 /*===========================================================================
96 * ed_pic
97 *===========================================================================*/
98 /*++++++
99 .PURPOSE Edit an integer number according to a picture.
100 .RETURNS The length of the edited number.
101 .REMARKS Digits are replaced with `~' if ``value'' cannot be edited
102 (number too large). The buffer is terminated with the EOS character.
103 ----------*/
104 int ed_pic(dest, pic, value)
105 char *dest; /* OUT: pointer to destination buffer */
106 char *pic; /* IN: EOS-terminated picture string */
107 long int value; /* IN: The integer number to edit */
108 {
109 unsigned long int n;
110 char *a, *p, *ps; /* ps indicates sign */
111 unsigned int op;
112 int length;
113
114 /* Initialisation */
115 sign = 0 ; base = 10; signs[0] = fill_char; op = 0;
116 ps = (char *)0;
117
118 /* Look for sign */
119 for (p=pic; *p; p++)
120 { if (isdigit(*p)) ;
121 else if (*p == '.') ;
122 else if (*p == '+') { if(!ps) sign = 2; continue; }
123 else if (*p == '-') { if(!ps) sign = 1; continue; }
124 else if (*p == 'X') ;
125 else if (*p == '\\') { op++; p++; continue; }
126 else if (*p == '&') break;
127 else continue;
128 if (!ps) ps = p;
129 }
130 if(!ps) ps = p;
131 pic_len = (p--) - pic;
132
133 length = pic_len - op;
134 a = dest + length; /* Last char of destination string */
135 *(a--) = EOS;
136
137 if (sign) /* Look for signed or unsigned number */
138 { n = ABSOLUTE(value);
139 sign -= (sign-1) ^ (value>=0); /* Leads to 0 or 1 if '-' asked,
140 1 or 2 if '+' asked */
141 }
142 else n = (unsigned int) value;
143
144 /* Edition of digits, from right to left */
145 for (; p>=pic; p--, a--)
146 { op = 3; /* Default operation */
147 if ((p>pic) && (*(p-1) == '\\')) { *a = *(p--); continue;}
148 switch(*p)
149 { case '0' : op = (n ? 1 : 0 ); break;
150 case '-' : case '+': if(p<ps) op = (n ? 1 : 2); break;
151 case 'X' : base = 16; op = 1; break;
152 default : if ((*p > '9')||(*p < '0')) break;
153 base = *p - '0' + 1; op = 1;
154 }
155 switch(op)
156 { case 0: *a = fill_char; break; /* Insert fill char */
157 case 1: *a = xdigit[n%base]; /* Insert a digit */
158 n /= base; break;
159 case 2: *a = signs[sign]; /* Insert the sign */
160 sign = 0; break;
161 case 3: *a = *p; /* Copy from picture */
162 }
163 }
164
165 if (n | sign) /* Number not completely edited */
166 { for (p=pic, a=dest; *p; p++, a++)
167 { switch(*p)
168 { case '\\': p++; continue;
169 default : if ((*p > '9')||(*p < '0')) continue;
170 case '+' : case '-' : case 'X': *a = _ERR_CHAR_;
171 }
172 }
173 }
174
175 return(length);
176 }
177
178 /*===========================================================================
179 * ed_pic4
180 *=========================================================================*/
181
182 /*++++++
183 .PURPOSE Edit an integer according to a picture, with specified fill character.
184 .RETURNS The length of the edited string.
185 .REMARKS The string is filled with `~' if ``value'' cannot be edited
186 (number too large);
187 .REMARKS The destination string if terminated with an EOS character.
188 ------------*/
189 int ed_pic4(dest, pic, value, fill)
190 char *dest; /* OUT: pointer to destination string */
191 char *pic; /* IN: EOS-terminated picture string */
192 long int value; /* IN: The integer number to edit */
193 char fill; /* IN: The fill character */
194 {
195 register int ret;
196
197 fill_char = fill;
198 ret = ed_pic(dest, pic, value);
199 fill_char = ' ';
200 return(ret);
201 }
202