|
19 | 19 |
|
20 | 20 | #include "json-writer.hh" |
21 | 21 |
|
| 22 | +#include "abstract-tree.hh" |
22 | 23 | #include "regex.hh" |
23 | 24 | #include "shared-string-ptree.hh" |
24 | 25 |
|
|
30 | 31 |
|
31 | 32 | typedef SharedStringPTree PTree; |
32 | 33 |
|
33 | | -struct JsonWriter::Private { |
34 | | - std::ostream &str; |
35 | | - std::queue<Defect> defQueue; |
36 | | - TScanProps scanProps; |
| 34 | +class SimpleTreeEncoder: public AbstractTreeEncoder { |
| 35 | + public: |
| 36 | + /// import supported scan properties |
| 37 | + void importScanProps(const TScanProps &) override; |
37 | 38 |
|
38 | | - Private(std::ostream &str_): |
39 | | - str(str_) |
40 | | - { |
41 | | - } |
42 | | -}; |
| 39 | + /// append single defect |
| 40 | + void appendDef(const Defect &) override; |
43 | 41 |
|
44 | | -JsonWriter::JsonWriter(std::ostream &str, const EFileFormat format): |
45 | | - d(new Private(str)) |
46 | | -{ |
47 | | - switch (format) { |
48 | | - case FF_JSON: |
49 | | - break; |
| 42 | + /// write everything to the given output stream |
| 43 | + void writeTo(std::ostream &) override; |
50 | 44 |
|
51 | | - default: |
52 | | - throw std::runtime_error("unknown output format"); |
53 | | - } |
54 | | -} |
| 45 | + private: |
| 46 | + PTree root_; |
| 47 | + PTree *pDefects_ = nullptr; |
| 48 | +}; |
55 | 49 |
|
56 | | -JsonWriter::~JsonWriter() |
| 50 | +void SimpleTreeEncoder::importScanProps(const TScanProps &scanProps) |
57 | 51 | { |
58 | | - delete d; |
59 | | -} |
| 52 | + if (scanProps.empty()) |
| 53 | + return; |
60 | 54 |
|
61 | | -const TScanProps& JsonWriter::getScanProps() const |
62 | | -{ |
63 | | - return d->scanProps; |
64 | | -} |
| 55 | + PTree scan; |
| 56 | + for (TScanProps::const_reference prop : scanProps) |
| 57 | + scan.put<std::string>(prop.first, prop.second); |
65 | 58 |
|
66 | | -void JsonWriter::setScanProps(const TScanProps &scanProps) |
67 | | -{ |
68 | | - d->scanProps = scanProps; |
| 59 | + root_.put_child("scan", scan); |
69 | 60 | } |
70 | 61 |
|
71 | | -void appendDefectNode(PTree &dst, const Defect &def) |
| 62 | +void SimpleTreeEncoder::appendDef(const Defect &def) |
72 | 63 | { |
73 | 64 | using std::string; |
74 | 65 |
|
@@ -113,8 +104,58 @@ void appendDefectNode(PTree &dst, const Defect &def) |
113 | 104 | defNode.put<int>("key_event_idx", def.keyEventIdx); |
114 | 105 | defNode.put_child("events", evtList); |
115 | 106 |
|
| 107 | + // create the node representing the list of defects |
| 108 | + if (!pDefects_) |
| 109 | + pDefects_ = &root_.put_child("defects", PTree()); |
| 110 | + |
116 | 111 | // append the node to the list |
117 | | - dst.push_back(std::make_pair("", defNode)); |
| 112 | + pDefects_->push_back(std::make_pair("", defNode)); |
| 113 | +} |
| 114 | + |
| 115 | +void SimpleTreeEncoder::writeTo(std::ostream &str) |
| 116 | +{ |
| 117 | + write_json(str, root_); |
| 118 | +} |
| 119 | + |
| 120 | +struct JsonWriter::Private { |
| 121 | + std::ostream &str; |
| 122 | + std::queue<Defect> defQueue; |
| 123 | + TScanProps scanProps; |
| 124 | + AbstractTreeEncoder *encoder; |
| 125 | + |
| 126 | + Private(std::ostream &str_): |
| 127 | + str(str_) |
| 128 | + { |
| 129 | + } |
| 130 | +}; |
| 131 | + |
| 132 | +JsonWriter::JsonWriter(std::ostream &str, const EFileFormat format): |
| 133 | + d(new Private(str)) |
| 134 | +{ |
| 135 | + switch (format) { |
| 136 | + case FF_JSON: |
| 137 | + d->encoder = new SimpleTreeEncoder; |
| 138 | + break; |
| 139 | + |
| 140 | + default: |
| 141 | + throw std::runtime_error("unknown output format"); |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +JsonWriter::~JsonWriter() |
| 146 | +{ |
| 147 | + delete d->encoder; |
| 148 | + delete d; |
| 149 | +} |
| 150 | + |
| 151 | +const TScanProps& JsonWriter::getScanProps() const |
| 152 | +{ |
| 153 | + return d->scanProps; |
| 154 | +} |
| 155 | + |
| 156 | +void JsonWriter::setScanProps(const TScanProps &scanProps) |
| 157 | +{ |
| 158 | + d->scanProps = scanProps; |
118 | 159 | } |
119 | 160 |
|
120 | 161 | void JsonWriter::handleDef(const Defect &def) |
@@ -143,24 +184,13 @@ void JsonWriter::flush() |
143 | 184 |
|
144 | 185 | str.push(d->str); |
145 | 186 |
|
146 | | - // encode scan properties if we have some |
147 | | - PTree root; |
148 | | - if (!d->scanProps.empty()) { |
149 | | - PTree scan; |
150 | | - for (TScanProps::const_reference prop : d->scanProps) |
151 | | - scan.put<std::string>(prop.first, prop.second); |
152 | | - |
153 | | - root.put_child("scan", scan); |
154 | | - } |
155 | | - |
156 | | - // node representing the list of defects |
157 | | - root.put_child("defects", PTree()); |
158 | | - PTree &defects = root.get_child("defects"); |
| 187 | + // transfer scan properties if available |
| 188 | + d->encoder->importScanProps(d->scanProps); |
159 | 189 |
|
160 | 190 | // go through the queue and move defects one by one to the property tree |
161 | 191 | for (; !d->defQueue.empty(); d->defQueue.pop()) |
162 | | - appendDefectNode(defects, d->defQueue.front()); |
| 192 | + d->encoder->appendDef(d->defQueue.front()); |
163 | 193 |
|
164 | 194 | // finally encode the tree as JSON |
165 | | - write_json(str, root); |
| 195 | + d->encoder->writeTo(str); |
166 | 196 | } |
0 commit comments