Cod sursa(job #1359235)

Utilizator monicalegendaLegenda Monica monicalegenda Data 24 februarie 2015 21:44:07
Problema Convertor Scor 100
Compilator c Status done
Runda rosedu_cdl_2015 Marime 4.12 kb
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BLSIZE 2000000
int count(FILE *, char, int *);
int main()
{
 
    FILE *input = fopen("convertor.in", "rb");
    FILE *output = fopen("convertor.out", "w");
    int i, j, nr = 0, crt_t = 0, crt_c, ok, chei;
    char *array, *p, *rest;
    int poz[1000];
    memset(poz, 0, 1000 * sizeof(int));
    nr = count(input, '}', poz);   
    array = calloc(poz[1], 1);
    fread(array, 1, poz[1], input);
    p = strchr(array, ':');
	nr = 0;
    while (p) {
		nr ++;
        crt_t ++;
        i = 0;
        while (p[i] != '\"')
            i--;
        i--;
        while (p[i] != '\"')
            i--;
        i++;
        while (p[i] != '\"') {
            fprintf(output, "%c", p[i]);
            i++;
        }
        fprintf(output, ",");
        p = strchr(p + 1, ':');
 
    }
    fprintf(output, "\n");
    fseek(input, 0, 0);
    free(array);
	i = 0;
	ok = 1;
    chei = 0;
	array = calloc(BLSIZE, 1);
    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;
	}
      
    /*crt_c = 0;
    for(i = 1; i < nr; i++) {
        array = calloc(poz[i] - poz[i - 1], 1);
        fread(array, 1, poz[i] - poz[i - 1], input);       
        p = strchr(array, ':');
        while (p) {
            crt_c ++;
            j = 1;
            while (p[j] <= 32)
                j++;
            if (p[j] == '\"') {
                j++;
                while (p[j] != '"') {
                    fprintf(output, "%c", p[j]);
                    j++;
                }
            } else {
                while (strchr("0123456789-", p[j])) {
                    fprintf(output, "%c", p[j]);
                    j++;
                }
            }
            fprintf(output, ",");
            if(crt_c % crt_t == 0)
                fprintf(output, "\n");
            p = strchr(p + 1, ':');
        }
        free(array);       
    }
    printf("%d\n", nr);*/
    fclose(input);
    fclose(output);
    return 0;
}
 
int count(FILE * f, char c, int *v)
{
    fseek(f, 0, 0);
    int nr = 1, poz = 0, len;
    char buff[BLSIZE], *p;
    while (fread(buff, 1, BLSIZE, f)) {
        p = strchr(buff, c);
        while (p) {
                v[nr] = poz + (p - buff);
                nr++;              
                p = strchr(p + 1, c);
        }
        poz += BLSIZE;
         
    }
    fseek(f, 0, 0);
    return nr;
}