Cod sursa(job #1357572)

Utilizator monicalegendaLegenda Monica monicalegenda Data 23 februarie 2015 23:24:35
Problema Convertor Scor 90
Compilator c Status done
Runda rosedu_cdl_2015 Marime 2.45 kb
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BLSIZE 1000000

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, poz = 0;
	char *array2, array[BLSIZE], delim[] = "\n}{][\"", *rest, *p;
	fseek(input, 0, SEEK_END);
	size = ftell(input);
	fseek(input, 0, SEEK_SET);
	while (fread(array, 1, BLSIZE, input)) {
		if(strchr(array, '}'))
			poz += strchr(array, '}') - array;
		else
			poz += BLSIZE;
	}
	fseek(input, 0, SEEK_SET);
	
	array2 = calloc(poz, sizeof(char));
	fread(array2, 1, poz, input);
	rest = strchr(array2, ':');
	while(rest) {
		p = rest;
		for (i = 0; ; i--)
			if(p[i] == '"')
				break;
		i --;
		for (; ; i--)
			if(p[i] == '"')
				break;
		i ++;
		for(; ; i++) {
			if(p[i] == '"')
				break;
			else
				fprintf(output, "%c", p[i]);
		}
		fprintf(output, ",");
		nr ++;
		rest = strchr(rest + 1, ':');
		
	}
	fprintf(output, "\n");
	free(array2);
	
	fseek(input, 0, SEEK_SET);
	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 != '"') {
						fprintf(output, "%c", *rest);
						rest ++;
						if(rest - array == BLSIZE) {
							memset(array, 0, BLSIZE);
							ok = fread(array, 1, BLSIZE, input);
							rest = array;
						}
					}
				}
				else {
					while(strchr("-0123456789", *rest)) {
						fprintf(output, "%c", *rest);
						rest ++;
						if(rest - array == BLSIZE) {
							memset(array, 0, BLSIZE);
							ok = fread(array, 1, BLSIZE, input);
							rest = array;
						}
					}
				}
				fprintf(output, ",");
				if(!(i %  nr)) {
					fprintf(output, "\n");					
				}
				if(!ok)
					break;
				rest = strchr(rest, ':');
			}
		if(!ok)
			break;				
	}
	fclose(output);
	fclose(input);
	return 0;
}