FlashGraph-ng
A new frontier in large-scale graph analysis and data mining
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
config_map.h
1 #ifndef __CONFIG_MAP_H__
2 #define __CONFIG_MAP_H__
3 
4 /*
5  * Copyright 2014 Open Connectome Project (http://openconnecto.me)
6  * Written by Da Zheng (zhengda1936@gmail.com)
7  *
8  * This file is part of libcommon.
9  * This library is used by SAFS and FlashGraph, and potentially by other
10  * systems.
11  *
12  * Licensed under the Apache License, Version 2.0 (the "License");
13  * you may not use this file except in compliance with the License.
14  * You may obtain a copy of the License at
15  *
16  * http://www.apache.org/licenses/LICENSE-2.0
17  *
18  * Unless required by applicable law or agreed to in writing, software
19  * distributed under the License is distributed on an "AS IS" BASIS,
20  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  * See the License for the specific language governing permissions and
22  * limitations under the License.
23  */
24 
25 #include <assert.h>
26 
27 #include <map>
28 #include <memory>
29 #include <string>
30 
31 /*
32  * This maintains key-value pairs for options.
33  */
34 class config_map
35 {
36  std::map<std::string, std::string> configs;
37 
38  config_map() {
39  }
40 
41 public:
42  typedef std::shared_ptr<config_map> ptr;
43 
44  static ptr create() {
45  return ptr(new config_map());
46  }
47 
48  static ptr create(const std::string &conf_file);
49 
50  void add_options(const char *opts[], int num);
51  // Multiple options may exist in the input string and
52  // the options are separated by space.
53  void add_options(const std::string &opts);
54 
55  const std::string &get_option(const std::string &name) const {
56  std::map<std::string, std::string>::const_iterator it
57  = configs.find(name);
58  assert(it != configs.end());
59  return it->second;
60  }
61 
62  bool has_option(const std::string &name) const {
63  return configs.find(name) != configs.end();
64  }
65 
66  bool read_option(const std::string &name, std::string &value) const {
67  if (has_option(name)) {
68  value = get_option(name);
69  return true;
70  }
71  else
72  return false;
73  }
74 
75  bool read_option_int(const std::string &name, int &value) const {
76  if (has_option(name)) {
77  value = atoi(get_option(name).c_str());
78  return true;
79  }
80  else
81  return false;
82  }
83 
84  bool read_option_long(const std::string &name, long &value) const {
85  if (has_option(name)) {
86  value = atol(get_option(name).c_str());
87  return true;
88  }
89  else
90  return false;
91  }
92 
93  bool read_option_bool(const std::string &name, bool &value) const {
94  if (has_option(name)) {
95  value = true;
96  return true;
97  }
98  else
99  return false;
100  }
101 
102  const std::map<std::string, std::string> &get_options() const {
103  return configs;
104  }
105 };
106 
107 #endif