Line data Source code
1 : /**
2 : * @file PrettyPrinter.h
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 Pretty printer of data.
6 : */
7 :
8 : #ifndef BENCODING_PRETTYPRINTER_H
9 : #define BENCODING_PRETTYPRINTER_H
10 :
11 : #include <memory>
12 : #include <string>
13 :
14 : #include "BItemVisitor.h"
15 :
16 : namespace bencoding {
17 :
18 : class BItem;
19 :
20 : /**
21 : * @brief Pretty printer of data.
22 : *
23 : * Can format data in a readable way.
24 : *
25 : * Use create() to create instances.
26 : */
27 39 : class PrettyPrinter: private BItemVisitor {
28 : public:
29 : static std::unique_ptr<PrettyPrinter> create();
30 :
31 : std::string getPrettyRepr(std::shared_ptr<BItem> data,
32 : const std::string &indent = " ");
33 :
34 : private:
35 : PrettyPrinter();
36 :
37 : /// @name BItemVisitor Interface
38 : /// @{
39 : virtual void visit(BDictionary *bDictionary) override;
40 : virtual void visit(BInteger *bInteger) override;
41 : virtual void visit(BList *bList) override;
42 : virtual void visit(BString *bString) override;
43 : /// @}
44 :
45 : /// @name Indentation
46 : /// @{
47 : void storeCurrentIndent();
48 : void increaseIndentLevel();
49 : void decreaseIndentLevel();
50 : /// @}
51 :
52 : private:
53 : /// Pretty representation of the data obtained so far.
54 : std::string prettyRepr = "";
55 :
56 : /// A single level of indentation.
57 : std::string indentLevel = " ";
58 :
59 : /// The current level of indentation.
60 : std::string currentIndent = "";
61 : };
62 :
63 : /// @name Printing Without Explicit Printer Creation
64 : /// @{
65 : std::string getPrettyRepr(std::shared_ptr<BItem> data,
66 : const std::string &indent = " ");
67 : /// @}
68 :
69 : } // namespace bencoding
70 :
71 : #endif
|