conf_parser.cpp 7.8 KB
Newer Older
K
Kaibing Chen 已提交
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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
#include <algorithm>
#include <fstream>
#include <iostream>
#include <sstream>
#include "logger.h"

#include "common.h"
#include "conf_parser.h"

std::string join_string(const std::string &prefix, const std::string &str) {
  if (prefix.empty() && str.empty()) {
    return "";
  } else if (prefix.empty()) {
    return str;
  } else if (str.empty()) {
    return prefix;
  }

  return prefix + str;
}

bool read_text_file(const std::string &file_name, std::string &str) {
  LOG(INFO) << "read_text_file!";

  if (!file_exist(file_name)) {
    LOG(FATAL) << "file: " << file_name << "is not exist!";
    return false;
  }

  std::ifstream ifs(file_name.c_str(), std::ios::binary);
  if (!ifs) {
    LOG(FATAL) << "fail to open " << file_name;
    return false;
  }

  std::stringstream ss;
  ss << ifs.rdbuf();
  str = ss.str();

  return true;
}

std::vector<std::string> split_str(const std::string &str,
                                   const std::string &sep,
                                   bool suppress_blanks) {
  std::vector<std::string> array;
  size_t position = 0;
  size_t last_position = 0;

  last_position = position = 0;
  while (position + sep.size() <= str.size()) {
    if (str[position] == sep[0] && str.substr(position, sep.size()) == sep) {
      if (!suppress_blanks || position - last_position > 0) {
        array.push_back(str.substr(last_position, position - last_position));
      }
      last_position = position = position + sep.size();
    } else {
      position++;
    }
  }

  if (!suppress_blanks || last_position - str.size()) {
    array.push_back(str.substr(last_position));
  }

  return array;
}

void strip(std::string &s) {
  if (s.empty()) {
    return;
  }

  s.erase(remove_if(s.begin(), s.end(), isspace), s.end());

  if (s.size() == 1 &&
      (s[0] == ' ' || s[0] == '\t' || s[0] == '\n' || s[0] == '\r')) {
    s = "";
  }

  int begin = -1;
  int end = 0;
  for (size_t i = 0; i < s.length(); i++) {
    if (!(s[i] == ' ' || s[i] == '\t' || s[i] == '\n' || s[i] == '\r')) {
      begin = i;
      break;
    }
  }

  if (begin < 0) {
    s = "";
    return;
  }

  for (int i = s.length() - 1; i >= 0; i--) {
    if (!(s[i] == ' ' || s[i] == '\t' || s[i] == '\n' || s[i] == '\r')) {
      end = i;
      break;
    }
  }

  if (((int)s.size()) != end - begin + 1) {
    s = s.substr(begin, end - begin + 1);
  }
}

bool ConfParserBase::load(const std::string &file_name) {
  std::string str;
  if (!read_text_file(file_name, str)) {
    LOG(FATAL) << "fail to read " << file_name;
    return false;
  }

  load_from_string(str);

  return true;
}

bool ConfParserBase::load_from_string(const std::string &str) {
  map_clear();
  std::vector<std::string> lines = split_str(str, "\n", true);

  int count = 0;
  for (size_t i = 0; i < lines.size(); i++) {
    if (parse_line(lines[i])) {
      count++;
    }
  }

  return (count > 0);
}

bool ConfParserBase::get_conf_float(const std::string &key,
                                    float &value) const {
  MapIter it = _map.find(key);
  if (it == _map.end()) {
    return false;
  }

  float temp = 0;
  if (!str2num(it->second, temp)) {
    LOG(WARNING) << "failure to convert " << it->second << " to float";
    return false;
  }

  value = temp;

  return true;
}

bool ConfParserBase::get_conf_uint(const std::string &key,
                                   unsigned int &value) const {
  MapIter it = _map.find(key);
  if (it == _map.end()) {
    LOG(WARNING) << "fail to get: " << key;
    return false;
  }

  unsigned int temp = 0;
  if (!str2num(it->second, temp)) {
    LOG(ERROR) << "fail to convert " << it->second << " to float";
    return false;
  }

  value = temp;

  return true;
}

