Cod sursa(job #1342478)

Utilizator rvintilaVintila Ioan-Raul rvintila Data 14 februarie 2015 01:41:42
Problema Convertor Scor 50
Compilator c Status done
Runda rosedu_cdl_2015 Marime 3.05 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("convertor.out","w");

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

	char *p = NULL,*e,temp[5000],temp1[5000];
	int nr_col = 0;

	memset(temp,0,5000);

	p = calloc(size + 1, 1);

	int v[5000],i,j,z,l;
	int t[5000];

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

	fread(p,1,size,file_json);
	
	int ok = 1;	

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

	if(t[nr_col - 1])
		v[0]--;


	while(ok) { 
		for(i = 0; i < nr_col && ok; i++) {
			for(j = 0; j < v[i] ; j++) {
				 e = strtok(NULL,"\"");
				if(e == NULL) {
				//	printf("%d\n",e-p);
					ok = 0;
					break;
				}	
			//	printf("%s {%d}\n",e,j);
			}
			if(t[i] && ok) {
				//printf("%s - %d\n",e,i);
				//printf("!! %d !!\n",j);
				for(z = 0 , l = 0; z < strlen(e); z++)
					if(isdigit(e[z])) {
					//	printf("ez%c\n",e[z]);
						temp1[l++] = e[z];
						while(isdigit(e[++z]))
							temp1[l++] = e[z];
						temp1[l] = ','; temp1[l + 1] = 0;
						strcat(temp,temp1);
						break;
					}
				continue;
			}
			if(ok) {
				strcat(temp,e);
				strcat(temp,",");
			}
		}
		fprintf(file_out,"%s\n",temp);
		temp[0] = 0;
	}
	free(p);
	fclose(file_json);
	fclose(file_out);
	return 0;
}

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

	fread(line,1,5000,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);
	/*for(i = 0; i < *nr_col; i++)
		printf("%d -> %d\n",c[i],v[i]); */
}