Cod sursa(job #1341780)

Utilizator theo.stoicanTheodor Stoican theo.stoican Data 13 februarie 2015 03:01:34
Problema Convertor Scor 0
Compilator c Status done
Runda rosedu_cdl_2015 Marime 6.22 kb
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

int main()
{
	int i, j, dim = 0, iterate = 0;
	FILE *f, *fout;
	char *line, *p, *x, *line2, *searchcomma, *searchkeycopy, *searchkeycopy2, *searchobj, *copyobj, *searchquote, **obj, **tok, **tok1, *searchkey, *copyp, **tok2;
	tok1 = (char **) malloc (1025 * sizeof (char*));
	obj = (char**) malloc (1025 * sizeof (char*));
	tok = (char**) malloc (1025 * sizeof (char*));
	tok2 = (char **) malloc (1025 * sizeof (char*));
	searchkey = (char*) malloc (1025 * sizeof (char));
	searchcomma = (char*) malloc (1025 * sizeof (char));
	searchquote = (char*) malloc (1025 * sizeof (char));
	for (i = 0; i <= 1025; i++)
	{
		tok2[i] = (char*) malloc (30 * sizeof (char));
		tok1[i] = (char*) malloc (30*sizeof(char));
		tok[i] = (char*) malloc (30 * sizeof (char));
		obj[i] = (char*) malloc (30 * sizeof (char));
	}
		searchobj = (char*)malloc(1025 * sizeof(char));
	searchquote = (char*) malloc (1025 * sizeof(char));
	searchkey = (char*) malloc (1025 * sizeof (char)); 
	line = (char*)malloc(1025*sizeof(char));
	f = fopen ("convertor.in","r");
	fout =  fopen ("convertor.out", "w");
	while (fgets (line, 1024, f))
	{
	//	puts(line);
	}
	rewind(f);
//	printf ("\n\n");
//	while (!feof(f))
//	{
		fgets (line, 1025, f);
		//puts(line);
		p = strchr(line, '{');
		if (p != NULL)
		{
			searchobj = strtok_r (line, ",", tok);
			//puts(line);
			//puts(searchobj);
			while (searchobj != NULL)
			{
				//puts (searchobj);
				copyobj = strdup (searchobj);
				searchquote = strtok (copyobj, "\"");
				searchquote = strtok (NULL, "\"");
				if (searchquote == NULL) break;
				dim ++;
				//char *buff = (char*)malloc (20*sizeof(char));
				//strcpy (obj
				//(obj + dim - 1) = (char*)strdup (searchquote);
				obj[dim-1] = strdup (searchquote);
				//puts(obj[dim-1]);
				searchobj = strtok_r (NULL, ",", tok);
//				puts(searchobj);
			}
//			break;
		}
		else
		{
			p = strtok_r (line, "," ,tok);
			while (p != NULL)
			{
				//puts (searchobj);
				copyobj = strdup (p);
				searchquote = strtok (copyobj, "\"");
				searchquote = strtok (NULL, "\"");
				if (searchquote == NULL) break;
				dim ++;
				obj[dim -1] = strdup (searchquote);
				//puts (obj[dim-1]);
				p = strtok_r (NULL, ",", tok);
			}
		}
//		break;
//	}
	for ( i = 0; i <= dim - 1; i++)
		printf ("%s,", obj[i]);
	printf ("\n");
	// I've found the objects, now i'm looking for the keys
	rewind(f);
	dim = 0;
//	printf ("here we go\n");
	while (fgets (line, 1000, f))
	{
		//puts (line);
		line2 = strdup (line);
		x = strchr (line2, '{');
		//copyp = strdup (p);
		//puts(p);
		//iterate = 0;
		if (x != NULL)
		{	
			p = strtok (line, "{");
		//	puts (p);
			while (p != NULL)
			{
				copyp = strdup (p);
		//		puts (copyp);
				//iterate ++;
			//	if (iterate >= 2)
			//	{
				//	for (i = 0; i <= dim-1; i++)
				//		puts(obj[i]);
			//		dim = 0;
			//	}
			//	puts (p);
				searchkeycopy2 = strdup (p);
				if (strchr (searchkeycopy2, ':') != NULL)
				{
					searchkey = strtok_r (copyp, ":", tok1);
					searchkey = strtok_r (NULL, ":", tok1);
				//	puts (searchkey);
				}
				else
				{
					p = strtok (NULL, "{");
					continue;
				}
			//	puts (searchkey);

				// it can be the beginning of the json file, where we have just "["
				// so we won't find any ":", which will lead to seg fault 
				// if we continue
	//			if (searchkey != NULL)
	//			{
	//				searchkey = strtok_r (NULL, ":", tok1);
			//		puts (searchkey);
	//			}
	//			else 
	//			{
	//				p = strtok (NULL, "{");
	//				continue;
	//			}
				//puts (searchkey);
				while (searchkey != NULL)
				{
		//			puts (searchkey);
					searchkeycopy = strdup (searchkey);
					searchcomma = strtok_r (searchkeycopy, ",", tok);
				//	puts (searchcomma);
					if (strchr (searchcomma, '"') != NULL)
					{
						searchquote = strtok_r (searchcomma, "\"", tok2);
						searchquote = strtok_r (NULL, "\"", tok2);
					//	puts (searchquote);
						dim ++;
				//		obj[dim - 1] = strdup (searchquote);
						strcpy (obj[dim - 1], searchquote);
				//		puts (obj[dim - 1]);
					}
					else
					{
						dim ++;
					//	x = strdup (searchcomma);
					//	obj [dim - 1] = strdup (searchcomma);
						j = 0;
						for (i = 0; i < strlen (searchcomma); i++)
							if (isdigit (searchcomma[i]) != 0) 
							{
							//	printf ("\n%c\n", searchcomma[i]);
								j++;		
								obj[dim-1][j-1] = searchcomma[i];
							}
						j++;
						obj[dim-1][j-1] = '\0';
					//	strcpy (obj[dim-1], searchcomma);
				//		puts (searchcomma);
					}
					if (strchr (searchkey, '}') != NULL)
					{
						for (i = 0; i <= dim - 1; i++)
						{
						//	if (obj[i][strlen(obj[i])-1] == '\n')
						//		obj[i][strlen(obj[i])-1] = '\0';
						//	for (j = 0; j < strlen (obj[i]); j++)
							//		if(obj[i][j] != ' ') printf("%c",obj[i][j]);
							printf ("%s,",obj[i]);
						//	printf(",");
						}
						printf ("\n");
						dim = 0;
						break;
					}
					else searchkey = strtok_r (NULL, ":", tok1);
				}
				p = strtok (NULL, "{");
				//puts (p);
			}
	//	        if (dim > 0)	
	//		{	
		//		for (i = 0; i <= dim - 1; i++)
		//			puts (obj[i]);
	//		}
		}
		else
		{
			if (strchr (line, ':') != NULL)
			{
				searchkey = strtok_r (line, ":", tok1);
				searchkey = strtok_r (NULL, ":", tok1);
			}
			else
				continue;
			while (searchkey != NULL)
			{
				searchkeycopy = strdup (searchkey);
				searchcomma = strtok_r(searchkeycopy, ",", tok);
				if (strchr (searchcomma, '"') != NULL)
				{
					searchquote = strtok_r (searchcomma, "\"", tok2);
					searchquote = strtok_r (NULL, "\"", tok2);
					dim ++;
				//	obj[dim-1] = strdup(searchquote);
					strcpy (obj[dim-1], searchquote);
				}
				else
				{
					dim ++;
					j = 0;
					for (i = 0; i < strlen (searchcomma); i++)
						if (isdigit(searchcomma[i]) != 0)
						{
							j++;
							obj[dim - 1][j-1] = searchcomma[i];
						}
					j++;
					obj[dim - 1][j - 1] = '\0';
					//obj[dim - 1] = strdup (searchcomma+1);
					//strcpy (obj[dim-1],searchcomma);
				}
				if (strchr (searchkey, '}') != NULL)
				{
					for (i = 0; i <= dim - 1; i++)
						printf("%s,",obj[i]);
					printf("\n");
					dim = 0;
				}
				searchkey = strtok_r (NULL, ":", tok1);
			}
		}
	}
	//printf("OVER HERE\n");
	//for (i = 0; i <= dim - 1; i++)
	//	puts (obj[i]);
	fclose (f);
	fclose (fout);
	return 0;
}