~ubuntu-branches/ubuntu/maverick/newsbeuter/maverick

« back to all changes in this revision

Viewing changes to src/configparser.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Nico Golde
  • Date: 2007-04-21 19:44:35 UTC
  • Revision ID: james.westby@ubuntu.com-20070421194435-21g6134ws2yvarlt
Tags: upstream-0.3
ImportĀ upstreamĀ versionĀ 0.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <configparser.h>
 
2
#include <xmlpullparser.h>
 
3
#include <exceptions.h>
 
4
#include <utils.h>
 
5
#include <logger.h>
 
6
#include <fstream>
 
7
#include <sstream>
 
8
#include <config.h>
 
9
 
 
10
namespace newsbeuter {
 
11
 
 
12
configparser::configparser(const char * file) : filename(file) { }
 
13
 
 
14
configparser::~configparser() { }
 
15
 
 
16
void configparser::parse() {
 
17
        unsigned int linecounter = 1;
 
18
        std::fstream f(filename.c_str());
 
19
        std::string line;
 
20
        getline(f,line);
 
21
        while (f.is_open() && !f.eof()) {
 
22
                GetLogger().log(LOG_DEBUG,"configparser::parse: tokenizing %s",line.c_str());
 
23
                std::vector<std::string> tokens = utils::tokenize_quoted(line);
 
24
                if (tokens.size() > 0) {
 
25
                        std::string cmd = tokens[0];
 
26
                        config_action_handler * handler = action_handlers[cmd];
 
27
                        if (handler) {
 
28
                                tokens.erase(tokens.begin()); // delete first element
 
29
                                action_handler_status status = handler->handle_action(cmd,tokens);
 
30
                                if (status != AHS_OK) {
 
31
                                        char buf[1024];
 
32
                                        char * errmsg = NULL;
 
33
                                        if (status == AHS_INVALID_PARAMS) {
 
34
                                                errmsg = _("invalid parameters.");
 
35
                                        } else if (status == AHS_TOO_FEW_PARAMS) {
 
36
                                                errmsg = _("too few parameters.");
 
37
                                        } else if (status == AHS_INVALID_COMMAND) {
 
38
                                                errmsg = _("unknown command (bug).");
 
39
                                        } else {
 
40
                                                errmsg = _("unknown error (bug).");
 
41
                                        }
 
42
                                        snprintf(buf, sizeof(buf), _("Error while processing command `%s' (%s line %u): %s"), cmd.c_str(), filename.c_str(), linecounter, errmsg);
 
43
                                        throw configexception(buf);
 
44
                                }
 
45
                        } else {
 
46
                                char buf[1024];
 
47
                                snprintf(buf, sizeof(buf), _("unknown command `%s'"), cmd.c_str());
 
48
                                throw configexception(buf);
 
49
                        }
 
50
                }
 
51
                getline(f,line);
 
52
                ++linecounter;
 
53
        }
 
54
}
 
55
 
 
56
void configparser::register_handler(const std::string& cmd, config_action_handler * handler) {
 
57
        GetLogger().log(LOG_DEBUG,"configparser::register_handler: cmd = %s handler = %p", cmd.c_str(), handler);
 
58
        action_handlers[cmd] = handler;
 
59
}
 
60
 
 
61
void configparser::unregister_handler(const std::string& cmd) {
 
62
        GetLogger().log(LOG_DEBUG,"configparser::unregister_handler: cmd = %s", cmd.c_str());
 
63
        action_handlers[cmd] = 0;
 
64
}
 
65
 
 
66
}