File: ConfigSource.h

package info (click to toggle)
spring 106.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 55,260 kB
  • sloc: cpp: 543,946; ansic: 44,800; python: 12,575; java: 12,201; awk: 5,889; sh: 1,796; asm: 1,546; xml: 655; perl: 405; php: 211; objc: 194; makefile: 76; sed: 2
file content (121 lines) | stat: -rw-r--r-- 2,827 bytes parent folder | download | duplicates (3)
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
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#ifndef CONFIG_SOURCE_H
#define CONFIG_SOURCE_H

#include <functional>
#include <map>
#include <string>
#include <stdio.h>

/**
 * @brief Abstraction of a read-only configuration source
 */
class ReadOnlyConfigSource
{
public:
	virtual ~ReadOnlyConfigSource() {}

	virtual bool IsSet(const std::string& key) const;
	virtual std::string GetString(const std::string& key) const;

	const std::map<std::string, std::string>& GetData() const { return data; }

protected:
	std::map<std::string, std::string> data;
};

/**
 * @brief Abstraction of a writable configuration source
 */
class ReadWriteConfigSource : public ReadOnlyConfigSource
{
public:
	virtual void SetString(const std::string& key, const std::string& value);
	virtual void Delete(const std::string& key);
};

/**
 * @brief Overlay configuration source
 *
 * Overlay settings get lost after the game ends. Values in the OPTIONS section
 * of the start script will be put into the overlay and Lua scripts can choose
 * to put certain values into the overlay.
 */
class OverlayConfigSource : public ReadWriteConfigSource
{
};

/**
 * @brief File-backed configuration source
 */
class FileConfigSource : public ReadWriteConfigSource
{
public:
	FileConfigSource(const std::string& filename);

	void SetString(const std::string& key, const std::string& value) override;
	void Delete(const std::string& key) override;

	std::string GetFilename() const { return filename; }

private:
	void SetStringInternal(const std::string& key, const std::string& value);
	void DeleteInternal(const std::string& key);
	void ReadModifyWrite(std::function<void ()> modify);

	std::string filename;
	std::map<std::string, std::string> comments;

	// helper functions
	void Read(FILE* file);
	void Write(FILE* file);
	char* Strip(char* begin, char* end);
};

/**
 * @brief Configuration source that holds static defaults
 *
 * Keys and default values for each engine configuration variable
 * are exposed by this class.
 */
class DefaultConfigSource : public ReadOnlyConfigSource
{
public:
	DefaultConfigSource();
};

/**
 * @brief Configuration source that holds safemode values
 *
 * Used when spring was started with "--safemode" param
 */
class SafemodeConfigSource : public ReadOnlyConfigSource
{
public:
	SafemodeConfigSource();
};

/**
 * @brief Configuration source that holds safemode values
 *
 * Used when spring was started with "--safemode" param
 */
class HeadlessConfigSource : public ReadOnlyConfigSource
{
public:
	HeadlessConfigSource();
};

/**
 * @brief Configuration source that holds safemode values
 *
 * Used when spring was started with "--safemode" param
 */
class DedicatedConfigSource : public ReadOnlyConfigSource
{
public:
	DedicatedConfigSource();
};

#endif // CONFIG_SOURCE_H