OpenTTD Source: src/settingsgen/settingsgen.cpp Source File

OpenTTD Source 15.0-RC2

settingsgen.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#include "../stdafx.h"
11#include "../core/string_consumer.hpp"
12#include "../string_func.h"
13#include "../strings_type.h"
14#include "../misc/getoptdata.h"
15#include "../ini_type.h"
16#include "../error_func.h"
17
18#include <filesystem>
19#include <fstream>
20
21#include "../safeguards.h"
22
28[[noreturn]] void FatalErrorI(const std::string &msg)
29{
30 fmt::print(stderr, "settingsgen: FATAL: {}\n", msg);
31 exit(1);
32}
33
34 static const size_t OUTPUT_BLOCK_SIZE = 16000;
35
37 class OutputBuffer {
38public:
40 void Clear()
41 {
42 this->size = 0;
43 }
44
51 size_t Add(std::string_view text)
52 {
53 size_t store_size = text.copy(this->data + this->size, OUTPUT_BLOCK_SIZE - this->size);
54 this->size += store_size;
55 return store_size;
56 }
57
62 void Write(FILE *out_fp) const
63 {
64 if (fwrite(this->data, 1, this->size, out_fp) != this->size) {
65 FatalError("Cannot write output");
66 }
67 }
68
73 bool HasRoom() const
74 {
75 return this->size < OUTPUT_BLOCK_SIZE;
76 }
77
78 size_t size;
79 char data[OUTPUT_BLOCK_SIZE];
80};
81
83 class OutputStore {
84public:
85 OutputStore()
86 {
87 this->Clear();
88 }
89
91 void Clear()
92 {
93 this->output_buffer.clear();
94 }
95
100 void Add(std::string_view text)
101 {
102 if (!text.empty() && this->BufferHasRoom()) {
103 size_t stored_size = this->output_buffer[this->output_buffer.size() - 1].Add(text);
104 text.remove_prefix(stored_size);
105 }
106 while (!text.empty()) {
107 OutputBuffer &block = this->output_buffer.emplace_back();
108 block.Clear(); // Initialize the new block.
109 size_t stored_size = block.Add(text);
110 text.remove_prefix(stored_size);
111 }
112 }
113
118 void Write(FILE *out_fp) const
119 {
120 for (const OutputBuffer &out_data : output_buffer) {
121 out_data.Write(out_fp);
122 }
123 }
124
125private:
130 bool BufferHasRoom() const
131 {
132 size_t num_blocks = this->output_buffer.size();
133 return num_blocks > 0 && this->output_buffer[num_blocks - 1].HasRoom();
134 }
135
136 typedef std::vector<OutputBuffer> OutputBufferVector;
137 OutputBufferVector output_buffer;
138};
139
140
142 struct SettingsIniFile : IniLoadFile {
148 SettingsIniFile(const IniGroupNameList &list_group_names = {}, const IniGroupNameList &seq_group_names = {}) :
149 IniLoadFile(list_group_names, seq_group_names)
150 {
151 }
152
153 std::optional<FileHandle> OpenFile(std::string_view filename, Subdirectory, size_t *size) override
154 {
155 /* Open the text file in binary mode to prevent end-of-line translations
156 * done by ftell() and friends, as defined by K&R. */
157 auto in = FileHandle::Open(filename, "rb");
158 if (!in.has_value()) return in;
159
160 fseek(*in, 0L, SEEK_END);
161 *size = ftell(*in);
162 fseek(*in, 0L, SEEK_SET); // Seek back to the start of the file.
163
164 return in;
165 }
166
167 void ReportFileError(std::string_view message) override
168 {
169 FatalError("{}", message);
170 }
171};
172
173 OutputStore _stored_output;
174 OutputStore _post_amble_output;
175
176 static const std::string_view PREAMBLE_GROUP_NAME = "pre-amble";
177 static const std::string_view POSTAMBLE_GROUP_NAME = "post-amble";
178 static const std::string_view TEMPLATES_GROUP_NAME = "templates";
179 static const std::string_view VALIDATION_GROUP_NAME = "validation";
180 static const std::string_view DEFAULTS_GROUP_NAME = "defaults";
181
187 static void DumpGroup(const IniLoadFile &ifile, std::string_view group_name)
188{
189 const IniGroup *grp = ifile.GetGroup(group_name);
190 if (grp != nullptr && grp->type == IGT_SEQUENCE) {
191 for (const IniItem &item : grp->items) {
192 if (!item.name.empty()) {
193 _stored_output.Add(item.name);
194 _stored_output.Add("\n");
195 }
196 }
197 }
198}
199
207 static std::optional<std::string_view> FindItemValue(std::string_view name, const IniGroup *grp, const IniGroup *defaults)
208{
209 const IniItem *item = grp->GetItem(name);
210 if (item == nullptr && defaults != nullptr) item = defaults->GetItem(name);
211 if (item == nullptr) return std::nullopt;
212 return item->value;
213}
214
222 static void DumpLine(const IniItem *item, const IniGroup *grp, const IniGroup *default_grp, OutputStore &output)
223{
224 /* Prefix with #if/#ifdef/#ifndef */
225 static const auto pp_lines = {"if", "ifdef", "ifndef"};
226 int count = 0;
227 for (const auto &name : pp_lines) {
228 auto condition = FindItemValue(name, grp, default_grp);
229 if (condition.has_value()) {
230 output.Add("#");
231 output.Add(name);
232 output.Add(" ");
233 output.Add(*condition);
234 output.Add("\n");
235 count++;
236 }
237 }
238
239 /* Output text of the template, except template variables of the form '$[_a-z0-9]+' which get replaced by their value. */
240 static const std::string_view variable_name_characters = "_abcdefghijklmnopqrstuvwxyz0123456789";
241 StringConsumer consumer{*item->value};
242 while (consumer.AnyBytesLeft()) {
243 char c = consumer.ReadChar();
244 if (c != '$' || consumer.ReadIf("$")) {
245 /* No $ or $$ (literal $). */
246 output.Add(std::string_view{&c, 1});
247 continue;
248 }
249
250 std::string_view variable = consumer.ReadUntilCharNotIn(variable_name_characters);
251 if (!variable.empty()) {
252 /* Find the text to output. */
253 auto valitem = FindItemValue(variable, grp, default_grp);
254 if (valitem.has_value()) output.Add(*valitem);
255 } else {
256 output.Add("$");
257 }
258 }
259 output.Add("\n"); // \n after the expanded template.
260 while (count > 0) {
261 output.Add("#endif\n");
262 count--;
263 }
264}
265
270 static void DumpSections(const IniLoadFile &ifile)
271{
272 static const auto special_group_names = {PREAMBLE_GROUP_NAME, POSTAMBLE_GROUP_NAME, DEFAULTS_GROUP_NAME, TEMPLATES_GROUP_NAME, VALIDATION_GROUP_NAME};
273
274 const IniGroup *default_grp = ifile.GetGroup(DEFAULTS_GROUP_NAME);
275 const IniGroup *templates_grp = ifile.GetGroup(TEMPLATES_GROUP_NAME);
276 const IniGroup *validation_grp = ifile.GetGroup(VALIDATION_GROUP_NAME);
277 if (templates_grp == nullptr) return;
278
279 /* Output every group, using its name as template name. */
280 for (const IniGroup &grp : ifile.groups) {
281 /* Exclude special group names. */
282 if (std::ranges::find(special_group_names, grp.name) != std::end(special_group_names)) continue;
283
284 const IniItem *template_item = templates_grp->GetItem(grp.name); // Find template value.
285 if (template_item == nullptr || !template_item->value.has_value()) {
286 FatalError("Cannot find template {}", grp.name);
287 }
288 DumpLine(template_item, &grp, default_grp, _stored_output);
289
290 if (validation_grp != nullptr) {
291 const IniItem *validation_item = validation_grp->GetItem(grp.name); // Find template value.
292 if (validation_item != nullptr && validation_item->value.has_value()) {
293 DumpLine(validation_item, &grp, default_grp, _post_amble_output);
294 }
295 }
296 }
297}
298
304 static void AppendFile(std::optional<std::string_view> fname, FILE *out_fp)
305{
306 if (!fname.has_value()) return;
307
308 auto in_fp = FileHandle::Open(*fname, "r");
309 if (!in_fp.has_value()) {
310 FatalError("Cannot open file {} for copying", *fname);
311 }
312
313 char buffer[4096];
314 size_t length;
315 do {
316 length = fread(buffer, 1, lengthof(buffer), *in_fp);
317 if (fwrite(buffer, 1, length, out_fp) != length) {
318 FatalError("Cannot copy file");
319 }
320 } while (length == lengthof(buffer));
321}
322
329 static bool CompareFiles(std::filesystem::path path1, std::filesystem::path path2)
330{
331 /* Check for equal size, but ignore the error code for cases when a file does not exist. */
332 std::error_code error_code;
333 if (std::filesystem::file_size(path1, error_code) != std::filesystem::file_size(path2, error_code)) return false;
334
335 std::ifstream stream1(path1, std::ifstream::binary);
336 std::ifstream stream2(path2, std::ifstream::binary);
337
338 return std::equal(std::istreambuf_iterator<char>(stream1.rdbuf()),
339 std::istreambuf_iterator<char>(),
340 std::istreambuf_iterator<char>(stream2.rdbuf()));
341}
342
344 static const OptionData _opts[] = {
345 { .type = ODF_NO_VALUE, .id = 'h', .shortname = 'h', .longname = "--help" },
346 { .type = ODF_NO_VALUE, .id = 'h', .shortname = '?' },
347 { .type = ODF_HAS_VALUE, .id = 'o', .shortname = 'o', .longname = "--output" },
348 { .type = ODF_HAS_VALUE, .id = 'b', .shortname = 'b', .longname = "--before" },
349 { .type = ODF_HAS_VALUE, .id = 'a', .shortname = 'a', .longname = "--after" },
350};
351
372 static void ProcessIniFile(std::string_view fname)
373{
374 static const IniLoadFile::IniGroupNameList seq_groups = {PREAMBLE_GROUP_NAME, POSTAMBLE_GROUP_NAME};
375
376 SettingsIniFile ini{{}, seq_groups};
377 ini.LoadFromDisk(fname, NO_DIRECTORY);
378
379 DumpGroup(ini, PREAMBLE_GROUP_NAME);
380 DumpSections(ini);
381 DumpGroup(ini, POSTAMBLE_GROUP_NAME);
382}
383
389 int CDECL main(int argc, char *argv[])
390{
391 std::optional<std::string_view> output_file;
392 std::optional<std::string_view> before_file;
393 std::optional<std::string_view> after_file;
394
395 std::vector<std::string_view> params;
396 for (int i = 1; i < argc; ++i) params.emplace_back(argv[i]);
397 GetOptData mgo(params, _opts);
398 for (;;) {
399 int i = mgo.GetOpt();
400 if (i == -1) break;
401
402 switch (i) {
403 case 'h':
404 fmt::print("settingsgen\n"
405 "Usage: settingsgen [options] ini-file...\n"
406 "with options:\n"
407 " -h, -?, --help Print this help message and exit\n"
408 " -b FILE, --before FILE Copy FILE before all settings\n"
409 " -a FILE, --after FILE Copy FILE after all settings\n"
410 " -o FILE, --output FILE Write output to FILE\n");
411 return 0;
412
413 case 'o':
414 output_file = mgo.opt;
415 break;
416
417 case 'a':
418 after_file = mgo.opt;
419 break;
420
421 case 'b':
422 before_file = mgo.opt;
423 break;
424
425 case -2:
426 fmt::print(stderr, "Invalid arguments\n");
427 return 1;
428 }
429 }
430
431 _stored_output.Clear();
432 _post_amble_output.Clear();
433
434 for (auto &argument : mgo.arguments) ProcessIniFile(argument);
435
436 /* Write output. */
437 if (!output_file.has_value()) {
438 AppendFile(before_file, stdout);
439 _stored_output.Write(stdout);
440 _post_amble_output.Write(stdout);
441 AppendFile(after_file, stdout);
442 } else {
443 static const std::string_view tmp_output = "tmp2.xxx";
444
445 auto fp = FileHandle::Open(tmp_output, "w");
446 if (!fp.has_value()) {
447 FatalError("Cannot open file {}", tmp_output);
448 }
449 AppendFile(before_file, *fp);
450 _stored_output.Write(*fp);
451 _post_amble_output.Write(*fp);
452 AppendFile(after_file, *fp);
453 fp.reset();
454
455 std::error_code error_code;
456 if (CompareFiles(tmp_output, *output_file)) {
457 /* Files are equal. tmp2.xxx is not needed. */
458 std::filesystem::remove(tmp_output, error_code);
459 } else {
460 /* Rename tmp2.xxx to output file. */
461 std::filesystem::rename(tmp_output, *output_file, error_code);
462 if (error_code) FatalError("rename({}, {}) failed: {}", tmp_output, *output_file, error_code.message());
463 }
464 }
465 return 0;
466}
467
474std::optional<FileHandle> FileHandle::Open(const std::string &filename, std::string_view mode)
475{
476 auto f = fopen(filename.c_str(), std::string{mode}.c_str());
477 if (f == nullptr) return std::nullopt;
478 return FileHandle(f);
479}
FileHandle
Definition fileio_type.h:132
FileHandle::Open
static std::optional< FileHandle > Open(const std::string &filename, std::string_view mode)
Open an RAII file handle if possible.
Definition fileio.cpp:1171
OutputBuffer
Output buffer for a block of data.
Definition settingsgen.cpp:37
OutputBuffer::HasRoom
bool HasRoom() const
Does the block have room for more data?
Definition settingsgen.cpp:73
OutputBuffer::Clear
void Clear()
Prepare buffer for use.
Definition settingsgen.cpp:40
OutputBuffer::Add
size_t Add(std::string_view text)
Add text to the output buffer.
Definition settingsgen.cpp:51
OutputBuffer::size
size_t size
Number of bytes stored in data.
Definition settingsgen.cpp:78
OutputBuffer::Write
void Write(FILE *out_fp) const
Dump buffer to the output stream.
Definition settingsgen.cpp:62
OutputBuffer::data
char data[OUTPUT_BLOCK_SIZE]
Stored data.
Definition settingsgen.cpp:79
OutputStore
Temporarily store output.
Definition settingsgen.cpp:83
OutputStore::OutputBufferVector
std::vector< OutputBuffer > OutputBufferVector
Vector type for output buffers.
Definition settingsgen.cpp:136
OutputStore::output_buffer
OutputBufferVector output_buffer
Vector of blocks containing the stored output.
Definition settingsgen.cpp:137
OutputStore::Add
void Add(std::string_view text)
Add text to the output storage.
Definition settingsgen.cpp:100
OutputStore::BufferHasRoom
bool BufferHasRoom() const
Does the buffer have room without adding a new OutputBuffer block?
Definition settingsgen.cpp:130
OutputStore::Write
void Write(FILE *out_fp) const
Write all stored output to the output stream.
Definition settingsgen.cpp:118
OutputStore::Clear
void Clear()
Clear the temporary storage.
Definition settingsgen.cpp:91
StringConsumer
Parse data from a string / buffer.
Definition string_consumer.hpp:27
Subdirectory
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition fileio_type.h:88
NO_DIRECTORY
@ NO_DIRECTORY
A path without any base directory.
Definition fileio_type.h:107
ODF_NO_VALUE
@ ODF_NO_VALUE
A plain option (no value attached to it).
Definition getoptdata.h:15
ODF_HAS_VALUE
@ ODF_HAS_VALUE
An option with a value.
Definition getoptdata.h:16
IGT_SEQUENCE
@ IGT_SEQUENCE
A list of uninterpreted lines, terminated by the next group block.
Definition ini_type.h:19
_stored_output
OutputStore _stored_output
Temporary storage of the output, until all processing is done.
Definition settingsgen.cpp:173
VALIDATION_GROUP_NAME
static const std::string_view VALIDATION_GROUP_NAME
Name of the group containing the validation statements.
Definition settingsgen.cpp:179
DumpSections
static void DumpSections(const IniLoadFile &ifile)
Output all non-special sections through the template / template variable expansion system.
Definition settingsgen.cpp:270
FindItemValue
static std::optional< std::string_view > FindItemValue(std::string_view name, const IniGroup *grp, const IniGroup *defaults)
Find the value of a template variable.
Definition settingsgen.cpp:207
DumpLine
static void DumpLine(const IniItem *item, const IniGroup *grp, const IniGroup *default_grp, OutputStore &output)
Parse a single entry via a template and output this.
Definition settingsgen.cpp:222
_post_amble_output
OutputStore _post_amble_output
Similar to _stored_output, but for the post amble.
Definition settingsgen.cpp:174
_opts
static const OptionData _opts[]
Options of settingsgen.
Definition settingsgen.cpp:344
POSTAMBLE_GROUP_NAME
static const std::string_view POSTAMBLE_GROUP_NAME
Name of the group containing the post amble.
Definition settingsgen.cpp:177
PREAMBLE_GROUP_NAME
static const std::string_view PREAMBLE_GROUP_NAME
Name of the group containing the pre amble.
Definition settingsgen.cpp:176
FatalErrorI
void FatalErrorI(const std::string &msg)
Report a fatal error.
Definition settingsgen.cpp:28
main
int CDECL main(int argc, char *argv[])
And the main program (what else?)
Definition settingsgen.cpp:389
CompareFiles
static bool CompareFiles(std::filesystem::path path1, std::filesystem::path path2)
Compare two files for identity.
Definition settingsgen.cpp:329
AppendFile
static void AppendFile(std::optional< std::string_view > fname, FILE *out_fp)
Append a file to the output stream.
Definition settingsgen.cpp:304
DEFAULTS_GROUP_NAME
static const std::string_view DEFAULTS_GROUP_NAME
Name of the group containing default values for the template variables.
Definition settingsgen.cpp:180
ProcessIniFile
static void ProcessIniFile(std::string_view fname)
Process a single INI file.
Definition settingsgen.cpp:372
DumpGroup
static void DumpGroup(const IniLoadFile &ifile, std::string_view group_name)
Dump a IGT_SEQUENCE group into _stored_output.
Definition settingsgen.cpp:187
TEMPLATES_GROUP_NAME
static const std::string_view TEMPLATES_GROUP_NAME
Name of the group containing the templates.
Definition settingsgen.cpp:178
OUTPUT_BLOCK_SIZE
static const size_t OUTPUT_BLOCK_SIZE
Block size of the buffer in OutputBuffer.
Definition settingsgen.cpp:34
lengthof
#define lengthof(array)
Return the length of an fixed size array.
Definition stdafx.h:271
GetOptData
Data storage for parsing command line options.
Definition getoptdata.h:29
GetOptData::opt
std::string_view opt
Option value, if available (else empty).
Definition getoptdata.h:35
GetOptData::arguments
ArgumentSpan arguments
Remaining command line arguments.
Definition getoptdata.h:33
GetOptData::GetOpt
int GetOpt()
Find the next option.
Definition getoptdata.cpp:22
IniGroup
A group within an ini file.
Definition ini_type.h:34
IniGroup::GetItem
const IniItem * GetItem(std::string_view name) const
Get the item with the given name.
Definition ini_load.cpp:51
IniGroup::type
IniGroupType type
type of group
Definition ini_type.h:36
IniGroup::items
std::list< IniItem > items
all items in the group
Definition ini_type.h:35
IniItem
A single "line" in an ini file.
Definition ini_type.h:23
IniItem::value
std::optional< std::string > value
The value of this item.
Definition ini_type.h:25
IniLoadFile
Ini file that only supports loading.
Definition ini_type.h:50
IniLoadFile::groups
std::list< IniGroup > groups
all groups in the ini
Definition ini_type.h:53
IniLoadFile::seq_group_names
const IniGroupNameList seq_group_names
list of group names that are sequences.
Definition ini_type.h:56
IniLoadFile::GetGroup
const IniGroup * GetGroup(std::string_view name) const
Get the group with the given name.
Definition ini_load.cpp:118
IniLoadFile::LoadFromDisk
void LoadFromDisk(std::string_view filename, Subdirectory subdir)
Load the Ini file's data from the disk.
Definition ini_load.cpp:185
IniLoadFile::list_group_names
const IniGroupNameList list_group_names
list of group names that are lists
Definition ini_type.h:55
OptionData
Data of an option.
Definition getoptdata.h:21
OptionData::type
OptionDataType type
The type of option.
Definition getoptdata.h:22
SettingsIniFile
Derived class for loading INI files without going through Fio stuff.
Definition settingsgen.cpp:142
SettingsIniFile::ReportFileError
void ReportFileError(std::string_view message) override
Report an error about the file contents.
Definition settingsgen.cpp:167
SettingsIniFile::SettingsIniFile
SettingsIniFile(const IniGroupNameList &list_group_names={}, const IniGroupNameList &seq_group_names={})
Construct a new ini loader.
Definition settingsgen.cpp:148
SettingsIniFile::OpenFile
std::optional< FileHandle > OpenFile(std::string_view filename, Subdirectory, size_t *size) override
Open the INI file.
Definition settingsgen.cpp:153