#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(){
FILE *f, *fout;
char *p, *p1, *p2, *p3, **tok1, **tok2, **tok3, *copyp;
int i, ok;
copyp = (char*) malloc (1025 * sizeof (char));
//tok1 = (char**) malloc (1025*sizeof(char*));
//tok2 = (char**) malloc (1025*sizeof (char*));
tok3 = (char **) malloc (1025 * sizeof(char*));
for (i = 0; i < 1000; i++)
{
//tok1[i]=(char*)malloc(sizeof(char)*1025);
//tok2[i]=(char*)malloc(sizeof(char)*1025);
tok3[i]=(char*)malloc(sizeof(char)*1025);
}
f = fopen ("convertor.in", "r");
fout = fopen("convertor.out","w");
fseek (f, 0, SEEK_END);
int filedim = ftell (f);
char *wholefile = malloc (filedim * sizeof(char));
fseek (f,0,SEEK_SET);
if(fread (wholefile, sizeof(char),filedim, f) == filedim)
{
//extract the objects
p = strchr (wholefile, '}');
strncpy (copyp, wholefile, p - wholefile);
//puts (copyp);
p2 = (char *)strtok_r (copyp, ",", tok3);
while (p2 != NULL)
{
//if (p3 != NULL)
//{
p3 = strchr (p2, '"');
if (p3 != NULL)
{
for (i = 1; i < strlen (p3); i++)
{
if (p3[i] == '"') break;
fprintf(fout, "%c", p3[i]);
}
fprintf (fout, ",");
p2 =(char*) strtok_r (NULL, ",", tok3);
}
}
fprintf (fout,"\n");
//extract the keys
p = strtok (wholefile, ",");
while (p != NULL)
{
// puts (p);
p1 = strchr (p, ':');
if (p1 != NULL)
{
p2 = strchr (p1, '"');
if (p2 != NULL)
{
for (i = 1; i < strlen (p2); i++)
if (p2[i] == '"') break;
else fprintf (fout,"%c", p2[i]);
}
else {
for (i = 0; i < strlen (p1); i++)
if (isdigit (p1[i]) != 0)
fprintf (fout,"%c", p1[i]);
}
}
fprintf (fout,",");
if (strchr (p, '}') != NULL) fprintf (fout,"\n");
p = strtok (NULL, ",");
}
/*while (p != NULL){
strcpy (copyp, p);
ok = 0;
p1 = strtok_r (copyp, ",", tok1);
while (p1 != NULL){
p3 = strchr (p1, ':');
if (p3 != NULL)
{
ok = 1;
if (strchr (p3, '"') != NULL){
p2 = strchr (p3, '"');
for (i = 0; i < strlen (p2); i++)
{
if (i > 1 && p2[i] == '"') break;
if (p2[i] != '"') fprintf(fout,"%c",p2[i]);
}
}
else{
for (i = 0; i < strlen (p3); i++)
if (isdigit (p3[i]) != 0)
fprintf (fout,"%c", p3[i]);
}
fprintf (fout, ",");
}
p1 = strtok_r (NULL, ",", tok1);
}
if (ok == 1)
fprintf (fout,"\n");
p = strtok (NULL, "}");
}*/
}
fclose(f);
fclose (fout);
return 0;
}