OpenTTD Source  1.11.2
settingsgen.cpp File Reference
#include "../stdafx.h"
#include "../string_func.h"
#include "../strings_type.h"
#include "../misc/getoptdata.h"
#include "../ini_type.h"
#include "../core/smallvec_type.hpp"
#include <stdarg.h>
#include <unistd.h>
#include <sys/stat.h>
#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 NORETURN CDECL error (const char *s,...)
 Report a fatal error. More...
 
static IniLoadFileLoadIniFile (const char *filename)
 Load the INI file. More...
 
static void DumpGroup (IniLoadFile *ifile, const char *const group_name)
 Dump a IGT_SEQUENCE group into _stored_output. More...
 
static const char * FindItemValue (const char *name, IniGroup *grp, IniGroup *defaults)
 Find the value of a template variable. More...
 
static void DumpLine (IniItem *item, IniGroup *grp, IniGroup *default_grp, OutputStore &output)
 Parse a single entry via a template and output this. More...
 
static void DumpSections (IniLoadFile *ifile)
 Output all non-special sections through the template / template variable expansion system. More...
 
static void CopyFile (const char *fname, FILE *out_fp)
 Copy a file to the output. More...
 
static bool CompareFiles (const char *n1, const char *n2)
 Compare two files for identity. More...
 
static void ProcessIniFile (const char *fname)
 Process a single INI file. More...
 
int CDECL main (int argc, char *argv[])
 And the main program (what else?) More...
 

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. More...
 

Detailed Description

Tool to create computer-readable settings.

Definition in file settingsgen.cpp.

Function Documentation

◆ 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 380 of file settingsgen.cpp.

Referenced by HeaderFileWriter::Finalise().

◆ CopyFile()

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

Copy a file to the output.

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

Definition at line 351 of file settingsgen.cpp.

Referenced by main().

◆ DumpGroup()

static void DumpGroup ( 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 216 of file settingsgen.cpp.

References _stored_output, OutputStore::Add(), IniLoadFile::GetGroup(), IGT_SEQUENCE, IniGroup::item, IniItem::next, and IniGroup::type.

Referenced by ProcessIniFile().

◆ DumpLine()

static void DumpLine ( IniItem item,
IniGroup grp,
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 251 of file settingsgen.cpp.

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

Referenced by DumpSections().

◆ DumpSections()

static void DumpSections ( IniLoadFile ifile)
static

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

Parameters
ifileLoaded INI data.

Definition at line 315 of file settingsgen.cpp.

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

Referenced by ProcessIniFile().

◆ error()

void NORETURN CDECL error ( const char *  s,
  ... 
)

Report a fatal error.

Parameters
sFormat string.
Note
Function does not return.

Definition at line 31 of file settingsgen.cpp.

References lastof, and vseprintf().

Referenced by SettingsIniFile::ReportFileError().

◆ FindItemValue()

static const char* FindItemValue ( const char *  name,
IniGroup grp,
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 236 of file settingsgen.cpp.

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

Referenced by DumpLine().

◆ LoadIniFile()

static IniLoadFile* LoadIniFile ( const char *  filename)
static

Load the INI file.

Parameters
filenameName of the file to load.
Returns
Loaded INI data.

Definition at line 202 of file settingsgen.cpp.

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

Referenced by ProcessIniFile().

◆ 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 455 of file settingsgen.cpp.

References _opts, _post_amble_output, _stored_output, GetOptData::argv, OutputStore::Clear(), CopyFile(), GetOptData::GetOpt(), GetOptData::numleft, GetOptData::opt, ProcessIniFile(), and OutputStore::Write().

Referenced by GetSettingsTree().

◆ 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 441 of file settingsgen.cpp.

References DumpGroup(), DumpSections(), LoadIniFile(), POSTAMBLE_GROUP_NAME, and PREAMBLE_GROUP_NAME.

Referenced by main().

Variable Documentation

◆ _opts

const OptionData _opts[]
static
Initial value:
= {
GETOPT_NOVAL( 'v', "--version"),
GETOPT_NOVAL( 'h', "--help"),
GETOPT_GENERAL('h', '?', nullptr, ODF_NO_VALUE),
GETOPT_VALUE( 'o', "--output"),
GETOPT_VALUE( 'b', "--before"),
GETOPT_VALUE( 'a', "--after"),
}

Options of settingsgen.

Definition at line 411 of file settingsgen.cpp.

Referenced by main().

GETOPT_VALUE
#define GETOPT_VALUE(shortname, longname)
Short option with value.
Definition: getoptdata.h:76
GETOPT_NOVAL
#define GETOPT_NOVAL(shortname, longname)
Short option without value.
Definition: getoptdata.h:69
GETOPT_END
#define GETOPT_END()
Option terminator.
Definition: getoptdata.h:107
GETOPT_GENERAL
#define GETOPT_GENERAL(id, shortname, longname, flags)
General macro for creating an option.
Definition: getoptdata.h:62
ODF_NO_VALUE
@ ODF_NO_VALUE
A plain option (no value attached to it).
Definition: getoptdata.h:15