#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define BUFFER_SIZE 1024
typedef struct element {
char *nume;
struct element *next;
} element;
typedef struct category {
char *nume;
element *e;
struct category *next;
} category;
typedef struct root {
int category_length;
category *categorie;
} root;
char *get_sample(FILE *in_file);
root *init();
void generate_categories(root *head, char *buffer);
void add_element(root *head, int here, char *nume);
void completare(root *head, char *buffer);
void afisare(root *head, int level, FILE *out_file);
int main()
{
FILE *in_file = fopen("convertor.in", "r");
FILE *out_file = fopen("convertor.out", "w");
char *buffer;
int level = 0;
buffer = get_sample(in_file);
root *head = init ();
generate_categories(head, buffer);
while(strcmp(buffer, "{error}") != 0) {
++level;
completare(head, buffer);
buffer = get_sample(in_file);
}
afisare(head, level, out_file);
fclose(out_file);
fclose(in_file);
return 0;
}
char *get_sample(FILE *in_file) {
int c;
char character;
char save = 0;
char *buffer;
buffer = (char*)malloc(BUFFER_SIZE * sizeof(char));
int used = 0;
int reallocs = 2;
do {
c = fgetc(in_file);
character = (char)c;
if (character == '}')
break;
if(save) {
buffer[used] = character;
used++;
if(used >= BUFFER_SIZE) {
buffer = (char*)realloc(buffer, reallocs * BUFFER_SIZE *
sizeof(char));
reallocs++;
}
buffer[used] = 0;
}
if (character == '{')
save = 1;
} while(c != EOF);
if (c == EOF)
return "{error}";
return buffer;
}
root *init() {
root *head = (root*)malloc(sizeof(root));
head->category_length = 0;
head->categorie = NULL;
return head;
}
void generate_categories(root *head, char *buffer) {
int j;
char *pct;
char *ghi1, *ghi2, *nume;
int contor;
category *aux;
while ((pct = strstr(buffer, ":")) != NULL){
contor = 0;
for(j = pct - buffer; j >= 0; j--) {
if (buffer[j] == '"') {
contor++;
if(contor == 1)
ghi2 = buffer + j;
else if(contor == 2)
ghi1 = buffer + j;
}
if (contor == 2){
contor = ghi2 - ghi1 - 1;
nume = (char*)calloc((contor + 1), sizeof(char));
strncpy(nume, ghi1 + 1, contor);
category *cat = (category*)malloc(sizeof(category));
cat->nume = nume;
cat->e = NULL;
cat->next = NULL;
head->category_length++;
if(head->categorie == NULL)
head->categorie = cat;
else {
aux = head->categorie;
while (aux->next)
aux = aux->next;
aux->next = cat;
}
break;
}
}
buffer = pct + 1;
}
}
void add_element(root *head, int here, char *nume) {
int i;
category *aux = head->categorie;
for ( i = 0; i < here; i++)
aux = aux->next;
element *el = aux->e;
element *nou = (element*) malloc(sizeof(element));
nou->nume = nume;
nou->next = NULL;
if (el == NULL) {
aux->e = nou;
} else {
while ( el->next ) {
el = el->next;
}
el->next = nou;
}
}
void completare(root *head, char *buffer) {
int i, j, k;
char *pct;
char *ghi1, *nume;
int val;
int size = strlen(buffer);
for(i = 0; i < head->category_length; i++) {
pct = strstr(buffer, ":");
for(j = 0 ;j < buffer + size - pct; j++) {
if(pct[j] == '"') {
ghi1 = pct + j;
for (k = 1; k < buffer + size - pct; k++) {
if(ghi1[k] == '"') {
nume = (char*)calloc((k), sizeof(char));
strncpy(nume, ghi1 + 1, k - 1);
add_element(head, i, nume);
break;
}
}
break;
} else if (pct[j] >= '0' && pct[j] <= '9' ) {
sscanf(pct + j, "%d", &val);
nume = (char*)calloc(10, sizeof(char));
sprintf(nume, "%d", val);
add_element(head, i, nume);
break;
}
}
buffer = pct + 1;
}
}
void afisare(root *head, int level, FILE *out_file) {
int i, j, k;
element *el;
category *aux = head->categorie;
for (i = 0; i < head->category_length; i++) {
fprintf(out_file, "%s,", aux->nume);
aux = aux->next;
}
fprintf(out_file, "\n");
for (i = 0; i < level; i++) {
aux = head->categorie;
for(j = 0; j < head->category_length; j++) {
el = aux->e;
for(k = 0; k < i; k++)
el = el->next;
fprintf(out_file, "%s,", el->nume);
aux = aux->next;
}
if (i < level - 1)
fprintf(out_file, "\n");
}
}