#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
void get_column(FILE *file_out,FILE *file_json,int *nr_col);
void get_lines(FILE *file_json,FILE *file_out,int *nr_col);
int main()
{
FILE *file_json = fopen("convertor.in","r");
FILE *file_out = fopen("convertor.out","w");
int nr_col = 0;
get_column(file_out,file_json,&nr_col);
get_lines(file_json,file_out,&nr_col);
return 0;
}
void get_column(FILE *file_out,FILE *file_json,int *nr_col) {
int i = 0,aux,n;
char *sample,c;
n = 16;
sample = malloc( n );
while((c = fgetc(file_json)) != '}') {
if (c == '}')
break;
sample[i++] = c;
if(i == n) {
n *= 2;
sample = realloc (sample, n);
}
}
sample[i] = 0;
i = 0;
start :
while(strchr(sample + i,'\"')) {
i = strchr(sample + i,'\"') + 1 - sample;
aux = strchr(sample + i,'\"') + 1 - sample;
fwrite(sample + i ,aux - i - 1,1,file_out);
fwrite(",",1,1,file_out);
i = aux;
(*nr_col)++;
while(i++)
switch (sample[i]) {
case '\"' :
aux = strchr(sample + i + 1,'\"') + 1 - sample;
i = aux;
goto start;
default :
if(isdigit(sample[i])) {
aux = i++;
while(isdigit(sample[i++]))
;
goto start;
}
}
}
fwrite("\n",1,1,file_out);
rewind(file_json);
}
void get_lines(FILE *file_json,FILE *file_out,int *nr_col) {
int k = 0,ok = 0,i;
char c;
start :
if(k == *nr_col) {
fprintf(file_out,"\n");
k = 0;
}
for(i = 0; i < 2; c = fgetc(file_json)) {
// printf("%c",c);
if(c == EOF)
return ;
if(c == '"')
i++;
}
while(1) {
c = fgetc(file_json);
switch (c) {
case '"' :
while((c = fgetc(file_json)) != '"')
fprintf(file_out,"%c",c);
fprintf(file_out,",");
k++;
c = fgetc(file_json);
goto start;
default :
if (isdigit(c)) {
fprintf(file_out,"%c",c);
while((c = fgetc(file_json)) && isdigit(c))
fprintf(file_out,"%c",c);
fprintf(file_out,",");
k++;
goto start;
}
}
}
fclose(file_json);
fclose(file_out);
}
/*while((c = fgetc(file_json)) != EOF) {
if(c == '"') {
while((c = fgetc(file_json)) != EOF) {
if(c == '"') {
ok = 1;
while((c = fgetc(file_json)) != EOF) {
if(c == '"') {
while((c = fgetc(file_json)) != EOF && c != '"')
fprintf(file_out,"%c",c);
fprintf(file_out,",");
k++;
if(k == *nr_col) {
fprintf(file_out,"\n");
k = 0;
}
ok = 0;
break;
}
else if (isdigit(c)) {
fprintf(file_out,"%c",c);
while((c = fgetc(file_json)) != EOF && isdigit(c)) {
fprintf(file_out,"%c",c);
}
fprintf(file_out,",");
k++;
if(k == *nr_col) {
fprintf(file_out,"\n");
k = 0;
}
ok = 0;
break;
}
}
if(!ok)
break;
}
}
}
} */