Tomato
KWUtilYaml.h
Go to the documentation of this file.
1 
8 #ifndef KWUTILYAML_H
9 #define KWUTILYAML_H
10 
11 #include "CmakeConfigForTomato.h"
12 #ifdef USE_YAML
13 
14 #include "yaml.h"
15 
16 //#define YAML_BUFFER_SIZE 65536
17 
18 class KWUtilYaml {
19 public:
20 
21  static int addMapping(yaml_document_t *document, int mapping_node_number, const std::string& name, const std::string& value){
22 
23  int last_node = (int)(document->nodes.top - document->nodes.start + 1);
24 
25  yaml_document_add_scalar(document, (yaml_char_t*)YAML_DEFAULT_SCALAR_TAG, (yaml_char_t*)name.c_str(), (int)name.length(), YAML_PLAIN_SCALAR_STYLE);
26  yaml_document_add_scalar(document, (yaml_char_t*)YAML_DEFAULT_SCALAR_TAG, (yaml_char_t*)value.c_str(), (int)value.length(), YAML_PLAIN_SCALAR_STYLE);
27  yaml_document_append_mapping_pair(document, mapping_node_number, last_node, last_node+1);
28 
29  return 0; // EXIT_SUCCESS
30  }
31 
32  template< typename MeasureType >
33  static int addSequenceOfNumbers(yaml_document_t *document, int mapping_node_number, const std::string& name, std::vector<MeasureType> vectorOfNumberValues){
34 
35  std::vector<std::string> vectorOfValues(vectorOfNumberValues.size());
36  for (size_t i = 0; i < vectorOfNumberValues.size(); i++){
37  vectorOfValues[i] = KWUtil::NumberToString(vectorOfNumberValues[i]);
38  }
39 
40  int last_node = (int)(document->nodes.top - document->nodes.start + 1);
41  yaml_document_add_sequence(document, (yaml_char_t*)YAML_DEFAULT_SEQUENCE_TAG, YAML_FLOW_SEQUENCE_STYLE);
42  yaml_document_add_scalar(document, (yaml_char_t*)YAML_DEFAULT_SCALAR_TAG, (yaml_char_t*)name.c_str(), (int)name.length(), YAML_PLAIN_SCALAR_STYLE);
43 
44  for (size_t i = 0; i < vectorOfValues.size(); i++) {
45  yaml_document_add_scalar(document, (yaml_char_t *) YAML_DEFAULT_SCALAR_TAG, (yaml_char_t*)vectorOfValues.at(i).c_str(), (int)vectorOfValues[i].length(), YAML_PLAIN_SCALAR_STYLE);
46  }
47  yaml_document_append_mapping_pair(document, mapping_node_number, last_node+1, last_node);
48  for (size_t i = 0; i < vectorOfValues.size(); i++) {
49  yaml_document_append_sequence_item(document, last_node, last_node+i+2);
50  }
51 
52  return 0; // EXIT_SUCCESS
53  }
54 
55  static int addSequence(yaml_document_t *document, int mapping_node_number, const std::string& name, std::vector<std::string> vectorOfValues){
56 
57  int last_node = (int)(document->nodes.top - document->nodes.start + 1);
58  yaml_document_add_sequence(document, (yaml_char_t*)YAML_DEFAULT_SEQUENCE_TAG, YAML_ANY_SEQUENCE_STYLE);
59  yaml_document_add_scalar(document, (yaml_char_t*)YAML_DEFAULT_SCALAR_TAG, (yaml_char_t*)name.c_str(), (int)name.length(), YAML_PLAIN_SCALAR_STYLE);
60 
61  for (size_t i = 0; i < vectorOfValues.size(); i++) {
62  yaml_document_add_scalar(document, (yaml_char_t *) YAML_DEFAULT_SCALAR_TAG, (yaml_char_t*)vectorOfValues.at(i).c_str(), (int)vectorOfValues[i].length(), YAML_DOUBLE_QUOTED_SCALAR_STYLE);
63  }
64  yaml_document_append_mapping_pair(document, mapping_node_number, last_node+1, last_node);
65  for (size_t i = 0; i < vectorOfValues.size(); i++) {
66  yaml_document_append_sequence_item(document, last_node, last_node+i+2);
67  }
68 
69  return 0; // EXIT_SUCCESS
70  }
71 
72 // static void printNodes(const yaml_document_t *document){
73 // yaml_node_pair_t *pair;
74 // yaml_node_item_t *item;
75 // yaml_node_t *node;
76 // int nn;
77 // for (node = document->nodes.start; node < document->nodes.top; node ++) {
78 // nn = node - document->nodes.start + 1;
79 // switch (node->type) {
80 // case YAML_SCALAR_NODE:
81 // printf("node %d node->type %d node->tag %s %d %s %c\n", nn, node->type, node->tag,
82 // (int)node->data.scalar.length, node->data.scalar.value,
83 // node->data.scalar.style);
84 // break;
85 // case YAML_MAPPING_NODE:
86 // for (pair = node->data.mapping.pairs.start;
87 // pair < node->data.mapping.pairs.top; pair++) {
88 // printf("node %d node->type %d node->tag %s pair->key %d pair->value %d\n", nn,
89 // node->type, node->tag, pair->key, pair->value);
90 // }
91 // break;
92 // case YAML_SEQUENCE_NODE:
93 // for (item = node->data.sequence.items.start;
94 // item < node->data.sequence.items.top; item++) {
95 // printf("node %d node->type %d node->tag %s item %d\n", nn, node->type, node->tag,
96 // *item);
97 // }
98 // break;
99 // default:
100 // break;
101 // }
102 // }
103 // }
104 };
105 
106 //#undef YAML_BUFFER_SIZE
107 
108 #endif //USE_YAML
109 
110 #endif //KWUTILYAML_H