Cod sursa(job #1343955)

Utilizator theory1TeodorCotet theory1 Data 16 februarie 2015 03:22:47
Problema Convertor Scor 80
Compilator c Status done
Runda rosedu_cdl_2015 Marime 1.46 kb
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

const int MAXI = 40000;

char *s1;
char *t ;

char *get_from(char *st, char *dr) {

	strncpy(s1, st, (dr - st));
	return s1;
}

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

void get_fields(char *s, FILE *in, FILE *out) {
	
	char *p = strtok(s, ",");
	int n = 0;
	while(p != NULL) {
		
		t = strchr(p, ':');
		p = strtok(NULL, ",");
		
		while(t != NULL && *t != 34 && !('0' <= *t && *t <= '9') )   ++t;
		
		
		if( '0' <= *t && *t <= '9')	{
			while('0' <= *t && *t <= '9')
				fprintf(out,"%c", *t), ++t;
			fprintf(out,",");
		} else {
			
			++t; n = 0;
			char *x = t;			
			while(*t != 34)  ++t;
			*t = '\0';
			fprintf(out, "%s", x);
			fprintf(out, ",");
			
		}
				
	}
}

char s[100000];

int main() {

	FILE *in = fopen("convertor.in", "r");
	FILE *out = fopen("convertor.out", "w");
	
	char c; int n;int first = 1;

	t = malloc(MAXI);
	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 = 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;
}