LCOV - code coverage report
Current view: top level - src - LineBuffer.hpp (source / functions) Hit Total Coverage
Test: lcov.info Lines: 7 7 100.0 %
Date: 2025-04-27 01:14:20 Functions: 7 7 100.0 %
Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            : ** Copyright (C) 2020 Sylvain Fargier
       3                 :            : **
       4                 :            : ** This software is provided 'as-is', without any express or implied
       5                 :            : ** warranty.  In no event will the authors be held liable for any damages
       6                 :            : ** arising from the use of this software.
       7                 :            : **
       8                 :            : ** Permission is granted to anyone to use this software for any purpose,
       9                 :            : ** including commercial applications, and to alter it and redistribute it
      10                 :            : ** freely, subject to the following restrictions:
      11                 :            : **
      12                 :            : ** 1. The origin of this software must not be misrepresented; you must not
      13                 :            : **    claim that you wrote the original software. If you use this software
      14                 :            : **    in a product, an acknowledgment in the product documentation would be
      15                 :            : **    appreciated but is not required.
      16                 :            : ** 2. Altered source versions must be plainly marked as such, and must not be
      17                 :            : **    misrepresented as being the original software.
      18                 :            : ** 3. This notice may not be removed or altered from any source distribution.
      19                 :            : **
      20                 :            : ** Created on: 2022-02-12T18:05:36+01:00
      21                 :            : **     Author: Sylvain Fargier <fargie_s> <fargier.sylvain@gmail.com>
      22                 :            : **
      23                 :            : */
      24                 :            : 
      25                 :            : #ifndef LINEBUFFER_HPP__
      26                 :            : #define LINEBUFFER_HPP__
      27                 :            : 
      28                 :            : #include <cstdint>
      29                 :            : #include <string>
      30                 :            : #include <vector>
      31                 :            : 
      32                 :            : namespace ccut {
      33                 :            : 
      34                 :            : /**
      35                 :            :  * @brief basic line oriented buffer
      36                 :            :  * @details add bytes then use takeLine to retrieve lines
      37                 :            :  */
      38                 :            : class LineBuffer
      39                 :            : {
      40                 :            : public:
      41                 :            :     /**
      42                 :            :      * @brief LineBuffer fixed-size line buffering utility
      43                 :            :      * @param size max-buffered bytes size
      44                 :            :      */
      45                 :            :     explicit LineBuffer(std::size_t size = defaultSize);
      46                 :            : 
      47                 :            :     /**
      48                 :            :      * @brief add a character in the buffer
      49                 :            :      * @param[in] c character to inject
      50                 :            :      * @return true if a line is available
      51                 :            :      * @details on overflow buffer is flushed
      52                 :            :      */
      53                 :            :     bool add(char c);
      54                 :            : 
      55                 :            :     /**
      56                 :            :      * @brief move buffer forward
      57                 :            :      * @details this is used when direct-injecting data in the buffer
      58                 :            :      * @param[in]  sz number of bytes to move to
      59                 :            :      * @return true if a line is available
      60                 :            :      */
      61                 :            :     bool fwd(std::size_t sz);
      62                 :            : 
      63                 :            :     /**
      64                 :            :      * @brief consume one line from the buffer
      65                 :            :      * @param[out]  line consumed line
      66                 :            :      * @return true on success
      67                 :            :      */
      68                 :            :     bool takeLine(std::string &line);
      69                 :            : 
      70                 :            :     /**
      71                 :            :      * @brief check if lines are available
      72                 :            :      * @return true if a line is available in buffer
      73                 :            :      */
      74                 :          2 :     inline bool hasLine() const { return m_count != 0; }
      75                 :            : 
      76                 :            :     /**
      77                 :            :      * @brief clear buffer
      78                 :            :      */
      79                 :          1 :     inline void reset() { m_pos = m_count = 0; }
      80                 :            : 
      81                 :            :     /**
      82                 :            :      * @return number of bytes free in buffer
      83                 :            :      */
      84                 :       1154 :     inline std::size_t remaining() const { return m_buffer.size() - m_pos; }
      85                 :            : 
      86                 :          2 :     inline std::size_t capacity() const { return m_buffer.size(); }
      87                 :          3 :     inline std::size_t size() const { return m_pos; }
      88                 :          3 :     inline bool empty() const { return m_pos == 0; }
      89                 :            : 
      90                 :            :     /**
      91                 :            :      * @return pointer past the end of buffered data
      92                 :            :      */
      93                 :          1 :     inline char *end() { return &m_buffer[m_pos]; }
      94                 :            : 
      95                 :            :     static constexpr std::size_t defaultSize = 1024;
      96                 :            : 
      97                 :            : protected:
      98                 :            :     std::size_t m_count;
      99                 :            :     std::size_t m_pos;
     100                 :            :     std::vector<char> m_buffer;
     101                 :            : };
     102                 :            : 
     103                 :            : } // namespace ccut
     104                 :            : 
     105                 :            : #endif

Generated by: LCOV version 1.14