C# patch Daniel Tang.
[external/ragel.git] / common / common.cpp
index 60c49ff..ba5b948 100644 (file)
 
 #include "pcheck.h"
 #include "common.h"
+#include "stdlib.h"
+#include <string.h>
+#include <assert.h>
 
 HostType hostTypesC[] =
 {
-       { "char",     0,       true,   CHAR_MIN,  CHAR_MAX,   sizeof(char) },
-       { "unsigned", "char",  false,  0,         UCHAR_MAX,  sizeof(unsigned char) },
-       { "short",    0,       true,   SHRT_MIN,  SHRT_MAX,   sizeof(short) },
-       { "unsigned", "short", false,  0,         USHRT_MAX,  sizeof(unsigned short) },
-       { "int",      0,       true,   INT_MIN,   INT_MAX,    sizeof(int) },
-       { "unsigned", "int",   false,  0,         UINT_MAX,   sizeof(unsigned int) },
-       { "long",     0,       true,   LONG_MIN,  LONG_MAX,   sizeof(long) },
-       { "unsigned", "long",  false,  0,         ULONG_MAX,  sizeof(unsigned long) }
+       { "char",     0,       "char",    true,   CHAR_MIN,  CHAR_MAX,   sizeof(char) },
+       { "unsigned", "char",  "uchar",   false,  0,         UCHAR_MAX,  sizeof(unsigned char) },
+       { "short",    0,       "short",   true,   SHRT_MIN,  SHRT_MAX,   sizeof(short) },
+       { "unsigned", "short", "ushort",  false,  0,         USHRT_MAX,  sizeof(unsigned short) },
+       { "int",      0,       "int",     true,   INT_MIN,   INT_MAX,    sizeof(int) },
+       { "unsigned", "int",   "uint",    false,  0,         UINT_MAX,   sizeof(unsigned int) },
+       { "long",     0,       "long",    true,   LONG_MIN,  LONG_MAX,   sizeof(long) },
+       { "unsigned", "long",  "ulong",   false,  0,         ULONG_MAX,  sizeof(unsigned long) }
 };
 
 HostType hostTypesD[] =
 {
-       { "byte",     0,  true,   CHAR_MIN,  CHAR_MAX,    1 },
-       { "ubyte",    0,  false,  0,         UCHAR_MAX,   1 },
-       { "char",     0,  false,  0,         UCHAR_MAX,   1 },
-       { "short",    0,  true,   SHRT_MIN,  SHRT_MAX,    2 },
-       { "ushort",   0,  false,  0,         USHRT_MAX,   2 },
-       { "wchar",    0,  false,  0,         USHRT_MAX,   2 },
-       { "int",      0,  true,   INT_MIN,   INT_MAX,     4 },
-       { "uint",     0,  false,  0,         UINT_MAX,    4 },
-       { "dchar",    0,  false,  0,         UINT_MAX,    4 }
+       { "byte",    0,  "byte",    true,   CHAR_MIN,  CHAR_MAX,    1 },
+       { "ubyte",   0,  "ubyte",   false,  0,         UCHAR_MAX,   1 },
+       { "char",    0,  "char",    false,  0,         UCHAR_MAX,   1 },
+       { "short",   0,  "short",   true,   SHRT_MIN,  SHRT_MAX,    2 },
+       { "ushort",  0,  "ushort",  false,  0,         USHRT_MAX,   2 },
+       { "wchar",   0,  "wchar",   false,  0,         USHRT_MAX,   2 },
+       { "int",     0,  "int",     true,   INT_MIN,   INT_MAX,     4 },
+       { "uint",    0,  "uint",    false,  0,         UINT_MAX,    4 },
+       { "dchar",   0,  "dchar",   false,  0,         UINT_MAX,    4 }
 };
 
 HostType hostTypesJava[] = 
 {
-       { "byte",     0,  true,   CHAR_MIN,  CHAR_MAX,    1 },
-       { "short",    0,  true,   SHRT_MIN,  SHRT_MAX,    2 },
-       { "char",     0,  false,  0,         USHRT_MAX,   2 },
-       { "int",      0,  true,   INT_MIN,   INT_MAX,     4 },
+       { "byte",    0,  "byte",   true,   CHAR_MIN,  CHAR_MAX,    1 },
+       { "short",   0,  "short",  true,   SHRT_MIN,  SHRT_MAX,    2 },
+       { "char",    0,  "char",   false,  0,         USHRT_MAX,   2 },
+       { "int",     0,  "int",    true,   INT_MIN,   INT_MAX,     4 },
 };
 
