-
Notifications
You must be signed in to change notification settings - Fork 2
/
config_parser.h
103 lines (84 loc) · 1.89 KB
/
config_parser.h
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
#pragma once
#include <map>
#include <string>
#include <string.h>
#include <fstream>
#include <cassert>
#include "picojson.h"
#include "myio.h"
class config_parser
{
public:
config_parser( int argc, char *argv[] )
{
for (size_t i = 1; i < argc; i++ )
{
std::string key;
std::string value;
if (strstr(argv[i],"-")==argv[i])
{
key = (argv[i]+1);
}
else
{
continue;
}
if ( argc>i+1 && strstr(argv[i+1], "-")!=argv[i+1])
{
value = argv[i + 1];
i++;
}
m_cmdargs[key] = value;
}
if ( m_cmdargs.find( "config" )!=m_cmdargs.end() )
{
std::ifstream fs;
picojson::value val;
fs.open( m_cmdargs["config"].c_str(), std::ios::binary );
if (!fs.is_open())
{
printf("Error: cannot open \"%s\"\n" , m_cmdargs["config"].c_str());
exit(-1);
}
fs >> val;
fs.close();
//m_jsonobj = val.get<picojson::object>()["config"].get<picojson::object>();
m_jsonobj = val.get<picojson::object>();
}
}
~config_parser()
{
}
template <typename T> bool get(const std::string key, T &value)
{
if (m_cmdargs.find(key) != m_cmdargs.end() && sscanfVar(&value, m_cmdargs[key].c_str()) == 1)
{
return true;
}
if (m_jsonobj.find(key)!=m_jsonobj.end())
{
std::string str = m_jsonobj[key].to_str();
if (sscanfVar(&value, str.c_str()) == 1)
{
return true;
}
}
return false;
}
template <typename T> void get_and_assert(const std::string key, T &value)
{
if ( !get(key, value) )
{
printf("Error: cannot get value \"%s\"\n", key.c_str());
exit(-1);
}
}
bool contains(const std::string key)
{
if (m_cmdargs.find(key) != m_cmdargs.end() || m_jsonobj.find(key)!=m_jsonobj.end()) return true;
return false;
}
protected:
std::map<std::string, std::string> m_cmdargs;
picojson::object m_jsonobj;
};