Cod sursa(job #1348025)

Utilizator GeorgianaElenaDolocan Georgiana Elena GeorgianaElena Data 19 februarie 2015 14:20:48
Problema Convertor Scor 90
Compilator cpp Status done
Runda rosedu_cdl_2015 Marime 4.15 kb
#include <string>
#include <fstream>
#include <streambuf>
#include <iostream>
#include <map>
 
using namespace std;
 
/*string read_the_file()
{
    ifstream t("convertor.in");
    string str((std::istreambuf_iterator<char>(t)),
                std::istreambuf_iterator<char>());
    return str;
}
*/
#define lines 3000
 
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++;
    }
    if (in.eof()){
        eof = 1;
        //cout << "!!!!!!!!!!!";
    }
}
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 quote_closed = true;
    int obj = 0;
    ofstream out("convertor.out");
    //out.open ("convertor.out");
 
    string keys;
    string values;
 
 
bool find_pairs(const string &str)
{
 
    size_t length = str.length();
    size_t i = 0;
    bool value_closed;
 
 
    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);
                //cout << value_closed << endl;
            }
 
        } else {
            if (str [i] == ':') {
                value_closed = return_values(str, values, i, quote_closed, false, out);
                //cout << value_closed << endl;
            }
        }
     
        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;
    read_the_file(pos, str, end_of_file, in);
   // cout << str << endl;
    value_closed = find_pairs(str);
    cout << value_closed << endl;
    
    while (!in.eof()) {
        pos = 0;
        if (value_closed) {
            str.clear();
        } else {
            int i = str.find_last_of (':');
            string sub_string = str.substr (i, str.length() - i + 1);
            str.clear();
            str += sub_string;
        }
        read_the_file(pos, str, end_of_file, in);
     //   cout << str << endl;  
        value_closed = find_pairs(str);
        cout << value_closed << " fis " << in.eof() << endl;
    }

    
   // cout << in.eof() << endl;
 
}