Cod sursa(job #1344713)

Utilizator theo.stoicanTheodor Stoican theo.stoican Data 16 februarie 2015 22:25:13
Problema Convertor Scor 100
Compilator c Status done
Runda rosedu_cdl_2015 Marime 1.73 kb
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.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 *) 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 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, ",");
			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;
}