summaryrefslogtreecommitdiff
path: root/2004/i/nono/src/config
diff options
context:
space:
mode:
authorschodet2004-02-07 16:55:41 +0000
committerschodet2004-02-07 16:55:41 +0000
commit9fdd6704947174fae292dfc6e14cc1029a7f7a6c (patch)
tree40a4fe775845ececdaf27e82d99d6a06f6b3803e /2004/i/nono/src/config
parentb673946d76e436c067d24e535b1e0a167b9dfc47 (diff)
Initial revision
Diffstat (limited to '2004/i/nono/src/config')
-rw-r--r--2004/i/nono/src/config/Makefile.defs4
-rw-r--r--2004/i/nono/src/config/config.cc162
-rw-r--r--2004/i/nono/src/config/config.h64
-rw-r--r--2004/i/nono/src/config/config_lex.h60
-rw-r--r--2004/i/nono/src/config/config_lex.ll127
5 files changed, 417 insertions, 0 deletions
diff --git a/2004/i/nono/src/config/Makefile.defs b/2004/i/nono/src/config/Makefile.defs
new file mode 100644
index 0000000..3aadde7
--- /dev/null
+++ b/2004/i/nono/src/config/Makefile.defs
@@ -0,0 +1,4 @@
+LIBS += config.a
+config_a_SOURCES = config.cc config_lex.cc
+
+config.a: ${config_a_SOURCES:%.cc=config.a(%.o)}
diff --git a/2004/i/nono/src/config/config.cc b/2004/i/nono/src/config/config.cc
new file mode 100644
index 0000000..c36d75f
--- /dev/null
+++ b/2004/i/nono/src/config/config.cc
@@ -0,0 +1,162 @@
+// config.cc - Lecture de fichiers de configuration.
+// nono - programme du robot 2004. {{{
+//
+// Copyright (C) 2004 Nicolas Schodet
+//
+// Robot APB Team/Efrei 2004.
+// Web: http://assos.efrei.fr/robot/
+// Email: robot AT efrei DOT fr
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// }}}
+#include "config.h"
+#include "config_lex.h"
+#include "erreur/erreur.h"
+
+#include <cstring>
+
+// Constructeur, prend l'identificateur de configuration en paramètre (nom de
+// fichier).
+Config::Config (const char *filename)
+ : m_filename (filename),
+ m_type (-1)
+{
+#ifdef CONFIG_VARIANT
+ if (config_yyopen (filename, wrap_handler, this) == -1)
+#else
+ if (config_yyopen (filename, 0, 0) == -1)
+#endif
+ throw ErreurConfig (filename, "Erreur d'ouverture.\n");
+}
+
+// Destructeur.
+Config::~Config (void)
+{
+ config_yyclose ();
+}
+
+// Attend un token, sinon, jette une exception.
+int
+Config::getNum (void)
+{
+ if (m_type == -1) m_type = config_yylex ();
+ if (m_type != NUM)
+ throw ErreurConfig (m_filename.c_str (), "Nombre attendu.\n");
+ m_type = -1;
+ return config_yylval.num;
+}
+
+const char *
+Config::getId (void)
+{
+ if (m_type == -1) m_type = config_yylex ();
+ if (m_type != ID)
+ throw ErreurConfig (m_filename.c_str (), "Identificateur attendu.\n");
+ m_type = -1;
+ return config_yylval.id;
+}
+
+bool
+Config::getBool (void)
+{
+ if (m_type == -1) m_type = config_yylex ();
+ if (m_type != BOOL)
+ throw ErreurConfig (m_filename.c_str (), "Booléen attendu.\n");
+ m_type = -1;
+ return config_yylval.boolean;
+}
+
+double
+Config::getFloat (void)
+{
+ if (m_type == -1) m_type = config_yylex ();
+ if (m_type == FLOAT)
+ {
+ m_type = -1;
+ return config_yylval.fl;
+ }
+ else if (m_type == NUM)
+ {
+ m_type = -1;
+ return (double) config_yylval.num;
+ }
+ else
+ throw ErreurConfig (m_filename.c_str (), "Flotant attendu.\n");
+}
+
+void
+Config::getString (std::string &s)
+{
+ if (m_type == -1) m_type = config_yylex ();
+ if (m_type != STRING)
+ throw ErreurConfig (m_filename.c_str (),
+ "Chaîne de caractères attendue.\n");
+ m_type = -1;
+ s = config_yylval.str;
+}
+
+void
+Config::getEof (void)
+{
+ if (m_type == -1) m_type = config_yylex ();
+ if (m_type)
+ throw ErreurConfig (m_filename.c_str (),
+ "Fin de fichier attendue.\n");
+}
+
+// Retourne true si à la fin du fichier.
+bool
+Config::eof (void)
+{
+ if (m_type == -1) m_type = config_yylex ();
+ return m_type == 0;
+}
+
+// Vérifie si le prochain token est...
+bool
+Config::isId (const char *s)
+{
+ if (m_type == -1) m_type = config_yylex ();
+ if (m_type != ID)
+ throw ErreurConfig (m_filename.c_str (), "Identificateur attendu.\n");
+ return strcmp (config_yylval.id, s) == 0;
+}
+
+// Lance une erreur, on a pas trouvé ce qu'on voulais.
+void
+Config::noId (void)
+{
+ throw ErreurConfig (m_filename.c_str (), config_yylval.id,
+ "Identificateur inconnu.\n");
+}
+
+// Appellé par le lexer pour changer de fichier.
+bool
+Config::wrap_handler (void *p)
+{
+ Config *c = reinterpret_cast<Config *> (p);
+ return c->wrap ();
+}
+
+bool
+Config::wrap (void)
+{
+#ifdef CONFIG_VARIANT
+ return (config_yyopen ((m_filename + "." CONFIG_VARIANT).c_str (), 0, 0)
+ == -1) ? false : true;
+#endif
+}
+
diff --git a/2004/i/nono/src/config/config.h b/2004/i/nono/src/config/config.h
new file mode 100644
index 0000000..a958844
--- /dev/null
+++ b/2004/i/nono/src/config/config.h
@@ -0,0 +1,64 @@
+#ifndef config_h
+#define config_h
+// config.h - Lecture de fichiers de configuration.
+// nono - programme du robot 2004. {{{
+//
+// Copyright (C) 2004 Nicolas Schodet
+//
+// Robot APB Team/Efrei 2004.
+// Web: http://assos.efrei.fr/robot/
+// Email: robot AT efrei DOT fr
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// }}}
+
+#include <string>
+
+class Config
+{
+ std::string m_filename;
+ int m_type;
+ public:
+ // Constructeur, prend l'identificateur de configuration en paramètre (nom
+ // de fichier).
+ Config (const char *filename);
+ // Destructeur.
+ ~Config (void);
+ // Attend un token, sinon, jette une exception.
+ int getNum (void);
+ Config &operator>> (int &num) { num = getNum (); return *this; }
+ const char *getId (void);
+ Config &operator>> (const char *&s) { s = getId (); return *this; }
+ bool getBool (void);
+ Config &operator>> (bool &b) { b = getBool (); return *this; }
+ double getFloat (void);
+ Config &operator>> (double &fl) { fl = getFloat (); return *this; }
+ void getString (std::string &s);
+ Config &operator>> (std::string &s) { getString (s); return *this; }
+ void getEof (void);
+ // Vérifie si le prochain token est...
+ bool isId (const char *s);
+ // Retourne true si à la fin du fichier.
+ bool eof (void);
+ // Lance une erreur, on a pas trouvé ce qu'on voulais.
+ void noId (void);
+ private:
+ // Appellé par le lexer pour changer de fichier.
+ static bool wrap_handler (void *p);
+ bool wrap (void);
+};
+
+#endif // config_h
diff --git a/2004/i/nono/src/config/config_lex.h b/2004/i/nono/src/config/config_lex.h
new file mode 100644
index 0000000..0ef7003
--- /dev/null
+++ b/2004/i/nono/src/config/config_lex.h
@@ -0,0 +1,60 @@
+#ifndef config_lex_h
+#define config_lex_h
+// config_lex.h
+// nono - programme du robot 2004. {{{
+//
+// Copyright (C) 2004 Nicolas Schodet
+//
+// Robot APB Team/Efrei 2004.
+// Web: http://assos.efrei.fr/robot/
+// Email: robot AT efrei DOT fr
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// }}}
+
+#define NUM 0x100
+#define FLOAT 0x101
+#define ID 0x102
+#define BOOL 0x103
+#define STRING 0x104
+#define ERR 0x105
+
+union config_yytype
+{
+ double fl;
+ int num;
+ const char *id;
+ bool boolean;
+ const char *str;
+};
+
+extern config_yytype config_yylval;
+
+typedef bool (*config_wrap_f) (void *);
+
+// Fournis par flex : renvois le type du prochain token.
+int
+config_yylex (void);
+
+// Ouvre un fichier pour l'utiliser avec le lexer.
+int
+config_yyopen (const char *filename, config_wrap_f wrap, void *data);
+
+// Ferme le fichier du lexer.
+void
+config_yyclose (void);
+
+#endif // config_lex_h
diff --git a/2004/i/nono/src/config/config_lex.ll b/2004/i/nono/src/config/config_lex.ll
new file mode 100644
index 0000000..1387203
--- /dev/null
+++ b/2004/i/nono/src/config/config_lex.ll
@@ -0,0 +1,127 @@
+%{
+/* nono - programme du robot 2004. {{{
+ *
+ * Copyright (C) 2004 Nicolas Schodet
+ *
+ * Robot APB Team/Efrei 2004.
+ * Web: http://assos.efrei.fr/robot/
+ * Email: robot AT efrei DOT fr
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * }}} */
+
+#include "config/config_lex.h"
+
+config_yytype config_yylval;
+config_wrap_f wrap_f = 0;
+void *wrap_data = 0;
+
+%}
+
+ID [_a-zA-Z][-_a-zA-Z0-9]*
+
+NUM [-+]?[0-9]+
+
+HNUM "0x"[0-9a-fzA-F]+
+
+BNUM "0b"[01]+
+
+FLOAT [-+]?[0-9]*"."[0-9]*
+
+STRING \"[^\n\"]*\"
+
+%option nounput
+%option prefix="config_yy"
+%option yylineno
+
+%%
+
+{FLOAT} {
+ config_yylval.fl = strtod (config_yytext, 0);
+ return FLOAT;
+}
+
+{BNUM} {
+ config_yylval.num = strtol (config_yytext + 2, 0, 2);
+ return NUM;
+}
+
+{HNUM} {
+ config_yylval.num = strtol (config_yytext + 2, 0, 16);
+ return NUM;
+}
+
+{NUM} {
+ config_yylval.num = strtol (config_yytext, 0, 10);
+ return NUM;
+}
+
+"true" { config_yylval.boolean = true; return BOOL; }
+"false" { config_yylval.boolean = false; return BOOL; }
+
+{ID} {
+ config_yylval.id = config_yytext;
+ return ID;
+}
+
+{STRING} {
+ config_yytext[config_yyleng - 1] = '\0';
+ config_yylval.str = config_yytext + 1;
+ return STRING;
+}
+
+"#".* /* Commentaires. */
+
+[ \t\n]+ /* Rien à battre. */
+
+. return ERR;
+
+%%
+
+// Ouvre un fichier pour l'utiliser avec le lexer.
+int
+config_yyopen (const char *filename, config_wrap_f wrap, void *data)
+{
+ if (config_yyin && config_yyin != stdin)
+ fclose (config_yyin);
+ config_yyin = fopen (filename, "r");
+ if (!config_yyin) return -1;
+ wrap_f = wrap;
+ wrap_data = data;
+ return 0;
+}
+
+// Ferme le fichier du lexer.
+void
+config_yyclose (void)
+{
+ if (config_yyin && config_yyin != stdin)
+ fclose (config_yyin);
+ wrap_f = 0;
+ wrap_data = 0;
+}
+
+// Appelé par flex pour changer de fichier.
+int
+config_yywrap (void)
+{
+ if (wrap_f)
+ return wrap_f (wrap_data) ? 0 : 1;
+ else
+ return 1;
+}
+
+// vim: ft=lex