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

          Line data    Source code
       1             : /**
       2             : * @file      Encoder.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 Encoder class.
       6             : */
       7             : 
       8             : #include "Encoder.h"
       9             : 
      10             : #include "BDictionary.h"
      11             : #include "BInteger.h"
      12             : #include "BList.h"
      13             : #include "BString.h"
      14             : #include "Utils.h"
      15             : 
      16             : namespace bencoding {
      17             : 
      18             : /**
      19             : * @brief Constructs an encoder.
      20             : */
      21          12 : Encoder::Encoder() {}
      22             : 
      23             : /**
      24             : * @brief Creates a new encoder.
      25             : */
      26          12 : std::unique_ptr<Encoder> Encoder::create() {
      27          12 :     return std::unique_ptr<Encoder>(new Encoder());
      28             : }
      29             : 
      30             : /**
      31             : * @brief Encodes the given @a data and returns them.
      32             : */
      33          11 : std::string Encoder::encode(std::shared_ptr<BItem> data) {
      34          11 :     data->accept(this);
      35          11 :     return encodedData;
      36             : }
      37             : 
      38           3 : void Encoder::visit(BDictionary *bDictionary) {
      39             :     // See the description of Decoder::decodeDictionary() for the format and
      40             :     // example.
      41           3 :     encodedData += "d";
      42           6 :     for (auto item : *bDictionary) {
      43           3 :         item.first->accept(this);
      44           3 :         item.second->accept(this);
      45             :     }
      46           3 :     encodedData += "e";
      47           3 : }
      48             : 
      49           7 : void Encoder::visit(BInteger *bInteger) {
      50             :     // See the description of Decoder::decodeInteger() for the format and
      51             :     // example.
      52          14 :     std::string encodedInteger("i" + std::to_string(bInteger->value()) + "e");
      53           7 :     encodedData += encodedInteger;
      54           7 : }
      55             : 
      56           2 : void Encoder::visit(BList *bList) {
      57             :     // See the description of Decoder::decodeList() for the format and example.
      58           2 :     encodedData += "l";
      59           4 :     for (auto bItem : *bList) {
      60           2 :         bItem->accept(this);
      61             :     }
      62           2 :     encodedData += "e";
      63           2 : }
      64             : 
      65           7 : void Encoder::visit(BString *bString) {
      66             :     // See the description of Decoder::decodeString() for the format and
      67             :     // example.
      68             :     std::string encodedString(
      69          14 :         std::to_string(bString->length()) + ":" + bString->value()
      70          14 :     );
      71           7 :     encodedData += encodedString;
      72           7 : }
      73             : 
      74             : /**
      75             : * @brief Encodes the given @a data and returns them.
      76             : *
      77             : * This function can be handy if you just want to encode data without explicitly
      78             : * creating an encoder and calling @c encode() on it.
      79             : *
      80             : * See Encoder::encode() for more details.
      81             : */
      82           1 : std::string encode(std::shared_ptr<BItem> data) {
      83           2 :     auto encoder = Encoder::create();
      84           2 :     return encoder->encode(data);
      85             : }
      86             : 
      87             : } // namespace bencoding

Generated by: LCOV version 1.13