Cod sursa(job #1360897)

Utilizator cosmincavCrecana Constantin-Cosmin cosmincav Data 25 februarie 2015 18:38:47
Problema Convertor Scor 100
Compilator cpp Status done
Runda rosedu_cdl_2015 Marime 4.13 kb
#include <iostream>
#include <fstream>
#include<string>

std::ifstream  fin("convertor.in");
std::ofstream fout("convertor.out");
int numberOfKeys = 0;
int index=0;

/*Function used to extract the key of a specific string
  having its starting index */
int findKey(std::string line, int start_index) {
    unsigned int j;
    for (j = start_index + 1; j < line.length(); ++j) {
        if (line.at(j) == '"') {
            fout << line.substr(start_index, j - start_index) << ",";
            numberOfKeys++;
            /*Function returns end index*/
            return j;
        }
    }
    return 0;
}

/*Function used to create Header*/
void createHeader() {
    int end_of_key = 0, found = 0;
    unsigned int i;
    std::string line;
    std::getline(fin, line);
     std::cout<<line<<"\n";
    /*Process the text until discover a '}' character */
    while (1) {
        /*Knowing that in the text the next element is a key
          I search line by line for its first index */
        for (i = 0; i < line.length(); ++ i) {
            if (line.at(i) == '"') {
                end_of_key = findKey(line, i + 1);
                found = 1;
                break;
            }
        }
        if (!found) {
            std::getline(fin, line);
        }
        else {
            found = 0;
            line = line.substr(end_of_key + 1);
            /*After the processing of a key the algorithm search line by line
              for a new one marked by character ',' or for the finished of the
              first object */
            while (1) {
                for (i = 0; i < line.length(); ++i) {
                    if (line.at(i) == ',') {
                        found = 1;
                        line = line.substr(i + 1);
                        break;
                    }
                    if (line.at(i) == '}') {
                        return;
                    }
                }
                if (!found) {
                    std::getline(fin, line);
                }
                else {
                    found = 0;
                    break;
                }
            }
        }
    }
}
std::string getValue(std::string line) {
    std::string value;
    unsigned int i;

            for( i = 0; i < line.length() && !( line[i] == '"' || std::isdigit(line[i] ) ); ++ i) ;

            line = line.substr(i);
            if (line.at(0) == '"') {
                value = line.substr(1);
                value = value.substr(0, value.find('"') );
            }
            else {
                for (i = 0; i < line.length(); i++) {
                    if ( std::isspace((line.at(i))) || line.at(i) == ',' || line.at(i) == '}') {
                     break;
                   }
                }
                value = line.substr(0, i);
            }
            fout << value << ",";
            index++;
            if (index % numberOfKeys == 0) {
                fout << "\n";
             }

            line = line.substr(value.length());
            return line;



}
bool containValue(std::string line ) {
    if( line.length() == 0) {
        return false;
    }
    if( std::string::npos != line.find_first_of("0123456789") ) {
        return true;
    }
    if( std::string::npos != line.find('"') ) {
        return true;
    }
    return false;
}
int main()
{
    std::string line;
    createHeader();
    fout << "\n";
    fin.close();
    std::ifstream  fin("convertor.in");
    std::getline(fin, line);
    while (1) {
        if (line.length() != 0) {
            if (line.find(':') != std::string::npos) {
                line = line.substr(line.find(':') + 1);
                while( !containValue(line) ) {
                    std::getline(fin,line);
                }
                line = getValue(line);
            } else {
                if (!std::getline(fin, line)) {
                    break;
                }
            }
        }
        else {
            if (!std::getline(fin, line)) {
                break;
            }
        }
    }
fin.close();
    fout.close();
    return 0;
    }