Cod sursa(job #1350346)

Utilizator RaresvisRares Visalom Mihail Raresvis Data 20 februarie 2015 19:26:40
Problema Convertor Scor 70
Compilator c Status done
Runda rosedu_cdl_2015 Marime 2.53 kb
#include<stdio.h>
#include<stdlib.h>
#include<string.h>


void keys(FILE* f, FILE* g, int* keyLenght, int* keyIndex)
{
  int currentKeyLength = 0, stringFlag = 0;
  char x = 0, string[100000];
  
  string[0] = '\0';

  while(x != '{'){
    x = getc(f);
  }
  
  while(x != '}'){
    x = getc(f);
    
    while(x != '"'){
      x = getc(f);
    }
    
    x = getc(f);
    
    while(x != '"'){
      fprintf(g, "%c", x);
      
      currentKeyLength++;
      
      x = getc(f);
    }
    
    keyLenght[(*keyIndex)++] = currentKeyLength;
    
    x = getc(f);
    
    while(x != ':'){
      x = getc(f);
    }
    
    currentKeyLength = 0;
    
    x = getc(f);
    
    while(x != ',' && x != '}'){
      x = getc(f);
      if(x == '"'){
	//stringFlag ^= 1;
	x = getc(f);
	
	while(x != '"'){
	  string[strlen(string)] = x;
	  
	  x = getc(f);
	}
	
	//x = getc(f);
      }
      /*else if(x != ' ' && x != '\n'){
	string[strlen(string)] = x;
      }
      else if(stringFlag){
	string[strlen(string)] = x;
      }*/
      else if(x != '\n' && x != ' '){
	string[strlen(string)] = x;
      }
    }
    fprintf(g, ",");
  }
  fprintf(g, "\n");
  
  string[strlen(string) - 1] = ',';
  string[strlen(string)] = '\0';
    
  fprintf(g, "%s", string);
}


void CSV(FILE* f, FILE* g, int* keyLenght, int keyIndex)
{
  int valueFlag = 0, objectFlag = 0, stringFlag = 0, currentObjectKey = 0;
  char x = 0;

  while(!feof(f)){
    x = getc(f);
    
    while(x != '{' && x > 0){
      x = getc(f);
    }
    
    x = getc(f);
   
    if(!feof(f)){
      fprintf(g, "\n");
    }
    
    int i = 0;
    if(!feof(f)){
      while(i < keyIndex){
	while(x != '"' && x > 0){
	  x = getc(f);
	}
	
	x = getc(f);
	
	int j = 0, limit = keyLenght[i];
	while(j < limit){
	  x = getc(f);
	  
	  j++;
	}
	
	x = getc(f);
	
	while(x != ':' && x > 0){
	  x = getc(f);
	}
	
	x = getc(f);
	
	while(x == ' ' || x == '\n'){
	  x = getc(f);
	  
	}
	
	while(x != ',' && x != '}' && x > 0){
	  if(x != '"' && x != '\n' && x != ' '){
	    fprintf(g, "%c", x);
	  }
	  else if(x == ' ' && stringFlag){
	    fprintf(g, "%c", x);
	  }
	  else{
	    if(x == '"'){
	      stringFlag ^= 1;
	    }
	  }
	  x = getc(f);
	}
	
	fprintf(g, ",");
	i++;
      }
    }
  }
}


int main()
{
  //open the files
  FILE* f = fopen("convertor.in", "r");
  FILE* g = fopen("convertor.out", "w");
  
  
  //get the keys
  int keyLenght[10100], keyIndex = 0;
  keys(f, g, keyLenght, &keyIndex);

  //start getting the objects and teir values
  CSV(f, g, keyLenght, keyIndex);

  //close the files
  fclose(f);
  fclose(g);

  return 0;
}