#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
void extract_keys (char*, int, FILE*);
int main(){
FILE *f, *fout;
char *p, *p1, *p2, *copyp, *wholefile;
int i, len, filedim;
//copyp = (char*) malloc (10128 * 1024 * sizeof (char));
f = fopen ("convertor.in", "r");
fout = fopen("convertor.out","w");
//if (f == NULL || fout == NULL ) exit(1);
fseek (f, 0, SEEK_END);
filedim = ftell (f);
wholefile = (char *) calloc (filedim + 1, sizeof(char));
copyp = (char*) calloc (filedim + 1, sizeof (char));
// memset (wholefile, 0, filedim);
// memset (copyp, 0, filedim);
fseek (f,0,SEEK_SET);
if (fread (wholefile, filedim, 1, f) == 1)
{
//extract the keys
extract_keys (wholefile, filedim, fout);
/*
p = strchr (wholefile, '}');
strncpy (copyp, wholefile, p - wholefile);
p2 = strchr (copyp, '"');
while (p2 != NULL)
{
for (i = 1; p2[i] != '"'; i++)
fprintf (fout, "%c", p2[i]);
fprintf (fout, ",");
//if (strchr (p2, '}') != NULL) break;
p = strchr (p2, ',');
if (p != NULL)
p2 = strchr (p, '"');
else break;
}
fprintf (fout,"\n");*/
//extract the keys
p = strtok (wholefile, ",");
while (p != NULL)
{
p1 = strchr (p, ':');
if (p1 != NULL)
{
p2 = strchr (p1, '"');
if (p2 != NULL)
{
len = strlen (p2);
for (i = 1; i < len; i++)
if (p2[i] == '"') break;
else fprintf (fout,"%c", p2[i]);
}
else {
len = strlen (p1);
for (i = 0; i < len; i++)
if (isdigit (p1[i]) != 0)
fprintf (fout,"%c", p1[i]);
}
}
fprintf (fout,",");
if (p != NULL)
{
if (strchr (p, '}') != NULL) fprintf (fout,"\n");
p = strtok (NULL, ",");
}
}
}
//free (p1);
//free (copyp);
// free(f);
// free (fout);
//p2 = NULL;
//p = NULL;
//p1 = NULL;
free (wholefile);
free(copyp);
// free (p1);
fclose(f);
fclose (fout);
return 0;
}
void extract_keys (char *f, int dim, FILE *out_file)
{
char *p, *copyp, *p2;
int i;
copyp = (char*) calloc (dim + 1, sizeof(char));
p = strchr (f, '}');
strncpy (copyp, f, p - f);
p2 = strchr (copyp, '"');
while (p2 != NULL)
{
for (i = 1; p2[i] != '"'; i++)
fprintf (out_file, "%c", p2[i]);
fprintf (out_file, ",");
p = strchr (p2, ',');
if (p != NULL)
p2 = strchr (p, '"');
else break;
}
free (copyp);
}