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
31using namespace std;
32using namespace fawkes;
33
34int
35main(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
Iterator interface to iterate over config values.
Definition: config.h:75
virtual const char * path() const =0
Path of value.
virtual bool next()=0
Check if there is another element and advance to this if possible.
virtual std::string get_as_string() const =0
Get value as string.
virtual bool is_string() const =0
Check if current value is a string.
virtual const char * type() const =0
Type of value.
virtual std::string get_string() const =0
Get string value.
virtual unsigned int get_uint(const char *path)=0
Get value from configuration which is of type unsigned int.
virtual ValueIterator * iterator()=0
Iterator for all values.
virtual ValueIterator * search(const char *path)=0
Iterator with search results.
virtual ValueIterator * get_value(const char *path)=0
Get value from configuration.
virtual void set_string(const char *path, std::string &s)=0
Set new value in configuration of type string.
virtual void load(const char *file_path)=0
Load configuration.
virtual std::string get_string(const char *path)=0
Get value from configuration which is of type string.
virtual void erase(const char *path)=0
Erase the given value from the configuration.
Thrown if config could not be opened.
Definition: config.h:59
Base class for exceptions in Fawkes.
Definition: exception.h:36
void print_trace() noexcept
Prints trace to stderr.
Definition: exception.cpp:601
virtual const char * what_no_backtrace() const noexcept
Get primary string (does not implicitly print the back trace).
Definition: exception.cpp:663
Configuration store using YAML documents.
Definition: yaml.h:43
Fawkes library namespace.