Cod sursa(job #1359674)

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

#define BRACKET 34

void nfprint(char *ptr, int len);
void parse(char **ptr1, char **ptr2);

int main(){
	short int i, quote, key, dots, counter = 0;
	char *ptr1, *ptr2, *str, *value, comma[2];	
	comma[0] = ','; comma[1] = '\0';
	FILE *pFile = fopen("convertor.in", "rt");
	freopen("convertor.out", "wt", stdout);
	
	//assert (pFile != NULL);

	str = malloc (1025);
	value = malloc (520);

	do{
		fgets(str, 1025, pFile);

		quote = 0;
		dots = 0;

		//puts(str);

		for (i = 0; i < strlen(str); ++i){
			//printf("nada");
			if (str[i] == '}'){
				if (dots){
					ptr2 = str+i;

					/*printf("1. ptr1 ");
					puts(ptr1);
					printf("1. ptr2 ");
					puts(ptr2);*/

					parse (&ptr1, &ptr2);

					/*printf("2. ptr1 ");
					puts(ptr1);
					printf("2. ptr2 ");
					puts(ptr2);*/

					strncat(value, ptr1, ptr2 - ptr1 + 1);
					strncat(value, comma, 2);
				}

				printf("\n%s", value);
				memset(value, 0, sizeof(value));

				quote = 0;
				counter++;
			}
			else if (str[i] == '"') // ASCII 34 - "
				++quote;
			else if (str[i] == ':'){ // ASCII 58 - :
				dots = 1;

				ptr1 = str+i;
			}
			else if (str[i] == ',' || str[i] == '\n' || str[i] == '\0'){ // ASCII 44 - ,
				if (dots){
					ptr2 = str+i;

					/*printf("1. ptr1 ");
					puts(ptr1);
					printf("1. ptr2 ");
					puts(ptr2);*/

					parse (&ptr1, &ptr2);

					/*printf("2. ptr1 ");
					puts(ptr1);
					printf("2. ptr2 ");
					puts(ptr2);*/

					strncat(value, ptr1, ptr2 - ptr1 + 1);
					strncat(value, comma, 2);
				}

				dots = 0;
				quote = 0;
			}
			
			if (!dots && !counter){
				if ((quote == 1) && (str[i] == '"')){
					ptr1 = str+i;

				}
				else if ((quote == 2) && (str[i] == '"')){
					ptr2 = str+i;
					
					nfprint(ptr1 + 1, ptr2 - ptr1 - 1);

					quote = 0;
				}
			}
		}
		//puts(str);
	}
	while( !feof(pFile) );

	fclose(pFile);

	printf("\n");

	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 == 0) || (**ptr1 == '{') || (**ptr1 == '}') || (**ptr1 == ',') || (**ptr1 == ':') || (**ptr1 == ' ') || (**ptr1 == '"') || (**ptr1 == '\n') || (**ptr1 == '\0') ) )
		(*ptr1)++;

	while ( ( (**ptr2 == 0) || (**ptr2 == '{') || (**ptr2 == '}') || (**ptr2 == ',') || (**ptr2 == ':') || (**ptr2 == ' ') || (**ptr2 == '"') || (**ptr2 == '\n') || (**ptr2 == '\0') ) )
		(*ptr2)--;
}