Cod sursa(job #1343063)

Utilizator RaresvisRares Visalom Mihail Raresvis Data 14 februarie 2015 20:36:29
Problema Convertor Scor 0
Compilator c Status done
Runda rosedu_cdl_2015 Marime 1.99 kb
#include<stdio.h>

void keys(FILE* f)
{
  int endFlag = 0, keyFlag = 1, stringFlag = 0;
  char x;

  while(!feof(f) && endFlag == 0){
    x = getc(f);
    
    if(x == '}'){
      endFlag = 1;
    }
    else if(!feof(f)){
      if(x == ':'){
        keyFlag = 0;
      }
      else if(x == ','){
        keyFlag = 1;
        printf(",");
      }
      else if(x == '[' || x == '{'){
        ;//do nothing
      }
      else if(x == '"' && stringFlag == 0){
        stringFlag = 1;
      }
      else if(x == '"' && stringFlag == 1){
        stringFlag = 0;
      }
      else if(x != '\n' && x != ' '){
        if(x != '"' && keyFlag == 1){
          printf("%c", x);
        }
      }
      else if(x == ' ' && stringFlag == 1 && keyFlag == 1){
        printf(" ");
      }
    }
  }
  printf(",");
}

void CSV(FILE* f)
{
  int valueFlag = 0, endFlag = 0, objectFlag = 0, stringFlag = 0;
  char x;

  while(endFlag == 0){
    x = getc(f);

    if(x == ']'){
      endFlag = 1;
    }
    else if(x == '['){
      ;//do nothing
    }
    else if(x == '{'){
      objectFlag = 1;
      printf("\n");
    }
    else if(x == '}'){
      ;//do nothing
    }
    else if(x == ',' && objectFlag == 0){
      ;//do nothing
    }
    else if(x > 0){
      if(x == ':'){
        valueFlag = 1;
      }
      else if(x == '"' && stringFlag == 1){
        stringFlag = 0;
      }
      else if(x == '"' && stringFlag == 0){
        stringFlag = 1;
      }
      else if(x == ',' && stringFlag == 0){
        valueFlag = 0;
        printf(",");
      }
      else if(x != '\n'){
        if(x == ' ' && valueFlag == 1 && stringFlag == 1){
          printf(" ");
        }
        else if(x != '"' && valueFlag == 1 && x != ' '){
          printf("%c", x);
        }
      }
    }
  }
}

int main()
{
  FILE* f = fopen("convertor.in", "r");
  
  //get the keys
  keys(f);

  //restart the position of the pointer in the file
  fseek(f, 0, SEEK_SET);

  //start getting the objects and teir values
  CSV(f);

  //close the file
  fclose(f);

  return 0;
}