File: plugin_config.cpp

package info (click to toggle)
bzflag 2.4.30-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 26,488 kB
  • sloc: cpp: 150,376; ansic: 3,463; sh: 2,535; makefile: 2,194; perl: 486; python: 260; objc: 246; php: 206
file content (205 lines) | stat: -rw-r--r-- 5,647 bytes parent folder | download
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
/* bzflag
 * Copyright (c) 1993-2025 Tim Riker
 *
 * This package is free software;  you can redistribute it and/or
 * modify it under the terms of the license found in the file
 * named COPYING that should have accompanied this file.
 *
 * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

// PluginUtils.h - Collection of useful utility functions for plugins

#include "bzfsAPI.h"
#include "plugin_config.h"
#include "plugin_utils.h"
#include <iostream>
#include <fstream>

/*
 * INI style configuration file parser class
 *
 * Comments start with # or ;
 * Ignores whitespace around sections, keys, and values
 * Default section if none is specified is [global]
 */

PluginConfig::PluginConfig()
{
}

PluginConfig::PluginConfig(const std::string &filename) : errors(0), whitespace(" \t\r"), configFilename(filename)
{
    parse();
}

void PluginConfig::read(const char* filename)
{
    read(std::string(filename));
}

void PluginConfig::read(const std::string &filename)
{
    sections.clear();
    whitespace = " \t\r";
    errors = 0;
    configFilename = filename;

    parse();
}


std::string PluginConfig::item(const char *section, const  char *key)
{
    return item(std::string(section),std::string(key));
}

std::string PluginConfig::item(const std::string &section, const std::string &key)
{
    std::string s = section,k = key;
    makelower(s);
    makelower(k);

    return sections[s][k];
}

std::vector<std::string> PluginConfig::getSections()
{
    std::vector<std::string> secNames;

    for (auto itr = sections.begin(); itr != sections.end(); itr++)
        secNames.push_back(itr->first);

    return secNames;
}

std::vector<std::pair<std::string, std::string>> PluginConfig::getSectionItems(const char* section)
{
    return getSectionItems(std::string(section));
}

std::vector<std::pair<std::string, std::string>> PluginConfig::getSectionItems(const std::string &section)
{
    std::vector<std::pair<std::string, std::string>> vec;

    std::string s = section;
    makelower(s);

    if (sections.find(s) != sections.end())
    {
        for (auto itr = sections[s].begin(); itr != sections[s].end(); itr++)
            vec.push_back(std::pair<std::string, std::string>(itr->first, itr->second));
    }

    return vec;
}

void PluginConfig::parse(void)
{
    std::string line;
    std::string section;
    std::string key;
    std::string value;
    std::ifstream iniFile;
    size_t start = 0, end = 0;
    size_t equalPos = 0;

    /*
     * Parse key,value pairs for sections out of the INI type
     * configuration file specified in 'filename'
     *
     */
    iniFile.open(configFilename.c_str(), std::ios::in);

    if (!iniFile.is_open())
    {
        bz_debugMessagef(1, "PluginConfig: Can't open configuration file: %s", configFilename.c_str());
        errors++;
        return;
    }

    section = "global";

    while (!iniFile.eof())
    {
        getline(iniFile, line);
        start = line.find_first_not_of(whitespace);

        /*
         * Look for comments and skip
         */
        if (line[start] == '#')
            continue;

        /*
         * Look for a section tag
         */
        if (line[start] == '[')
        {
            start = line.find_first_not_of(whitespace, start + 1);

            /* Check if the last non whitespace character is a close bracket */
            end = line.find_last_not_of(whitespace);
            if (line[end] == ']')
            {
                end = line.find_last_not_of(whitespace, end - 1);
                /* Got a section header - save it */
                section = line.substr(start, end - start + 1);
                bz_debugMessagef(4, "PluginConfig: Found section [%s]", section.c_str());
                continue;
            }
            /*
             * We either got a valid section or we have a
             * Malformed line - '[' but not matching close ']' which we ignore.
             */
            bz_debugMessagef(1, "PluginConfig: Malformed line ignored: %s", line.c_str());
            continue;
        }

        /*
         * No section tag, look for 'key = value' pairs
         *
         * Split the line into 'key = value' pairs
         */
        equalPos = line.find("=", start);

        /* If there is no '=' sign then ignore the line - treated as a comment */
        if (equalPos == std::string::npos)
        {
            if (line.find_first_not_of(whitespace) != std::string::npos)
                bz_debugMessagef(1, "PluginConfig: Malformed line ignored: %s", line.c_str());
            continue;
        }

        /* Extract the key */
        end = line.find_last_not_of(whitespace, equalPos - 1);
        key = line.substr(start, end - start + 1);

        /* Extract the value */
        start = line.find_first_not_of(whitespace, equalPos + 1);
        end = line.find_last_not_of(whitespace);
        if (start == std::string::npos || end == std::string::npos)
            value = "";
        else
            value = line.substr(start, end - start + 1);

        makelower(key);
        makelower(section);

        /* Save the section, key and value in the std::map for later retrieval */
        sections[section][key] = value;
        bz_debugMessagef(4, "PluginConfig: Found key [%s].%s = %s", section.c_str(), key.c_str(), value.c_str());
    }

    iniFile.close();
}

// Local Variables: ***
// mode: C++ ***
// tab-width: 4 ***
// c-basic-offset: 4 ***
// indent-tabs-mode: nil ***
// End: ***
// ex: shiftwidth=4 tabstop=4