Cod sursa(job #1342256)

Utilizator rvintilaVintila Ioan-Raul rvintila Data 13 februarie 2015 18:26:55
Problema Convertor Scor 0
Compilator c Status done
Runda rosedu_cdl_2015 Marime 2.4 kb
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define BUFSIZE 1024

void get_column(int *v,int *c,FILE *file_json,FILE *file_out,int *nr_col,int size);
void get_pattern(char *file,int *v,int *t);


int main() {
	int size;
	FILE *file_json = fopen("convertor.in","r");
	FILE *file_out = fopen("yolo","w");

	fseek(file_json,0,2);
	size = ftell(file_json);
	rewind(file_json);

	char *p = NULL,temp[2000],temp1[2000],columns[200][200];
	int nr_col = 0;

	memset(temp,0,2000);

	p = malloc(size);

	int v[2000],i,j,k,z,l;
	int t[2000];

	get_column(v,t,file_json,file_out,&nr_col,size);

	fread(p,1,size,file_json);
	
	
	p = strtok(p,"\"");
	p = strtok(NULL,"\"");
	p = strtok(NULL,"\"");
	p = strtok(NULL,"\"");
	
	p = strtok(NULL,"\"");
	p = strtok(NULL,"\"");
	p = strtok(NULL,"\"");
	
	p = strtok(NULL,"\"");
	p = strtok(NULL,"\"");

	p = strtok(NULL,"\"");
	p = strtok(NULL,"\"");
	p = strtok(NULL,"\"");


	for(k = 0; k < 17000; k++) {
		for(i = 0; i < nr_col; i++) {
			for(j = 0; j < v[i]; p = strtok(NULL,"\"") , j++)
				;
			if(t[i]) {
				for(z = 0 , l = 0; z < strlen(temp); z++)
					if(isdigit(p[z])) {
						temp1[l++] = p[z];
						while(isdigit(p[++z]))
							temp1[l++] = p[z];
						temp1[l] = ','; temp1[l + 1] = 0;
						strcat(temp,temp1);
						break;
					}
				continue;
			}
			strcat(temp,p);
			strcat(temp,",");
		}
		fprintf(file_out,"%s\n",temp);
		temp[0] = 0;
	}
	
	return 0;
}

void get_column(int *v,int *c,FILE *file_json,FILE *file_out,int *nr_col,int size) {
	char line[2000],rand[2000],*p;
	memset(line,0,2000);
	memset(rand,0,2000);
	memset(v,0,2000);
	memset(c,0,2000);

	fread(line,1,2000,file_json);   
	int i,j = 0,lenght = strlen(line);

	v[0] = 1;


	for(i = 0; i < lenght && line[i] != '}'; i++) {
		if(line[i] == '"') {
			(*nr_col)++;
			while(line[++i] != '"') {
				rand[strlen(rand)] = line[i];
				} 
			rand[strlen(rand)] = ',';
			//printf("%s\n",rand);
			//i = strchr(line + i + 1,'"') + 1 - line ;
			while(i++) {
				if(line[i] == '"') {
					v[j] += 3;
					if(j != 0)
						if(!c[j - 1])
							v[j]++;
					j++;
					i = strchr(line + i + 1,'"') + 1 - line ;
					break;
				}
				if(isdigit(line[i])) {
					while(isdigit(line[++i]))
						;
					v[j] += 2;
					if(j != 0)
						if(!c[j - 1])
							v[j]++;
					c[j++] = 1;
					break;
				}
			}
		} 
	}
	fprintf(file_out,"%s",rand);
	fprintf(file_out,"\n");
	rewind(file_json);
}