Cod sursa(job #1354839)

Utilizator GeorgianaElenaDolocan Georgiana Elena GeorgianaElena Data 22 februarie 2015 02:36:44
Problema Convertor Scor 0
Compilator cpp Status done
Runda rosedu_cdl_2015 Marime 3.82 kb
#include <string>
#include <fstream>
#include <streambuf>
#include <iostream>

using namespace std;
  
#define lines 4000
  
void read_the_file(int &pos, string &str, int &eof, ifstream &in)
{
    string line;
  
    while (pos <= lines && !in.eof()) {           
            getline(in, line);
            str += line;
            pos++;
    }
}
size_t eat_whitespace(const string &str, size_t pos)
{    
    while (str[pos] == ' ' || str[pos] == '\t' || str[pos] == '\n') {
        pos++;
    }

    return pos;
}
  
void inside_quote (const string &str, string &keys, size_t &pos, 
    bool quote_closed, bool first_object, ofstream &file) 
{
    quote_closed = !quote_closed;
    if (quote_closed == false) {
        pos++;
        while (str[pos] != '\"') {
            if (first_object == true){
                keys += str[pos];
            } else {
                file << str[pos];
            }
            pos++;
        }
    }
    quote_closed = true;
}
  
void return_keys(const string &str, string &keys, size_t &pos,
                 bool quote_closed, bool first_object, ofstream &file)
{
    inside_quote (str, keys, pos, quote_closed, first_object, file);
    keys += ',';
}
  
bool return_values (const string &str, string &values, size_t &pos,
                    bool quote_closed, bool first_object, ofstream &file)
{
    bool value_closed = false;
    pos = eat_whitespace(str, pos + 1);
     
    while (str[pos] != ',' && str[pos] != '}' && pos != str.length()) {
        if (str[pos] != '\"' && quote_closed == true &&
         str[pos] != ' ' && str[pos] != '\n') {
            if (first_object == true) {
                values += str[pos];
            } else {
                file << str[pos];
            }
        } else if (str[pos] == '\"') {
            inside_quote (str, values, pos, quote_closed, first_object, file);
        }
        pos++;
    }

    if (str[pos] == ',' || str[pos] == '}') {
        value_closed = true;
        if (first_object == true) {
            values += ',';
        } else {
            file << ",";
        }
    }
 
    return value_closed;
}
  
  
bool find_pairs(const string &str, int &obj, ofstream &out,
                 string &keys, string &values, bool quote_closed)
{
  
    size_t length = str.length();
    size_t i = 0;
    bool value_closed = false;
  
    while (i < length) {
        if (obj == 0){
            if (str[i] == '\"') {
                return_keys(str, keys, i, quote_closed, true, out);
            } else if (str [i] == ':') {
                value_closed = return_values(str, values, i, quote_closed, true, out);
            }
  
        } else {
            if (str [i] == ':') {
                value_closed = return_values(str, values, i, quote_closed, false, out);
            }
        }
      
        if (quote_closed == true && str[i] == '}') {
            if (obj == 0) {
                out << keys << endl;
            }
            obj++;
            out << values << endl;
            values.clear();
        } 
        i++;
    }
 
    return value_closed;
}
  
int main()
{
    ifstream in("convertor.in");
    int pos = 0;
    string str;
    int end_of_file = 0;
    bool value_closed;
    
    int obj = 0;
    ofstream out("convertor.out");
  
    string keys;
    string values;  

    read_the_file(pos, str, end_of_file, in);
    value_closed = find_pairs(str, obj, out, keys, values, true);
     
    while (!in.eof()) {
        pos = 0;
        if (value_closed) {
            str.clear();
        } else {
            int i = str.find_last_of (':');
            if (str.length() > 0 && i != -1) {
            	string sub_string = str.substr (i, str.length() - i + 1);
            	str.clear();
            	str += sub_string;
        	}
        }
        read_the_file(pos, str, end_of_file, in);
        value_closed = find_pairs(str, obj, out, keys, values, quote_closed);
    }
}