conf_parser.h 1.9 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
#pragma once

#include <map>
#include <string>

typedef std::map<std::string, std::string> Map;
typedef Map::const_iterator MapIter;

class ConfParserBase {
 public:
  ConfParserBase() {}

  bool load(const std::string &file_name);

  bool load_from_string(const std::string &str);

  bool get_conf_float(const std::string &key, float &value) const;

  bool get_conf_uint(const std::string &key, unsigned int &value) const;

  bool get_conf_int(const std::string &key, int &value) const;

  bool get_conf_str(const std::string &key, std::string &value) const;

  bool exist(const char *name) const;

  void map_clear();

 private:
  bool parse_line(const std::string &line);

  Map _map;
};

class ConfParser {
 public:
  ConfParser() : _conf(NULL){};
  ~ConfParser();

  bool init(const std::string &conf_file);

  bool get_uint(const std::string &prefix,
                const std::string &key,
                unsigned int &value) const;

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

  bool get_int(const std::string &prefix,
               const std::string &key,
               int &value) const;

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

  bool get_float(const std::string &prefix,
                 const std::string &key,
                 float &value) const;

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

  bool get_string(const std::string &prefix,
                  const std::string &key,
                  std::string &value) const;

  bool get_strings(const std::string &prefix,
                   const std::string &key,
                   std::vector<std::string> &values) const;

 public:
  ConfParserBase *_conf;
};