Skip to content
ReadFile.cc 4.93 KiB
Newer Older
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;


string title;
vector <string> coordinates;
vector <string> colors;
vector <float> stepTime;
vector <vector <vector <float>>> data; // first dim = step; second dim = point; third dim = coordinate ( or component )

double stringToDouble( const std::string& s )
{
    std::istringstream i(s);
    double x;
    if (!(i >> x))
        return 0;
    return x;
} 

bool check_double( const std::string& s )
{
    std::istringstream i(s);
    double x;
    if (!(i >> x))
        return false;
    return true;
} 



bool stripeInfo(string &line, string tag)
{

    auto titlePos = line.find(tag);
    auto titleFound = false;

    if (titlePos != std::string::npos) {
        //cout << "TAG " << tag << " FOUND!!!!" << endl;
        titleFound = false;
        auto const pos = line.find_last_of(':');
        line = line.substr(pos+1);

        return true;
    }

    return false;

}


std::string ReplaceAll(std::string str, const std::string& from, const std::string& to)
{
    size_t start_pos = 0;

    while((start_pos = str.find(from, start_pos)) != std::string::npos) {
        str.replace(start_pos, from.length(), to);
        start_pos += to.length(); // Handles case where 'to' is a substring of 'from'
    }

    return str;
}


bool isEmpty(std::string str)
{

    if ( ReplaceAll(str, " ", "").empty() )
        return true;
    else
        return false;

} 


void extractInfo(string line)
{

    cout << "check comment info in: " << line << endl;

    auto titleFound = stripeInfo(line, "Title");
    auto coordinatesFound = stripeInfo(line, "Coordinates");
    auto colorsFound = stripeInfo(line, "Colors");
    auto stepTimeFound = stripeInfo(line, "Time");

    istringstream iss(line);

    if ( titleFound || coordinatesFound || colorsFound || stepTimeFound )
    {
        do
        {
            string word;
            iss >> word;
            auto i = 0;

            if ( !word.empty() ) 
            {
                if ( titleFound )
                {
                    cout << "Title: " << word << endl;
                    title += word;
                }

                if ( coordinatesFound )
                {
                    cout << "Coordinate[" << coordinates.size() << "]: " << word << endl;
                    coordinates.push_back(word);
                }

                if ( colorsFound )
                {
                    cout << "Colors[" << colors.size() << "]: " << word << endl;
                    colors.push_back(word);
                }

                if ( stepTimeFound )
                {
                    cout << "Time: " << word << endl;
                    stepTime.push_back(stringToDouble(word));
                }
            }
        }
        while (iss);
    }

}


void t2(void)
{

    ifstream file;
    file.open ("testc.dat");

    string line; line.clear();    
    auto commentFound = false;
    auto emptyLineFound = false;


    //coordinates.erase(coordinates.begin());

    cout << "-----------------------" << endl;

    // Read file line by line
    while (!file.eof())
    {
        getline(file, line);

        // Check if it is a comment line
        auto commentPos = line.find("#");
        if ( commentPos == 0 ) {
            //cout << "This is a comment. " << endl;
            commentFound = true;

            // Check if it has any information tag
            extractInfo(line);

        }
        else {
            //cout << "This is NOT a comment. " << endl;
            commentFound = false;
        }


        // Check if the line is empty (will start new group of data) 
        if ( isEmpty(line) ) {
            emptyLineFound = true;
            cout << "EMPTY LINE" << endl;

        } 
        else if ( !commentFound ) {
            emptyLineFound = false;

            // Split line into words
            istringstream iss(line);

            do
            {
                string word;
                iss >> word;

                if ( !word.empty()) 
                {
                    cout << "Word: " << word;
                    if ( check_double(word) )
                        cout << " : is a double" << endl;
                    else
                        cout << " : is NOT a double" << endl;

                }
            }
            while (iss);

        }


        cout << "-----------------------" << endl;
    }

}


void show(void)
{

    cout << "==============================" << endl;
    cout << "Title = " << title << endl;

    for (auto i = 0; i < coordinates.size(); i++)
        cout << "Coordinate[" << i << "] = " << coordinates[i] << endl;

    for (auto i = 0; i < stepTime.size(); i++)
        cout << "Time[" << i << "] = " << stepTime[i] << endl;

    for (auto i = 0; i < colors.size(); i++)
        cout << "Colors[" << i << "] = " << colors[i] << endl;

    cout << "==============================" << endl;

}


int main (void) 
{
    t2();

    show();
}