#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
void creare_string(char *string, FILE *input)
{
char c;
int nr = 0;
while( ( c = fgetc(input)) != EOF)
{
string[nr] = c;
nr++;
}
}
void identificare_elemente(char *string, char *elemente)
{
int i, j, nr = 0;
for(i = 0; i < strlen(string) - 2; i++)
{
if(string[i] == ':')
{
j = i + 2;
while(string[j-1] != ',')
{
elemente[nr] = string[j];
j++;
nr++;
}
}
}
}
void identificare_chei(char *string, char *chei)
{
int i, j, nr = 0;
for(i = 0; i < strlen(string) - 2; i++)
{
if(string[i] == '{' || string[i] == ',')
{
j = i + 2;
while(string[j-1] != ':')
{
chei[nr] = string[j];
j++;
nr++;
}
}
}
}
int numar_chei(char *string)
{
int i, numar = 0;
for(i = 0; i < strlen(string); i++)
{
if(string[i] == '}')
{
break;
}
if(string[i] == ':')
{
numar++;
}
}
return numar;
}
void introducere_chei(char *chei, int nr_chei, FILE *output)
{
char *token;
int numar = 0;
token = strtok(chei, "\"");
while(token != NULL)
{
if(isalpha(token[0]) != 0 || isdigit(token[0]) != 0)
{
fprintf(output, "%s,", token);
numar++;
}
if(numar == nr_chei)
{
fprintf(output, "%s", "\n");
break;
}
token = strtok(NULL, "\"");
}
}
void introducere_elemente(char *elemente, int nr_chei, FILE *output)
{
char *token;
int numar = 0;
token = strtok(elemente, ",\"");
while(token != NULL)
{
if(isalpha(token[0]) != 0 || isdigit(token[0]) != 0)
{
fprintf(output, "%s,", token);
numar++;
}
if(numar == nr_chei)
{
fprintf(output, "%s", "\n");
numar = 0;
}
token = strtok(NULL, ",\"");
}
}
void stergere_trash(char *str, char c)
{
char *src, *dst;
for(src = dst = str; *src != '\0'; src++)
{
*dst = *src;
if(*dst != c)
{
dst++;
}
}
*dst = '\0';
}
void stergere_spatii(char *elemente)
{
int cont = 0;
char *dst, *src;
for(dst = src = elemente; *src != '\0'; src++)
{
if(*src == '"')
{
cont++;
}
if(cont % 2 == 1)
{
*dst = *src;
dst++;
}
if(cont % 2 == 0)
{
*dst = *src;
if(*dst != ' ')
{
dst++;
}
}
}
*dst = '\0';
}
int main()
{
FILE *input, *output;
char string[500000], elemente[500000], chei[500000], trash[7] = {'}', ']', '[','{','\n', ':'};
int nr_chei, i;
input = fopen("convertor.in", "rt");
output = fopen("convertor.out", "wt");
creare_string(string, input);
identificare_elemente(string, elemente);
identificare_chei(string, chei);
nr_chei = numar_chei(string);
for(i = 0; i < strlen(trash); i++)
{
stergere_trash(elemente, trash[i]);
stergere_trash(chei, trash[i]);
}
stergere_spatii(elemente);
introducere_chei(chei, nr_chei, output);
introducere_elemente(elemente, nr_chei, output);
fclose(input);
fclose(output);
return 0;
}