#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int numar_aparitii(char *string, char *token)
{
int nr = 0;
for(string = strstr(string, token); string; string = strstr(string + strlen(token), token))
{
nr++;
}
return nr;
}
int numar_linii(FILE *input)
{
char c;
int numar = 1;
c = fgetc(input);
while(c != EOF)
{
if(c == '\n')
{
numar++;
}
c = fgetc(input);
}
return numar;
}
void creare_string(FILE *input, char *string)
{
int i = 0;
char c;
c = fgetc(input);
while( c != EOF )
{
string[i] = c;
i++;
c = fgetc(input);
}
}
int numar_chei(FILE *input)
{
char c;
int nr = 0;
c = fgetc(input);
while(c != '}')
{
if(c == ':')
{
nr++;
}
c = fgetc(input);
}
return nr;
}
void introducere_chei(FILE *input, FILE *output, char *string, int nr_chei, int nr_linii)
{
char c, str[100], aux[20], *token;
int i, j, numar_introd_in_output = 0;
fgets(str,100,input);
token = strtok(str, "\"");
while(token != NULL)
{
if(numar_aparitii(string, token) != 1)
{
if(!isalpha(token[0]))
{
;
}
else
{
fprintf(output, "%s,", token);
numar_introd_in_output++;
}
}
token = strtok(NULL, "\"");
}
if(numar_introd_in_output != nr_chei)
{
fgets(str, 100, input);
token = strtok(str, "\"");
while(token != NULL)
{
if(numar_aparitii(string, token) != 1)
{
if(!isalpha(token[0]))
{
;
}
else
{
fprintf(output, "%s,", token);
numar_introd_in_output++;
}
}
if(numar_introd_in_output == nr_chei)
{
fprintf(output, "%s", "\n");
break;
}
token = strtok(NULL, "\"");
}
}
}
void introducere_elemente(FILE *input, FILE *output, char *string, int nr_chei, int nr_linii)
{
char str[100], *token;
//char aux[20] = {':','{','}','[',']','\n',','};
int numar = 0, i, j;
for(i = 0; i < nr_linii; i++)
{
fgets(str, 100, input);
token = strtok(str, "\":}]{[,");
while(token != NULL)
{
if(numar_aparitii(string, token) == 1)
{
if(token[0] == ' ')
{
strcpy(token, strrev(strrev(token + 1)));
}
if(token[strlen(token) - 1] == ',')
{
strcpy(token, token + strlen(token));
}
fprintf(output, "%s,", token);
numar++;
if(numar == nr_chei)
{
fprintf(output, "%s", "\n");
numar = 0;
}
}
token = strtok(NULL, "\":}]{[,");
}
}
}
int main()
{
FILE *input, *output;
char *string;
int nr_linii, nr_chei, nr_carac, nr_cuv, nr_elem, numar, i;
//------->Numar de caractere din fisierul input
input = fopen("convertor.in", "rb");
if(input == NULL)
{
printf("Eroare la deschidere!");
exit(1);
}
fseek(input, 0L, SEEK_END);
nr_carac = ftell(input);
fclose(input);
//------>deschidere
input = fopen("convertor.in", "rt");
if(input == NULL)
{
printf("Eroare la deschidere!");
exit(1);
}
output = fopen("convertor.txt", "wt");
if(output == NULL)
{
printf("Eroare la deschidere!");
exit(1);
}
//-------->Creare string
string = malloc(sizeof(char) * nr_carac);
if(string == NULL)
{
printf("Alocare dinamica esuata!");
exit(1);
}
creare_string(input, string);
rewind(input);
//--------->Numarul de linii din fisier
nr_linii = numar_linii(input);
rewind(input);
//--------->Numarul de chei
nr_chei = numar_chei(input);
rewind(input);
//--------->
introducere_chei(input, output, string, nr_chei, nr_linii);
rewind(input);
introducere_elemente(input, output, string, nr_chei, nr_linii);
fclose(input);
fclose(output);
getch();
return 0;
}