Cod sursa(job #1340461)

Utilizator dinuandAndrei-Mario Dinu dinuand Data 11 februarie 2015 20:34:31
Problema Convertor Scor 60
Compilator c Status done
Runda rosedu_cdl_2015 Marime 3.42 kb
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define LINE_LENGTH 1024
#define INPUT_FILE  "convertor.in"
#define OUTPUT_FILE "convertor.out"

typedef struct pair {
    char *key;
    char *value;
} pair;

int main() {

    // Declaration part
    FILE *fdin;
    FILE *fdout;
    char line[LINE_LENGTH + 1];
    int mark_for_first_entry = 0, mark_for_colon = 0, mark_for_paranthesis = 0;
    int position, pair_index = 0, i;
    int pair_max = 512;
    char *indicator;
    pair *e;

    // Open files
    fdin  = fopen( INPUT_FILE, "r" );
    fdout = fopen( OUTPUT_FILE, "w" );

    // Initial malloc for the number of pairs (key, value) in a single entry 
    e = (pair*)malloc( pair_max * sizeof( pair ) );

    // Read a single line of text
    indicator = fgets( line, LINE_LENGTH + 1, fdin );

    while ( indicator != NULL ) {

        position = 0;
        
        int length = strlen(line);
        while ( position < length ) {

            if ( line[ position ] == '}' ) {
                // Print the table header if necessary
                if ( !mark_for_first_entry ) {
                    for (i = 0; i < pair_index; i++) {
                        fprintf(fdout, "%s,", e[i].key);
                    }
                    fprintf(fdout, "\n");
                    mark_for_first_entry = 1;
                }

                // Print the entry
                for (i = 0; i < pair_index; i++) {
                    fprintf(fdout, "%s,", e[i].value);
                }
                fprintf(fdout, "\n");

                // Reinit parameters
                mark_for_colon = 0;
                mark_for_paranthesis = 0;
                pair_index = 0;
            }

            if (line[ position ] == ':' ) {
                ++position;
                // Clear multiple spaces
                while ( line[ position ] == ' ' ) {
                    ++position;
                }
                mark_for_colon = 1;
            }

            // Read the key
            if ( line[ position ] == '"' && !mark_for_colon ) {
                int prev = position + 1;
                position = position + 2;
                while (line[ position ] != '"' ) {
                    ++position;
                }
                // Check if we need to realloc
                if ( pair_index + 1 == pair_max ) {
                    pair *new_p = realloc( e, 2 * pair_max);
                    e = new_p;
                    pair_max = 2 * pair_max;
                }
                e[pair_index++].key = strndup(line + prev, position - prev);
            } else if ( line[ position ] == '"' && mark_for_colon ) {
                int prev = position + 1;
                position = position + 2;
                while (line[ position ] != '"' ) {
                    ++position;
                }
                e[pair_index - 1].value = strndup( line + prev, position - prev );
            } else if ( line[ position ] >= '0' && line[ position ] <= '9' && mark_for_colon) {
                int prev = position;
                ++position;
                while ( line[ position ] >= '0' && line[ position ] <= '9' ) {
                    ++position;
                }
                e[pair_index - 1].value = strndup(line + prev, position - prev);
            }

            if (line[ position ] == ',' ) mark_for_colon = 0;

            position++;
        }

        // read the next line
        indicator = fgets( line, LINE_LENGTH + 1, fdin );
    }

    return 0;
}