Cod sursa(job #1337775)

Utilizator crushackPopescu Silviu crushack Data 9 februarie 2015 14:54:35
Problema Convertor Scor 90
Compilator cpp Status done
Runda rosedu_cdl_2015 Marime 2.14 kb
#include <fstream>
#include <string>
#include <map>
#include <set>
#include <vector>

const char IN[] = "convertor.in", OUT[] = "convertor.out";
std::ifstream fin(IN);
std::ofstream fout (OUT);

std::string line;
std::string text;

std::set<std::string> header;
std::vector<std::map<std::string, std::string> > values;
std::map<std::string, std::string> m;

void get( std::string const & s, std::string & t, int & poz ) {

    t = "";
    for ( ++ poz; s[poz] != '\"'; ++ poz )
        t += s[poz];

}

void get2( std::string const & s, std::string & t, int & poz ) {

    t = "";
    for ( ++ poz; s[poz] == ' '; ++ poz );
    for ( ; s[poz] != ',' && s[poz] != '}'; ++ poz )
        t += s[poz];

}

int main() {

    while (std::getline(fin, line))
        text += line;

    for ( int i = 1; i < text.length(); ++ i ) {

        if ( text[i] == '{' ) {

            m.clear();
            for ( ; text[i] != '}' ; ++ i ) {

                if ( text[i] == '\"') {

                    std::string name, value;

                    get( text, name, i );
                    for ( ++ i; text[i] != ':'; ++ i );
                    get2( text, value, i );

                    header.insert(name);

                    while ( value[value.length()] == ' ') value = value.substr(0, value.length() - 1);
                    while ( value[0] == ' ') value = value.substr(1);

                    if ( value[0] == '\"') {
                        value = value.substr(1, value.length());
                        while ( value[value.length() - 1] != '\"') value = value.substr(0,value.length() - 1);
                        value = value.substr(0, value.length() - 1);
                    }


                    m[name] = value;
                }

                if ( text[i] == '}' ) -- i;
            }
            values.push_back(m);
        }
    }

    for ( auto it = header.begin(); it != header.end(); ++ it )
        fout << * it << ",";

    fout << std::endl;
    for ( int i = 0; i < values.size(); ++ i ) {
        for ( auto it = header.begin(); it != header.end(); ++ it ) {
            fout << values[i][*it] << ",";
        }
        fout << std::endl;
    }

    return 0;
}