00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include "params.h"
00034 #include <stdio.h>
00035 #include <iostream>
00036 #include <fstream>
00037 #include <sstream>
00038 #include <string.h>
00039 #include <stdlib.h>
00040 #include <sys/types.h>
00041 #include <sys/stat.h>
00042 #include <unistd.h>
00043
00044 #include "mace-macros.h"
00045 #include "ParamReader.h"
00046
00047 using namespace std;
00048
00049 namespace params {
00050
00051 void Params::addRequired(const string& name, const string& desc) {
00052 required[name] = desc;
00053 }
00054
00055 void Params::verifyRequired() {
00056
00057 bool error = false;
00058 for (StringMap::iterator i = required.begin(); i != required.end(); i++) {
00059 if (!containsKey(i->first)) {
00060 fprintf(stderr, "required argument: %s - %s\n", i->first.c_str(), i->second.c_str());
00061 error = true;
00062 }
00063 }
00064 if (error) {
00065 exit(-1);
00066 }
00067 }
00068
00069 int Params::loadparams(int& argc, char**& argv, bool requireFile) {
00070
00071
00072
00073
00074
00075
00076 int startarg = 1;
00077 int found = 0;
00078 if (argc==1 || argv[1][0] == '-') {
00079
00080 loadfile("params.default");
00081 } else if (argc > 1) {
00082 startarg = 2;
00083
00084 struct stat sbuf;
00085 if ((stat(argv[1], &sbuf) == 0) && S_ISREG(sbuf.st_mode)) {
00086 loadfile(argv[1]);
00087 argv[1] = 0;
00088 found = 1;
00089 }
00090 else if (requireFile) {
00091 std::cerr << "could not open params file " << argv[1] << std::endl;
00092 exit(-1);
00093 }
00094 }
00095 else {
00096 return 0;
00097 }
00098
00099 for (int i = startarg; i<argc; i++) {
00100 if (!argv[i]) {
00101 continue;
00102 }
00103 char* arg = *(argv+i);
00104 if (arg[0] != '-' || strlen(arg) < 2) continue;
00105 arg++;
00106 if (arg[0] == '-') {
00107 arg++;
00108 }
00109
00110
00111 if (i < argc-1) {
00112 set(arg, argv[i+1]);
00113 argv[i] = 0;
00114 argv[i+1] = 0;
00115 found += 2;
00116 } else {
00117 argv[i] = 0;
00118 set(arg, string());
00119 found++;
00120 }
00121 }
00122
00123 for (int i = 1; i < argc; i++) {
00124 if (!argv[i]) {
00125 for (int j = i + 1; j < argc; j++) {
00126 if (argv[j]) {
00127 argv[i] = argv[j];
00128 argv[j] = 0;
00129 break;
00130 }
00131 }
00132 }
00133 }
00134 argc -= found;
00135
00136
00137
00138
00139
00140
00141
00142 verifyRequired();
00143 return 0;
00144 }
00145
00146 string Params::trim(const string& s) {
00147 int start = 0;
00148 while (s[start] == ' ' || s[start] == '\t') {
00149 start++;
00150 }
00151
00152 int end = s.size();
00153 while (s[end - 1] == ' ' || s[end - 1] == '\t') {
00154 end--;
00155 }
00156
00157 if (end < start) {
00158 return "";
00159 }
00160
00161 return s.substr(start, end - start);
00162 }
00163
00164 int Params::loadfile(const char* filename, bool allRequired) {
00165 ifstream f(filename);
00166 if (!f.is_open()) {
00167
00168 return 0;
00169 }
00170
00171 uint count = 0;
00172 while (!f.eof()) {
00173 count++;
00174 const int BUF_SIZE = 2048;
00175 char buf[BUF_SIZE];
00176 f.getline(buf, BUF_SIZE);
00177
00178 string s(buf);
00179 if (s.find('#') != string::npos) {
00180 string::size_type openPos = s.find('"');
00181 string::size_type closePos = s.find_last_of('"');
00182 string::size_type commentPos = s.find('#');
00183 if (!(openPos != string::npos && closePos != string::npos &&
00184 openPos < commentPos && commentPos < closePos)) {
00185 s = s.substr(0, commentPos);
00186 }
00187 }
00188 s = trim(s);
00189 if (s.empty()) {
00190 continue;
00191 }
00192
00193 string::size_type i = s.find('=');
00194 if ((i == string::npos) || (i == 0) || (i == (s.size() - 1))) {
00195 cerr << "error in parameters file " << filename << endl
00196 << "line " << count << ": " << s << endl
00197 << "parameters format is key=\"value\"" << endl;
00198 }
00199 string key = s.substr(0, i);
00200 key = trim(key);
00201 string value = s.substr(i + 1);
00202 value = trim(value);
00203 if (value[0] == '"') {
00204 value = value.substr(1);
00205 }
00206 if (value[value.size() - 1] == '"') {
00207 value = value.substr(0, value.size() - 1);
00208 }
00209
00210 setAppendNewline(key, value);
00211 }
00212 f.close();
00213
00214 if (allRequired) {
00215 verifyRequired();
00216 }
00217
00218 return 0;
00219 }
00220
00221 int Params::print(FILE* ostream) {
00222 int total=fprintf(ostream,"REPLAY PARAMETERS:\n");
00223 for (StringMap::iterator i = data.begin(); i != data.end(); i++) {
00224 total += fprintf(ostream, "REPLAY PARAMETER: %s = \"%s\"\n",
00225 i->first.c_str(), i->second.c_str());
00226 }
00227 return total;
00228 }
00229
00230 int Params::print(std::ostream& os) {
00231 for (StringMap::iterator i = data.begin(); i != data.end(); i++) {
00232 os << i->first << " = " << i->second << std::endl;
00233 }
00234 return data.size();
00235 }
00236
00237 void Params::log() {
00238 static log_id_t lid = Log::getId("Params");
00239
00240 Log::binaryLog(lid, ParamReader_namespace::ParamReader(data), 0);
00241 }
00242
00243 void Params::printUnusedParams(FILE* ostream) {
00244 fprintf(ostream, "UNUSED PARAMETERS:\n");
00245 for (StringMap::iterator i = data.begin(); i != data.end(); i++) {
00246 if (!touched.contains(i->first)) {
00247 fprintf(ostream, "UNUSED PARAMETER: %s = \"%s\"\n", i->first.c_str(), i->second.c_str());
00248 }
00249 }
00250 }
00251
00252 Params::Params() : watch(false) {
00253 }
00254
00255 void Params::getBootstrapPeers(const MaceKey& laddr, NodeSet& peers, bool& autoBootstrap) {
00256 ADD_SELECTORS("Params::getBootstrapPeers");
00257 autoBootstrap = containsKey(MACE_AUTO_BOOTSTRAP_PEERS);
00258 string s;
00259 if (autoBootstrap) {
00260 s = get<string>(MACE_AUTO_BOOTSTRAP_PEERS, "");
00261 }
00262 else {
00263 s = get<string>(MACE_BOOTSTRAP_PEERS, "");
00264 }
00265
00266 if (s.size()) {
00267 istringstream is(s);
00268 while (!is.eof()) {
00269 string h;
00270 is >> h;
00271 MaceKey k(ipv4, h);
00272 if (k != laddr) {
00273 peers.insert(k);
00274 }
00275 }
00276 }
00277
00278 if (autoBootstrap && peers.empty()) {
00279 maceerr << "No MACE_AUTO_BOOTSTRAP_PEERS defined" << Log::endl;
00280
00281 }
00282 }
00283
00284
00285 Params* Params::_inst;
00286
00287 }