OpenTTD Source 20241224-master-gee860a5c8e
settingsgen.cpp File Reference

Tool to create computer-readable settings. More...

#include "../stdafx.h"
#include "../string_func.h"
#include "../strings_type.h"
#include "../misc/getoptdata.h"
#include "../ini_type.h"
#include "../core/mem_func.hpp"
#include "../error_func.h"
#include <filesystem>
#include "../safeguards.h"

Go to the source code of this file.

Data Structures

class  OutputBuffer
 Output buffer for a block of data. More...
 
class  OutputStore
 Temporarily store output. More...
 
struct  SettingsIniFile
 Derived class for loading INI files without going through Fio stuff. More...
 

Functions

void FatalErrorI (const std::string &msg)
 Report a fatal error.
 
static void DumpGroup (const IniLoadFile &ifile, const char *const group_name)
 Dump a IGT_SEQUENCE group into _stored_output.
 
static const char * FindItemValue (const char *name, const IniGroup *grp, const IniGroup *defaults)
 Find the value of a template variable.
 
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.
 
static void DumpSections (const IniLoadFile &ifile)
 Output all non-special sections through the template / template variable expansion system.
 
static void AppendFile (const char *fname, FILE *out_fp)
 Append a file to the output stream.
 
static bool CompareFiles (const char *n1, const char *n2)
 Compare two files for identity.
 
static void ProcessIniFile (const char *fname)
 Process a single INI file.
 
int CDECL main (int argc, char *argv[])
 And the main program (what else?)
 

Variables

static const size_t OUTPUT_BLOCK_SIZE = 16000
 Block size of the buffer in OutputBuffer.
 
OutputStore _stored_output
 Temporary storage of the output, until all processing is done.
 
OutputStore _post_amble_output
 Similar to _stored_output, but for the post amble.
 
static const char * PREAMBLE_GROUP_NAME = "pre-amble"
 Name of the group containing the pre amble.
 
static const char * POSTAMBLE_GROUP_NAME = "post-amble"
 Name of the group containing the post amble.
 
static const char * TEMPLATES_GROUP_NAME = "templates"
 Name of the group containing the templates.
 
static const char * VALIDATION_GROUP_NAME = "validation"
 Name of the group containing the validation statements.
 
static const char * DEFAULTS_GROUP_NAME = "defaults"
 Name of the group containing default values for the template variables.
 
static const OptionData _opts []
 Options of settingsgen.
 

Detailed Description

Tool to create computer-readable settings.

Definition in file settingsgen.cpp.

Function Documentation

◆ AppendFile()

static void AppendFile ( const char *  fname,
FILE *  out_fp 
)
static

Append a file to the output stream.

Parameters
fnameFilename of file to append.
out_fpOutput stream to write to.

Definition at line 326 of file settingsgen.cpp.

References lengthof, and FileHandle::Open().

Referenced by main().

◆ CompareFiles()

static bool CompareFiles ( const char *  n1,
const char *  n2 
)
static

Compare two files for identity.

Parameters
n1First file.
n2Second file.
Returns
True if both files are identical.

Definition at line 351 of file settingsgen.cpp.

References FileHandle::Open().

Referenced by main().

◆ DumpGroup()

static void DumpGroup ( const IniLoadFile ifile,
const char *const  group_name 
)
static

Dump a IGT_SEQUENCE group into _stored_output.

Parameters
ifileLoaded INI data.
group_nameName of the group to copy.

Definition at line 193 of file settingsgen.cpp.

References _stored_output, OutputStore::Add(), IniLoadFile::GetGroup(), IGT_SEQUENCE, IniGroup::items, and IniGroup::type.

Referenced by ProcessIniFile().

◆ DumpLine()

static void DumpLine ( const IniItem item,
const IniGroup grp,
const IniGroup default_grp,
OutputStore output 
)
static

Parse a single entry via a template and output this.

Parameters
itemThe template to use for the output.
grpGroup current being used for template rendering.
default_grpDefault values for items not set in @grp.
outputOutput to use for result.

Definition at line 228 of file settingsgen.cpp.

References OutputStore::Add(), FindItemValue(), and IniItem::value.

Referenced by DumpSections().

◆ DumpSections()

static void DumpSections ( const IniLoadFile ifile)
static

Output all non-special sections through the template / template variable expansion system.

Parameters
ifileLoaded INI data.

Definition at line 292 of file settingsgen.cpp.

References _post_amble_output, _stored_output, DEFAULTS_GROUP_NAME, DumpLine(), IniLoadFile::GetGroup(), IniGroup::GetItem(), IniLoadFile::groups, POSTAMBLE_GROUP_NAME, PREAMBLE_GROUP_NAME, TEMPLATES_GROUP_NAME, VALIDATION_GROUP_NAME, and IniItem::value.

Referenced by ProcessIniFile().

◆ FatalErrorI()

void FatalErrorI ( const std::string &  msg)

Report a fatal error.

Error handling for fatal non-user errors.

Parameters
sFormat string.
Note
Function does not return.

Definition at line 27 of file settingsgen.cpp.

