Debian Codegraph @

prim/tc3/libsrc/str2.c

raw
1 /*===========================================================================
2 Copyright (C) 1990-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 str2.c
30 .AUTHOR Francois Ochsenbein [ESO]
31 .LANGUAGE C
32 .CATEGORY Basic String Tokenisation
33
34 .COMMENTS
35 \begin{TeX}
36 This module computes the length of basic tokens;
37 tokens recognized here are:
38 \begin{itemize}
39 \item {\bf stkid}: identifiers (starting {\em alpha},
40 then {\em alpha}numeric, where
41 {\em alpha} includes also {\bf\$} and {\bf\_}).
42 \item {\bf stkint}: integers written as (signed)digits, or {\tt 0x...} in hexadecimal,
43 or {\tt 0o...} for octal.
44 \item {\bf stknum}: numbers that can match floating-point numbers (no hexadecimal facility)
45 \item {\bf stkstr}: quoted strings (the \b is used to escape the quote)
46 \item {\bf stkcomment}: comments between / * and * /
47 \end{itemize}
48 All routines return a length which represents the matching length.
49 \end{TeX}
50
51 .VERSION 1.0 30-Jan-1990: Creation
52 .VERSION 1.1 22-Mar-1990: Renamed as stk (string token)
53
54 090831 last modif
55 ----------------------------------------------------------------------------*/
56
57 #include <osdefos.h>
58 #include <macrogen.h>
59 #include <atype.h>
60
61 #define issign(c) ((c == '+') || (c == '-'))
62
63 /*===========================================================================
64 * Identifier (starting with alphabetic)
65 *===========================================================================*/
66
67 int stkid(s)
68 /*+++++++
69 .PURPOSE Reads an `identifier' (starting alphabetic, then alphanumeric)
70 .RETURNS Length of valid token (0 if none)
71 .REMARKS
72 ---------*/
73 char *s; /* IN: string to scan */
74 {
75 char *p;
76
77 p = s;
78 if (isid1(*p))
79 { p++;
80 while (isid(*p)) p++;
81 }
82
83 return(p - s);
84 }
85
86 /*===========================================================================
87 * Integer (numeric as digits)
88 *===========================================================================*/
89
90 int stkint(s)
91 /*+++++++
92 .PURPOSE Tokenize an `integer'; possibilities 0x (hexa), 0O (octal).
93 .RETURNS Length of token
94 .REMARKS s starts with sign or digit
95 ---------*/
96 char *s; /* IN: string to scan */
97 {
98 char *p, base;
99
100 p = s, base = 0;
101
102 if (*p == '0') /* Look for Octal or Hexa */
103 p++, base = toupper(*p);
104 else if (issign(*p)) p++;
105 else if (isdigit(*p)) ;
106 else goto FIN;
107
108 if (base) /* There is a special base character */
109 { if (base == 'X') base = _XDIGIT_;
110 else if (base == 'O') base = _DIGIT_;
111 else goto FIN; /* Stopped by an alphabetic */
112 p++; /* Skip the base letter. */
113 }
114 else base = _DIGIT_;
115
116 /* Now span over the digits */
117
118 while(ischar(*p, base)) p++;
119
120 FIN: return(p-s);
121 }
122
123 /*===========================================================================
124 * Numeric (floating-point)
125 *===========================================================================*/
126
127 int stknum(s)
128 /*++++++++++++++++++
129 .PURPOSE Match Number, written as a floating-point number (exponent expressed
130 as e, E, d or D)
131 .RETURNS Length of token
132 .REMARKS s starts with sign or . or digit
133 ---------------------*/
134 char *s; /* IN: String to scan */
135 {
136 char *p, x;
137
138 p = s;
139
140 if (issign(*p)) p++;
141 while (isdigit(*p)) p++;
142 if (*p == '.') for (++p; isdigit(*p); p++) ;
143
144 x = toupper(*p);
145 if ( (x == 'E') || (x == 'D')) /* Look for Exponent */
146 { p++;
147 if (issign(*p)) p++;
148 while (isdigit(*p)) p++;
149 }
150 return(p-s);
151 }
152
153 /*===========================================================================
154 * Quoted String (quote escaped with \)
155 *===========================================================================*/
156
157 int stkstr(s)
158 /*++++++++++++++++++
159 .PURPOSE Match a quoted string
160 .RETURNS Length of quoted string, INCLUDING terminators
161 .REMARKS s starts with ' or " or `. The escape character is \ .
162 ---------------------*/
163 char *s; /* IN: String to scan */
164 {
165 char *p, quote;
166
167
168
169 p = (char *) 0;
170
171 quote = *s;
172
173 if ((quote != '"') && (quote != '\'') && (quote != '`'))
174 goto FIN;
175
176 for (p = s+1; *p; p++)
177 { /* Find End Of String. Take care of quote escaped with \ */
178 if (*p == '\\') { p++; continue; } /* Escaped char */
179 if (*p == quote){ p++; break; } /* Quote found. */
180 }
181
182 FIN: return(p-s);
183 }
184
185 /*===========================================================================
186 * Comment (between / * and * /)
187 *===========================================================================*/
188
189 int stkcomment(s)
190 /*+++++++
191 .PURPOSE Extracts the comment from a string
192 .RETURNS Length of comment, including limits / * * /
193 .REMARKS String assumed to start with / *
194 ---------*/
195 char *s; /* IN: string to scan */
196 {
197 char *p;
198
199 p = s;
200 if (*p != '/') goto FIN;
201 if (*++p != '*') goto FIN;
202
203 for (++p; *p; p++)
204 { if (*p != '*') continue;
205 if (*(p+1) == '/') { p += 2; break; }
206 /* Char following the * must be a / ... */
207 }
208
209 FIN: return(p-s);
210 }
211