FileDocCategorySizeDatePackage
CCodeWriter.javaAPI DocJ2ME CLDC 1.15252Wed Feb 05 15:56:02 GMT 2003runtime

CCodeWriter

public class CCodeWriter extends Object

Fields Summary
util.BufferedPrintStream
out
private static String[]
hexString
private int
column
Constructors Summary
public CCodeWriter(OutputStream file)

    out = new BufferedPrintStream(file);
    
Methods Summary
public booleancheckError()

 
    return out.checkError();
    
public voidclose()

    out.close();
    
public voidflush()

 
    out.flush();
    
public voidprint(java.lang.Object x)

 
    print(x.toString());
    
public voidprint(java.lang.String x)

 
    int length = x.length();
    for (int i = 0; i < length; i++) { 
        write(x.charAt(i));
    }
    
public voidprint(int x)

 
    print(Integer.toString(x));
    
public final voidprintHexInt(int v)

        hexString = new String[0x100];
        for(int i = 0; i < 256; i++){
        hexString[i] = Integer.toHexString(i + 256).substring(1);
        }
    
        int a, b, c, d;
        a = v>>>24;
        b = (v>>>16) & 0xff;
        c = (v>>>8) & 0xff;
        d = v & 0xff;
        print("0x");
        if ( a != 0 ){
            print(hexString[ a ]);
            print(hexString[ b ]);
            print(hexString[ c ]);
            print(hexString[ d ]);
        } else if ( b != 0 ){
            print(hexString[ b ]);
            print(hexString[ c ]);
            print(hexString[ d ]);
        } else if ( c != 0 ){
            print(hexString[ c ]);
            print(hexString[ d ]);
        } else {
            print(hexString[ d ]);
        }
    
voidprintSafeString(java.lang.String x)

 
        final int STRING_PURE = 0;
        final int STRING_SOME_BAD = 1;
        final int STRING_ALL_BAD = 2;
        // STRING_PURE means there are no problematic characters.
        //
        // STRING_SOME_BAD means that we should print out the string
        // normally, but escape the uncertain characters.  
        //
        // STRING_ALL_BAD means that we encountered a character that makes
        // us believe that this is an encoded string, and we should print
        // out everything except letters using escape sequences.

        int stringType = STRING_PURE;
        int length = x.length();

        for (int i = 0; i < length; i++) { 
            char c = x.charAt(i);
            if (c < ' " || c > '~") { 
                stringType = STRING_ALL_BAD;
                break;
            }
            if (c == '"" || c == '\\" || c == '/") { 
                stringType = STRING_SOME_BAD;
            }
        }

        write ('"");
        if (stringType == STRING_PURE) { 
            // There were no bad characters whatsoever.  Just print it
            print(x); 
            write('"");
            return;
        }

        for (int i = 0; i < length; i++) { 
            char c = x.charAt(i);
            if (Character.isLetterOrDigit(c) && c < 128) { 
                write(c);
            } else if ((stringType != STRING_ALL_BAD)
                 &&  ( c >= (byte)' " ) && ( c <= (byte)'~" )
                 && ( c != (byte)'"") && (c  != (byte)'\\" )  
                 && ( c != (byte)'/")) {
                    // the only dangers between space and ~ are " and \  
                    // We must also be careful about writing */ by accident.
                write(c);
            } else {
                switch ( c ){
                case '\\":
                    print("\\\\");
                    break;
                case '\n":
                    print("\\n");
                    break;
                case '\r":
                    print("\\r");
                    break;
                case '\t":
                    print("\\t");
                    break;
                case '"":
                    print("\\\"");
                    break;
                case '/":
                    // we only have a problem if the previous char was an
                    // asterisk.  It looks like we're ending the comment.
                    if (i > 0 && x.charAt(i - 1) == (byte)'*")
                        write('\\");
                    write('/");
                    break;
                default: 
                    int temp = (c & 0xFF) + 01000;
                    print("\\");
                    print(Integer.toOctalString(temp).substring(1));
                    break;
                    // end of switch
                }
            }
        } // end of for

        write('"");
    
public voidprintln(java.lang.String x)

 
    print(x);
    write('\n");
    
public voidprintln()

 
    write('\n");
    
public voidwrite(int x)

         
    if (x == '\n") { 
        out.write(x); 
        column = 0;
    } else if (x == '\t") { 
        do { out.write(' "); column++;
        } while ((column % 4) != 0);
    } else { 
        out.write(x);
        column++;
    }