Fawkes API  Fawkes Development Version
qa_yaml.cpp
1 
2 /***************************************************************************
3  * qa_yaml.cpp - QA for YAML configuration storage
4  *
5  * Created: Wed Aug 01 18:53:22 2012
6  * Copyright 2012 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 /// @cond QA
25 
26 #include <config/yaml.h>
27 
28 #include <iostream>
29 #include <cstdio>
30 
31 using namespace std;
32 using namespace fawkes;
33 
34 int
35 main(int argc, char **argv)
36 {
37  YamlConfiguration *config = new YamlConfiguration(CONFDIR);
38 
39  try {
40  printf("=== Loading configuration ===\n");
41  config->load("config.yaml");
42  cout << "...done" << endl;
43  } catch (CouldNotOpenConfigException &e) {
44  cout << "...failed" << endl;
45  e.print_trace();
46  return -1;
47  }
48 
49 
50  printf("\n\n=== Reading some assorted values ===\n");
51 
52  unsigned int u = config->get_uint("/fawkes/mainapp/blackboard_size");
53  printf("Blackboard size: %u\n", u);
54 
55  std::string s = config->get_string("/hardware/roomba/connection_type");
56  printf("Roomba connection type: %s\n", s.c_str());
57 
58  Configuration::ValueIterator *i = config->get_value("/hardware/roomba/connection_type");
59  if (i->next() && i->is_string()) {
60  printf("Again as iterator: %s\n", i->get_string().c_str());
61  } else {
62  printf("!!! Failed, iterator value is not a string\n");
63  }
64  delete i;
65 
66  printf("\n\n=== Printing ALL values ===\n");
67  i = config->iterator();
68  while (i->next()) {
69  printf("%s: %s (%s)\n", i->path(), i->get_as_string().c_str(), i->type());
70  }
71  delete i;
72 
73 
74  printf("\n\n=== Printing values with prefix /webview ===\n");
75  i = config->search("/webview");
76  while (i->next()) {
77  printf("%s: %s (%s)\n", i->path(), i->get_as_string().c_str(), i->type());
78  }
79  delete i;
80 
81  printf("\n\n=== Printing values with prefix /hardware/laser/ ===\n");
82  i = config->search("/hardware/laser/");
83  while (i->next()) {
84  printf("%s: %s (%s)\n", i->path(), i->get_as_string().c_str(), i->type());
85  }
86  delete i;
87 
88  printf("\n\n=== Setting /z/foo/bar to test ===\n");
89  config->set_string("/z/foo/bar", "test");
90  printf("Reading back: %s\n", config->get_string("/z/foo/bar").c_str());
91 
92 
93  printf("\n\n=== Erase test ===\n");
94  config->set_string("/z/erase/1", "test1");
95  config->set_string("/z/erase/2", "test2");
96  config->set_string("/z/erase/3", "test3");
97  config->set_string("/z/erase/4", "test4");
98  config->set_string("/z/erase/5", "test5");
99  printf("- Before erasing:\n");
100  i = config->search("/z/erase");
101  while (i->next()) {
102  printf(" %s: %s (%s)\n", i->path(), i->get_as_string().c_str(), i->type());
103  }
104  delete i;
105 
106  printf("- Now erasing /z/erase/4... afterwards:\n");
107  config->erase("/z/erase/4");
108  i = config->search("/z/erase");
109  while (i->next()) {
110  printf(" %s: %s (%s)\n", i->path(), i->get_as_string().c_str(), i->type());
111  }
112  delete i;
113 
114  printf("- Now erasing /z/erase/6 (which does not exist)\n");
115  try {
116  config->erase("/z/erase/6");
117  } catch (Exception &e) {
118  printf(" Got exception as expected: %s\n", e.what_no_backtrace());
119  }
120 
121 
122  config->set_string("/z/erase/second/1", "test1");
123  config->set_string("/z/erase/second/2", "test2");
124  printf("- Before second erasing:\n");
125  i = config->search("/z/erase");
126  while (i->next()) {
127  printf(" %s: %s (%s)\n", i->path(), i->get_as_string().c_str(), i->type());
128  }
129  delete i;
130 
131  printf("- Now erasing /z/erase/second/*... afterwards:\n");
132  config->erase("/z/erase/second/1");
133  config->erase("/z/erase/second/2");
134  i = config->search("/z/erase");
135  while (i->next()) {
136  printf(" %s: %s (%s)\n", i->path(), i->get_as_string().c_str(), i->type());
137  }
138  delete i;
139 
140 
141  delete config;
142 
143  return 0;
144 }
145 
146 
147 
148 /// @endcond
ValueIterator * search(const char *path)
Iterator with search results.
Definition: yaml.cpp:1384
ValueIterator * iterator()
Iterator for all values.
Definition: yaml.cpp:1376
Fawkes library namespace.
Thrown if config could not be opened.
Definition: config.h:61
STL namespace.
virtual void set_string(const char *path, std::string &s)
Set new value in configuration of type string.
Definition: yaml.cpp:1205
virtual ValueIterator * get_value(const char *path)
Get value from configuration.
Definition: yaml.cpp:1147
virtual void load(const char *file_path)
Load configuration.
Definition: yaml.cpp:427
virtual std::string get_string(const char *path)
Get value from configuration which is of type string.
Definition: yaml.cpp:1027
virtual unsigned int get_uint(const char *path)
Get value from configuration which is of type unsigned int.
Definition: yaml.cpp:1009
Configuration store using YAML documents.
Definition: yaml.h:49
Base class for exceptions in Fawkes.
Definition: exception.h:36
virtual const char * what_no_backtrace() const
Get primary string (does not implicitly print the back trace).
Definition: exception.cpp:686
void print_trace()
Prints trace to stderr.
Definition: exception.cpp:619
Iterator interface to iterate over config values.
Definition: config.h:72
virtual void erase(const char *path)
Erase the given value from the configuration.
Definition: yaml.cpp:1269