Cod sursa(job #1342760)

Utilizator theo.stoicanTheodor Stoican theo.stoican Data 14 februarie 2015 15:02:51
Problema Convertor Scor 60
Compilator c Status done
Runda rosedu_cdl_2015 Marime 2.32 kb
#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);
	fread (wholefile, sizeof(char),filedim, f);
	//extract the objects
	p = strchr (wholefile, '}');
	strncpy (copyp, wholefile, p - wholefile);
	//puts (copyp);
	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, ",");
	while (p != NULL)
	{
		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, '}')) 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;
}