/****************************************************************************/1// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2// Copyright (C) 2001-2025 German Aerospace Center (DLR) and others.3// This program and the accompanying materials are made available under the4// terms of the Eclipse Public License 2.0 which is available at5// https://www.eclipse.org/legal/epl-2.0/6// This Source Code may also be made available under the following Secondary7// Licenses when the conditions for such availability set forth in the Eclipse8// Public License 2.0 are satisfied: GNU General Public License, version 29// or later which is available at10// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html11// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later12/****************************************************************************/13/// @file LineReader.h14/// @author Daniel Krajzewicz15/// @author Michael Behrisch16/// @date Fri, 19 Jul 200217///18// Retrieves a file linewise and reports the lines to a handler.19/****************************************************************************/20#pragma once21#include <config.h>2223#include <string>24#include <fstream>25#include <utils/common/UtilExceptions.h>262728// ===========================================================================29// class declarations30// ===========================================================================31class LineHandler;323334// ===========================================================================35// class definitions36// ===========================================================================37/**38* @class LineReader39* @brief Retrieves a file linewise and reports the lines to a handler.40*41* This class reads the contents from a file line by line and report them to42* a LineHandler-derivate.43* @see LineHandler44* @todo No checks are done so far during reading/setting position etc.45* @todo Should not IOError be thrown if something fails?46*/47class LineReader {48public:49/// @brief Constructor50LineReader();515253/** @brief Constructor54*55* Initialises reading from the file with the given name using setFile.56*57* @param[in] file The name of the file to open58* @see setFile59*/60LineReader(const std::string& file);616263/// @brief Destructor64~LineReader();656667/** @brief Returns whether another line may be read (the file was not read completely)68* @return Whether further reading is possible69*/70bool hasMore() const;717273/** @brief Reads the whole file linewise, reporting every line to the given LineHandler74*75* When the LineHandler returns false, the reading will be aborted76*77* @param[in] lh The LineHandler to report read lines to78*/79void readAll(LineHandler& lh);808182/** @brief Reads a single (the next) line from the file and reports it to the given LineHandler83*84* When the LineHandler returns false, the reading will be aborted85*86* @param[in] lh The LineHandler to report read lines to87* @return Whether a further line exists88*/89bool readLine(LineHandler& lh);909192/** @brief Reads a single (the next) line from the file and returns it93*94* @return The next line in the file95*/96std::string readLine();979899/// @brief Closes the reading100void close();101102103/** @brief Returns the name of the used file104* @return The name of the opened file105*/106std::string getFileName() const;107108109/** @brief Reinitialises the reader for reading from the given file110*111* Returns false when the file is not readable112*113* @param[in] file The name of the file to open114* @return Whether the file could be opened115*/116bool setFile(const std::string& file);117118119/** @brief Returns the current position within the file120* @return The current position within the opened file121*/122unsigned long getPosition();123124125/// @brief Reinitialises the reading (of the previous file)126void reinit();127128129/** @brief Sets the current position within the file to the given value130*131* @param[in] pos The new position within the file132*/133void setPos(unsigned long pos);134135136/** @brief Returns the information whether the stream is readable137* @return Whether the file is usable (good())138*/139bool good() const;140141142int getLineNumber() {143return myLinesRead;144}145146private:147/// @brief the name of the file to read the contents from148std::string myFileName;149150/// @brief the stream used151std::ifstream myStrm;152153/// @brief To override MSVC++-bugs, we use an own getline which uses this buffer154char myBuffer[1024];155156/// @brief a string-buffer157std::string myStrBuffer;158159/// @brief Information about how many characters were supplied to the LineHandler160int myRead;161162/// @brief Information how many bytes are available within the used file163int myAvailable;164165/// @brief Information how many bytes were read by the reader from the file166int myRread;167168/// @brief Information how many lines were read for meaningful error messages169int myLinesRead;170171/// @brief Number of skipped characters at the file begin (UTF-8 BOM)172int mySkipBOM;173};174175176