#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
void strdel(char *p, int n){
char *a;
a=p+n;
strcpy(p,a);
p[strlen(p)]='\0';
}
char * element(char *a , char * b){//returneaza elementul dintre adresa a si b fara spatii in afara ghilimelelor
char *element,*no_spaces;//e un element ce are toate etributele elementului
//no spaces va fi returnat
element=(char*)malloc((b-a+1)*sizeof(char));
strncpy(element,a+1,b-a);
element[b-a+1]='\0';
//elinam spatiile idn afara ghilimelelor
int ghm;//verificam daca avem nr par sau impar de ghilimele
//daca e par eliminam spatiile
//daca e impar le lasam in pace
ghm=0;
int j=0,i;
char aux[3000];//la sfarsit vom trece intr-un alt vector , alocat dinamic , ce va fi returnat
for(i=0;i<strlen(element);i++){
if(ghm%2==0 && isspace(element[i]))
continue;
aux[j]=element[i];
j++;
if(element[i]=='\"')
ghm++;
}
aux[j]='\0';
no_spaces=strdup(aux);//alocam spatiu pt no spaces;
return no_spaces;
}
void write(char *s,FILE *f){//s -> elementul din care extragem atributele , n , nr de atribute , f fisierul in care scriem
int i , j;
char *a;
a=strchr(s,':');
while(a!=NULL ){
a=a+1;
j=0;
while(a[j]!=',' && a[j] !='}'){
if(a[j]=='\"'){
j++;
continue;
}
fputc(a[j],f);
j++;
}
fputc(',',f);
a=strchr(a,':');
}
fputc('\n',f);
}
int main(){
FILE *in=fopen("convertor.in","r");
FILE *out=fopen("convertor.out","w");
char tmp[2000];
char first[1000];//in el retinem prima linie , cea cu categoriile
char **categ;
categ=(char**)malloc(20*sizeof(char*));
if (in==NULL){
fprintf(stderr, "err\n");
exit(1);
}
char s[1025];
char fin[1048577];
int i;
//FORMATARE INPUT
while(fgets(s,1024,in)){
strcat(fin,s);
}
//OBTINEM CATEGORIILE
char *p,*q;
//separam primul element din json , cu toate atributele sale
p=strchr(fin,'{');
q=strchr(fin,'}');
i=0;
while(p<=q){
tmp[i]=*p;
i++;
p++;
}
tmp[i]='\n';
char *a;
a=strtok(tmp,",:");
i=0;
while(a!=NULL){
if(i%2==0){
strcat(first,a);
}
a=strtok(NULL,",:");
i++;
}
char *b;
char tst[]=" ";
b=tst;
a=strchr(first,'"');
int n=0 ;
char aux[1000];
while(a!=NULL && b!=NULL){
b=strchr(a+1,'"');
strncpy(aux,a+1, (int)(b-a-1));
aux[b-a-1]='\0';
categ[n]=strdup(aux);
fprintf(out,"%s,",categ[n]);
n++;
a=strchr(b+1,'"');
}
fprintf(out,"\n");
//in continuare incerc sa scot cuvintele dintre ghilimele si sa le pun in vector cu ctg
a=strchr(fin,'{');
b=strchr(fin,'}');
char *no_spaces;
while(a!=NULL && b!=NULL){
no_spaces=element(a,b);
write(no_spaces,out);
a=strchr(b,'{');
if(a!=NULL)
b=strchr(a,'}');
else
break;
}
fclose(out);
fclose(in);
return 0;
}