File size: 12,356 Bytes
9375c9a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
<html><!-- Created using the cpp_pretty_printer from the dlib C++ library. See http://dlib.net for updates. --><head><title>dlib C++ Library - config_reader_ex.cpp</title></head><body bgcolor='white'><pre>
<font color='#009900'>// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
</font><font color='#009900'>/*
This is an example illustrating the use of the config_reader component
from the dlib C++ Library.
This example uses the config_reader to load a config file and then
prints out the values of various fields in the file.
*/</font>
<font color='#0000FF'>#include</font> <font color='#5555FF'><</font>dlib<font color='#5555FF'>/</font>config_reader.h<font color='#5555FF'>></font>
<font color='#0000FF'>#include</font> <font color='#5555FF'><</font>iostream<font color='#5555FF'>></font>
<font color='#0000FF'>#include</font> <font color='#5555FF'><</font>fstream<font color='#5555FF'>></font>
<font color='#0000FF'>#include</font> <font color='#5555FF'><</font>vector<font color='#5555FF'>></font>
<font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> std;
<font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> dlib;
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font><font color='#009900'>// For reference, the contents of the config file used in this example is reproduced below:
</font><font color='#009900'>/*
# This is an example config file. Note that # is used to create a comment.
# At its most basic level a config file is just a bunch of key/value pairs.
# So for example:
key1 = value2
dlib = a C++ library
# You can also define "sub blocks" in your config files like so
user1
{
# Inside a sub block you can list more key/value pairs.
id = 42
name = davis
# you can also nest sub-blocks as deep as you want
details
{
editor = vim
home_dir = /home/davis
}
}
user2 {
id = 1234
name = joe
details {
editor = emacs
home_dir = /home/joe
}
}
*/</font>
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#0000FF'><u>void</u></font> <b><a name='print_config_reader_contents'></a>print_config_reader_contents</b> <font face='Lucida Console'>(</font>
<font color='#0000FF'>const</font> config_reader<font color='#5555FF'>&</font> cr,
<font color='#0000FF'><u>int</u></font> depth <font color='#5555FF'>=</font> <font color='#979000'>0</font>
<font face='Lucida Console'>)</font>;
<font color='#009900'>/*
This is a simple function that recursively walks through everything in
a config reader and prints it to the screen.
*/</font>
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#0000FF'><u>int</u></font> <b><a name='main'></a>main</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>
<b>{</b>
<font color='#0000FF'>try</font>
<b>{</b>
config_reader <font color='#BB00BB'>cr</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>config.txt</font>"<font face='Lucida Console'>)</font>;
<font color='#009900'>// Use our recursive function to print everything in the config file.
</font> <font color='#BB00BB'>print_config_reader_contents</font><font face='Lucida Console'>(</font>cr<font face='Lucida Console'>)</font>;
<font color='#009900'>// Now let's access some of the fields of the config file directly. You
</font> <font color='#009900'>// use [] for accessing key values and .block() for accessing sub-blocks.
</font>
<font color='#009900'>// Print out the string value assigned to key1 in the config file
</font> cout <font color='#5555FF'><</font><font color='#5555FF'><</font> cr["<font color='#CC0000'>key1</font>"] <font color='#5555FF'><</font><font color='#5555FF'><</font> endl;
<font color='#009900'>// Print out the name field inside the user1 sub-block
</font> cout <font color='#5555FF'><</font><font color='#5555FF'><</font> cr.<font color='#BB00BB'>block</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>user1</font>"<font face='Lucida Console'>)</font>["<font color='#CC0000'>name</font>"] <font color='#5555FF'><</font><font color='#5555FF'><</font> endl;
<font color='#009900'>// Now print out the editor field in the details block
</font> cout <font color='#5555FF'><</font><font color='#5555FF'><</font> cr.<font color='#BB00BB'>block</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>user1</font>"<font face='Lucida Console'>)</font>.<font color='#BB00BB'>block</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>details</font>"<font face='Lucida Console'>)</font>["<font color='#CC0000'>editor</font>"] <font color='#5555FF'><</font><font color='#5555FF'><</font> endl;
<font color='#009900'>// Note that you can use get_option() to easily convert fields into
</font> <font color='#009900'>// non-string types. For example, the config file has an integer id
</font> <font color='#009900'>// field that can be converted into an int like so:
</font> <font color='#0000FF'><u>int</u></font> id1 <font color='#5555FF'>=</font> <font color='#BB00BB'>get_option</font><font face='Lucida Console'>(</font>cr,"<font color='#CC0000'>user1.id</font>",<font color='#979000'>0</font><font face='Lucida Console'>)</font>;
<font color='#0000FF'><u>int</u></font> id2 <font color='#5555FF'>=</font> <font color='#BB00BB'>get_option</font><font face='Lucida Console'>(</font>cr,"<font color='#CC0000'>user2.id</font>",<font color='#979000'>0</font><font face='Lucida Console'>)</font>;
cout <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>user1's id is </font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> id1 <font color='#5555FF'><</font><font color='#5555FF'><</font> endl;
cout <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>user2's id is </font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> id2 <font color='#5555FF'><</font><font color='#5555FF'><</font> endl;
<font color='#009900'>// The third argument to get_option() is the default value returned if
</font> <font color='#009900'>// the config reader doesn't contain a corresponding entry. So for
</font> <font color='#009900'>// example, the following prints 321 since there is no user3.
</font> <font color='#0000FF'><u>int</u></font> id3 <font color='#5555FF'>=</font> <font color='#BB00BB'>get_option</font><font face='Lucida Console'>(</font>cr,"<font color='#CC0000'>user3.id</font>",<font color='#979000'>321</font><font face='Lucida Console'>)</font>;
cout <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>user3's id is </font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> id3 <font color='#5555FF'><</font><font color='#5555FF'><</font> endl;
<b>}</b>
<font color='#0000FF'>catch</font> <font face='Lucida Console'>(</font>exception<font color='#5555FF'>&</font> e<font face='Lucida Console'>)</font>
<b>{</b>
<font color='#009900'>// Finally, note that the config_reader throws exceptions if the config
</font> <font color='#009900'>// file is corrupted or if you ask it for a key or block that doesn't exist.
</font> <font color='#009900'>// Here we print out any such error messages.
</font> cout <font color='#5555FF'><</font><font color='#5555FF'><</font> e.<font color='#BB00BB'>what</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'><</font><font color='#5555FF'><</font> endl;
<b>}</b>
<b>}</b>
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#0000FF'><u>void</u></font> <b><a name='print_config_reader_contents'></a>print_config_reader_contents</b> <font face='Lucida Console'>(</font>
<font color='#0000FF'>const</font> config_reader<font color='#5555FF'>&</font> cr,
<font color='#0000FF'><u>int</u></font> depth
<font face='Lucida Console'>)</font>
<b>{</b>
<font color='#009900'>// Make a string with depth*4 spaces in it.
</font> <font color='#0000FF'>const</font> string <font color='#BB00BB'>padding</font><font face='Lucida Console'>(</font>depth<font color='#5555FF'>*</font><font color='#979000'>4</font>, '<font color='#FF0000'> </font>'<font face='Lucida Console'>)</font>;
<font color='#009900'>// We can obtain a list of all the keys and sub-blocks defined
</font> <font color='#009900'>// at the current level in the config reader like so:
</font> vector<font color='#5555FF'><</font>string<font color='#5555FF'>></font> keys, blocks;
cr.<font color='#BB00BB'>get_keys</font><font face='Lucida Console'>(</font>keys<font face='Lucida Console'>)</font>;
cr.<font color='#BB00BB'>get_blocks</font><font face='Lucida Console'>(</font>blocks<font face='Lucida Console'>)</font>;
<font color='#009900'>// Now print all the key/value pairs
</font> <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> i <font color='#5555FF'>=</font> <font color='#979000'>0</font>; i <font color='#5555FF'><</font> keys.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>i<font face='Lucida Console'>)</font>
cout <font color='#5555FF'><</font><font color='#5555FF'><</font> padding <font color='#5555FF'><</font><font color='#5555FF'><</font> keys[i] <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'> = </font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> cr[keys[i]] <font color='#5555FF'><</font><font color='#5555FF'><</font> endl;
<font color='#009900'>// Now print all the sub-blocks.
</font> <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> i <font color='#5555FF'>=</font> <font color='#979000'>0</font>; i <font color='#5555FF'><</font> blocks.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>i<font face='Lucida Console'>)</font>
<b>{</b>
<font color='#009900'>// First print the block name
</font> cout <font color='#5555FF'><</font><font color='#5555FF'><</font> padding <font color='#5555FF'><</font><font color='#5555FF'><</font> blocks[i] <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'> { </font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> endl;
<font color='#009900'>// Now recursively print the contents of the sub block. Note that the cr.block()
</font> <font color='#009900'>// function returns another config_reader that represents the sub-block.
</font> <font color='#BB00BB'>print_config_reader_contents</font><font face='Lucida Console'>(</font>cr.<font color='#BB00BB'>block</font><font face='Lucida Console'>(</font>blocks[i]<font face='Lucida Console'>)</font>, depth<font color='#5555FF'>+</font><font color='#979000'>1</font><font face='Lucida Console'>)</font>;
cout <font color='#5555FF'><</font><font color='#5555FF'><</font> padding <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>}</font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> endl;
<b>}</b>
<b>}</b>
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
</pre></body></html> |