Cod sursa(job #1338005)

Utilizator CostinVFMI CostinVictorGabriel CostinV Data 9 februarie 2015 18:43:26
Problema Convertor Scor 100
Compilator cpp Status done
Runda rosedu_cdl_2015 Marime 1.92 kb
#include <fstream>
#include <string>
#include <vector>

using namespace std;

void parse (ifstream& in, vector<vector<string> >& csv)
{
    string buffer;
    vector <string> keys, line;
    bool first_time = true;
    getline (in, buffer);
    char ch;
    int it1 = 0, it2 = 0;

    while(1)
    {
        while (buffer[it1] != '"')
        {
            if (buffer[it1] == '\0')
            {
                getline (in, buffer);
                it1 = -1;
            }

            if (buffer[it1] == '}')
            {
                if (first_time)
                {
                    csv.push_back(keys);
                    first_time = false;
                }
                csv.push_back(line);
                line.clear();
            }

            if (buffer[it1] == ']')
                return;
            it1++;
        }

        it2 = it1 + 1;
        while (buffer[it2] != '"') it2++;

        if (first_time)
            keys.push_back(buffer.substr(it1+1, it2-it1-1));

        it1 = it2+1;

        while (buffer[it1] != '"' && !(isdigit(buffer[it1])))
        {
            if (buffer[it1] == '\0')
            {
                getline(in, buffer);
                it1 = 0;
            }
            else
                it1++;
        }
        it2 = it1;

        if (buffer[it2] != '"')
        {
            while (isdigit(buffer[it2])) it2++;
            line.push_back(buffer.substr(it1, it2-it1));
            it1 = it2;
        }
        else
        {
            it2++;
            while (buffer[it2] != '"') it2++;
            line.push_back(buffer.substr(it1+1, it2-it1-1));
            it1 = it2+1;
        }


    }
}

int main()
{
    ifstream in ("convertor.in");
    ofstream out ("convertor.out");

    vector <vector<string> > csv;

    parse (in, csv);

    for (int i=0; i<csv.size(); i++)
    {
        for (int j=0; j<csv[i].size(); j++)
            out << csv[i][j]<<',';
        out<<'\n';
    }

    return 0;
}