| 67 | // ---------------------------------------------------------------------------------------- |
| 68 | |
| 69 | int main() |
| 70 | { |
| 71 | try |
| 72 | { |
| 73 | config_reader cr("config.txt"); |
| 74 | |
| 75 | // Use our recursive function to print everything in the config file. |
| 76 | print_config_reader_contents(cr); |
| 77 | |
| 78 | // Now let's access some of the fields of the config file directly. You |
| 79 | // use [] for accessing key values and .block() for accessing sub-blocks. |
| 80 | |
| 81 | // Print out the string value assigned to key1 in the config file |
| 82 | cout << cr["key1"] << endl; |
| 83 | |
| 84 | // Print out the name field inside the user1 sub-block |
| 85 | cout << cr.block("user1")["name"] << endl; |
| 86 | // Now print out the editor field in the details block |
| 87 | cout << cr.block("user1").block("details")["editor"] << endl; |
| 88 | |
| 89 | |
| 90 | // Note that you can use get_option() to easily convert fields into |
| 91 | // non-string types. For example, the config file has an integer id |
| 92 | // field that can be converted into an int like so: |
| 93 | int id1 = get_option(cr,"user1.id",0); |
| 94 | int id2 = get_option(cr,"user2.id",0); |
| 95 | cout << "user1's id is " << id1 << endl; |
| 96 | cout << "user2's id is " << id2 << endl; |
| 97 | // The third argument to get_option() is the default value returned if |
| 98 | // the config reader doesn't contain a corresponding entry. So for |
| 99 | // example, the following prints 321 since there is no user3. |
| 100 | int id3 = get_option(cr,"user3.id",321); |
| 101 | cout << "user3's id is " << id3 << endl; |
| 102 | |
| 103 | } |
| 104 | catch (exception& e) |
| 105 | { |
| 106 | // Finally, note that the config_reader throws exceptions if the config |
| 107 | // file is corrupted or if you ask it for a key or block that doesn't exist. |
| 108 | // Here we print out any such error messages. |
| 109 | cout << e.what() << endl; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | // ---------------------------------------------------------------------------------------- |
| 114 |
nothing calls this directly
no test coverage detected