Cod sursa(job #1343057)

Utilizator theo.stoicanTheodor Stoican theo.stoican Data 14 februarie 2015 20:29:39
Problema Convertor Scor 100
Compilator c Status done
Runda rosedu_cdl_2015 Marime 2.84 kb
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){
	FILE *f, *fout;
	char *p, *p1, *p2, *p3, **tok1, **tok2, **tok3, *copyp;
	int i, ok, len;
	copyp = (char*) malloc (10000 * 1024 * 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");
	if (f == NULL || fout == NULL ) exit(1);
	fseek (f, 0, SEEK_END);
	int filedim = ftell (f);
	char *wholefile = (char *) malloc (filedim * sizeof(char));
	fseek (f,0,SEEK_SET);
	//fread (wholefile, filedim, 1, f);
	//puts (wholefile);
	if(fread (wholefile, filedim, 1, f) == 1)
	{
		//puts(wholefile);
	//extract the objects
	p = strchr (wholefile, '}');
	strncpy (copyp, wholefile, p - wholefile);
	//puts (copyp);
	p2 = strchr (copyp, '"');
	//puts (p2);
	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;
	}		
	/*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)
			{
				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, ",");
	}
	/*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, "}");
	}*/
	}
	else exit(1);
	fclose(f);
	fclose (fout);
	return 0;
}