Cod sursa(job #1349405)

Utilizator allexx2200Atanasiu Alexandru-Marian allexx2200 Data 20 februarie 2015 10:38:31
Problema Convertor Scor 0
Compilator cpp Status done
Runda rosedu_cdl_2015 Marime 1.84 kb
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define FIN  "convertor.in"
#define FOUT "convertor.out"

#define DIM_BUFFER 1024

#define START_SQ     "[{\0"
#define SEPARATOR_SQ "}, {\n\0"
#define END_SQ       "}]\0"

char* getKey(char* param){
    int length = strlen(param),i;
    int ocur = 0;
    for(i=0; i < length && ocur < 2 ; i++ ){
        if(param[i] == '\"'){
            ocur++;
        }
    }
    param[--i] = '\0';
    return param+1;
}

char* getValue(char* param){
    int length = strlen(param),i = 0;
    for(   ; i < length && param[i] != ':'; i++);
    //for(i++; i < length && param[i] == '\"'; i++);
    //for(i++; i < length && param[i] == '\"'; i++);
    i += 2;
    if(param[i] == '\"'){
        i++;
        param[length-3] = ',';
        param[length-2] = '\0';
    }
    param[length-1] = '\0';
    printf("\"%s\":%d\n", param, i);
    return param+i;
}

int main(){
    FILE* fin  = fopen(FIN , "rt");
    FILE* fout = fopen(FOUT, "wt");
    char buffer[DIM_BUFFER];
    char *result;

    fgets(buffer,DIM_BUFFER,fin);

    if(!strcmp(buffer,START_SQ)) return 1;

    //PARSE KEYS
    while(!feof(fin)){
        fgets(buffer,DIM_BUFFER,fin);

        if(!strcmp(buffer,SEPARATOR_SQ)) break;
        result = getKey(buffer);
        fputs(result,fout);
        fputs(",",fout);
    }


    //PARSE VALUES
    fputs("\n",fout);
    fseek(fin, SEEK_SET,0);
    fgets(buffer,DIM_BUFFER,fin);

    while(!feof(fin)){
        fgets(buffer,DIM_BUFFER,fin);

        if(!strcmp(buffer, END_SQ)){ break; }

        if(!strcmp(buffer,SEPARATOR_SQ)) {
            fputs("\n",fout);
            continue;
        }

        result = getValue(buffer);
        fputs(result,fout);
        //fputs(",",fout);
    }

    fclose(fin);
    fclose(fout);
    return 0;
}