2011-03-03 20:59:59 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file settingsgen.cpp Tool to create computer-readable settings. */
|
|
|
|
|
|
|
|
#include "../stdafx.h"
|
|
|
|
#include "../string_func.h"
|
|
|
|
#include "../strings_type.h"
|
|
|
|
#include "../misc/getoptdata.h"
|
|
|
|
#include "../ini_type.h"
|
2023-05-18 09:20:35 +00:00
|
|
|
#include "../core/mem_func.hpp"
|
2023-04-19 20:47:36 +00:00
|
|
|
#include "../error_func.h"
|
2011-03-03 20:59:59 +00:00
|
|
|
|
2018-12-09 01:28:14 +00:00
|
|
|
#if !defined(_WIN32) || defined(__CYGWIN__)
|
2011-03-14 22:24:08 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#endif
|
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "../safeguards.h"
|
|
|
|
|
2011-03-03 20:59:59 +00:00
|
|
|
/**
|
|
|
|
* Report a fatal error.
|
|
|
|
* @param s Format string.
|
|
|
|
* @note Function does not return.
|
|
|
|
*/
|
2023-04-19 20:47:36 +00:00
|
|
|
void NORETURN FatalErrorI(const std::string &msg)
|
2011-03-03 20:59:59 +00:00
|
|
|
{
|
2023-05-21 06:13:28 +00:00
|
|
|
fmt::print(stderr, "settingsgen: FATAL: {}\n", msg);
|
2011-03-03 20:59:59 +00:00
|
|
|
exit(1);
|
2012-02-12 19:46:40 +00:00
|
|
|
}
|
2011-03-03 20:59:59 +00:00
|
|
|
|
2019-03-28 20:25:21 +00:00
|
|
|
static const size_t OUTPUT_BLOCK_SIZE = 16000; ///< Block size of the buffer in #OutputBuffer.
|
2011-03-03 20:59:59 +00:00
|
|
|
|
|
|
|
/** Output buffer for a block of data. */
|
|
|
|
class OutputBuffer {
|
|
|
|
public:
|
|
|
|
/** Prepare buffer for use. */
|
|
|
|
void Clear()
|
|
|
|
{
|
|
|
|
this->size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add text to the output buffer.
|
|
|
|
* @param text Text to store.
|
|
|
|
* @param length Length of the text in bytes.
|
|
|
|
* @return Number of bytes actually stored.
|
|
|
|
*/
|
2019-03-28 20:25:21 +00:00
|
|
|
size_t Add(const char *text, size_t length)
|
2011-03-03 20:59:59 +00:00
|
|
|
{
|
2021-01-08 10:16:18 +00:00
|
|
|
size_t store_size = std::min(length, OUTPUT_BLOCK_SIZE - this->size);
|
2011-03-03 20:59:59 +00:00
|
|
|
assert(store_size <= OUTPUT_BLOCK_SIZE);
|
|
|
|
MemCpyT(this->data + this->size, text, store_size);
|
|
|
|
this->size += store_size;
|
|
|
|
return store_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dump buffer to the output stream.
|
|
|
|
* @param out_fp Stream to write the \a data to.
|
|
|
|
*/
|
|
|
|
void Write(FILE *out_fp) const
|
|
|
|
{
|
2019-03-28 20:25:21 +00:00
|
|
|
if (fwrite(this->data, 1, this->size, out_fp) != this->size) {
|
2023-05-05 08:22:03 +00:00
|
|
|
FatalError("Cannot write output");
|
2011-03-03 20:59:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Does the block have room for more data?
|
|
|
|
* @return \c true if room is available, else \c false.
|
|
|
|
*/
|
|
|
|
bool HasRoom() const
|
|
|
|
{
|
|
|
|
return this->size < OUTPUT_BLOCK_SIZE;
|
|
|
|
}
|
|
|
|
|
2019-03-28 20:25:21 +00:00
|
|
|
size_t size; ///< Number of bytes stored in \a data.
|
2011-03-03 20:59:59 +00:00
|
|
|
char data[OUTPUT_BLOCK_SIZE]; ///< Stored data.
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Temporarily store output. */
|
|
|
|
class OutputStore {
|
|
|
|
public:
|
|
|
|
OutputStore()
|
|
|
|
{
|
|
|
|
this->Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Clear the temporary storage. */
|
|
|
|
void Clear()
|
|
|
|
{
|
2018-09-20 22:44:14 +00:00
|
|
|
this->output_buffer.clear();
|
2011-03-03 20:59:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add text to the output storage.
|
|
|
|
* @param text Text to store.
|
|
|
|
* @param length Length of the text in bytes, \c 0 means 'length of the string'.
|
|
|
|
*/
|
2019-03-28 20:25:21 +00:00
|
|
|
void Add(const char *text, size_t length = 0)
|
2011-03-03 20:59:59 +00:00
|
|
|
{
|
|
|
|
if (length == 0) length = strlen(text);
|
|
|
|
|
|
|
|
if (length > 0 && this->BufferHasRoom()) {
|
2019-03-28 20:25:21 +00:00
|
|
|
size_t stored_size = this->output_buffer[this->output_buffer.size() - 1].Add(text, length);
|
2011-03-03 20:59:59 +00:00
|
|
|
length -= stored_size;
|
|
|
|
text += stored_size;
|
|
|
|
}
|
|
|
|
while (length > 0) {
|
2020-12-14 23:22:04 +00:00
|
|
|
OutputBuffer &block = this->output_buffer.emplace_back();
|
2019-02-18 22:39:06 +00:00
|
|
|
block.Clear(); // Initialize the new block.
|
2019-03-28 20:25:21 +00:00
|
|
|
size_t stored_size = block.Add(text, length);
|
2011-03-03 20:59:59 +00:00
|
|
|
length -= stored_size;
|
|
|
|
text += stored_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write all stored output to the output stream.
|
|
|
|
* @param out_fp Stream to write the \a data to.
|
|
|
|
*/
|
|
|
|
void Write(FILE *out_fp) const
|
|
|
|
{
|
2019-02-17 11:20:52 +00:00
|
|
|
for (const OutputBuffer &out_data : output_buffer) {
|
|
|
|
out_data.Write(out_fp);
|
2011-03-03 20:59:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
/**
|
|
|
|
* Does the buffer have room without adding a new #OutputBuffer block?
|
|
|
|
* @return \c true if room is available, else \c false.
|
|
|
|
*/
|
|
|
|
bool BufferHasRoom() const
|
|
|
|
{
|
2019-03-28 20:25:21 +00:00
|
|
|
size_t num_blocks = this->output_buffer.size();
|
2011-03-03 20:59:59 +00:00
|
|
|
return num_blocks > 0 && this->output_buffer[num_blocks - 1].HasRoom();
|
|
|
|
}
|
|
|
|
|
2019-03-03 17:30:09 +00:00
|
|
|
typedef std::vector<OutputBuffer> OutputBufferVector; ///< Vector type for output buffers.
|
2011-03-03 20:59:59 +00:00
|
|
|
OutputBufferVector output_buffer; ///< Vector of blocks containing the stored output.
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/** Derived class for loading INI files without going through Fio stuff. */
|
|
|
|
struct SettingsIniFile : IniLoadFile {
|
2011-05-01 19:51:52 +00:00
|
|
|
/**
|
|
|
|
* Construct a new ini loader.
|
2019-04-10 21:07:06 +00:00
|
|
|
* @param list_group_names A \c nullptr terminated list with group names that should be loaded as lists instead of variables. @see IGT_LIST
|
|
|
|
* @param seq_group_names A \c nullptr terminated list with group names that should be loaded as lists of names. @see IGT_SEQUENCE
|
2011-05-01 19:51:52 +00:00
|
|
|
*/
|
2019-04-10 21:07:06 +00:00
|
|
|
SettingsIniFile(const char * const *list_group_names = nullptr, const char * const *seq_group_names = nullptr) :
|
2011-03-03 20:59:59 +00:00
|
|
|
IniLoadFile(list_group_names, seq_group_names)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-12-06 20:11:49 +00:00
|
|
|
virtual FILE *OpenFile(const std::string &filename, Subdirectory subdir, size_t *size)
|
2011-03-03 20:59:59 +00:00
|
|
|
{
|
|
|
|
/* Open the text file in binary mode to prevent end-of-line translations
|
|
|
|
* done by ftell() and friends, as defined by K&R. */
|
2020-12-06 20:11:49 +00:00
|
|
|
FILE *in = fopen(filename.c_str(), "rb");
|
2019-04-10 21:07:06 +00:00
|
|
|
if (in == nullptr) return nullptr;
|
2011-03-03 20:59:59 +00:00
|
|
|
|
|
|
|
fseek(in, 0L, SEEK_END);
|
|
|
|
*size = ftell(in);
|
|
|
|
|
|
|
|
fseek(in, 0L, SEEK_SET); // Seek back to the start of the file.
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void ReportFileError(const char * const pre, const char * const buffer, const char * const post)
|
|
|
|
{
|
2023-04-19 20:47:36 +00:00
|
|
|
FatalError("{}{}{}", pre, buffer, post);
|
2011-03-03 20:59:59 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
OutputStore _stored_output; ///< Temporary storage of the output, until all processing is done.
|
2021-03-01 12:12:17 +00:00
|
|
|
OutputStore _post_amble_output; ///< Similar to _stored_output, but for the post amble.
|
2011-03-03 20:59:59 +00:00
|
|
|
|
2021-03-01 12:12:17 +00:00
|
|
|
static const char *PREAMBLE_GROUP_NAME = "pre-amble"; ///< Name of the group containing the pre amble.
|
2011-03-03 20:59:59 +00:00
|
|
|
static const char *POSTAMBLE_GROUP_NAME = "post-amble"; ///< Name of the group containing the post amble.
|
2021-03-01 12:12:17 +00:00
|
|
|
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.
|
2011-03-03 20:59:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Load the INI file.
|
|
|
|
* @param filename Name of the file to load.
|
|
|
|
* @return Loaded INI data.
|
|
|
|
*/
|
|
|
|
static IniLoadFile *LoadIniFile(const char *filename)
|
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
static const char * const seq_groups[] = {PREAMBLE_GROUP_NAME, POSTAMBLE_GROUP_NAME, nullptr};
|
2011-03-03 20:59:59 +00:00
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
IniLoadFile *ini = new SettingsIniFile(nullptr, seq_groups);
|
2011-08-24 13:38:26 +00:00
|
|
|
ini->LoadFromDisk(filename, NO_DIRECTORY);
|
2011-03-03 20:59:59 +00:00
|
|
|
return ini;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dump a #IGT_SEQUENCE group into #_stored_output.
|
|
|
|
* @param ifile Loaded INI data.
|
|
|
|
* @param group_name Name of the group to copy.
|
|
|
|
*/
|
|
|
|
static void DumpGroup(IniLoadFile *ifile, const char * const group_name)
|
|
|
|
{
|
2020-05-17 21:32:03 +00:00
|
|
|
IniGroup *grp = ifile->GetGroup(group_name, false);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (grp != nullptr && grp->type == IGT_SEQUENCE) {
|
|
|
|
for (IniItem *item = grp->item; item != nullptr; item = item->next) {
|
2020-05-17 21:32:03 +00:00
|
|
|
if (!item->name.empty()) {
|
|
|
|
_stored_output.Add(item->name.c_str());
|
2011-03-03 20:59:59 +00:00
|
|
|
_stored_output.Add("\n", 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the value of a template variable.
|
|
|
|
* @param name Name of the item to find.
|
|
|
|
* @param grp Group currently being expanded (searched first).
|
2019-04-10 21:07:06 +00:00
|
|
|
* @param defaults Fallback group to search, \c nullptr skips the search.
|
|
|
|
* @return Text of the item if found, else \c nullptr.
|
2011-03-03 20:59:59 +00:00
|
|
|
*/
|
|
|
|
static const char *FindItemValue(const char *name, IniGroup *grp, IniGroup *defaults)
|
|
|
|
{
|
2023-06-05 18:29:52 +00:00
|
|
|
IniItem *item = grp->GetItem(name);
|
|
|
|
if (item == nullptr && defaults != nullptr) item = defaults->GetItem(name);
|
2020-05-17 21:32:03 +00:00
|
|
|
if (item == nullptr || !item->value.has_value()) return nullptr;
|
|
|
|
return item->value->c_str();
|
2011-03-03 20:59:59 +00:00
|
|
|
}
|
|
|
|
|
2021-03-01 12:12:17 +00:00
|
|
|
/**
|
|
|
|
* Parse a single entry via a template and output this.
|
|
|
|
* @param item The template to use for the output.
|
|
|
|
* @param grp Group current being used for template rendering.
|
|
|
|
* @param default_grp Default values for items not set in @grp.
|
|
|
|
* @param output Output to use for result.
|
|
|
|
*/
|
|
|
|
static void DumpLine(IniItem *item, IniGroup *grp, IniGroup *default_grp, OutputStore &output)
|
|
|
|
{
|
|
|
|
static const int MAX_VAR_LENGTH = 64;
|
|
|
|
|
|
|
|
/* Prefix with #if/#ifdef/#ifndef */
|
|
|
|
static const char * const pp_lines[] = {"if", "ifdef", "ifndef", nullptr};
|
|
|
|
int count = 0;
|
|
|
|
for (const char * const *name = pp_lines; *name != nullptr; name++) {
|
|
|
|
const char *condition = FindItemValue(*name, grp, default_grp);
|
|
|
|
if (condition != nullptr) {
|
|
|
|
output.Add("#", 1);
|
|
|
|
output.Add(*name);
|
|
|
|
output.Add(" ", 1);
|
|
|
|
output.Add(condition);
|
|
|
|
output.Add("\n", 1);
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Output text of the template, except template variables of the form '$[_a-z0-9]+' which get replaced by their value. */
|
|
|
|
const char *txt = item->value->c_str();
|
|
|
|
while (*txt != '\0') {
|
|
|
|
if (*txt != '$') {
|
|
|
|
output.Add(txt, 1);
|
|
|
|
txt++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
txt++;
|
|
|
|
if (*txt == '$') { // Literal $
|
|
|
|
output.Add(txt, 1);
|
|
|
|
txt++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read variable. */
|
|
|
|
char variable[MAX_VAR_LENGTH];
|
|
|
|
int i = 0;
|
|
|
|
while (i < MAX_VAR_LENGTH - 1) {
|
|
|
|
if (!(txt[i] == '_' || (txt[i] >= 'a' && txt[i] <= 'z') || (txt[i] >= '0' && txt[i] <= '9'))) break;
|
|
|
|
variable[i] = txt[i];
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
variable[i] = '\0';
|
|
|
|
txt += i;
|
|
|
|
|
|
|
|
if (i > 0) {
|
|
|
|
/* Find the text to output. */
|
|
|
|
const char *valitem = FindItemValue(variable, grp, default_grp);
|
|
|
|
if (valitem != nullptr) output.Add(valitem);
|
|
|
|
} else {
|
|
|
|
output.Add("$", 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
output.Add("\n", 1); // \n after the expanded template.
|
|
|
|
while (count > 0) {
|
|
|
|
output.Add("#endif\n");
|
|
|
|
count--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-03 20:59:59 +00:00
|
|
|
/**
|
|
|
|
* Output all non-special sections through the template / template variable expansion system.
|
|
|
|
* @param ifile Loaded INI data.
|
|
|
|
*/
|
|
|
|
static void DumpSections(IniLoadFile *ifile)
|
|
|
|
{
|
2021-03-01 12:12:17 +00:00
|
|
|
static const char * const special_group_names[] = {PREAMBLE_GROUP_NAME, POSTAMBLE_GROUP_NAME, DEFAULTS_GROUP_NAME, TEMPLATES_GROUP_NAME, VALIDATION_GROUP_NAME, nullptr};
|
2011-03-03 20:59:59 +00:00
|
|
|
|
2020-05-17 21:32:03 +00:00
|
|
|
IniGroup *default_grp = ifile->GetGroup(DEFAULTS_GROUP_NAME, false);
|
|
|
|
IniGroup *templates_grp = ifile->GetGroup(TEMPLATES_GROUP_NAME, false);
|
2021-03-01 12:12:17 +00:00
|
|
|
IniGroup *validation_grp = ifile->GetGroup(VALIDATION_GROUP_NAME, false);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (templates_grp == nullptr) return;
|
2011-03-03 20:59:59 +00:00
|
|
|
|
|
|
|
/* Output every group, using its name as template name. */
|
2019-04-10 21:07:06 +00:00
|
|
|
for (IniGroup *grp = ifile->group; grp != nullptr; grp = grp->next) {
|
2011-03-03 20:59:59 +00:00
|
|
|
const char * const *sgn;
|
2020-05-17 21:32:03 +00:00
|
|
|
for (sgn = special_group_names; *sgn != nullptr; sgn++) if (grp->name == *sgn) break;
|
2019-04-10 21:07:06 +00:00
|
|
|
if (*sgn != nullptr) continue;
|
2011-03-03 20:59:59 +00:00
|
|
|
|
2023-06-05 18:29:52 +00:00
|
|
|
IniItem *template_item = templates_grp->GetItem(grp->name); // Find template value.
|
2020-05-17 21:32:03 +00:00
|
|
|
if (template_item == nullptr || !template_item->value.has_value()) {
|
2023-05-05 08:22:03 +00:00
|
|
|
FatalError("Cannot find template {}", grp->name);
|
2011-03-03 20:59:59 +00:00
|
|
|
}
|
2021-03-01 12:12:17 +00:00
|
|
|
DumpLine(template_item, grp, default_grp, _stored_output);
|
2011-03-03 20:59:59 +00:00
|
|
|
|
2021-03-01 12:12:17 +00:00
|
|
|
if (validation_grp != nullptr) {
|
2023-06-05 18:29:52 +00:00
|
|
|
IniItem *validation_item = validation_grp->GetItem(grp->name); // Find template value.
|
2021-03-01 12:12:17 +00:00
|
|
|
if (validation_item != nullptr && validation_item->value.has_value()) {
|
|
|
|
DumpLine(validation_item, grp, default_grp, _post_amble_output);
|
2011-03-03 20:59:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy a file to the output.
|
|
|
|
* @param fname Filename of file to copy.
|
|
|
|
* @param out_fp Output stream to write to.
|
|
|
|
*/
|
|
|
|
static void CopyFile(const char *fname, FILE *out_fp)
|
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
if (fname == nullptr) return;
|
2011-03-03 20:59:59 +00:00
|
|
|
|
|
|
|
FILE *in_fp = fopen(fname, "r");
|
2019-04-10 21:07:06 +00:00
|
|
|
if (in_fp == nullptr) {
|
2023-05-05 08:22:03 +00:00
|
|
|
FatalError("Cannot open file {} for copying", fname);
|
2011-03-03 20:59:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char buffer[4096];
|
|
|
|
size_t length;
|
|
|
|
do {
|
|
|
|
length = fread(buffer, 1, lengthof(buffer), in_fp);
|
|
|
|
if (fwrite(buffer, 1, length, out_fp) != length) {
|
2023-05-05 08:22:03 +00:00
|
|
|
FatalError("Cannot copy file");
|
2011-03-03 20:59:59 +00:00
|
|
|
}
|
|
|
|
} while (length == lengthof(buffer));
|
|
|
|
|
|
|
|
fclose(in_fp);
|
|
|
|
}
|
|
|
|
|
2011-03-14 22:24:08 +00:00
|
|
|
/**
|
|
|
|
* Compare two files for identity.
|
|
|
|
* @param n1 First file.
|
|
|
|
* @param n2 Second file.
|
|
|
|
* @return True if both files are identical.
|
|
|
|
*/
|
|
|
|
static bool CompareFiles(const char *n1, const char *n2)
|
|
|
|
{
|
|
|
|
FILE *f2 = fopen(n2, "rb");
|
2019-04-10 21:07:06 +00:00
|
|
|
if (f2 == nullptr) return false;
|
2011-03-14 22:24:08 +00:00
|
|
|
|
|
|
|
FILE *f1 = fopen(n1, "rb");
|
2019-04-10 21:07:06 +00:00
|
|
|
if (f1 == nullptr) {
|
2018-10-14 22:36:14 +00:00
|
|
|
fclose(f2);
|
2023-04-19 20:47:36 +00:00
|
|
|
FatalError("can't open {}", n1);
|
2018-10-14 22:36:14 +00:00
|
|
|
}
|
2011-03-14 22:24:08 +00:00
|
|
|
|
|
|
|
size_t l1, l2;
|
|
|
|
do {
|
|
|
|
char b1[4096];
|
|
|
|
char b2[4096];
|
|
|
|
l1 = fread(b1, 1, sizeof(b1), f1);
|
|
|
|
l2 = fread(b2, 1, sizeof(b2), f2);
|
|
|
|
|
|
|
|
if (l1 != l2 || memcmp(b1, b2, l1) != 0) {
|
|
|
|
fclose(f2);
|
|
|
|
fclose(f1);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} while (l1 != 0);
|
|
|
|
|
|
|
|
fclose(f2);
|
|
|
|
fclose(f1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-03-03 20:59:59 +00:00
|
|
|
/** Options of settingsgen. */
|
|
|
|
static const OptionData _opts[] = {
|
|
|
|
GETOPT_NOVAL( 'h', "--help"),
|
2019-04-10 21:07:06 +00:00
|
|
|
GETOPT_GENERAL('h', '?', nullptr, ODF_NO_VALUE),
|
2011-03-03 20:59:59 +00:00
|
|
|
GETOPT_VALUE( 'o', "--output"),
|
|
|
|
GETOPT_VALUE( 'b', "--before"),
|
|
|
|
GETOPT_VALUE( 'a', "--after"),
|
|
|
|
GETOPT_END(),
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
2018-10-28 02:17:36 +00:00
|
|
|
* It starts with a optional \c \#if line if an 'if' item exists in the group. The item
|
|
|
|
* value is used as condition. Similarly, \c \#ifdef and \c \#ifndef lines are also written.
|
2011-03-03 20:59:59 +00:00
|
|
|
* 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.
|
2018-10-28 02:17:36 +00:00
|
|
|
* Finally, \c \#endif lines are written to match the macro processor lines.
|
2011-03-03 20:59:59 +00:00
|
|
|
*
|
|
|
|
* Last but not least, the [post-amble] group is copied verbatim.
|
|
|
|
*
|
|
|
|
* @param fname Ini file to process. @return Exit status of the processing.
|
|
|
|
*/
|
|
|
|
static void ProcessIniFile(const char *fname)
|
|
|
|
{
|
|
|
|
IniLoadFile *ini_data = LoadIniFile(fname);
|
|
|
|
DumpGroup(ini_data, PREAMBLE_GROUP_NAME);
|
|
|
|
DumpSections(ini_data);
|
|
|
|
DumpGroup(ini_data, POSTAMBLE_GROUP_NAME);
|
|
|
|
delete ini_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* And the main program (what else?)
|
|
|
|
* @param argc Number of command-line arguments including the program name itself.
|
|
|
|
* @param argv Vector of the command-line arguments.
|
|
|
|
*/
|
|
|
|
int CDECL main(int argc, char *argv[])
|
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
const char *output_file = nullptr;
|
|
|
|
const char *before_file = nullptr;
|
|
|
|
const char *after_file = nullptr;
|
2011-03-03 20:59:59 +00:00
|
|
|
|
|
|
|
GetOptData mgo(argc - 1, argv + 1, _opts);
|
|
|
|
for (;;) {
|
|
|
|
int i = mgo.GetOpt();
|
|
|
|
if (i == -1) break;
|
|
|
|
|
|
|
|
switch (i) {
|
|
|
|
case 'h':
|
2023-05-21 06:33:04 +00:00
|
|
|
fmt::print("settingsgen\n"
|
2011-03-03 20:59:59 +00:00
|
|
|
"Usage: settingsgen [options] ini-file...\n"
|
|
|
|
"with options:\n"
|
|
|
|
" -h, -?, --help Print this help message and exit\n"
|
|
|
|
" -b FILE, --before FILE Copy FILE before all settings\n"
|
|
|
|
" -a FILE, --after FILE Copy FILE after all settings\n"
|
|
|
|
" -o FILE, --output FILE Write output to FILE\n");
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case 'o':
|
|
|
|
output_file = mgo.opt;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'a':
|
|
|
|
after_file = mgo.opt;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'b':
|
|
|
|
before_file = mgo.opt;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case -2:
|
2023-05-21 06:13:28 +00:00
|
|
|
fmt::print(stderr, "Invalid arguments\n");
|
2011-03-03 20:59:59 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_stored_output.Clear();
|
2021-03-01 12:12:17 +00:00
|
|
|
_post_amble_output.Clear();
|
2011-03-03 20:59:59 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < mgo.numleft; i++) ProcessIniFile(mgo.argv[i]);
|
|
|
|
|
|
|
|
/* Write output. */
|
2019-04-10 21:07:06 +00:00
|
|
|
if (output_file == nullptr) {
|
2011-03-03 20:59:59 +00:00
|
|
|
CopyFile(before_file, stdout);
|
|
|
|
_stored_output.Write(stdout);
|
2021-03-01 12:12:17 +00:00
|
|
|
_post_amble_output.Write(stdout);
|
2011-03-03 20:59:59 +00:00
|
|
|
CopyFile(after_file, stdout);
|
|
|
|
} else {
|
2011-03-14 22:24:08 +00:00
|
|
|
static const char * const tmp_output = "tmp2.xxx";
|
|
|
|
|
|
|
|
FILE *fp = fopen(tmp_output, "w");
|
2019-04-10 21:07:06 +00:00
|
|
|
if (fp == nullptr) {
|
2023-05-05 08:22:03 +00:00
|
|
|
FatalError("Cannot open file {}", tmp_output);
|
2011-03-04 22:34:10 +00:00
|
|
|
}
|
2011-03-03 20:59:59 +00:00
|
|
|
CopyFile(before_file, fp);
|
|
|
|
_stored_output.Write(fp);
|
2021-03-01 12:12:17 +00:00
|
|
|
_post_amble_output.Write(fp);
|
2011-03-03 20:59:59 +00:00
|
|
|
CopyFile(after_file, fp);
|
|
|
|
fclose(fp);
|
2011-03-14 22:24:08 +00:00
|
|
|
|
|
|
|
if (CompareFiles(tmp_output, output_file)) {
|
|
|
|
/* Files are equal. tmp2.xxx is not needed. */
|
|
|
|
unlink(tmp_output);
|
|
|
|
} else {
|
|
|
|
/* Rename tmp2.xxx to output file. */
|
2018-12-09 01:28:14 +00:00
|
|
|
#if defined(_WIN32)
|
2011-03-14 22:24:08 +00:00
|
|
|
unlink(output_file);
|
|
|
|
#endif
|
2023-04-19 20:47:36 +00:00
|
|
|
if (rename(tmp_output, output_file) == -1) FatalError("rename() failed");
|
2011-03-14 22:24:08 +00:00
|
|
|
}
|
2011-03-03 20:59:59 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|