Cod sursa(job #1357024)

Utilizator razvan3895Razvan-Mihai Chitu razvan3895 Data 23 februarie 2015 18:30:21
Problema Convertor Scor 100
Compilator c Status done
Runda rosedu_cdl_2015 Marime 2.83 kb
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define TTL 2048    //Total
#define HLF TTL/2   //Half
 
int blk_read(char*, FILE*);
 
int main() {
    FILE *f = fopen("convertor.in", "r"), *g = fopen("convertor.out", "w");
    char *line1, *c, a[100], b[100], *p;
    int i, ok = 0, nr = 0, k, test = 0;
    line1 = calloc(TTL, 1);
    memset(a, 0, 100);
    memset(b, 0, 100);
    while(fread(line1 + HLF, 1, HLF, f)) {
        c = strchr(line1 + HLF, ':');
        while(c) {
            for(i = 0; c[i] != '"'; i--);
            p = c + i - 1;
            for(i = 0; p[i] != '"'; i--);
            p = p + i + 1;
            if(!nr) {
                strncpy(a, p, -i);
                nr ++;
                fprintf(g, "%s,", a);
            }
            else {
                strncpy(b, p, -i);
                if(strcmp(a, b) == 0) {
                    ok = -1;
                    break;
                }
                nr ++;
                fprintf(g, "%s,", b);
                memset(b, 0, 100);
            }
            c = strchr(c + 1, ':');        
        }
        memcpy(line1, line1 + HLF, HLF);
        memset(line1 + HLF, 0, HLF);
        if(ok == -1)
            break;
    }
    fprintf(g, "\n");
    fseek(f, 0, 0);
    k = 0;
    ok = fread(line1, 1, TTL, f);
    while(ok || test) {
        c = strchr(line1, ':');
        while(c && c < line1 + HLF) {
            for(c = c + 1; *c < 33; c++) {
                if(c - line1 >= TTL) {
                    ok = blk_read(line1, f);
                    c = line1 + HLF - 1;
                }
            }
            if(*c == '"') {
                c ++;
                while(*c != '"') {
                    fprintf(g, "%c", *c);
			if(*c == '}') fprintf(g, "WOLOLOLOLO\n");             
                    c ++;
                    if(c - line1 >= TTL) {
                        ok = blk_read(line1, f);
                        c = line1 + HLF;
                    }
                }
            }  
            else
                while(strchr("0123456789-", *c)) {
                    fprintf(g, "%c", *c);              
                    c ++;
                    if(c - line1 >= TTL) {
                        ok = blk_read(line1, f);
                        c = line1 + HLF;
                    }
                }
            k++;
            fprintf(g, ",");
            if(k == nr) {
                k = 0;
                fprintf(g, "\n");
            }
            c = strchr(c, ':');
        }
        ok = blk_read(line1, f);
        if(test)
            break;
        if(!ok) {
            test = 1;
            memcpy(line1 + HLF, line1, HLF);
        }
    }  
    free(line1);
    fclose(f);
    fclose(g);             
    return 0;
}
 
int blk_read(char *line, FILE *f) {
    memcpy(line, line + HLF, HLF);
    memset(line + HLF, 0, HLF);
    return fread(line + HLF, 1, HLF, f);
}