Cod sursa(job #1344061)

Utilizator theo.stoicanTheodor Stoican theo.stoican Data 16 februarie 2015 12:04:30
Problema Convertor Scor 100
Compilator c Status done
Runda rosedu_cdl_2015 Marime 1.46 kb
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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 *) malloc (filedim * sizeof(char));
	copyp = (char*) malloc (filedim * sizeof (char));
	fseek (f,0,SEEK_SET);
	fread (wholefile, filedim, 1, f);
	//extract the objects
	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, ",");
		copyp = strchr (p2, ',');
		if (copyp != NULL)
			p2 = strchr (copyp, '"');
		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 (strchr (p, '}') != NULL) fprintf (fout,"\n");
		p = strtok (NULL, ",");
	}
	free (copyp);
	free (wholefile);
	fclose(f);
	fclose (fout);
	return 0;
}