Cod sursa(job #1350720)

Utilizator RaresvisRares Visalom Mihail Raresvis Data 20 februarie 2015 22:04:03
Problema Convertor Scor 10
Compilator c Status done
Runda rosedu_cdl_2015 Marime 2.35 kb
#include<stdio.h>

void keys(FILE* f, FILE* g, int* keyLenght, int* keyIndex)
{
  int currentKeyLength = 0, strlen = 0;
  char x = 0, string[100000];
  
  string[0] = '\0';
  
  x = getc(f);
  while(x != '}'){
    while(x == ' ' || x == '\n'){
	x = getc(f);
      }
    
    if(x == '"'){
      x = getc(f);
      
      //key
      while(x != '"'){
	fprintf(g, "%c", x);
	
	currentKeyLength++;
	
	x = getc(f);
      }
    }
    else if(x == ':'){
      x = getc(f);
      
      keyLenght[(*keyIndex)++] = currentKeyLength;
      
      currentKeyLength = 0;
      
      while(x == ' ' || x == '\n'){
	x = getc(f);
      }
      
      //if string value
      if(x == '"'){
	x = getc(f);
	
	while(x != '"'){
	  string[strlen++] = x;
	  
	  x = getc(f);
	}
	string[strlen++] = ',';
      }
      //int value
      else{
	while(x >= '0' && x <= '9'){
	  string[strlen++] = x;
	  
	  x = getc(f);
	}
	string[strlen++] = ',';
      }
    }
    if(x == ','){
      fprintf(g, ",");
    }
    
    x = getc(f);
    
  }
  fprintf(g, ",\n");
  fprintf(g, "%s\n", string);
}


void values(FILE* f, FILE* g, int* keyLenght, int keyIndex)
{
  int currentObjectKey = 0, currentKey = 0;
  char x = 0, buffer[1000];
  
  while(x != '{' && !feof(f)){
    x = getc(f);
  }
  
  x = getc(f);
  
  while(x != ']' && !feof(f)){
    while(x != '}'){
      while(x == ' ' || x == '\n'){
	  x = getc(f);
	}
      
      if(x == '"'){
	x = getc(f);
	
	//key
	while(x != '"'){
	  ;//fprintf(g, "%c", x);
	  
	  x = getc(f);
	}
      }
      else if(x == ':'){
	x = getc(f);
	
	while(x == ' ' || x == '\n'){
	  x = getc(f);
	}
	
	//if string value
	if(x == '"'){
	  x = getc(f);
	  
	  while(x != '"'){
	    fprintf(g, "%c", x);
	    
	    x = getc(f);
	  }
	  fprintf(g, ",");
	}
	//int value
	else{
	  while(x >= '0' && x <= '9'){
	    fprintf(g, "%c", x);
	    
	    x = getc(f);
	  }
	  fprintf(g, ",");
	}
      }
      if(x == ','){
	currentKey++;//fprintf(g, ",");
      }
      x = getc(f);
    }
    fprintf(g, "\n");
    
    x = getc(f);
  }
}


int main()
{
  //open the files
  FILE* f = fopen("convertor.in", "r");
  FILE* g = fopen("convertor.out", "w");
  
  //get the keys and the first row of values
  int keyLenght[10100], keyIndex = 0;
  keys(f, g, keyLenght, &keyIndex);
  
  //get the rest of the values
  values(f, g, keyLenght, keyIndex);
  
  //close the files
  fclose(f);
  fclose(g);
  
  return 0;
}