bool ConfParserBase::get_conf_int(const std::string &key, int &value) const {
  MapIter it = _map.find(key);
  if (it == _map.end()) {
    return false;
  }

  int temp = 0;
  if (!str2num(it->second, temp)) {
    LOG(ERROR) << "fail to convert " << it->second << " to float";
    return false;
  }

  value = temp;

  return true;
}

bool ConfParserBase::get_conf_str(const std::string &key,
                                  std::string &value) const {
  MapIter it = _map.find(key);
  if (it == _map.end()) {
    LOG(WARNING) << "fail to get: " << key;
    return false;
  } else {
    value = it->second;
  }

  return true;
}

bool ConfParserBase::exist(const char *name) const {
  return _map.find(name) != _map.end();
}

void ConfParserBase::map_clear() { _map.clear(); }

bool ConfParserBase::parse_line(const std::string &line) {
  std::string strip_line = line;
  strip(strip_line);
  if (strip_line.empty() || strip_line[0] == '#' || strip_line[0] == ';') {
    return false;
  }

  std::basic_string<char>::size_type index_pos = strip_line.find(':');
  if (index_pos == std::string::npos) {
    LOG(ERROR) << "wrong setting format of line: " << line;
    return false;
  }

  std::string key = strip_line.substr(0, index_pos);
  std::string value =
      strip_line.substr(index_pos + 1, strip_line.size() - index_pos - 1);
  if (!_map.insert(std::pair<std::string, std::string>(key, value)).second) {
    LOG(WARNING) << "value already exist for key: " << key;
    return false;
  }

  return true;
}

ConfParser::~ConfParser() {
  if (NULL != _conf) {
    delete _conf;
    _conf = NULL;
  }
}

bool ConfParser::init(const std::string &conf_file) {
  _conf = new ConfParserBase();
  if (!_conf->load(conf_file)) {
    LOG(FATAL) << "fail to laod conf file: " << conf_file;
    return false;
  }

  return true;
}

bool ConfParser::get_uint(const std::string &prefix,
                          const std::string &key,
                          unsigned int &value) const {
  std::string pre_key = join_string(prefix, key);
  if (!_conf->get_conf_uint(pre_key, value)) {
    return false;
  }

  return true;
}

bool ConfParser::get_uints(const std::string &prefix,
                           const std::string &key,
                           std::vector<unsigned int> &values) const {
  std::vector<std::string> str_values;
  get_strings(prefix, key, str_values);

  return strs2nums(str_values, values);
}

bool ConfParser::get_int(const std::string &prefix,
                         const std::string &key,
                         int &value) const {
  std::string pre_key = join_string(prefix, key);
  if (!_conf->get_conf_int(pre_key, value)) {
    return false;
  }

  return true;
}

bool ConfParser::get_ints(const std::string &prefix,
                          const std::string &key,
                          std::vector<int> &values) const {
  std::vector<std::string> str_values;
  get_strings(prefix, key, str_values);

  return strs2nums(str_values, values);
}

bool ConfParser::get_float(const std::string &prefix,
                           const std::string &key,
                           float &value) const {
  std::string pre_key = join_string(prefix, key);
  if (!_conf->get_conf_float(pre_key, value)) {
    return false;
  }

  return true;
}

bool ConfParser::get_floats(const std::string &prefix,
                            const std::string &key,
                            std::vector<float> &values) const {
  std::vector<std::string> str_values;
  get_strings(prefix, key, str_values);

  return strs2nums(str_values, values);
}

bool ConfParser::get_string(const std::string &prefix,
                            const std::string &key,
                            std::string &value) const {
  std::string pre_key = join_string(prefix, key);
  if (!_conf->get_conf_str(pre_key, value)) {
    return false;
  }

  return true;
}

bool ConfParser::get_strings(const std::string &prefix,
                             const std::string &key,
                             std::vector<std::string> &values) const {
  std::string pre_key = join_string(prefix, key);
  std::string value;
  if (!_conf->get_conf_str(pre_key, value)) {
    return false;
  }

  std::vector<std::string> split_value = split_str(value, ",", true);
  values.swap(split_value);

  return true;
}