Aging_MouthReplace / dlibs /docs /config_reader_ex.cpp.html
AshanGimhana's picture
Upload folder using huggingface_hub
9375c9a verified
<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'>&lt;</font>dlib<font color='#5555FF'>/</font>config_reader.h<font color='#5555FF'>&gt;</font>
<font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>iostream<font color='#5555FF'>&gt;</font>
<font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>fstream<font color='#5555FF'>&gt;</font>
<font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>vector<font color='#5555FF'>&gt;</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'>&amp;</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'>&lt;</font><font color='#5555FF'>&lt;</font> cr["<font color='#CC0000'>key1</font>"] <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;
<font color='#009900'>// Print out the name field inside the user1 sub-block
</font> cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</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'>&lt;</font><font color='#5555FF'>&lt;</font> endl;
<font color='#009900'>// Now print out the editor field in the details block
</font> cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</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'>&lt;</font><font color='#5555FF'>&lt;</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'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>user1's id is </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> id1 <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;
cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>user2's id is </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> id2 <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</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'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>user3's id is </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> id3 <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;
<b>}</b>
<font color='#0000FF'>catch</font> <font face='Lucida Console'>(</font>exception<font color='#5555FF'>&amp;</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'>&lt;</font><font color='#5555FF'>&lt;</font> e.<font color='#BB00BB'>what</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</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'>&amp;</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'>&lt;</font>string<font color='#5555FF'>&gt;</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'>&lt;</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'>&lt;</font><font color='#5555FF'>&lt;</font> padding <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> keys[i] <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'> = </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> cr[keys[i]] <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</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'>&lt;</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'>&lt;</font><font color='#5555FF'>&lt;</font> padding <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> blocks[i] <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'> { </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</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'>&lt;</font><font color='#5555FF'>&lt;</font> padding <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>}</font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;
<b>}</b>
<b>}</b>
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
</pre></body></html>