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 <cstdio>
29 #include <iostream>
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  printf("\n\n=== Reading some assorted values ===\n");
50 
51  unsigned int u = config->get_uint("/fawkes/mainapp/blackboard_size");
52  printf("Blackboard size: %u\n", u);
53 
54  std::string s = config->get_string("/hardware/roomba/connection_type");
55  printf("Roomba connection type: %s\n", s.c_str());
56 
57  Configuration::ValueIterator *i = config->get_value("/hardware/roomba/connection_type");
58  if (i->next() && i->is_string()) {
59  printf("Again as iterator: %s\n", i->get_string().c_str());
60  } else {
61  printf("!!! Failed, iterator value is not a string\n");
62  }
63  delete i;
64 
65  printf("\n\n=== Printing ALL values ===\n");
66  i = config->iterator();
67  while (i->next()) {
68  printf("%s: %s (%s)\n", i->path(), i->get_as_string().c_str(), i->type());
69  }
70  delete i;
71 
72  printf("\n\n=== Printing values with prefix /webview ===\n");
73  i = config->search("/webview");
74  while (i->next()) {
75  printf("%s: %s (%s)\n", i->path(), i->get_as_string().c_str(), i->type());
76  }
77  delete i;
78 
79  printf("\n\n=== Printing values with prefix /hardware/laser/ ===\n");
80  i = config->search("/hardware/laser/");
81  while (i->next()) {
82  printf("%s: %s (%s)\n", i->path(), i->get_as_string().c_str(), i->type());
83  }
84  delete i;
85 
86  printf("\n\n=== Setting /z/foo/bar to test ===\n");
87  config->set_string("/z/foo/bar", "test");
88  printf("Reading back: %s\n", config->get_string("/z/foo/bar").c_str());
89 
90  printf("\n\n=== Erase test ===\n");
91  config->set_string("/z/erase/1", "test1");
92  config->set_string("/z/erase/2", "test2");
93  config->set_string("/z/erase/3", "test3");
94  config->set_string("/z/erase/4", "test4");
95  config->set_string("/z/erase/5", "test5");
96  printf("- Before erasing:\n");
97  i = config->search("/z/erase");
98  while (i->next()) {
99  printf(" %s: %s (%s)\n", i->path(), i->get_as_string().c_str(), i->type());
100  }
101  delete i;
102 
103  printf("- Now erasing /z/erase/4... afterwards:\n");
104  config->erase("/z/erase/4");
105  i = config->search("/z/erase");
106  while (i->next()) {
107  printf(" %s: %s (%s)\n", i->path(), i->get_as_string().c_str(), i->type());
108  }
109  delete i;
110 
111  printf("- Now erasing /z/erase/6 (which does not exist)\n");
112  try {
113  config->erase("/z/erase/6");
114  } catch (Exception &e) {
115  printf(" Got exception as expected: %s\n", e.what_no_backtrace());
116  }
117 
118  config->set_string("/z/erase/second/1", "test1");
119  config->set_string("/z/erase/second/2", "test2");
120  printf("- Before second erasing:\n");
121  i = config->search("/z/erase");
122  while (i->next()) {
123  printf(" %s: %s (%s)\n", i->path(), i->get_as_string().c_str(), i->type());
124  }
125  delete i;
126 
127  printf("- Now erasing /z/erase/second/*... afterwards:\n");
128  config->erase("/z/erase/second/1");
129  config->erase("/z/erase/second/2");
130  i = config->search("/z/erase");
131  while (i->next()) {
132  printf(" %s: %s (%s)\n", i->path(), i->get_as_string().c_str(), i->type());
133  }
134  delete i;
135 
136  delete config;
137 
138  return 0;
139 }
140 
141 /// @endcond
fawkes::Configuration::set_string
virtual void set_string(const char *path, std::string &s)=0
fawkes::Configuration::iterator
virtual ValueIterator * iterator()=0
fawkes::YamlConfiguration
Definition: yaml.h:46
fawkes::Configuration::load
virtual void load(const char *file_path)=0
fawkes::Configuration::ValueIterator
Definition: config.h:75
fawkes::Configuration::search
virtual ValueIterator * search(const char *path)=0
fawkes::Configuration::erase
virtual void erase(const char *path)=0
fawkes
fawkes::Exception::print_trace
void print_trace()
Prints trace to stderr.
Definition: exception.cpp:600
fawkes::Exception::what_no_backtrace
virtual const char * what_no_backtrace() const
Get primary string (does not implicitly print the back trace).
Definition: exception.cpp:662
fawkes::CouldNotOpenConfigException
Definition: config.h:62
fawkes::Configuration::get_uint
virtual unsigned int get_uint(const char *path)=0
fawkes::Configuration::get_string
virtual std::string get_string(const char *path)=0
fawkes::Configuration::get_value
virtual ValueIterator * get_value(const char *path)=0
fawkes::Exception
Definition: exception.h:39