-HostLang hostLangC =    { hostTypesC,    8, hostTypesC+0,    true };
-HostLang hostLangD =    { hostTypesD,    9, hostTypesD+2,    true };
-HostLang hostLangJava = { hostTypesJava, 4, hostTypesJava+2, false };
+/* What are the appropriate types for ruby? */
+HostType hostTypesRuby[] = 
+{
+       { "char",    0,  "char",   true,   CHAR_MIN,  CHAR_MAX,    1 },
+       { "int",     0,  "int",    true,   INT_MIN,   INT_MAX,     4 },
+};
+
+HostType hostTypesCSharp[] =
+{
+       { "sbyte",    0, "sbyte",   true,   CHAR_MIN,  CHAR_MAX,    1 },
+       { "byte",   0,   "byte",    false,  0,         UCHAR_MAX,   1 },
+       { "short",   0,  "short",   true,   SHRT_MIN,  SHRT_MAX,    2 },
+       { "ushort",  0,  "ushort",  false,  0,         USHRT_MAX,   2 },
+       { "char",    0,  "char",    false,  0,         USHRT_MAX,   2 },
+       { "int",     0,  "int",     true,   INT_MIN,   INT_MAX,     4 },
+       { "uint",    0,  "uint",    false,  0,         UINT_MAX,    4 },
+       { "long",    0,  "long",    true,   LONG_MIN,  LONG_MAX,    8 },
+       { "ulong",   0,  "ulong",   false,  0,         ULONG_MAX,   8 }
+};
+
+HostLang hostLangC =    { HostLang::C,    hostTypesC,    8, hostTypesC+0,    true };
+HostLang hostLangD =    { HostLang::D,    hostTypesD,    9, hostTypesD+2,    true };
+HostLang hostLangJava = { HostLang::Java, hostTypesJava, 4, hostTypesJava+2, false };
+HostLang hostLangRuby = { HostLang::Ruby, hostTypesRuby, 2, hostTypesRuby+0, false };
+HostLang hostLangCSharp = { HostLang::CSharp, hostTypesCSharp, 9, hostTypesCSharp+4, true };
 
 HostLang *hostLang = &hostLangC;
-HostLangType hostLangType = CCode;
+
+HostType *findAlphType( char *s1 )
+{
+       for ( int i = 0; i < hostLang->numHostTypes; i++ ) {
+               if ( strcmp( s1, hostLang->hostTypes[i].data1 ) == 0 && 
+                               hostLang->hostTypes[i].data2 == 0 )
+               {
+                       return hostLang->hostTypes + i;
+               }
+       }
+
+       return 0;
+}
+
+HostType *findAlphType( char *s1, char *s2 )
+{
+       for ( int i = 0; i < hostLang->numHostTypes; i++ ) {
+               if ( strcmp( s1, hostLang->hostTypes[i].data1 ) == 0 && 
+                               hostLang->hostTypes[i].data2 != 0 && 
+                               strcmp( s2, hostLang->hostTypes[i].data2 ) == 0 )
+               {
+                       return hostLang->hostTypes + i;
+               }
+       }
+
+       return 0;
+}
+
+HostType *findAlphTypeInternal( char *s1 )
+{
+       for ( int i = 0; i < hostLang->numHostTypes; i++ ) {
+               if ( strcmp( s1, hostLang->hostTypes[i].internalName ) == 0 )
+                       return hostLang->hostTypes + i;
+       }
+
+       return 0;
+}
 
 /* Construct a new parameter checker with for paramSpec. */
-ParamCheck::ParamCheck(char *paramSpec, int argc, char **argv)
+ParamCheck::ParamCheck( const char *paramSpec, int argc, char **argv )
 :
        state(noparam),
        argOffset(0),
@@ -119,7 +180,7 @@ bool ParamCheck::check()
        char argChar = *argOffset;
        
        /* Loop over all the parms and look for a match. */
-       char *pSpec = paramSpec;
+       const char *pSpec = paramSpec;
        while ( *pSpec != 0 ) {
                char pSpecChar = *pSpec;
 
@@ -191,4 +252,80 @@ bool ParamCheck::check()
        return true;
 }
 
+/* Counts newlines before sending sync. */
+int output_filter::sync( )
+{
+       line += 1;
+       return std::filebuf::sync();
+}
+
+/* Counts newlines before sending data out to file. */
+std::streamsize output_filter::xsputn( const char *s, std::streamsize n )
+{
+       for ( int i = 0; i < n; i++ ) {
+               if ( s[i] == '\n' )
+                       line += 1;
+       }
+       return std::filebuf::xsputn( s, n );
+}
+
+/* Scans a string looking for the file extension. If there is a file
+ * extension then pointer returned points to inside the string
+ * passed in. Otherwise returns null. */
+char *findFileExtension( char *stemFile )
+{
+       char *ppos = stemFile + strlen(stemFile) - 1;
+
+       /* Scan backwards from the end looking for the first dot.
+        * If we encounter a '/' before the first dot, then stop the scan. */
+       while ( 1 ) {
+               /* If we found a dot or got to the beginning of the string then
+                * we are done. */
+               if ( ppos == stemFile || *ppos == '.' )
+                       break;
+
+               /* If we hit a / then there is no extension. Done. */
+               if ( *ppos == '/' ) {
+                       ppos = stemFile;
+                       break;
+               }
+               ppos--;
+       } 
+
+       /* If we got to the front of the string then bail we 
+        * did not find an extension  */
+       if ( ppos == stemFile )
+               ppos = 0;
 
+       return ppos;
+}
+
+/* Make a file name from a stem. Removes the old filename suffix and
+ * replaces it with a new one. Returns a newed up string. */
+char *fileNameFromStem( char *stemFile, const char *suffix )
+{
+       long len = strlen( stemFile );
+       assert( len > 0 );
+
+       /* Get the extension. */
+       char *ppos = findFileExtension( stemFile );
+
+       /* If an extension was found, then shorten what we think the len is. */
+       if ( ppos != 0 )
+               len = ppos - stemFile;
+
+       /* Make the return string from the stem and the suffix. */
+       char *retVal = new char[ len + strlen( suffix ) + 1 ];
+       strncpy( retVal, stemFile, len );
+       strcpy( retVal + len, suffix );
+
+       return retVal;
+}
+
+exit_object endp;
+
+void operator<<( std::ostream &out, exit_object & )
+{
+    out << std::endl;
+    exit(1);
+}