Movement of common classes and code to common.a.
[external/ragel.git] / common / common.cpp
1 /*
2  *  Copyright 2006-2007 Adrian Thurston <thurston@cs.queensu.ca>
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 <assert.h>
25
26 HostType hostTypesC[] =
27 {
28         { "char",     0,       true,   CHAR_MIN,  CHAR_MAX,   sizeof(char) },
29         { "unsigned", "char",  false,  0,         UCHAR_MAX,  sizeof(unsigned char) },
30         { "short",    0,       true,   SHRT_MIN,  SHRT_MAX,   sizeof(short) },
31         { "unsigned", "short", false,  0,         USHRT_MAX,  sizeof(unsigned short) },
32         { "int",      0,       true,   INT_MIN,   INT_MAX,    sizeof(int) },
33         { "unsigned", "int",   false,  0,         UINT_MAX,   sizeof(unsigned int) },
34         { "long",     0,       true,   LONG_MIN,  LONG_MAX,   sizeof(long) },
35         { "unsigned", "long",  false,  0,         ULONG_MAX,  sizeof(unsigned long) }
36 };
37
38 HostType hostTypesD[] =
39 {
40         { "byte",     0,  true,   CHAR_MIN,  CHAR_MAX,    1 },
41         { "ubyte",    0,  false,  0,         UCHAR_MAX,   1 },
42         { "char",     0,  false,  0,         UCHAR_MAX,   1 },
43         { "short",    0,  true,   SHRT_MIN,  SHRT_MAX,    2 },
44         { "ushort",   0,  false,  0,         USHRT_MAX,   2 },
45         { "wchar",    0,  false,  0,         USHRT_MAX,   2 },
46         { "int",      0,  true,   INT_MIN,   INT_MAX,     4 },
47         { "uint",     0,  false,  0,         UINT_MAX,    4 },
48         { "dchar",    0,  false,  0,         UINT_MAX,    4 }
49 };
50
51 HostType hostTypesJava[] = 
52 {
53         { "byte",     0,  true,   CHAR_MIN,  CHAR_MAX,    1 },
54         { "short",    0,  true,   SHRT_MIN,  SHRT_MAX,    2 },
55         { "char",     0,  false,  0,         USHRT_MAX,   2 },
56         { "int",      0,  true,   INT_MIN,   INT_MAX,     4 },
57 };
58
59 HostType hostTypesRuby[] = 
60 {
61         { "byte",     0,  true,   CHAR_MIN,  CHAR_MAX,    1 },
62         { "short",    0,  true,   SHRT_MIN,  SHRT_MAX,    2 },
63         { "char",     0,  false,  0,         USHRT_MAX,   2 },
64         { "int",      0,  true,   INT_MIN,   INT_MAX,     4 },
65 };
66
67 HostLang hostLangC =    { hostTypesC,    8, hostTypesC+0,    true };
68 HostLang hostLangD =    { hostTypesD,    9, hostTypesD+2,    true };
69 HostLang hostLangJava = { hostTypesJava, 4, hostTypesJava+2, false };
70 HostLang hostLangRuby = { hostTypesRuby, 4, hostTypesRuby+2, false };
71
72 HostLang *hostLang = &hostLangC;
73 HostLangType hostLangType = CCode;
74
75 /* Construct a new parameter checker with for paramSpec. */
76 ParamCheck::ParamCheck(char *paramSpec, int argc, char **argv)
77 :
78         state(noparam),
79         argOffset(0),
80         curArg(0),
81         iCurArg(1),
82         paramSpec(paramSpec), 
83         argc(argc), 
84         argv(argv)
85 {
86 }
87
88 /* Check a single option. Returns the index of the next parameter.  Sets p to
89  * the arg character if valid, 0 otherwise.  Sets parg to the parameter arg if
90  * there is one, NULL otherwise. */
91 bool ParamCheck::check()
92 {
93         bool requiresParam;
94
95         if ( iCurArg >= argc ) {            /* Off the end of the arg list. */
96                 state = noparam;
97                 return false;
98         }
99
100         if ( argOffset != 0 && *argOffset == 0 ) {
101                 /* We are at the end of an arg string. */
102                 iCurArg += 1;
103                 if ( iCurArg >= argc ) {
104                         state = noparam;
105                         return false;
106                 }
107                 argOffset = 0;
108         }
109
110         if ( argOffset == 0 ) {
111                 /* Set the current arg. */
112                 curArg = argv[iCurArg];
113
114                 /* We are at the beginning of an arg string. */
115                 if ( argv[iCurArg] == 0 ||        /* Argv[iCurArg] is null. */
116                          argv[iCurArg][0] != '-' ||   /* Not a param. */
117                          argv[iCurArg][1] == 0 ) {    /* Only a dash. */
118                         parameter = 0;
119                         parameterArg = 0;
120
121                         iCurArg += 1;
122                         state = noparam;
123                         return true;
124                 }
125                 argOffset = argv[iCurArg] + 1;
126         }
127
128         /* Get the arg char. */
129         char argChar = *argOffset;
130         
131         /* Loop over all the parms and look for a match. */
132         char *pSpec = paramSpec;
133         while ( *pSpec != 0 ) {
134                 char pSpecChar = *pSpec;
135
136                 /* If there is a ':' following the char then
137                  * it requires a parm.  If a parm is required
138                  * then move ahead two in the parmspec. Otherwise
139                  * move ahead one in the parm spec. */
140                 if ( pSpec[1] == ':' ) {
141                         requiresParam = true;
142                         pSpec += 2;
143                 }
144                 else {
145                         requiresParam = false;
146                         pSpec += 1;
147                 }
148
149                 /* Do we have a match. */
150                 if ( argChar == pSpecChar ) {
151                         if ( requiresParam ) {
152                                 if ( argOffset[1] == 0 ) {
153                                         /* The param must follow. */
154                                         if ( iCurArg + 1 == argc ) {
155                                                 /* We are the last arg so there
156                                                  * cannot be a parameter to it. */
157                                                 parameter = argChar;
158                                                 parameterArg = 0;
159                                                 iCurArg += 1;
160                                                 argOffset = 0;
161                                                 state = invalid;
162                                                 return true;
163                                         }
164                                         else {
165                                                 /* the parameter to the arg is the next arg. */
166                                                 parameter = pSpecChar;
167                                                 parameterArg = argv[iCurArg + 1];
168                                                 iCurArg += 2;
169                                                 argOffset = 0;
170                                                 state = match;
171                                                 return true;
172                                         }
173                                 }
174                                 else {
175                                         /* The param for the arg is built in. */
176                                         parameter = pSpecChar;
177                                         parameterArg = argOffset + 1;
178                                         iCurArg += 1;
179                                         argOffset = 0;
180                                         state = match;
181                                         return true;
182                                 }
183                         }
184                         else {
185                                 /* Good, we matched the parm and no
186                                  * arg is required. */
187                                 parameter = pSpecChar;
188                                 parameterArg = 0;
189                                 argOffset += 1;
190                                 state = match;
191                                 return true;
192                         }
193                 }
194         }
195
196         /* We did not find a match. Bad Argument. */
197         parameter = argChar;
198         parameterArg = 0;
199         argOffset += 1;
200         state = invalid;
201         return true;
202 }
203
204 /* Counts newlines before sending sync. */
205 int output_filter::sync( )
206 {
207         line += 1;
208         return std::filebuf::sync();
209 }
210
211 /* Counts newlines before sending data out to file. */
212 std::streamsize output_filter::xsputn( const char *s, std::streamsize n )
213 {
214         for ( int i = 0; i < n; i++ ) {
215                 if ( s[i] == '\n' )
216                         line += 1;
217         }
218         return std::filebuf::xsputn( s, n );
219 }
220
221 /* Scans a string looking for the file extension. If there is a file
222  * extension then pointer returned points to inside the string
223  * passed in. Otherwise returns null. */
224 char *findFileExtension( char *stemFile )
225 {
226         char *ppos = stemFile + strlen(stemFile) - 1;
227
228         /* Scan backwards from the end looking for the first dot.
229          * If we encounter a '/' before the first dot, then stop the scan. */
230         while ( 1 ) {
231                 /* If we found a dot or got to the beginning of the string then
232                  * we are done. */
233                 if ( ppos == stemFile || *ppos == '.' )
234                         break;
235
236                 /* If we hit a / then there is no extension. Done. */
237                 if ( *ppos == '/' ) {
238                         ppos = stemFile;
239                         break;
240                 }
241                 ppos--;
242         } 
243
244         /* If we got to the front of the string then bail we 
245          * did not find an extension  */
246         if ( ppos == stemFile )
247                 ppos = 0;
248
249         return ppos;
250 }
251
252 /* Make a file name from a stem. Removes the old filename suffix and
253  * replaces it with a new one. Returns a newed up string. */
254 char *fileNameFromStem( char *stemFile, char *suffix )
255 {
256         int len = strlen( stemFile );
257         assert( len > 0 );
258
259         /* Get the extension. */
260         char *ppos = findFileExtension( stemFile );
261
262         /* If an extension was found, then shorten what we think the len is. */
263         if ( ppos != 0 )
264                 len = ppos - stemFile;
265
266         /* Make the return string from the stem and the suffix. */
267         char *retVal = new char[ len + strlen( suffix ) + 1 ];
268         strncpy( retVal, stemFile, len );
269         strcpy( retVal + len, suffix );
270
271         return retVal;
272 }
273
274