Example #1
0
File: String.c Project: ptII/ptII
/*** String_convert() ***/
Token String_convert(Token token, ...) {
    char* stringPointer;

    switch (token.type) {
#ifdef TYPE_Boolean
    case TYPE_Boolean:
        stringPointer = BooleantoString(token.payload.Boolean);
        break;
#endif

#ifdef TYPE_Int
    case TYPE_Int:
        stringPointer = InttoString(token.payload.Int);
        break;
#endif

#ifdef TYPE_Double
    case TYPE_Double:
        stringPointer = DoubletoString(token.payload.Double);
        break;
#endif

    default:
        // FIXME: not finished
        fprintf(stderr, "String_convert(): Conversion from an unsupported type. (%d)\n", token.type);
        break;
    }
    token.payload.String = stringPointer;
    token.type = TYPE_String;
    return token;
}
Example #2
0
/*** toString_BooleanArray() ***/
char* toString_BooleanArray(Token thisToken) {
        int i;
    int currentSize, allocatedSize;
    char* string;
    char elementString[6];
    allocatedSize = 256;
    string = (char*) malloc(allocatedSize);
    string[0] = '{';
    string[1] = '\0';

    // Space for '{', '}', and '\0' characters.
    currentSize = 3;

    //printf("%d\n", thisToken.payload.BooleanArray->size);
    for (i = 0; i < thisToken.payload.BooleanArray->size; i++) {
                // Calculate the require storage size.

            // boolean temp = BooleanArray_get(thisToken, i);
        sprintf(elementString, "%s", BooleantoString(BooleanArray_get(thisToken, i)));
        currentSize += strlen(elementString);
                if (i != 0) {
                        currentSize += 2;
                }

                // Re-allocate storage.
                if (currentSize > allocatedSize) {
            allocatedSize *= 2;
            string = (char*) realloc(string, allocatedSize);
        }

                // Concat the element strings and separators.
                if (i != 0) {
            strcat(string, ", ");
        }
        strcat(string, elementString);
    }

    strcat(string, "}");
    return string;
}