Cod sursa(job #1357526)

Utilizator monicalegendaLegenda Monica monicalegenda Data 23 februarie 2015 22:49:41
Problema Convertor Scor 0
Compilator c Status done
Runda rosedu_cdl_2015 Marime 2.62 kb
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BLSIZE 2048

void frumos(char *s, char *delim)
{
	int i, j, n, nr_gh = 0;
	n = strlen(s);
	for (i = 0; i < n; i++) {
		if (s[i] == '"')
			nr_gh++;
		if (strchr(delim, s[i]) || (nr_gh != 1 && s[i] == ' ')) {
			for (j = i; j < n; j++)
				s[j] = s[j + 1];
			n--;
			i--;
		}
	}
}


int main()
{
	FILE *input = fopen("convertor.in", "rb");
	FILE *output = fopen("convertor.out", "w");
	int i, nr = 0, size, jum = 0, chei = 0, lungime, ok;
	char *array2, array[BLSIZE], delim[] = "\n}{][\"", *rest, key[2 * BLSIZE], prima[2 * BLSIZE];
	fseek(input, 0, SEEK_END);
	size = ftell(input);
	fseek(input, 0, SEEK_SET);
	while (fread(array, 1, BLSIZE, input)) {
		while (array && !chei) {
			rest = strchr(array, ':');
			if (!rest) {
				memset(prima, 0, 2 * BLSIZE);
				memcpy(prima, array, BLSIZE);
				jum = 1;

			} else {
				lungime = rest - array;
				if (lungime) {
					memset(key, 0, 2 * BLSIZE);
					memcpy(key, array, lungime);
					if (jum) {
						strncat(prima, array, lungime);
						memcpy(key, prima,
						       strlen(prima));

						jum = 0;
					}
					if (strchr(key, '{')
					    && !strchr(key, '[')) {
						chei = 1;
						printf("\n");
						break;
					}
					
					frumos(key, delim);
					if (key) {
						nr ++;
						printf("%s,", key);
					}
				}
			}
			array2 = strchr(array, ',');
			if (array2)
				strcpy(array, array2 + 1);
			else
				break;
		}

		memset(array, 0, BLSIZE);
	}
	fseek(input, 0, 0);
	i = 0;
	ok = 1;
	chei = 0;
	while (fread(array, 1, BLSIZE, input)) {
			rest = strchr(array, ':');
			while(rest) {
				i ++;
				while(!strchr("\"-0123456789", *rest)) {
					rest ++;
					if(rest - array == BLSIZE) {
						memset(array, 0, BLSIZE);
						ok = fread(array, 1, BLSIZE, input);
						rest = array;
					}
				}
				if(*rest == '"') {
					rest ++;
					if(rest - array == BLSIZE) {
							memset(array, 0, BLSIZE);
							ok = fread(array, 1, BLSIZE, input);
							rest = array;
					}
					while(*rest != '"') {
						printf("%c", *rest);
						rest ++;
						if(rest - array == BLSIZE) {
							memset(array, 0, BLSIZE);
							ok = fread(array, 1, BLSIZE, input);
							rest = array;
						}
					}
				}
				else {
					while(strchr("-0123456789", *rest)) {
						printf("%c", *rest);
						rest ++;
						if(rest - array == BLSIZE) {
							memset(array, 0, BLSIZE);
							ok = fread(array, 1, BLSIZE, input);
							rest = array;
						}
					}
				}
				printf(",");
				if(!(i %  nr)) {
					printf("\n");					
				}
				if(!ok)
					break;
				rest = strchr(rest, ':');
			}
		if(!ok)
			break;				
	}
	fclose(output);
	fclose(input);
	return 0;
}