Cod sursa(job #1361811)

Utilizator ene.corneliaEne Maria Cornelia ene.cornelia Data 25 februarie 2015 23:52:38
Problema Convertor Scor 100
Compilator c Status done
Runda rosedu_cdl_2015 Marime 3.47 kb
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
  
int main()
{
    char c, *key, *val, *line;
    int dp=0, ghil=0, i=0, j=0, k=0, l=0, poz=0;
     
    FILE *f=fopen("convertor.in","rt");
    FILE *g=fopen("convertor.out","wt");
     
    key=(char *)calloc(10000000, sizeof(char));
    val=(char *)calloc(10000000, sizeof(char));
    line=(char *)calloc(1024, sizeof(char));
     
    i=j=k=0;
    while(c!=']')
    {
        //fscanf(f,"%c",&c);
        if(poz==l)
        {
            fgets(line, 1024, f);
            poz=0;
            l=strlen(line);
        }
        c=line[poz++];
         
        if(c=='{')
        {
            while(c!='}')
            {
                //fscanf(f,"%c",&c);
                if(poz==l)
                {
                    fgets(line, 1024, f);
                    poz=0;
                    l=strlen(line);
                }
                c=line[poz++];
                 
                if(c=='"' && dp==0)
                {
                    //fscanf(f,"%c",&c);
                    if(poz==l)
                    {
                        fgets(line, 1024, f);
                        poz=0;
                        l=strlen(line);
                    }
                    c=line[poz++];
                     
                    while(c!='"')
                    {
                        key[k++]=c;
                        //fscanf(f,"%c",&c);
                        if(poz==l)
                        {
                            fgets(line, 1024, f);
                            poz=0;
                            l=strlen(line);
                        }
                        c=line[poz++];
                    }
                    key[k++]=',';
                }
                else
                {
                    if(c==':') dp=1;
                    else
                    {
                        if(dp==1 && (c!=' ' && c!='\n'))
                        {
                            if(c=='"')  
                            {
                                //fscanf(f,"%c",&c);
                                if(poz==l)
                                {
                                    fgets(line, 1024, f);
                                    poz=0;
                                    l=strlen(line);
                                }
                                c=line[poz++];
                                ghil=1;
                            }
                            while(!(c=='"' || (ghil==0 && (c==',' || c==' ' || c=='\n' || c=='}'))))
                            {
                                val[j++]=c;
                                //fscanf(f,"%c",&c);
                                if(poz==l)
                                {
                                    fgets(line, 1024, f);
                                    poz=0;
                                    l=strlen(line);
                                }
                                c=line[poz++];
                            }
                            val[j++]=',';
                            val[j]='\0';
                            dp=ghil=0;
                        }
                    }
                }
            }
            if(i==0)
                fprintf(g,"%s\n",key);
            fprintf(g,"%s\n",val);
            i++;
            j=k=0;
        }
    }
     
    free(key);
    free(val);
    free(line);
    return 0;
}