summaryrefslogtreecommitdiff
path: root/2005/i/robert/src/config/config_data.cc
diff options
context:
space:
mode:
Diffstat (limited to '2005/i/robert/src/config/config_data.cc')
-rw-r--r--2005/i/robert/src/config/config_data.cc49
1 files changed, 41 insertions, 8 deletions
diff --git a/2005/i/robert/src/config/config_data.cc b/2005/i/robert/src/config/config_data.cc
index e737bed..33a78c0 100644
--- a/2005/i/robert/src/config/config_data.cc
+++ b/2005/i/robert/src/config/config_data.cc
@@ -22,9 +22,9 @@
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// }}}
-#include "config/config.hh"
-#include "config/config_data.hh"
-#include "config/parser_extra.hh"
+#include "config.hh"
+#include "config_data.hh"
+#include "parser_extra.hh"
#include "parser.hh"
#include "lexer.hh"
@@ -35,14 +35,14 @@
/// ligne de commande.
ConfigData::ConfigData (int &argc, char **&argv)
{
- parse ("rc/config");
+ init (argc, argv, "rc/config");
}
/// Constructeur. Fourni en plus un fichier de configuration différent de
/// celui par défaut.
ConfigData::ConfigData (int &argc, char **&argv, const std::string &file)
{
- parse (file);
+ init (argc, argv, file);
}
/// Récupère une valeur de configuration.
@@ -64,11 +64,18 @@ ConfigData::add (const std::string &id, any &val)
a.swap (val);
}
+/// Initialise (lit la ligne de commande et les fichiers de config.
+void
+ConfigData::init (int &argc, char **&argv, const std::string &file)
+{
+ parseFile (file);
+}
+
int yyparse (void *);
-/// Lance le parseur.
+/// Lance le parseur sur un fichier.
void
-ConfigData::parse (const std::string &file)
+ConfigData::parseFile (const std::string &file)
{
FILE *f;
f = fopen (file.c_str (), "r");
@@ -76,7 +83,7 @@ ConfigData::parse (const std::string &file)
throw std::runtime_error ("can not open config file \"" + file + "\"");
try
{
- // Crée un scaner, initialise son tampon d'entré, puis parse.
+ // Crée un scanner, initialise son tampon d'entré, puis parse.
yyscan_t scanner;
YY_BUFFER_STATE buf;
ParserExtra pe (*this);
@@ -99,6 +106,32 @@ ConfigData::parse (const std::string &file)
fclose (f);
}
+/// Lance le parseur sur une chaîne.
+void
+ConfigData::parseString (const std::string &s)
+{
+ try
+ {
+ // Crée un scanner, initialise son tampon d'entré, puis parse.
+ yyscan_t scanner;
+ YY_BUFFER_STATE buf;
+ ParserExtra pe (*this);
+ yylex_init (&scanner);
+ yyset_extra (&pe, scanner);
+ buf = yy_scan_bytes (s.data (), s.size (), scanner);
+ int ret = yyparse (scanner);
+ yy_delete_buffer (buf, scanner);
+ yylex_destroy (scanner);
+ if (ret)
+ throw std::runtime_error ("parse error");
+ }
+ catch (const std::runtime_error &e)
+ {
+ throw std::runtime_error ("in config string \"" + s + "\": "
+ + e.what ());
+ }
+}
+
/* Shut up warning for this wrongly declared static function. */
static int
yy_init_globals (yyscan_t yyscanner)