LCOV - code coverage report
Current view: top level - src - LineBuffer.cpp (source / functions) Hit Total Coverage
Test: lcov.info Lines: 31 33 93.9 %
Date: 2025-04-27 01:14:20 Functions: 5 5 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:06:23+01:00
      21                 :            : **     Author: Sylvain Fargier <fargie_s> <fargier.sylvain@gmail.com>
      22                 :            : **
      23                 :            : */
      24                 :            : 
      25                 :            : #include "LineBuffer.hpp"
      26                 :            : 
      27                 :            : #include <algorithm>
      28                 :            : 
      29                 :            : #include <logger/Logger.hpp>
      30                 :            : 
      31                 :            : #include "utils.hpp"
      32                 :            : 
      33                 :            : static const std::string s_loggerCat{"ccut:linebuffer"};
      34                 :            : 
      35                 :            : namespace ccut {
      36                 :            : 
      37                 :          3 : LineBuffer::LineBuffer(std::size_t size) : m_count(0), m_pos(0), m_buffer(size)
      38                 :          3 : {}
      39                 :            : 
      40                 :       1166 : bool LineBuffer::add(char c)
      41                 :            : {
      42                 :       1166 :     if (m_pos + 1 > m_buffer.size())
      43                 :            :     {
      44                 :          1 :         logger::crit(s_loggerCat) << "buffer full, flushing";
      45                 :          1 :         m_pos = 0;
      46                 :            :     }
      47                 :       1166 :     m_buffer[m_pos++] = c;
      48                 :       1166 :     if (c == '\n')
      49                 :          4 :         ++m_count;
      50                 :       1166 :     return m_count != 0;
      51                 :            : }
      52                 :            : 
      53                 :          1 : bool LineBuffer::fwd(std::size_t sz)
      54                 :            : {
      55                 :          1 :     if (m_pos + sz > m_buffer.size())
      56                 :            :     {
      57                 :          0 :         logger::emerg(s_loggerCat) << "buffer overflow";
      58                 :          0 :         sz = m_buffer.size() - m_pos;
      59                 :            :     }
      60                 :          1 :     std::for_each(m_buffer.cbegin() + m_pos, m_buffer.cbegin() + m_pos + sz,
      61                 :          6 :                   [this](char c) {
      62                 :          5 :                       if (c == '\n')
      63                 :          1 :                           ++m_count;
      64                 :          5 :                   });
      65                 :          1 :     m_pos += sz;
      66                 :          1 :     return m_count != 0;
      67                 :            : }
      68                 :            : 
      69                 :          7 : bool LineBuffer::takeLine(std::string &line)
      70                 :            : {
      71                 :          7 :     const std::vector<char>::const_iterator itEnd = m_buffer.cbegin() + m_pos;
      72                 :          7 :     std::vector<char>::const_iterator it = std::find(m_buffer.cbegin(), itEnd,
      73                 :          7 :                                                      '\n');
      74                 :          7 :     if (it != itEnd)
      75                 :            :     {
      76                 :          5 :         line = std::string(m_buffer.cbegin(), it);
      77                 :          5 :         m_pos -= line.size() + 1;
      78                 :          5 :         std::copy(++it, itEnd, m_buffer.begin());
      79                 :          5 :         --m_count;
      80                 :          5 :         line = ccut::trim(line);
      81                 :          5 :         return true;
      82                 :            :     }
      83                 :          2 :     return false;
      84                 :            : }
      85                 :            : 
      86                 :            : } // namespace ccut

Generated by: LCOV version 1.14