Cod sursa(job #1350094)

Utilizator dumytruKana Banana dumytru Data 20 februarie 2015 17:39:44
Problema Convertor Scor 100
Compilator cpp Status done
Runda rosedu_cdl_2015 Marime 4.81 kb
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define STRING 2
#define INT 1


int main()
{
    FILE* IN = fopen("convertor.in", "r");
    FILE* OUT = fopen("convertor.out", "w");

    char line[1024], *readBuffer, ***table;
    int length, crtKey = 0, crtVal = 0, crtObj = 0, bufferCrt = 0, bufferLength = 10, bufferStep = 10;
    int i, j;
    short isReading = 0, isValue = 0, type = 0;

    readBuffer = (char *)malloc(bufferLength * sizeof(char));
    table = (char***)malloc( sizeof(char**) );//table header / keys' row
    table[0]  = (char**)malloc(sizeof(char*));

    //start of JSON
    while ( fgetc(IN) != '[' );

    while( fgets(line, 1024, IN) != NULL )
    {
        length = strlen(line);
        for( i = 0 ; i < length ; i++ )
        {
            if( !isReading )
            {
                if(line[i] == ']') //
                    break;
                //daca nu citeste o cheie sau o valoare
                switch(line[i])
                {
                    case '{': //next object
                        crtObj++;
                        crtVal = 0;
                        table = (char***)realloc(table, (crtObj+1) * sizeof(char**));
                        table[crtObj]  = (char**)malloc(sizeof(char*));
                        break;
                    case '"': //cheie
                        isReading = 1;
                        isValue = 0;
                        type = STRING; //cheia e de tip string
                        break;
                    case ':': //valoare
                        isReading = 1;
                        isValue = 1;
                        break;
                    default: //pentru spatii, tab-uri...
                        continue;
                }
            }
            else
            {
                //get value type, if not done so already
                if(!type && isValue)
                {
                    if(line[i] >= '0' && line [i] <= '9')
                        type = INT; //int
                    else if(line[i] == '"')
                    {
                        type = STRING; //string
                        continue; //string-ul incepe de la urmatorul caracter
                    }
                    else
                        continue;//salt peste caracterele dintre : si value
                }

                //sfarsitul citirii unei chei sau valori
                if(     (type == STRING && line[i] == '"') ||
                        (type == INT && !(line[i] >= '0' && line [i] <= '9') ) )
                {
                    readBuffer[bufferCrt++] = '\0';//end of string

                    //write the keys in table header, only for first object
                    if(!isValue && crtObj == 1)
                    {
                        table[0]  = (char**)realloc( table[0], (crtKey+1) * sizeof(char*));
                        table[0][crtKey] = (char*)malloc( bufferCrt * sizeof(char) );
                        //copy the key from buffer to table
                        for( j=0 ; j<bufferCrt ; j++ )
                            table[0][crtKey][j] = readBuffer[j];
                        crtKey++;
                    }
                    else if(isValue)
                    {
                        table[crtObj]  = (char**)realloc( table[ crtObj ], (crtVal+1) * sizeof(char*));
                        table[crtObj][crtVal] = (char*)malloc( bufferCrt * sizeof(char) );
                        //copy the value from buffer to table
                        for( j=0 ; j<bufferCrt ; j++)
                            table[crtObj][crtVal][j] = readBuffer[j];
                        crtVal++;
                    }

                    //reset the reading buffer
                    free(readBuffer);
                    bufferCrt = isReading = isValue = type = 0;
                    bufferLength = 10;
                    readBuffer = (char *) malloc (bufferLength * sizeof(char));
                    continue;
                }

                //insert current char in buffer
                if(bufferCrt <= bufferLength)
                {
                    bufferLength += bufferStep;
                    readBuffer = (char *)realloc( readBuffer, bufferLength * sizeof(char) );
                }
                readBuffer[bufferCrt++] = line[i];
            }
        }
    }

    //print
    for(i = 0 ; i <= crtObj ; i++)
    {
        for(j = 0 ; j < crtKey ; j++)
            fprintf(OUT, "%s,", table[i][j]);
        fprintf(OUT, "\n");
    }

    //free memory
    for( i = 0 ; i <= crtObj ; i++)
    {
        for( j = 0 ; j < crtKey ; j++)
            free( table[i][j] );
        free( table[i] );
    }
    free( table );

    //close files
    fclose( IN );
    fclose( OUT );

    return 0;
}