Cod sursa(job #1343885)

Utilizator theory1TeodorCotet theory1 Data 16 februarie 2015 00:18:29
Problema Convertor Scor 70
Compilator c Status done
Runda rosedu_cdl_2015 Marime 1.46 kb
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


char *get_from(char *st, char *dr) {
	char *s = malloc(9000);
	strncpy(s, st, (dr - st));
	return s;
}

void get_names(char *s, FILE *in, FILE *out) {
	
	char *p = strtok(s, ",");
	
	while(p != NULL) {
	
		char *t = strchr(p, '\"');
		
		p = strtok(NULL, ",");
	
		fprintf(out, "%s,", get_from(t + 1, strchr(t + 1, '\"') ) );
	}
}

void get_fields(char *s, FILE *in, FILE *out) {
	
	char *p = strtok(s, ",");
	while(p != NULL) {
		
		char *t = malloc(9000);
		t = strchr(p, ':');
		p = strtok(NULL, ",");
		
		while(t != NULL && *t != '\"' && !('0' <= *t && *t <= '9') )   ++t;
		
		//fprintf(out, "%s ", t);
		
		
		if( '0' <= *t && *t <= '9')	{
			char *num = strchr(t, ' ');			
			if(num == NULL)
				fprintf(out, "%s,", get_from(t, t + strlen(t)) );
					else 
				fprintf(out, "%s,", get_from(t, num));
		} else 
		fprintf(out,"%s,", get_from(t + 1, strchr(t + 1, '\"') ) );
					
	}
}

int main() {

	FILE *in = fopen("convertor.in", "r");
	FILE *out = fopen("convertor.out", "w");
	
	char s[50000]; char c; int n;int first = 1;
	c = fgetc(in);
	while(c != EOF) {

		n = 0;
		while(c != EOF && c != '}') {
			
			if(c != '\n' && c != '\0') s[n++] = c;
			c = fgetc(in);
		}
	
		s[n++] = '\0';
		char *s_cp;
		s_cp = strdup(s);
	
		if(first) {
			get_names(s, in, out);
			first = 0;
			fprintf(out, "\n");
		}

		get_fields(s_cp, in, out);
		fprintf(out, "\n");
		while(c != EOF && c != '{') c = fgetc(in);
	}
	return 0;
}