summaryrefslogtreecommitdiff
path: root/2003/i/buzz/src/config/config.cc
blob: 7cbf5f0610bf0f09446b1e4ab13c1f4c828f7136 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// 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, config_yylval.id,
			"Identificateur inconnu.\n");
}