Cod sursa(job #1342464)

Utilizator theo.stoicanTheodor Stoican theo.stoican Data 14 februarie 2015 01:27:31
Problema Convertor Scor 70
Compilator c Status done
Runda rosedu_cdl_2015 Marime 2.39 kb
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
	FILE *f, *fout;
	char *line, *p, *p1, *p2, *p3, **tok1, **tok2, **tok3, *copyp, *copyp1;
	int i, ok;
	tok1 = (char**) malloc (1000*sizeof(char*));
	tok2 = (char**) malloc (1000*sizeof (char*));
	tok3 = (char **) malloc (1000 * 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);
	}
	p1= (char *)malloc (1000*sizeof(char));
	p3 = (char*)malloc(1000*sizeof(char));
	p = (char*)malloc(1000*sizeof(char));
	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);
	//line = (char *) malloc (1025 * sizeof(char));
	fread (wholefile, sizeof(char),filedim, f);
	//extract the objects
	//puts (wholefile);
	p = strtok (wholefile, "}");
	copyp = strdup (p);
	p2 = strtok_r (copyp, ",", tok3);
	while (p2 != NULL)
	{
		p3 = strchr (p2, '"');
		for (i = 1; i < strlen (p3); i++)
		{
			if (p3[i] == '"') break;
			fprintf(fout, "%c", p3[i]);
		}
		fprintf (fout, ",");
		p2 = strtok_r (NULL, ",", tok3);
	}
	fprintf (fout,"\n");
	//extract the keys
//	p = strtok (wholefile, "}");
//	puts(p);
	while (p != NULL){
		//printf ("aici:");
	//	puts (p);
		strcpy (copyp, p);
		ok = 0;
		//puts (copyp);
		p1 = strtok_r (copyp, ",", tok1);
		//puts (p1);
	//	if (strchr (p, ',') != NULL)
	//	{
		//	puts (p);
			while (p1 != NULL){
			//	puts (p1);
				p3 = strchr (p1, ':');
				//puts(p3);
				if (p3 != NULL)
				{
					//puts (p3);
					ok = 1;
					if (strchr (p3, '"') != NULL){
						//copyp1 = strdup (p3);
						//p2 = strtok_r (copyp1, "\"", tok2);
						//p2 = strtok_r (copyp1, "\"", tok2);
						//p2 = strchr(copyp1
						//printf ("%s", p2);
						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{
					//	puts (p3);
						for (i = 0; i < strlen (p3); i++)
							if (isdigit (p3[i]) != 0)
								fprintf (fout,"%c", p3[i]);
					}	
					fprintf (fout, ",");
				}
				p1 = strtok_r (NULL, ",", tok1);
				//puts (p1);
			}
		//}
		if (ok == 1)
			fprintf (fout,"\n");
	//	puts (p);
		p = strtok (NULL, "}");
	}
	//free (line);
	fclose(f);
	fclose (fout);
	return 0;
}