Cod sursa(job #1355617)

Utilizator rvintilaVintila Ioan-Raul rvintila Data 22 februarie 2015 21:39:22
Problema Convertor Scor 100
Compilator c Status done
Runda rosedu_cdl_2015 Marime 2.49 kb
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
  
void get_column(FILE *,FILE *,int *);
void get_lines(FILE *,FILE *,int *);
  
int main()
{
    FILE *file_json = fopen("convertor.in","r");
    FILE *file_out = fopen("convertor.out","w");
    int nr_col = 0;
    get_column(file_out,file_json,&nr_col);
    get_lines(file_json,file_out,&nr_col);
    return 0;
}
void get_column(FILE *file_out,FILE *file_json,int *nr_col) {
    int i = 0,aux,n;
    char *sample,c;

    n = 16;
    sample = malloc( n );
  
    while((c = fgetc(file_json)) != '}') {
        if (c == '}')
            break;
        sample[i++] = c;
        if(i == n) {
            n *= 2;
            sample = realloc (sample, n);
        }
    }
 
    sample[i] = 0;    
 	i = 0;
  
    start :
    while(strchr(sample + i,'\"')) {
        i = strchr(sample + i,'\"') + 1 - sample;
        aux = strchr(sample + i,'\"') + 1 - sample;
        fwrite(sample + i ,aux - i - 1,1,file_out);
        fwrite(",",1,1,file_out);
        i = aux;
        (*nr_col)++;
        while(i++)
            switch (sample[i]) {
                case '\"' :
                    aux = strchr(sample + i + 1,'\"') + 1 - sample;
                    i = aux;
                    goto start;
                default :
                    if(isdigit(sample[i])) {
                        aux = i++;
                        while(isdigit(sample[i++]))   
                            ;
                        goto start;                                                  
                    }
            }
    }
    fwrite("\n",1,1,file_out);
    rewind(file_json);
}
  
void get_lines(FILE *file_json,FILE *file_out,int *nr_col) {
    int k = 0,i;
    char c = 0;

	start :
	if(k == *nr_col) {
		fprintf(file_out,"\n");
		k = 0;
	}
    for(i = 0; i < 2; c = fgetc(file_json)) {
		if(c == EOF)
			return ;
		if(c == '"')
			i++;
	}
	while(1) {
		c = fgetc(file_json);
		switch (c) {
			case '"' :
				while((c = fgetc(file_json)) != '"')
                	fprintf(file_out,"%c",c);
                fprintf(file_out,",");
                k++;
				c = fgetc(file_json);
				goto start;
			default :
				if (isdigit(c)) {                    
             		fprintf(file_out,"%c",c);
                    while((c = fgetc(file_json)) && isdigit(c))                           
               			fprintf(file_out,"%c",c);            
                    fprintf(file_out,",");
                    k++;
					goto start;
				}				
		}
	}		
	fclose(file_json);
    fclose(file_out);
}