Cod sursa(job #1360732)

Utilizator TwistedFaithStanescu Jean Alexandru TwistedFaith Data 25 februarie 2015 17:32:34
Problema Convertor Scor 0
Compilator c Status done
Runda rosedu_cdl_2015 Marime 3.28 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);

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){
	register short int i;

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

	printf(",");
}

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

		(*ptr1)++;
	}

	while ( (*ptr1 < *ptr2) && !(((**ptr2 >= 48) && (**ptr2 <= 57)) || ((**ptr2 >= 65) && (**ptr2 <= 90)) || ((**ptr2 >= 97) && (**ptr2 <= 122))) ){
		if (**ptr2 == '"'){
			(*ptr2)--;
			break;
		}

		(*ptr2)--;
	}
}

char *otherParse(char **ptr1){
	register char *ptr2, *ptr3;
	ptr3 = strchr(*ptr1, '"');

	while ( !(((**ptr1 >= 48) && (**ptr1 <= 57)) || ((**ptr1 >= 65) && (**ptr1 <= 90)) || ((**ptr1 >= 97) && (**ptr1 <= 122))) ){
		if (**ptr1 == '"'){
			(*ptr1)++;
			break;
		}

		(*ptr1)++;
	}

	ptr2 = strchr(*ptr1, '"');
	
	if( !((ptr2 > ptr3) && (ptr2) && (ptr3)) ){
		ptr2 = *ptr1;

		while ( (*ptr2 >= 48) && (*ptr2 <= 57) )
			ptr2++;
	}
	

	return ptr2;
}

void solveKeys(FILE *pFile){
	register char *ptr1, *ptr2, *ptr3, *ptr4, *ptr5;
	char *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); 
}

short int validCharacters(char *ptr){
	register short int i;

	for (i = 0; i < strlen(ptr); ++i)
		if ( ((ptr[i] >= 48) && (ptr[i] <= 57)) || ((ptr[i] >= 65) && (ptr[i] <= 90)) || ((ptr[i] >= 97) && (ptr[i] <= 122)) )
			return 1;

	return 0;
}

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

	str = (char*) malloc (1024);

	do{
		fgets(str, 1024, pFile);

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

		if (validCharacters(str)){
			if (dots){
				ptr4 = str;
				ptr5 = otherParse(&ptr4);

				nfprint(ptr4, ptr5 - ptr4);
				dots = 0;
			}
			
			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 ( !validCharacters(ptr1) )
					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);
}