summaryrefslogtreecommitdiff
path: root/2003/i/buzz/src/config
diff options
context:
space:
mode:
Diffstat (limited to '2003/i/buzz/src/config')
-rw-r--r--2003/i/buzz/src/config/Makefile.defs4
-rw-r--r--2003/i/buzz/src/config/config.cc97
-rw-r--r--2003/i/buzz/src/config/config.h34
-rw-r--r--2003/i/buzz/src/config/config_lex.h30
-rw-r--r--2003/i/buzz/src/config/config_lex.ll76
5 files changed, 241 insertions, 0 deletions
diff --git a/2003/i/buzz/src/config/Makefile.defs b/2003/i/buzz/src/config/Makefile.defs
new file mode 100644
index 0000000..3aadde7
--- /dev/null
+++ b/2003/i/buzz/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/2003/i/buzz/src/config/config.cc b/2003/i/buzz/src/config/config.cc
new file mode 100644
index 0000000..3e764ae
--- /dev/null
+++ b/2003/i/buzz/src/config/config.cc
@@ -0,0 +1,97 @@
+// config.cc
+// buzz - Programme du robot Efrei Robotique I1-I2 2003
+// Copyright (C) 2003 Nicolas Schodet
+//
+#include "config.h"
+#include "config_lex.h"
+#include "erreur/erreur.h"
+
+#include <cstring>
+
+// Constructeur.
+Config::Config (const char *filename)
+{
+ m_filename = filename;
+ config_yyopen (filename);
+ m_type = -1;
+}
+
+// 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, "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, "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, "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)
+ throw ErreurConfig (m_filename, "Flotant attendu.\n");
+ m_type = -1;
+ return config_yylval.fl;
+}
+
+void
+Config::getEof (void)
+{
+ if (m_type == -1) m_type = config_yylex ();
+ if (m_type)
+ throw ErreurConfig (m_filename, "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, "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, "Identificateur inconnu.\n");
+}
diff --git a/2003/i/buzz/src/config/config.h b/2003/i/buzz/src/config/config.h
new file mode 100644
index 0000000..1335886
--- /dev/null
+++ b/2003/i/buzz/src/config/config.h
@@ -0,0 +1,34 @@
+#ifndef config_h
+#define config_h
+// config.h
+// buzz - Programme du robot Efrei Robotique I1-I2 2003
+// Copyright (C) 2003 Nicolas Schodet
+
+class Config
+{
+ const char *m_filename;
+ int m_type;
+ public:
+ // Constructeur.
+ 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 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);
+};
+
+#endif // config_h
diff --git a/2003/i/buzz/src/config/config_lex.h b/2003/i/buzz/src/config/config_lex.h
new file mode 100644
index 0000000..784a7e1
--- /dev/null
+++ b/2003/i/buzz/src/config/config_lex.h
@@ -0,0 +1,30 @@
+#ifndef config_lex_h
+#define config_lex_h
+// config_lex.h
+// buzz - Programme du robot Efrei Robotique I1-I2 2003
+// Copyright (C) 2003 Nicolas Schodet
+
+#define NUM 0x100
+#define FLOAT 0x101
+#define ID 0x102
+#define BOOL 0x103
+#define ERR 0x104
+
+union config_yytype
+{
+ double fl;
+ int num;
+ const char *id;
+ bool boolean;
+};
+
+extern config_yytype config_yylval;
+
+int
+config_yylex (void);
+int
+config_yyopen (const char *filename);
+void
+config_yyclose (void);
+
+#endif // config_lex_h
diff --git a/2003/i/buzz/src/config/config_lex.ll b/2003/i/buzz/src/config/config_lex.ll
new file mode 100644
index 0000000..31f980e
--- /dev/null
+++ b/2003/i/buzz/src/config/config_lex.ll
@@ -0,0 +1,76 @@
+%{
+
+#include "config_lex.h"
+
+config_yytype config_yylval;
+
+%}
+
+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 noyywrap nounput
+%option prefix="config_yy"
+
+%%
+
+{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;
+}
+
+"#".* /* Commentaires. */
+
+[ \t\n]+ /* Rien à battre. */
+
+. return ERR;
+
+%%
+
+int
+config_yyopen (const char *filename)
+{
+ config_yyin = fopen (filename, "r");
+ if (!config_yyin) return -1;
+ return 0;
+}
+
+void
+config_yyclose (void)
+{
+ fclose (config_yyin);
+}
+
+// vim: ft=lex