#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BLSIZE 2048
void frumos(char *s, char *delim)
{
int i, j, n, nr_gh = 0;
n = strlen(s);
for (i = 0; i < n; i++) {
if (s[i] == '"')
nr_gh++;
if (strchr(delim, s[i]) || (nr_gh != 1 && s[i] == ' ')) {
for (j = i; j < n; j++)
s[j] = s[j + 1];
n--;
i--;
}
}
}
int main()
{
FILE *input = fopen("convertor.in", "rb");
FILE *output = fopen("convertor.out", "w");
int i, nr = 0, size, jum = 0, chei = 0, lungime, ok;
char *array2, array[BLSIZE], delim[] = "\n}{][\"", *rest, key[2 * BLSIZE], prima[2 * BLSIZE];
fseek(input, 0, SEEK_END);
size = ftell(input);
fseek(input, 0, SEEK_SET);
while (fread(array, 1, BLSIZE, input)) {
while (array && !chei) {
rest = strchr(array, ':');
if (!rest) {
memset(prima, 0, 2 * BLSIZE);
memcpy(prima, array, BLSIZE);
jum = 1;
} else {
lungime = rest - array;
if (lungime) {
memset(key, 0, 2 * BLSIZE);
memcpy(key, array, lungime);
if (jum) {
strncat(prima, array, lungime);
memcpy(key, prima,
strlen(prima));
jum = 0;
}
if (strchr(key, '{')
&& !strchr(key, '[')) {
chei = 1;
fprintf(output, "\n");
break;
}
frumos(key, delim);
if (key) {
nr ++;
fprintf(output, "%s,", key);
}
}
}
array2 = strchr(array, ',');
if (array2)
strcpy(array, array2 + 1);
else
break;
}
memset(array, 0, BLSIZE);
}
fseek(input, 0, 0);
i = 0;
ok = 1;
chei = 0;
while (fread(array, 1, BLSIZE, input)) {
rest = strchr(array, ':');
while(rest) {
i ++;
while(!strchr("\"-0123456789", *rest)) {
rest ++;
if(rest - array == BLSIZE) {
memset(array, 0, BLSIZE);
ok = fread(array, 1, BLSIZE, input);
rest = array;
}
}
if(*rest == '"') {
rest ++;
if(rest - array == BLSIZE) {
memset(array, 0, BLSIZE);
ok = fread(array, 1, BLSIZE, input);
rest = array;
}
while(*rest != '"') {
fprintf(output, "%c", *rest);
rest ++;
if(rest - array == BLSIZE) {
memset(array, 0, BLSIZE);
ok = fread(array, 1, BLSIZE, input);
rest = array;
}
}
}
else {
while(strchr("-0123456789", *rest)) {
fprintf(output, "%c", *rest);
rest ++;
if(rest - array == BLSIZE) {
memset(array, 0, BLSIZE);
ok = fread(array, 1, BLSIZE, input);
rest = array;
}
}
}
fprintf(output, ",");
if(!(i % nr)) {
fprintf(output, "\n");
}
if(!ok)
break;
rest = strchr(rest, ':');
}
if(!ok)
break;
}
fclose(output);
fclose(input);
return 0;
}