Untitled

 avatar
unknown
c_cpp
a year ago
1.4 kB
7
Indexable
Station::Station(const std::string &strStation)
    {
        Utilities utils;
        size_t next_pos = 0;
        m_id = id_generator++;
        bool more = true;

        try
        {
            m_name = utils.extractToken(strStation, next_pos, more);
            m_serialNum = std::stoi(utils.extractToken(strStation, next_pos, more));
            m_quantity = std::stoi(utils.extractToken(strStation, next_pos, more));

            m_widthField = std::max(m_widthField, utils.getFieldWidth());

            m_description = utils.extractToken(strStation, next_pos, more);
        }
        catch (const std::runtime_error &e)
        {
            std::cerr << "Error: " << e.what() << std::endl;
        }
    }


 std::string Utilities::extractToken(const std::string &str, size_t &next_pos, bool &more)
    {
        std::string token;
        size_t start = str.find(getDelimiter(), next_pos);

        if (start == next_pos)
        {
            more = false;
            throw "delimiter at next position";
        }
        else if (start != std::string::npos)
        {
            more = true;
            token = str.substr(next_pos, start - next_pos);
            next_pos = start + 1;
        }
        else
        {
            more = false;
            token = str.substr(next_pos);
        }

        setFieldWidth(token.length());

        return token;
    }
Editor is loading...
Leave a Comment