dimanche 22 avril 2018

Unkown bug ? (most likely related to the usage of string)

In this program, in the 'void database' function, i have some issues,it doesn't enter correctly the ' if (it->first.find(path) != std::string::npos)' statement.The database, i need to work with , looks like this ( after i used getline on it ) : "name","./path","date".Meanwhile, the map, which was filled in 'allpoints' function , is filled with "/root/path" + value.I tried several things, such as : compare,find,length,substr .The goal is to identify "/path" in /root/path (and path has to be the latest part in the /root/path string).Sorry for posting a relatively big code, but i tried things which work in theory, and they failed working here, if anyone can spot a bug, please tell me :) Thank you in advance !(link to City https://www.dropbox.com/s/5ewk3ock5qchkmh/City.tgz?dl=0)(link db-2018-04-14 http://smartcity.inf.unideb.hu/~norbi/db-2018-04-14.csv)

   #include <iostream>
#include <string>
#include <sstream>
#include <cctype>
#include <vector>
#include <map>
#include <iterator>

#include <boost/filesystem.hpp>
#include <boost/program_options.hpp>
class Date
{
  public:
    Date(int day = 0, int month = 0, int year = 0) : _day(day), _month(month), _year(year) {}
    int get_day() { return _day; }
    int get_month() { return _month; }
    int get_year() { return _year; }
    void writestuff() { std::cout << _day << "/" << _month << "/" << _year << std::endl; }
    ~Date() {}

  private:
    int _day;
    int _month;
    int _year;
};
class Adatok
{
  public:
    Adatok(std::string name, std::string path, int points, Date date) : _name(name), _path(path), _points(points), _date(date) {}
    Adatok(const Adatok &other) = default;
    Adatok &operator=(const Adatok &other) = default;
    void writestuff()
    {
        std::cout << _name << " " << _path << " " << _points;
        _date.writestuff();
        std::cout << std::endl;
    }
    bool operator<(const Adatok &regi) const
    {
        return _name < regi._name;
    }
    std::string get_name() { return _name; }
    std::string get_path() { return _path; }
    int get_points() { return _points; }
    ~Adatok() {}

  private:
    std::string _name;
    std::string _path;
    Date _date;
    int _points;
};

typedef std::vector<boost::filesystem::path> vec_type;
typedef std::map<std::string, int> map_type;
int last;







void database(std::string &temp, std::vector<Adatok> &my_vec, map_type &mymap)
{
    temp += ',';
    std::string name = temp.substr(temp.find_first_of('"')+1,temp.find_first_of(',')-2);
    temp.erase(0,temp.find_first_of(',')+1);
    std::string path = temp.substr(temp.find_first_of('"')+2,temp.find_first_of(',')-3);
    temp.erase(0,temp.find_first_of(',')+1);
    std::string numbers(temp.substr(temp.find_first_of('"') + 1, temp.find_first_of('-')));
    int year, month, day;
    year = std::atoi(numbers.c_str());
    temp.erase(0, temp.find_first_of('-') + 1);
    numbers = temp.substr(0, temp.find_first_of('-'));
    month = std::atoi(numbers.c_str());
    temp.erase(0, temp.find_first_of('-') + 1);
    numbers = temp.substr(0, temp.find_first_of(' '));
    day = std::atoi(numbers.c_str());

    for (map_type::iterator it{mymap.begin()}; it != mymap.end(); it++)
    {

          if (it->first.substr(it->first.length()-path.length(),it->first.length() )== path)
        {
            bool ok = false;

            for (std::vector<Adatok>::iterator jt{my_vec.begin()}; jt != my_vec.end(); jt++)
            {
                if ((jt->get_name() == name) && (jt->get_path() == path))
                    ok = true;
            }
            if (!ok)
            {
                my_vec.push_back(Adatok(name, path, it->second, Date(day, month, year)));
            }
        }
    }

}
void allpoints(boost::filesystem::path &mypath, vec_type myvec, int &sum, map_type &mymap)
{
    if (boost::filesystem::is_regular_file(mypath))
    {
        std::string temp = "";
        std::string temp2 = "";
        int temp_sum = 0;
        boost::filesystem::ifstream myif{mypath};
        int sum2 = 0; //
        while (std::getline(myif, temp))
        {

            if ((temp.find_last_of("0123456789")) != temp.npos)
            {
                temp2 = temp.substr(temp.find_last_not_of("0123456789") + 1, temp.find_last_of("0123456789"));
                std::stringstream ph(temp2);
                ph >> temp_sum;
                if (temp_sum == 0) //ha a legutolso szam utan van meg valami
                {
                    temp2 = temp.substr(temp.find_last_of("/") + 1, temp.find_last_of("0123456789"));
                    temp2 = temp2.substr(temp2.find_first_of(" "), temp2.find_last_of("0123456789"));
                    temp2 = temp2.substr(temp2.find_first_of("0123456789"), temp2.find_last_of("0123456789"));
                    std::stringstream ph2(temp2);
                    ph2 >> temp_sum;
                }
                sum2 += temp_sum;
                sum += temp_sum;
            }
        }
        if (sum2 && (mypath.extension() == ".props"))
        {
            //std::cout << sum2 << " " << mypath << std::endl;
            std::string result = mypath.string();
            mymap.insert(std::pair<std::string, int>(result.substr(0, result.find_last_of('/')), sum2));
        }
    }
    else if (boost::filesystem::is_directory(mypath))
    {
        int last = myvec.size();
        std::copy(boost::filesystem::directory_iterator(mypath), boost::filesystem::directory_iterator(), back_inserter(myvec));
        for (vec_type::iterator it(myvec.begin() + last); it != myvec.end(); it++)
        {
            allpoints(*it, myvec, sum, mymap);
        }
    }
}
int main()
{
map_type mymap;
vec_type myvec;
std::vector<Adatok> Vec;
int sum =0;
boost::filesystem::path path{"/home/erik/Downloads/City"};
boost::filesystem::ifstream mif{"/home/erik/Downloads/db-2018-04-14.csv"};
std::string temp;
allpoints(path,myvec,sum,mymap);
while (getline(mif,temp))
database(temp,Vec,mymap);
for (std::vector<Adatok>::iterator it = Vec.begin();it != Vec.end();it++)
   if(it->get_name()=="Varga Erik")
     std::cout<<it->get_name()<<std::endl<<it->get_path()<<std::endl<<it->get_points()<<std::endl<<std::endl;
}

Aucun commentaire:

Enregistrer un commentaire