summaryrefslogtreecommitdiff
path: root/src/cfgparser.h
blob: c7f0063f9d7881a91b3e3907710f0a62f5df4be5 (plain)
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
//
// Created by luqiu on 2018-4-26.
//

#ifndef TFE_CFGPARSER_H
#define TFE_CFGPARSER_H

#ifndef TFE_STRING_MAX
#define TFE_STRING_MAX      2048
#endif

#include <stdexcept>
#include <string>
#include <cstring>

extern "C"
{
#include <MESA_prof_load.h>
}

class TfeConfigParser
{
public:
    TfeConfigParser(std::string cfgfile) : str_cfgfile_(std::move(cfgfile))
    {}
    ~TfeConfigParser() = default;

    /* 读入函数 */
    template<typename T>
    T GetValue(const std::string &str_section, const std::string &str_entry);
    template<typename T>
    T GetValueWithDefault(const std::string &str_section, const std::string &str_entry, const T &default_value);

private:
    std::string str_cfgfile_;
};

struct tfe_expection_cfg_invalid_format : std::runtime_error
{
    explicit tfe_expection_cfg_invalid_format(
        const std::string &file,
        const std::string &section,
        const std::string &item,
        const std::string &what) : file(file), section(section), item(item), runtime_error(what)
    {}

    std::string file;
    std::string section;
    std::string item;
};

struct tfe_expection_cfg_lost_entry : std::runtime_error
{
    explicit tfe_expection_cfg_lost_entry(
        const std::string &file,
        const std::string &section,
        const std::string &item,
        const std::string &what) : file(file), section(section), item(item), runtime_error(what)
    {}

    std::string file;
    std::string section;
    std::string item;
};

template<>
std::string TfeConfigParser::GetValue(const std::string &str_section, const std::string &str_entry)
{
    char __str_buffer[TFE_STRING_MAX];
    memset(__str_buffer, 0, sizeof(__str_buffer));

    int ret = MESA_load_profile_string_nodef(str_cfgfile_.c_str(), str_section.c_str(), str_entry.c_str(),
                                             __str_buffer, sizeof(__str_buffer));

    if (ret < 0)
        throw tfe_expection_cfg_lost_entry(str_cfgfile_, str_section, str_entry, "");

    return std::string(__str_buffer);
}

template<>
unsigned long TfeConfigParser::GetValue(const std::string &str_section, const std::string &str_entry)
{
    char *__ptr = nullptr;
    std::string __str_value = GetValue<std::string>(str_section, str_entry);

    unsigned long __value = strtoul(__str_value.c_str(), &__ptr, 0);
    if (__ptr == __str_value.c_str())
    {
        throw tfe_expection_cfg_invalid_format(str_cfgfile_, str_section, str_entry,
                                               __str_value + " is not valid unsigned number. ");
    }

    return __value;
}

template<>
bool TfeConfigParser::GetValue(const std::string &str_section, const std::string &str_entry)
{
    return GetValue<unsigned long>(str_section, str_entry) != 0;
}

template<typename T>
T TfeConfigParser::GetValueWithDefault(
    const std::string &str_section,
    const std::string &str_entry,
    const T &default_value)
{
    T __value;

    try
    {
        __value = GetValue<T>(str_section, str_entry);
    }
    catch (...)
    {
        return default_value;
    }

    return std::move(__value);
}

#endif //TFE_CFGPARSER_H