Cod sursa(job #1360575)

Utilizator TwistedFaithStanescu Jean Alexandru TwistedFaith Data 25 februarie 2015 16:32:50
Problema Convertor Scor 30
Compilator c Status done
Runda rosedu_cdl_2015 Marime 2.78 kb
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

void nfprint(char *ptr, int len);
void parse(char **ptr1, char **ptr2);
char * otherParse(char **ptr1);
void solveKeys(FILE *pFile);
void solveValues(FILE *pFile);

void fie(){
	int i;

	for (i = 0; i < 10; ++i)
		printf("%d", i);
}

int main(){
	/* Open input file */
	FILE *pFile = fopen("convertor.in", "rt");

	/* Redirect output from stdout to convertor.out */
	freopen("convertor.out", "wt", stdout);

	assert (pFile != NULL);

	solveKeys(pFile);
	rewind(pFile);
	solveValues(pFile);

	fclose(pFile);
	return 0;
}

void nfprint(char *ptr, int len){
	int i;

	for (i = 0; i < len; ++i)
		printf("%c", ptr[i]);

	printf(",");
}

void parse(char **ptr1, char **ptr2){
	while ( !(((**ptr1 >= 48) && (**ptr1 <= 57)) || ((**ptr1 >= 65) && (**ptr1 <= 90)) || ((**ptr1 >= 97) && (**ptr1 <= 122))) )
		(*ptr1)++;

	while ( !(((**ptr2 >= 48) && (**ptr2 <= 57)) || ((**ptr2 >= 65) && (**ptr2 <= 90)) || ((**ptr2 >= 97) && (**ptr2 <= 122))) )
		(*ptr2)--;
}

char *otherParse(char **ptr1){
	char *ptr2;

	while ( !(((**ptr1 >= 48) && (**ptr1 <= 57)) || ((**ptr1 >= 65) && (**ptr1 <= 90)) || ((**ptr1 >= 97) && (**ptr1 <= 122))) )
		(*ptr1)++;

	ptr2 = *ptr1;

	while ( (((*ptr2 >= 48) && (*ptr2 <= 57)) || ((*ptr2 >= 65) && (*ptr2 <= 90)) || ((*ptr2 >= 97) && (*ptr2 <= 122))) )
		ptr2++;

	return ptr2;
}

void solveKeys(FILE *pFile){
	char *ptr1, *ptr2, *ptr3, *ptr4, *ptr5, *str;

	str = (char*) malloc (1024);

	do{
		fgets(str, 1024, pFile);

		ptr1 = strchr(str, ':');
		ptr2 = strchr(str, ',');
		ptr3 = strchr(str, '}');

		while( ((ptr1 < ptr3) || (!ptr3)) && (ptr1) ){
			if( (!ptr2) || (ptr1 < ptr2) )
				ptr2 = str;

			ptr4 = ptr2;
			ptr5 = ptr1;

			parse(&ptr4, &ptr5);
			
			nfprint(ptr4, ptr5 - ptr4 + 1);

			ptr1 = strchr(ptr1 + 1, ':');
			ptr2 = strchr(ptr2 + 1, ',');
		}
	}
	while(!ptr3);

	printf("\n");

	free(str); 
}

void solveValues(FILE *pFile){
	char *ptr1, *ptr2, *ptr3, *ptr4, *ptr5, *str;
	int dots = 0;

	str = (char*) malloc (1024);

	do{
		fgets(str, 1024, pFile);

		if (dots){
			ptr3 = strchr(str, '"');
			if(ptr3){
				ptr1 = str;

				ptr2 = otherParse(&ptr1);

				nfprint(ptr1, ptr2 - ptr1);

				dots = 0;
			}
		}

		ptr1 = strchr(str, ':');
		ptr2 = strchr(str, ',');
		ptr3 = strchr(str, '}');

		while( (ptr1) ){
			while( (ptr2) && (ptr1 > ptr2) )
				ptr2 = strchr(ptr2 + 1, ',');

			if(!ptr2)
				ptr2 = str + strlen(str) - 1;

			ptr4 = ptr1;
			ptr5 = ptr2;

			parse(&ptr4, &ptr5);
			
			if (ptr4 > ptr5)
				dots = 1;
			else
				nfprint(ptr4, ptr5 - ptr4 + 1);

			ptr1 = strchr(ptr1 + 1, ':');
			ptr2 = strchr(ptr2 + 1, ',');

			if ((ptr1 > ptr3) && (ptr3)){
				printf("\n");
				ptr3 = strchr(ptr3 + 1, '}');
			}
		}

		if ( (!ptr1) && (ptr3) )
			printf("\n");
	}
	while( !feof(pFile) );

	free(str);
}