00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00024 #include "config.h"
00025
00026 #include <cstdlib>
00027 #include <iostream>
00028 #include <strings.h>
00029
00030 #include <scalestack/kernel/module.h>
00031 #include <scalestack/kernel/option.h>
00032
00033 using namespace std;
00034
00035 namespace scalestack
00036 {
00037 namespace kernel
00038 {
00039
00040
00041
00042
00043
00044 option::~option()
00045 {
00046 }
00047
00048 module& option::get_module(void) const
00049 {
00050 return _module;
00051 }
00052
00053 const string& option::get_name(void) const
00054 {
00055 return _name;
00056 }
00057
00058 const string& option::get_help(void) const
00059 {
00060 return _help;
00061 }
00062
00063 const string& option::get_value_name(void) const
00064 {
00065 return _value_name;
00066 }
00067
00068 const string& option::get_default_value(void) const
00069 {
00070 return _default_value;
00071 }
00072
00073 const string& option::get_value(void) const
00074 {
00075 return _value;
00076 }
00077
00078 int option::get_int_value(void) const
00079 {
00080 return atoi(_value.c_str());
00081 }
00082
00083 size_t option::get_size_value(void) const
00084 {
00085 return strtoul(_value.c_str(), NULL, 0);
00086 }
00087
00088 bool option::get_bool_value(void) const
00089 {
00090 if (!strcasecmp(_value.c_str(), "false") ||
00091 !strcasecmp(_value.c_str(), "f") ||
00092 !strcasecmp(_value.c_str(), "no") ||
00093 !strcasecmp(_value.c_str(), "n") ||
00094 _value == "0")
00095 {
00096 return false;
00097 }
00098
00099 return true;
00100 }
00101
00102 vector<string> option::get_list_value(void) const
00103 {
00104 vector<string> list;
00105 string::size_type current = 0;
00106
00107 do
00108 {
00109 string::size_type next = _value.find_first_of(",", current);
00110 list.push_back(_value.substr(current, next - current));
00111 current = _value.find_first_not_of(",", next);
00112 }
00113 while (current != string::npos);
00114
00115 return list;
00116 }
00117
00118 bool option::is_valid(void) const
00119 {
00120 return _is_valid;
00121 }
00122
00123
00124
00125
00126
00127 option::option(module& module, const std::string& name):
00128 _is_valid(),
00129 _is_set(),
00130 _module(module),
00131 _name(name),
00132 _help(),
00133 _value_name(),
00134 _default_value(),
00135 _value()
00136 {
00137 }
00138
00139 void option::_set_help(const std::string& help)
00140 {
00141 _help = help;
00142 }
00143
00144 void option::_set_value_name(const std::string& value_name)
00145 {
00146 _value_name = value_name;
00147 }
00148
00149 void option::_set_default_value(const std::string& default_value)
00150 {
00151 _default_value = default_value;
00152 }
00153
00154 void option::_set_value(const std::string& value, bool overwrite)
00155 {
00156 if (overwrite || !_is_set)
00157 {
00158 _value = value;
00159 _is_set = true;
00160 }
00161 }
00162
00163 void option::_set_valid(bool valid)
00164 {
00165 _is_valid = valid;
00166 }
00167
00168 void option::_print_help(void)
00169 {
00170 cout << " " << _name << "=" << _value_name << " - " << _help;
00171
00172 if (_default_value != "")
00173 cout << _(" (Default: ") << _default_value << ")";
00174
00175 cout << endl;
00176 }
00177
00178 void option::_print_config(void)
00179 {
00180 cout << _module.get_name() << "." << _name << "=" << _value << endl;
00181 }
00182
00183 }
00184 }