Cod sursa(job #1349466)

Utilizator teo721Pavel Teo teo721 Data 20 februarie 2015 11:23:58
Problema Convertor Scor 100
Compilator cpp Status done
Runda rosedu_cdl_2015 Marime 4.35 kb
#include <iostream>
#include <fstream>
#include <string>
#include <list>
using namespace std;

// extrag elementele din json 
// in functie de acolada inchisa si deschisa
list<string> extract_elements_from_json(string input_string){
	list<string> objects;
  std::size_t found_start = 0;
  std::size_t found_final = 0;
  string acolada_deschisa = "{";
  string acolada_inchisa = "}";
  	while(found_final < input_string.size() )
  	{
      if( found_final != found_start){
        input_string = input_string.erase(0,found_final+1);
      }
      found_start = input_string.find(acolada_deschisa);
  		found_final = input_string.find(acolada_inchisa);
  		string object = input_string.substr(found_start+1,found_final-found_start-1);
      if(found_final < input_string.size()){
    	 objects.push_back(object);
      }	
  	}
    return objects;
}

// Intorc valoarea din sir
// daca e sir ma uit extrag
// ce este intre ghilimele
// daca e numar extrag 
// tot inafara de spatii si virgula
// din spatele numarului
string get_value(string sir,int vigula_pozitie){
  string ghilimele = "\"";
  std::size_t found_doua_puncte = sir.find(":");
  std::size_t found_start;
  std::size_t found_final;
  sir = sir.substr(found_doua_puncte+1,vigula_pozitie);
  string value;
  found_start = sir.find(ghilimele);
  found_final = sir.find(ghilimele,found_start+1);
  if( found_final < sir.size()){
    value = sir.substr(found_start+1,found_final - found_start -1); 
  }
  else{
    for ( std::string::iterator it=sir.begin(); it!=sir.end(); ++it){
      if(*it != ' ' && *it != ','){
        value += *it;
      }
    }
  }
  return value;
      
}

// citesc din fisier
// contatenez totul
// intr-un sir mare
string json_read(){
  string line,input_string;
  ifstream input_file("convertor.in");
  while ( getline (input_file,line) ){
      input_string += line;
  }

  input_file.close();
  return input_string;
}

int main () {
  bool virgula = false;
  bool ghilimele = false;
  bool avem_key = false;
	string input_string = json_read();
	string input_fara_spatii;
  list<string> values;
  string sir;
  int vigula_pozitie = 0;
  std::size_t found_start;
  std::size_t found_final;
 
  list<string> objects = extract_elements_from_json(input_string);
  ofstream output_file("convertor.out");
  for ( std::list<string>::iterator it_object=objects.begin(); it_object!=objects.end(); ++it_object){
    string object = *it_object;
    string aux;
    //parsez fiecare element din json
    for ( std::string::iterator it=object.begin(); it!=object.end(); ++it){
      aux += *it;
      if( *it == ','){
        virgula = true;
        vigula_pozitie = it - object.begin();
      }
      if( *it == '"'){
        ghilimele = true;
      }
      // o pereche (key,value) se termina
      //cand a intalnit ultima virgula
      //si glilimelele de la noul key
      if( virgula && ghilimele){
        virgula = false;
        ghilimele = false;
        // la prima parcurgere afisez mai intai
        // cheile si apoi valoriele
        if(! avem_key){
          // obtin cheia
          found_start = aux.find("\"");
          found_final = aux.find("\"",found_start+1);
          string key = aux.substr(found_start+1,found_final - found_start -1);
          
          output_file<<key<<",";
          
          sir = get_value(aux,vigula_pozitie);
          values.push_back(sir);
          aux = "";
        }
        else{
          // daca nu e prima parcurgere afisez valorea
          sir = get_value(aux,vigula_pozitie);
          output_file<<sir<<",";
          aux = "";
        }
      }
    }
    // daca e prima parcurgere 
    // afisez ultimul key
    // adaug ultimul value 
    // afisez value
    if( ! avem_key){
      found_start = aux.find("\"");
      found_final = aux.find("\"",found_start+1);
      string key = aux.substr(found_start+1,found_final - found_start -1);
          
      output_file<<key<<",";
      sir = get_value(aux,vigula_pozitie);
      values.push_back(sir);
      
      output_file<<'\n';
      for ( std::list<string>::iterator it=values.begin(); it!=values.end(); ++it){
        output_file<<(*it)<<",";
      }
      
      aux = "";
      avem_key = true;
      
    }
    else{
      // daca nu e prima parcurgere afisez
      // ultima valorea
      sir = get_value(aux,vigula_pozitie);
      output_file<<sir<<",";
      aux = "";
    }
    output_file<<"\n";
  }
  


  output_file.close();
  return 0;
}