Cod sursa(job #1350209)

Utilizator RaresvisRares Visalom Mihail Raresvis Data 20 februarie 2015 18:27:31
Problema Convertor Scor 90
Compilator c Status done
Runda rosedu_cdl_2015 Marime 2.24 kb
#include<stdio.h>
#include<stdlib.h>


void keys(FILE* f, FILE* g, int* keyLenght, int* keyIndex)
{
  int currentKeyLength = 0;
  char x = 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);
    }
    
    fprintf(g, ",");
    
  }
}


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;
    while(i < keyIndex && !feof(f)){
      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;
  //keyLenght = (int*)malloc(200 * sizeof(int));
  keys(f, g, keyLenght, &keyIndex);

  //restart the position of the pointer in the file
  fclose(f);
  FILE* h = fopen("convertor.in", "r");

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

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

  //free the memory
  //free(keyLenght);

  return 0;
}