"Fossies" - the Fresh Open Source Software Archive 
Member "ragel-6.10/ragel/common.cpp" (24 Mar 2017, 12796 Bytes) of package /linux/misc/ragel-6.10.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "common.cpp" see the
Fossies "Dox" file reference documentation and the latest
Fossies "Diffs" side-by-side code changes report:
6.9_vs_6.10.
1 /*
2 * Copyright 2006-2007 Adrian Thurston <thurston@complang.org>
3 */
4
5 /* This file is part of Ragel.
6 *
7 * Ragel is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * Ragel is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with Ragel; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22 #include "pcheck.h"
23 #include "common.h"
24 #include "stdlib.h"
25 #include <string.h>
26 #include <assert.h>
27
28 HostType hostTypesC[] =
29 {
30 { "char", 0, "char", true, true, false, CHAR_MIN, CHAR_MAX, 0, 0, sizeof(char) },
31 { "unsigned", "char", "uchar", false, true, false, 0, 0, 0, UCHAR_MAX, sizeof(unsigned char) },
32 { "short", 0, "short", true, true, false, SHRT_MIN, SHRT_MAX, 0, 0, sizeof(short) },
33 { "unsigned", "short", "ushort", false, true, false, 0, 0, 0, USHRT_MAX, sizeof(unsigned short) },
34 { "int", 0, "int", true, true, false, INT_MIN, INT_MAX, 0, 0, sizeof(int) },
35 { "unsigned", "int", "uint", false, true, false, 0, 0, 0, UINT_MAX, sizeof(unsigned int) },
36 { "long", 0, "long", true, true, false, LONG_MIN, LONG_MAX, 0, 0, sizeof(long) },
37 { "unsigned", "long", "ulong", false, true, false, 0, 0, 0, ULONG_MAX, sizeof(unsigned long) }
38 };
39
40 #define S8BIT_MIN -128
41 #define S8BIT_MAX 127
42
43 #define U8BIT_MIN 0
44 #define U8BIT_MAX 255
45
46 #define S16BIT_MIN -32768
47 #define S16BIT_MAX 32767
48
49 #define U16BIT_MIN 0
50 #define U16BIT_MAX 65535
51
52 #define S31BIT_MIN -1073741824L
53 #define S31BIT_MAX 1073741823L
54
55 #define S32BIT_MIN -2147483648L
56 #define S32BIT_MAX 2147483647L
57
58 #define U32BIT_MIN 0
59 #define U32BIT_MAX 4294967295UL
60
61 #define S64BIT_MIN -9223372036854775807LL
62 #define S64BIT_MAX 9223372036854775807LL
63
64 #define U64BIT_MIN 0
65 #define U64BIT_MAX 18446744073709551615ULL
66
67 HostType hostTypesD[] =
68 {
69 { "byte", 0, "byte", true, true, false, CHAR_MIN, CHAR_MAX, 0, 0, 1 },
70 { "ubyte", 0, "ubyte", false, true, false, 0, 0, 0, UCHAR_MAX, 1 },
71 { "char", 0, "char", false, true, false, 0, 0, 0, UCHAR_MAX, 1 },
72 { "short", 0, "short", true, true, false, SHRT_MIN, SHRT_MAX, 0, 0, 2 },
73 { "ushort", 0, "ushort", false, true, false, 0, 0, 0, USHRT_MAX, 2 },
74 { "wchar", 0, "wchar", false, true, false, 0, 0, 0, USHRT_MAX, 2 },
75 { "int", 0, "int", true, true, false, INT_MIN, INT_MAX, 0, 0, 4 },
76 { "uint", 0, "uint", false, true, false, 0, 0, 0, UINT_MAX, 4 },
77 { "dchar", 0, "dchar", false, true, false, 0, 0, 0, UINT_MAX, 4 }
78 };
79
80 HostType hostTypesGo[] =
81 {
82 { "byte", 0, "uint8", false, true, false, 0, 0, U8BIT_MIN, U8BIT_MAX, 1 },
83 { "int8", 0, "int8", true, true, false, S8BIT_MIN, S8BIT_MAX, 0, 0, 1 },
84 { "uint8", 0, "uint8", false, true, false, 0, 0, U8BIT_MIN, U8BIT_MAX, 1 },
85 { "int16", 0, "int16", true, true, false, S16BIT_MIN, S16BIT_MAX, 0, 0, 2 },
86 { "uint16", 0, "uint16", false, true, false, 0, 0, U16BIT_MIN, U16BIT_MAX, 2 },
87 { "int32", 0, "int32", true, true, false, S32BIT_MIN, S32BIT_MAX, 0, 0, 4 },
88 { "uint32", 0, "uint32", false, true, false, 0, 0, U32BIT_MIN, U32BIT_MAX, 4 },
89 { "int64", 0, "int64", true, true, false, S64BIT_MIN, S64BIT_MAX, 0, 0, 8 },
90 { "uint64", 0, "uint64", false, true, false, 0, 0, U64BIT_MIN, U64BIT_MAX, 8 },
91 { "rune", 0, "int32", true, true, true, S32BIT_MIN, S32BIT_MAX, 0, 0, 4 }
92 };
93
94 HostType hostTypesJava[] =
95 {
96 { "byte", 0, "byte", true, true, false, CHAR_MIN, CHAR_MAX, 0, 0, 1 },
97 { "short", 0, "short", true, true, false, SHRT_MIN, SHRT_MAX, 0, 0, 2 },
98 { "char", 0, "char", false, true, false, 0, 0, 0, USHRT_MAX, 2 },
99 { "int", 0, "int", true, true, false, INT_MIN, INT_MAX, 0, 0, 4 },
100 };
101
102 /* What are the appropriate types for ruby? */
103 HostType hostTypesRuby[] =
104 {
105 { "char", 0, "char", true, true, false, CHAR_MIN, CHAR_MAX, 0, 0, 1 },
106 { "int", 0, "int", true, true, false, INT_MIN, INT_MAX, 0, 0, 4 },
107 };
108
109 HostType hostTypesCSharp[] =
110 {
111 { "sbyte", 0, "sbyte", true, true, false, CHAR_MIN, CHAR_MAX, 0, 0, 1 },
112 { "byte", 0, "byte", false, true, false, 0, 0, 0, UCHAR_MAX, 1 },
113 { "short", 0, "short", true, true, false, SHRT_MIN, SHRT_MAX, 0, 0, 2 },
114 { "ushort", 0, "ushort", false, true, false, 0, 0, 0, USHRT_MAX, 2 },
115 { "char", 0, "char", false, true, true, 0, 0, 0, USHRT_MAX, 2 },
116 { "int", 0, "int", true, true, false, INT_MIN, INT_MAX, 0, 0, 4 },
117 { "uint", 0, "uint", false, true, false, 0, 0, 0, UINT_MAX, 4 },
118 { "long", 0, "long", true, true, false, LONG_MIN, LONG_MAX, 0, 0, 8 },
119 { "ulong", 0, "ulong", false, true, false, 0, 0, 0, ULONG_MAX, 8 }
120 };
121
122 HostType hostTypesOCaml[] =
123 {
124 { "int", 0, "int", true, true, false, S31BIT_MIN, S31BIT_MAX, 0, 0, 4 },
125 };
126
127 HostLang hostLangC = { HostLang::C, hostTypesC, 8, hostTypesC+0, true };
128 HostLang hostLangD = { HostLang::D, hostTypesD, 9, hostTypesD+2, true };
129 HostLang hostLangD2 = { HostLang::D2, hostTypesD, 9, hostTypesD+2, true };
130 HostLang hostLangGo = { HostLang::Go, hostTypesGo, 10, hostTypesGo+0, false };
131 HostLang hostLangJava = { HostLang::Java, hostTypesJava, 4, hostTypesJava+2, false };
132 HostLang hostLangRuby = { HostLang::Ruby, hostTypesRuby, 2, hostTypesRuby+0, false };
133 HostLang hostLangCSharp = { HostLang::CSharp, hostTypesCSharp, 9, hostTypesCSharp+4, true };
134 HostLang hostLangOCaml = { HostLang::OCaml, hostTypesOCaml, 1, hostTypesOCaml+0, false };
135
136 HostLang *hostLang = &hostLangC;
137
138 HostType *findAlphType( const char *s1 )
139 {
140 for ( int i = 0; i < hostLang->numHostTypes; i++ ) {
141 if ( strcmp( s1, hostLang->hostTypes[i].data1 ) == 0 &&
142 hostLang->hostTypes[i].data2 == 0 )
143 {
144 return hostLang->hostTypes + i;
145 }
146 }
147
148 return 0;
149 }
150
151 HostType *findAlphType( const char *s1, const char *s2 )
152 {
153 for ( int i = 0; i < hostLang->numHostTypes; i++ ) {
154 if ( strcmp( s1, hostLang->hostTypes[i].data1 ) == 0 &&
155 hostLang->hostTypes[i].data2 != 0 &&
156 strcmp( s2, hostLang->hostTypes[i].data2 ) == 0 )
157 {
158 return hostLang->hostTypes + i;
159 }
160 }
161
162 return 0;
163 }
164
165 HostType *findAlphTypeInternal( const char *s1 )
166 {
167 for ( int i = 0; i < hostLang->numHostTypes; i++ ) {
168 if ( strcmp( s1, hostLang->hostTypes[i].internalName ) == 0 )
169 return hostLang->hostTypes + i;
170 }
171
172 return 0;
173 }
174
175 /* Construct a new parameter checker with for paramSpec. */
176 ParamCheck::ParamCheck( const char *paramSpec, int argc, const char **argv )
177 :
178 state(noparam),
179 argOffset(0),
180 curArg(0),
181 iCurArg(1),
182 paramSpec(paramSpec),
183 argc(argc),
184 argv(argv)
185 {
186 }
187
188 /* Check a single option. Returns the index of the next parameter. Sets p to
189 * the arg character if valid, 0 otherwise. Sets parg to the parameter arg if
190 * there is one, NULL otherwise. */
191 bool ParamCheck::check()
192 {
193 bool requiresParam;
194
195 if ( iCurArg >= argc ) { /* Off the end of the arg list. */
196 state = noparam;
197 return false;
198 }
199
200 if ( argOffset != 0 && *argOffset == 0 ) {
201 /* We are at the end of an arg string. */
202 iCurArg += 1;
203 if ( iCurArg >= argc ) {
204 state = noparam;
205 return false;
206 }
207 argOffset = 0;
208 }
209
210 if ( argOffset == 0 ) {
211 /* Set the current arg. */
212 curArg = argv[iCurArg];
213
214 /* We are at the beginning of an arg string. */
215 if ( argv[iCurArg] == 0 || /* Argv[iCurArg] is null. */
216 argv[iCurArg][0] != '-' || /* Not a param. */
217 argv[iCurArg][1] == 0 ) { /* Only a dash. */
218 parameter = 0;
219 paramArg = 0;
220
221 iCurArg += 1;
222 state = noparam;
223 return true;
224 }
225 argOffset = argv[iCurArg] + 1;
226 }
227
228 /* Get the arg char. */
229 char argChar = *argOffset;
230
231 /* Loop over all the parms and look for a match. */
232 const char *pSpec = paramSpec;
233 while ( *pSpec != 0 ) {
234 char pSpecChar = *pSpec;
235
236 /* If there is a ':' following the char then
237 * it requires a parm. If a parm is required
238 * then move ahead two in the parmspec. Otherwise
239 * move ahead one in the parm spec. */
240 if ( pSpec[1] == ':' ) {
241 requiresParam = true;
242 pSpec += 2;
243 }
244 else {
245 requiresParam = false;
246 pSpec += 1;
247 }
248
249 /* Do we have a match. */
250 if ( argChar == pSpecChar ) {
251 if ( requiresParam ) {
252 if ( argOffset[1] == 0 ) {
253 /* The param must follow. */
254 if ( iCurArg + 1 == argc ) {
255 /* We are the last arg so there
256 * cannot be a parameter to it. */
257 parameter = argChar;
258 paramArg = 0;
259 iCurArg += 1;
260 argOffset = 0;
261 state = invalid;
262 return true;
263 }
264 else {
265 /* the parameter to the arg is the next arg. */
266 parameter = pSpecChar;
267 paramArg = argv[iCurArg + 1];
268 iCurArg += 2;
269 argOffset = 0;
270 state = match;
271 return true;
272 }
273 }
274 else {
275 /* The param for the arg is built in. */
276 parameter = pSpecChar;
277 paramArg = argOffset + 1;
278 iCurArg += 1;
279 argOffset = 0;
280 state = match;
281 return true;
282 }
283 }
284 else {
285 /* Good, we matched the parm and no
286 * arg is required. */
287 parameter = pSpecChar;
288 paramArg = 0;
289 argOffset += 1;
290 state = match;
291 return true;
292 }
293 }
294 }
295
296 /* We did not find a match. Bad Argument. */
297 parameter = argChar;
298 paramArg = 0;
299 argOffset += 1;
300 state = invalid;
301 return true;
302 }
303
304 /* Counts newlines before sending sync. */
305 int output_filter::sync( )
306 {
307 line += 1;
308 return std::filebuf::sync();
309 }
310
311 /* Counts newlines before sending data out to file. */
312 std::streamsize output_filter::xsputn( const char *s, std::streamsize n )
313 {
314 for ( int i = 0; i < n; i++ ) {
315 if ( s[i] == '\n' )
316 line += 1;
317 }
318 return std::filebuf::xsputn( s, n );
319 }
320
321 /* Scans a string looking for the file extension. If there is a file
322 * extension then pointer returned points to inside the string
323 * passed in. Otherwise returns null. */
324 const char *findFileExtension( const char *stemFile )
325 {
326 const char *ppos = stemFile + strlen(stemFile) - 1;
327
328 /* Scan backwards from the end looking for the first dot.
329 * If we encounter a '/' before the first dot, then stop the scan. */
330 while ( 1 ) {
331 /* If we found a dot or got to the beginning of the string then
332 * we are done. */
333 if ( ppos == stemFile || *ppos == '.' )
334 break;
335
336 /* If we hit a / then there is no extension. Done. */
337 if ( *ppos == '/' ) {
338 ppos = stemFile;
339 break;
340 }
341 ppos--;
342 }
343
344 /* If we got to the front of the string then bail we
345 * did not find an extension */
346 if ( ppos == stemFile )
347 ppos = 0;
348
349 return ppos;
350 }
351
352 /* Make a file name from a stem. Removes the old filename suffix and
353 * replaces it with a new one. Returns a newed up string. */
354 const char *fileNameFromStem( const char *stemFile, const char *suffix )
355 {
356 long len = strlen( stemFile );
357 assert( len > 0 );
358
359 /* Get the extension. */
360 const char *ppos = findFileExtension( stemFile );
361
362 /* If an extension was found, then shorten what we think the len is. */
363 if ( ppos != 0 )
364 len = ppos - stemFile;
365
366 /* Make the return string from the stem and the suffix. */
367 char *retVal = new char[ len + strlen( suffix ) + 1 ];
368 strncpy( retVal, stemFile, len );
369 strcpy( retVal + len, suffix );
370
371 return retVal;
372 }
373
374 exit_object endp;
375
376 void operator<<( std::ostream &out, exit_object & )
377 {
378 out << std::endl;
379 exit(1);
380 }