Debian Codegraph @

src/gengine/OptionParams.cpp

raw
1 /*
2 * Copyright (C) 2004 Ivo Danihelka ([email protected])
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 */
9 #include "OptionParams.h"
10
11 #include "Log.h"
12 #include "Environ.h"
13 #include "LogicException.h"
14
15 //-----------------------------------------------------------------
16 void
17 OptionParams::addParam(const std::string &name, eType type,
18 const std::string &help)
19 {
20 Param param(type, help);
21 m_params.insert(
22 std::pair<std::string,Param>(name, param));
23 int paramSize = name.size() + getType(type).size();
24 if (paramSize > m_maxSize) {
25 m_maxSize = paramSize;
26 }
27 }
28 //-----------------------------------------------------------------
29 std::string
30 OptionParams::getHelp(const Environ * /*options*/) const
31 {
32 std::string help;
33 t_params::const_iterator end = m_params.end();
34 for (t_params::const_iterator i = m_params.begin(); i != end; ++i) {
35 int paramSize = i->first.size() + getType(i->second.type).size();
36 std::string space = std::string(m_maxSize - paramSize, ' ');
37 help += " " + i->first + "=<" + getType(i->second.type) + ">";
38 help += space + " " + i->second.help + "\n";
39 }
40 return help;
41 }
42 //-----------------------------------------------------------------
43 std::string
44 OptionParams::getConfig(const Environ *options) const
45 {
46 std::string config;
47 t_params::const_iterator end = m_params.end();
48 for (t_params::const_iterator i = m_params.begin(); i != end; ++i) {
49 config += i->first + "='" + options->getParam(i->first) + "'\n";
50 }
51 return config;
52 }
53 //-----------------------------------------------------------------
54 std::string
55 OptionParams::getType(eType type) const
56 {
57 switch (type) {
58 case TYPE_NUMBER:
59 return "number";
60 case TYPE_BOOLEAN:
61 return "boolean";
62 case TYPE_STRING:
63 return "string";
64 case TYPE_PATH:
65 return "path";
66 default:
67 LOG_WARNING(ExInfo("unknown param type")
68 .addInfo("type", type));
69 }
70 return "unknown";
71 }
72 //-----------------------------------------------------------------
73 /**
74 * Throws LogicException when the given option name or value is invalid.
75 */
76 void
77 OptionParams::checkValidity(const std::string &name, const std::string &/*value*/) const
78 {
79 t_params::const_iterator it = m_params.find(name);
80 if (m_params.end() == it) {
81 throw LogicException(ExInfo("unknown option")
82 .addInfo("name", name));
83 }
84
85 //TODO: check value type
86 }
87
88