LCOV - code coverage report
Current view: top level - src - Utils.cpp (source / functions) Hit Total Coverage
Test: cpp-bencoding code coverage Lines: 18 18 100.0 %
Date: 2018-04-21 15:28:44 Functions: 3 3 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /**
       2             : * @file      Utils.cpp
       3             : * @copyright (c) 2014 by Petr Zemek (s3rvac@gmail.com) and contributors
       4             : * @license   BSD, see the @c LICENSE file for more details
       5             : * @brief     Implementation of the utilities.
       6             : */
       7             : 
       8             : #include "Utils.h"
       9             : 
      10             : namespace bencoding {
      11             : 
      12             : /**
      13             : * @brief Reads data from the given @a stream up to @a sentinel, which is left
      14             : *        in @a stream.
      15             : *
      16             : * @param[in] stream Stream from which the data are read.
      17             : * @param[out] readData String into which the read data are stored.
      18             : * @param[in] sentinel The data are up to this character.
      19             : *
      20             : * @return @c true if all the data were read correctly up to @a sentinel, @c
      21             : *         false otherwise.
      22             : *
      23             : * @a sentinel is not read and is kept in the stream. If @a sentinel is not
      24             : * found during the reading, this function returns @c false. Read data are
      25             : * appended into @a readData.
      26             : */
      27          47 : bool readUpTo(std::istream &stream, std::string &readData, char sentinel) {
      28             :     // Do not use std::getline() because it eats the sentinel from the stream.
      29         114 :     while (stream.peek() != std::char_traits<char>::eof() &&
      30          40 :             stream.peek() != sentinel) {
      31          27 :         readData += stream.get();
      32             :     }
      33          20 :     return stream && stream.peek() == sentinel;
      34             : }
      35             : 
      36             : /**
      37             : * @brief Reads data from the given @a stream until @a last, which is also read.
      38             : *
      39             : * @param[in] stream Stream from which the data are read.
      40             : * @param[out] readData String into which the read data are stored.
      41             : * @param[in] last The last character to be read.
      42             : *
      43             : * @return @c true if all the data were read correctly up and including @a last,
      44             : *         @c false otherwise.
      45             : *
      46             : * In contrast to readUpTo(), @a last is also read into @a readData. If @a last
      47             : * is not found during the reading, this function returns @c false. Read data
      48             : * are appended into @a readData.
      49             : */
      50         118 : bool readUntil(std::istream &stream, std::string &readData, char last) {
      51             :     char c;
      52         201 :     while (stream.get(c)) {
      53         112 :         readData += c;
      54         112 :         if (c == last) {
      55          29 :             return true;
      56             :         }
      57             :     }
      58           6 :     return false;
      59             : }
      60             : 
      61             : /**
      62             : * @brief Replaces all occurrences of @a what with @a withWhat in @a str and
      63             : *        returns the resulting string.
      64             : */
      65          15 : std::string replace(const std::string &str, char what,
      66             :         const std::string &withWhat) {
      67          15 :     std::string result;
      68          71 :     for (auto c : str) {
      69          56 :         if (c == what) {
      70           8 :             result += withWhat;
      71             :         } else {
      72          48 :             result += c;
      73             :         }
      74             :     }
      75          15 :     return result;
      76             : }
      77             : 
      78             : } // namespace bencoding

Generated by: LCOV version 1.13