Cod sursa(job #1359495)
Utilizator | Data | 24 februarie 2015 23:01:50 | |
---|---|---|---|
Problema | Convertor | Scor | 30 |
Compilator | c | Status | done |
Runda | rosedu_cdl_2015 | Marime | 8.8 kb |
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *in, *out;
in=fopen("convertor.in","r");
if(in==NULL)
{
fprintf(stderr, "Could not open input file");
return -1;
}
out=fopen("convertor.out","w");
if(out==NULL)
{
fprintf(stderr, "Could not open output file");
fclose(in);
return -1;
}
char keys[255][255];
char entry[255][255];
char word[255];
int i=0, j=0, k=0, ok=0;
int it=0;
fscanf(in, "%s", &word);
fscanf(in, "%s", &word);
fscanf(in, "%s", &word);
int p=0;
int no=1;
while(word[0]!='}'&&no==1)
{
if(word[0]=='{')
if(strlen(word)==1)
{
p=0;
k=0;
fscanf(in, "%s", &word);
}
else memcpy(word, word+1, strlen(word)-1);
if(strlen(word)==1&&word[0]==':')
{
p=1;
k=0;
fscanf(in, "%s", &word);
}
if(p==0)
{
if(k==0)
{
if(word[strlen(word)-1]==':')
{
memcpy(keys[i],word+1,strlen(word)-3);
i++;
p=1;
}
else if(word[strlen(word)-1]=='"')
{
memcpy(keys[i],word+1,strlen(word)-2);
i++;
p=1;
}
else
{
memcpy(keys[i],word+1,strlen(word));
p=0;
k=1;
}
}
else
{
if(word[strlen(word)-1]==':')
{
memcpy(keys[i]+strlen(keys[i])," ",1);
memcpy(keys[i]+strlen(keys[i]),word,strlen(word)-2);
i++;
p=1;
k=0;
}
else if(word[strlen(word)-1]=='"')
{
memcpy(keys[i]+strlen(keys[i])," ",1);
memcpy(keys[i]+strlen(keys[i]),word,strlen(word)-1);
i++;
p=1;
k=0;
}
else
{
memcpy(keys[i]+strlen(keys[i])," ",1);
memcpy(keys[i]+strlen(keys[i]),word+1,strlen(word));
p=0;
k=1;
}
}
}
else
{
if(k==0)
{
if(word[strlen(word)-1]=='}')
{
memcpy(entry[j],word+1,strlen(word)-4);
p=0;
j++;
no=0;
}
if(word[0]=='"'&&word[strlen(word)-1]==',')
{
memcpy(entry[j],word+1,strlen(word)-3);
p=0;
j++;
}
else if(word[0]=='"'&&word[strlen(word)-1]=='"')
{
memcpy(entry[j],word+1,strlen(word)-2);
j++;
p=0;
}
else if(word[0]=='"')
{
memcpy(entry[j],word+1,strlen(word)-1);
k=1;
p=1;
}
else
{
memcpy(entry[j],word, strlen(word)-1);
j++;
p=0;
}
}
else if(word[strlen(word)-1]=='}')
{
memcpy(entry[j],word+1,strlen(word)-3);
p=0;
j++;
no=0;
}
else if(word[strlen(word)-1]==',')
{
memcpy(entry[j]+strlen(entry[j])," ",1);
memcpy(entry[j]+strlen(entry[j]),word,strlen(word)-2);
p=0;
k=0;
j++;
}
else if(word[strlen(word)-1]=='"')
{
memcpy(entry[j]+strlen(entry[j])," ",1);
memcpy(entry[j]+strlen(entry[j]),word,strlen(word)-1);
j++;
p=0;
k=0;
}
else
{
memcpy(entry[j]+strlen(entry[j])," ",1);
memcpy(entry[j]+strlen(entry[j]),word, strlen(word));
p=1;
}
}
fscanf(in, "%s", &word);
}
for(it=0; it<i; it++)
fprintf(out,"%s,",keys[it]);
fprintf(out,"\n");
fscanf(in, "%s", &word);
int pd=0;
while(word[0]!=']'&&pd==0)
{
if(j==i||no==0)
{
for(it=0; it<j; it++)
{
fprintf(out,"%s,",entry[it]);
memset(entry[it],0,sizeof(entry[it]));
}
fprintf(out,"\n");
j=0;
no=1;
}
if(word[0]=='{')
if(strlen(word)==1)
{
p=0;
k=0;
fscanf(in, "%s", &word);
}
else memcpy(word, word+1, strlen(word)-1);
if(word[0]=='}')
{
p=0;
k=0;
fscanf(in, "%s", &word);
}
if(strlen(word)==1&&word[0]==':')
{
p=1;
k=0;
fscanf(in, "%s", &word);
}
if(p==0)
{
if(k==0)
{
if(word[strlen(word)-1]==':'||word[strlen(word)-1]=='"')
p=1;
else
{
p=0;
k=1;
}
}
else
{
if(word[strlen(word)-1]==':'||word[strlen(word)-1]=='"')
{
p=1;
k=0;
}
else
{
p=0;
k=1;
}
}
}
else
{
if(k==0)
{
if(word[strlen(word)-1]==']')
{
pd=1;
memcpy(entry[j],word+1,strlen(word)-5);
p=0;
j++;
}
else if(word[strlen(word)-1]=='}')
{
no=0;
memcpy(entry[j],word+1,strlen(word)-4);
p=0;
j++;
}
else if(word[0]=='"'&&word[strlen(word)-1]==',')
{
memcpy(entry[j],word+1,strlen(word)-3);
p=0;
j++;
}
else if(word[0]=='"'&&word[strlen(word)-1]=='"')
{
memcpy(entry[j],word+1,strlen(word)-2);
j++;
p=0;
}
else if(word[0]=='"')
{
memcpy(entry[j],word+1,strlen(word)-1);
k=1;
p=1;
}
else
{
memcpy(entry[j],word, strlen(word)-1);
j++;
p=0;
}
}
else if(word[strlen(word)-1]=='}')
{
no=0;
memcpy(entry[j],word+1,strlen(word)-3);
p=0;
j++;
}
else if(word[strlen(word)-1]==']')
{
pd=1;
memcpy(entry[j],word+1,strlen(word)-4);
p=0;
j++;
}
else if(word[strlen(word)-1]==',')
{
memcpy(entry[j]+strlen(entry[j])," ",1);
memcpy(entry[j]+strlen(entry[j]),word,strlen(word)-2);
p=0;
k=0;
j++;
}
else if(word[strlen(word)-1]=='"')
{
memcpy(entry[j]+strlen(entry[j])," ",1);
memcpy(entry[j]+strlen(entry[j]),word,strlen(word)-1);
j++;
p=0;
k=0;
}
else
{
memcpy(entry[j]+strlen(entry[j])," ",1);
memcpy(entry[j]+strlen(entry[j]),word, strlen(word));
p=1;
}
}
fscanf(in, "%s", &word);
}
if(j==i||no==0)
for(it=0; it<j; it++)
fprintf(out,"%s,",entry[it]);
fclose(in);
fclose(out);
return 0;
}