Cod sursa(job #1361343)

Utilizator dorinel.filipFilip Ion Dorinel dorinel.filip Data 25 februarie 2015 20:48:55
Problema Convertor Scor 0
Compilator cpp Status done
Runda rosedu_cdl_2015 Marime 2.14 kb
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
// Headers
void read_all_lines_from_file(std::string filename, std::string& result) {
    std::ifstream f(filename.c_str());

    if(f.is_open()) {
        std::string line;
        result.erase();
        while(std::getline(f, line)) {
                result += line;
        }

    } else {
        std::cout << "Failed to open file\n";
        throw errno;
    }
}

int main ( void )
{
    std::string buffer;
    read_all_lines_from_file("convertor.in", buffer);

    std::stringstream f(buffer);
    std::ofstream g("convertor.out");
    if( !g.is_open() )
    {
        std::cout << "Failed to open the file." << std::endl;
        throw errno;
    }

    std::string entry;
    std::getline(f, entry, '}');
    size_t pos = 0;
    size_t next_quote;
    while((next_quote = entry.find('"',pos)) != std::string::npos)
    {
        size_t start = entry.find('"', pos);
        size_t end = entry.find('"', start+1);
        g << entry.substr(start + 1, end - start - 1) << ",";
        //pos = entry.find(',', end+1);
        pos = std::min(entry.length(), entry.find('"', end+1));
        pos = std::min(entry.length(), entry.find('"', pos+1));

    }

    g << std::endl;
    f.seekg(0, f.beg);

    while(std::getline(f, entry, '}'))
    {
        size_t pos = 0;
        size_t next_quote;
        while((next_quote = entry.find('"',pos)) != std::string::npos)
        {
            pos = entry.find('"', pos+1);
            pos = entry.find('"', pos+1);
            pos = entry.find(":",pos+1);
            size_t start = entry.find_first_not_of(" ", pos+1);
            size_t end;
            if(entry[start] == '"')
            {
                end = entry.find('"', start+1);
            }
            else
            {
                end = std::min(entry.length(), entry.find_first_not_of("0123456789", start));
                start--;
            }

            g << entry.substr(start + 1, end - start - 1) << ",";

            pos = end +1;
        }
        g << std::endl;
    }

    g.close();
    return 0;
}