◆ FindItemValue()

static const char * FindItemValue ( const char *  name,
const IniGroup grp,
const IniGroup defaults 
)
static

Find the value of a template variable.

Parameters
nameName of the item to find.
grpGroup currently being expanded (searched first).
defaultsFallback group to search, nullptr skips the search.
Returns
Text of the item if found, else nullptr.

Definition at line 213 of file settingsgen.cpp.

References IniGroup::GetItem(), and IniItem::value.

Referenced by DumpLine().

◆ main()

int CDECL main ( int  argc,
char *  argv[] 
)

And the main program (what else?)

Parameters
argcNumber of command-line arguments including the program name itself.
argvVector of the command-line arguments.

Definition at line 422 of file settingsgen.cpp.

References _opts, _post_amble_output, _stored_output, AppendFile(), GetOptData::arguments, OutputStore::Clear(), CompareFiles(), GetOptData::GetOpt(), FileHandle::Open(), GetOptData::opt, ProcessIniFile(), and OutputStore::Write().

◆ ProcessIniFile()

static void ProcessIniFile ( const char *  fname)
static

Process a single INI file.

The file should have a [templates] group, where each item is one template. Variables in a template have the form '$[a-z0-9]+' (a literal '$' followed by one or more '', lowercase letters, or lowercase numbers).

After loading, the [pre-amble] group is copied verbatim if it exists.

For every group with a name that matches a template name the template is written. It starts with a optional #if line if an 'if' item exists in the group. The item value is used as condition. Similarly, #ifdef and #ifndef lines are also written. Below the macro processor directives, the value of the template is written at a line with its variables replaced by item values of the group being written. If the group has no item for the variable, the [defaults] group is tried as fall back. Finally, #endif lines are written to match the macro processor lines.

Last but not least, the [post-amble] group is copied verbatim.

Parameters
fnameIni file to process.
Returns
Exit status of the processing.

Definition at line 405 of file settingsgen.cpp.

References DumpGroup(), DumpSections(), IniLoadFile::LoadFromDisk(), NO_DIRECTORY, POSTAMBLE_GROUP_NAME, and PREAMBLE_GROUP_NAME.

Referenced by main().

Variable Documentation

◆ _opts

const OptionData _opts[]
static
Initial value:
= {
{ .type = ODF_NO_VALUE, .id = 'h', .shortname = 'h', .longname = "--help" },
{ .type = ODF_NO_VALUE, .id = 'h', .shortname = '?' },
{ .type = ODF_HAS_VALUE, .id = 'o', .shortname = 'o', .longname = "--output" },
{ .type = ODF_HAS_VALUE, .id = 'b', .shortname = 'b', .longname = "--before" },
{ .type = ODF_HAS_VALUE, .id = 'a', .shortname = 'a', .longname = "--after" },
}
@ ODF_NO_VALUE
A plain option (no value attached to it).
Definition getoptdata.h:15
@ ODF_HAS_VALUE
An option with a value.
Definition getoptdata.h:16

Options of settingsgen.

Definition at line 377 of file settingsgen.cpp.

Referenced by main().

◆ _post_amble_output

OutputStore _post_amble_output

Similar to _stored_output, but for the post amble.

Definition at line 180 of file settingsgen.cpp.

Referenced by DumpSections(), and main().

◆ _stored_output

OutputStore _stored_output

Temporary storage of the output, until all processing is done.

Definition at line 179 of file settingsgen.cpp.

Referenced by DumpGroup(), DumpSections(), and main().

◆ DEFAULTS_GROUP_NAME

const char* DEFAULTS_GROUP_NAME = "defaults"
static

Name of the group containing default values for the template variables.

Definition at line 186 of file settingsgen.cpp.

Referenced by DumpSections().

◆ OUTPUT_BLOCK_SIZE

const size_t OUTPUT_BLOCK_SIZE = 16000
static

Block size of the buffer in OutputBuffer.

Definition at line 33 of file settingsgen.cpp.

Referenced by OutputBuffer::Add(), and OutputBuffer::HasRoom().

◆ POSTAMBLE_GROUP_NAME

const char* POSTAMBLE_GROUP_NAME = "post-amble"
static

Name of the group containing the post amble.

Definition at line 183 of file settingsgen.cpp.

Referenced by DumpSections(), and ProcessIniFile().

◆ PREAMBLE_GROUP_NAME

const char* PREAMBLE_GROUP_NAME = "pre-amble"
static

Name of the group containing the pre amble.

Definition at line 182 of file settingsgen.cpp.

Referenced by DumpSections(), and ProcessIniFile().

◆ TEMPLATES_GROUP_NAME

const char* TEMPLATES_GROUP_NAME = "templates"
static

Name of the group containing the templates.

Definition at line 184 of file settingsgen.cpp.

Referenced by DumpSections().

◆ VALIDATION_GROUP_NAME

const char* VALIDATION_GROUP_NAME = "validation"
static

Name of the group containing the validation statements.

Definition at line 185 of file settingsgen.cpp.

Referenced by DumpSections().