Commit be4fe105 authored by Davis King's avatar Davis King

Added overloads to the config_reader's methods to allow it to load directly from

a file name given as a string in addition to taking istream objects.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%404085
parent 7166e69b
......@@ -7,6 +7,7 @@
#include <string>
#include <iostream>
#include <sstream>
#include <fstream>
#include "../algs.h"
#include "../stl_checked/std_vector_c.h"
......@@ -65,6 +66,22 @@ namespace dlib
const bool redefinition;
};
class file_not_found : public dlib::error
{
friend class config_reader_kernel_1;
file_not_found(
const std::string& file_name_
) :
dlib::error(ECONFIG_READER, "Error in config_reader, unable to open file " + file_name_),
file_name(file_name_)
{}
~file_not_found() throw() {}
public:
const std::string file_name;
};
class config_reader_access_error : public dlib::error
{
public:
......@@ -91,6 +108,10 @@ namespace dlib
const std::string key_name;
};
config_reader_kernel_1(
const std::string& config_file
);
config_reader_kernel_1(
std::istream& in
);
......@@ -105,6 +126,10 @@ namespace dlib
std::istream& in
);
void load_from (
const std::string& config_file
);
bool is_key_defined (
const std::string& key
) const;
......@@ -263,6 +288,26 @@ namespace dlib
}
}
// ----------------------------------------------------------------------------------------
template <
typename map_string_string,
typename map_string_void,
typename tokenizer
>
void config_reader_kernel_1<map_string_string,map_string_void,tokenizer>::
load_from(
const std::string& config_file
)
{
clear();
std::ifstream fin(config_file.c_str());
if (!fin)
throw file_not_found(config_file);
load_from(fin);
}
// ----------------------------------------------------------------------------------------
template <
......@@ -278,6 +323,21 @@ namespace dlib
load_from(in);
}
// ----------------------------------------------------------------------------------------
template <
typename map_string_string,
typename map_string_void,
typename tokenizer
>
config_reader_kernel_1<map_string_string,map_string_void,tokenizer>::
config_reader_kernel_1(
const std::string& config_file
)
{
load_from(config_file);
}
// ----------------------------------------------------------------------------------------
template <
......
......@@ -109,6 +109,22 @@ namespace dlib
const bool redefinition;
};
class file_not_found : public dlib::error
{
/*!
GENERAL
This exception is thrown if the config file can't be opened for
some reason. The type member of this exception will be set
to ECONFIG_READER.
INTERPRETING THIS EXCEPTION
- file_name == the name of the config file which we failed to open
!*/
public:
const std::string file_name;
};
class config_reader_access_error : public dlib::error
{
/*!
......@@ -154,10 +170,32 @@ namespace dlib
- reads the config file to parse from the given input stream,
parses it and loads this object up with all the sub blocks and
key/value pairs it finds.
- before the load is performed, the previous state of the config file
reader is erased. So after the load the config file reader will contain
only information from the given config file.
- This object will represent the top most block of the config file.
throws
- std::bad_alloc
- config_reader_error
!*/
config_reader(
const std::string& config_file
);
/*!
ensures
- #*this is properly initialized
- parses the config file named by the config_file string. Specifically,
parses it and loads this object up with all the sub blocks and
key/value pairs it finds in the file.
- before the load is performed, the previous state of the config file
reader is erased. So after the load the config file reader will contain
only information from the given config file.
- This object will represent the top most block of the config file.
throws
- std::bad_alloc
- config_reader_error
- file_not_found
!*/
virtual ~config_reader(
......@@ -186,6 +224,9 @@ namespace dlib
- reads the config file to parse from the given input stream,
parses it and loads this object up with all the sub blocks and
key/value pairs it finds.
- before the load is performed, the previous state of the config file
reader is erased. So after the load the config file reader will contain
only information from the given config file.
- *this will represent the top most block of the config file contained
in the input stream in.
throws
......@@ -197,6 +238,30 @@ namespace dlib
revert to its initial value.
!*/
void load_from (
const std::string& config_file
);
/*!
ensures
- parses the config file named by the config_file string. Specifically,
parses it and loads this object up with all the sub blocks and
key/value pairs it finds in the file.
- before the load is performed, the previous state of the config file
reader is erased. So after the load the config file reader will contain
only information from the given config file.
- This object will represent the top most block of the config file.
throws
- std::bad_alloc
If this exception is thrown then *this is unusable
until clear() is called and succeeds
- config_reader_error
If this exception is thrown then this object will
revert to its initial value.
- file_not_found
If this exception is thrown then this object will
revert to its initial value.
!*/
bool is_key_defined (
const std::string& key_name
) const;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment