LCOV - code coverage report
Current view: top level - src/utils/importio - LineReader.h (source / functions) Coverage Total Hit
Test: lcov.info Lines: 100.0 % 1 1
Test Date: 2024-11-20 15:55:46 Functions: - 0 0

            Line data    Source code
       1              : /****************************************************************************/
       2              : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3              : // Copyright (C) 2001-2024 German Aerospace Center (DLR) and others.
       4              : // This program and the accompanying materials are made available under the
       5              : // terms of the Eclipse Public License 2.0 which is available at
       6              : // https://www.eclipse.org/legal/epl-2.0/
       7              : // This Source Code may also be made available under the following Secondary
       8              : // Licenses when the conditions for such availability set forth in the Eclipse
       9              : // Public License 2.0 are satisfied: GNU General Public License, version 2
      10              : // or later which is available at
      11              : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
      12              : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
      13              : /****************************************************************************/
      14              : /// @file    LineReader.h
      15              : /// @author  Daniel Krajzewicz
      16              : /// @author  Michael Behrisch
      17              : /// @date    Fri, 19 Jul 2002
      18              : ///
      19              : // Retrieves a file linewise and reports the lines to a handler.
      20              : /****************************************************************************/
      21              : #pragma once
      22              : #include <config.h>
      23              : 
      24              : #include <string>
      25              : #include <fstream>
      26              : #include <utils/common/UtilExceptions.h>
      27              : 
      28              : 
      29              : // ===========================================================================
      30              : // class declarations
      31              : // ===========================================================================
      32              : class LineHandler;
      33              : 
      34              : 
      35              : // ===========================================================================
      36              : // class definitions
      37              : // ===========================================================================
      38              : /**
      39              :  * @class LineReader
      40              :  * @brief Retrieves a file linewise and reports the lines to a handler.
      41              :  *
      42              :  * This class reads the contents from a file line by line and report them to
      43              :  *  a LineHandler-derivate.
      44              :  * @see LineHandler
      45              :  * @todo No checks are done so far during reading/setting position etc.
      46              :  * @todo Should not IOError be thrown if something fails?
      47              :  */
      48              : class LineReader {
      49              : public:
      50              :     /// @brief Constructor
      51              :     LineReader();
      52              : 
      53              : 
      54              :     /** @brief Constructor
      55              :      *
      56              :      * Initialises reading from the file with the given name using setFile.
      57              :      *
      58              :      * @param[in] file The name of the file to open
      59              :      * @see setFile
      60              :      */
      61              :     LineReader(const std::string& file);
      62              : 
      63              : 
      64              :     /// @brief Destructor
      65              :     ~LineReader();
      66              : 
      67              : 
      68              :     /** @brief Returns whether another line may be read (the file was not read completely)
      69              :      * @return Whether further reading is possible
      70              :      */
      71              :     bool hasMore() const;
      72              : 
      73              : 
      74              :     /** @brief Reads the whole file linewise, reporting every line to the given LineHandler
      75              :      *
      76              :      * When the LineHandler returns false, the reading will be aborted
      77              :      *
      78              :      * @param[in] lh The LineHandler to report read lines to
      79              :      */
      80              :     void readAll(LineHandler& lh);
      81              : 
      82              : 
      83              :     /** @brief Reads a single (the next) line from the file and reports it to the given LineHandler
      84              :      *
      85              :      * When the LineHandler returns false, the reading will be aborted
      86              :      *
      87              :      * @param[in] lh The LineHandler to report read lines to
      88              :      * @return Whether a further line exists
      89              :      */
      90              :     bool readLine(LineHandler& lh);
      91              : 
      92              : 
      93              :     /** @brief Reads a single (the next) line from the file and returns it
      94              :      *
      95              :      * @return The next line in the file
      96              :      */
      97              :     std::string readLine();
      98              : 
      99              : 
     100              :     /// @brief Closes the reading
     101              :     void close();
     102              : 
     103              : 
     104              :     /** @brief Returns the name of the used file
     105              :      * @return The name of the opened file
     106              :      */
     107              :     std::string getFileName() const;
     108              : 
     109              : 
     110              :     /** @brief Reinitialises the reader for reading from the given file
     111              :      *
     112              :      * Returns false when the file is not readable
     113              :      *
     114              :      * @param[in] file The name of the file to open
     115              :      * @return Whether the file could be opened
     116              :      */
     117              :     bool setFile(const std::string& file);
     118              : 
     119              : 
     120              :     /** @brief Returns the current position within the file
     121              :      * @return The current position within the opened file
     122              :      */
     123              :     unsigned long getPosition();
     124              : 
     125              : 
     126              :     /// @brief Reinitialises the reading (of the previous file)
     127              :     void reinit();
     128              : 
     129              : 
     130              :     /** @brief Sets the current position within the file to the given value
     131              :      *
     132              :      * @param[in] pos The new position within the file
     133              :      */
     134              :     void setPos(unsigned long pos);
     135              : 
     136              : 
     137              :     /** @brief Returns the information whether the stream is readable
     138              :      * @return Whether the file is usable (good())
     139              :      */
     140              :     bool good() const;
     141              : 
     142              : 
     143              :     int getLineNumber() {
     144          163 :         return myLinesRead;
     145              :     }
     146              : 
     147              : private:
     148              :     /// @brief the name of the file to read the contents from
     149              :     std::string myFileName;
     150              : 
     151              :     /// @brief the stream used
     152              :     std::ifstream myStrm;
     153              : 
     154              :     /// @brief To override MSVC++-bugs, we use an own getline which uses this buffer
     155              :     char myBuffer[1024];
     156              : 
     157              :     /// @brief a string-buffer
     158              :     std::string myStrBuffer;
     159              : 
     160              :     /// @brief Information about how many characters were supplied to the LineHandler
     161              :     int myRead;
     162              : 
     163              :     /// @brief Information how many bytes are available within the used file
     164              :     int myAvailable;
     165              : 
     166              :     /// @brief Information how many bytes were read by the reader from the file
     167              :     int myRread;
     168              : 
     169              :     /// @brief Information how many lines were read for meaningful error messages
     170              :     int myLinesRead;
     171              : 
     172              :     /// @brief Number of skipped characters at the file begin (UTF-8 BOM)
     173              :     int mySkipBOM;
     174              : };
        

Generated by: LCOV version 2.0-1