OpenTTD Source  1.11.0-beta2
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 <http://www.gnu.org/licenses/>.
6  */
7 
10 #include "../stdafx.h"
11 #include "../string_func.h"
12 #include "../strings_type.h"
13 #include "../misc/getoptdata.h"
14 #include "../ini_type.h"
15 #include "../core/smallvec_type.hpp"
16 
17 #include <stdarg.h>
18 
19 #if !defined(_WIN32) || defined(__CYGWIN__)
20 #include <unistd.h>
21 #include <sys/stat.h>
22 #endif
23 
24 #include "../safeguards.h"
25 
31 void NORETURN CDECL error(const char *s, ...)
32 {
33  char buf[1024];
34  va_list va;
35  va_start(va, s);
36  vseprintf(buf, lastof(buf), s, va);
37  va_end(va);
38  fprintf(stderr, "FATAL: %s\n", buf);
39  exit(1);
40 }
41 
42 static const size_t OUTPUT_BLOCK_SIZE = 16000;
43 
45 class OutputBuffer {
46 public:
48  void Clear()
49  {
50  this->size = 0;
51  }
52 
59  size_t Add(const char *text, size_t length)
60  {
61  size_t store_size = std::min(length, OUTPUT_BLOCK_SIZE - this->size);
62  assert(store_size <= OUTPUT_BLOCK_SIZE);
63  MemCpyT(this->data + this->size, text, store_size);
64  this->size += store_size;
65  return store_size;
66  }
67 
72  void Write(FILE *out_fp) const
73  {
74  if (fwrite(this->data, 1, this->size, out_fp) != this->size) {
75  fprintf(stderr, "Error: Cannot write output\n");
76  }
77  }
78 
83  bool HasRoom() const
84  {
85  return this->size < OUTPUT_BLOCK_SIZE;
86  }
87 
88  size_t size;
90 };
91 
93 class OutputStore {
94 public:
95  OutputStore()
96  {
97  this->Clear();
98  }
99 
101  void Clear()
102  {
103  this->output_buffer.clear();
104  }
105 
111  void Add(const char *text, size_t length = 0)
112  {
113  if (length == 0) length = strlen(text);
114 
115  if (length > 0 && this->BufferHasRoom()) {
116  size_t stored_size = this->output_buffer[this->output_buffer.size() - 1].Add(text, length);
117  length -= stored_size;
118  text += stored_size;
119  }
120  while (length > 0) {
121  OutputBuffer &block = this->output_buffer.emplace_back();
122  block.Clear(); // Initialize the new block.
123  size_t stored_size = block.Add(text, length);
124  length -= stored_size;
125  text += stored_size;
126  }
127  }
128 
133  void Write(FILE *out_fp) const
134  {
135  for (const OutputBuffer &out_data : output_buffer) {
136  out_data.Write(out_fp);
137  }
138  }
139 
140 private:
145  bool BufferHasRoom() const
146  {
147  size_t num_blocks = this->output_buffer.size();
148  return num_blocks > 0 && this->output_buffer[num_blocks - 1].HasRoom();
149  }
150 
151  typedef std::vector<OutputBuffer> OutputBufferVector;
153 };
154 
155 
163  SettingsIniFile(const char * const *list_group_names = nullptr, const char * const *seq_group_names = nullptr) :
165  {
166  }
167 
168  virtual FILE *OpenFile(const std::string &filename, Subdirectory subdir, size_t *size)
169  {
170  /* Open the text file in binary mode to prevent end-of-line translations
171  * done by ftell() and friends, as defined by K&R. */
172  FILE *in = fopen(filename.c_str(), "rb");
173  if (in == nullptr) return nullptr;
174 
175  fseek(in, 0L, SEEK_END);
176  *size = ftell(in);
177 
178  fseek(in, 0L, SEEK_SET); // Seek back to the start of the file.
179  return in;
180  }
181 
182  virtual void ReportFileError(const char * const pre, const char * const buffer, const char * const post)
183  {
184  error("%s%s%s", pre, buffer, post);
185  }
186 };
187 
189 
190 static const char *PREAMBLE_GROUP_NAME = "pre-amble";
191 static const char *POSTAMBLE_GROUP_NAME = "post-amble";
192 static const char *TEMPLATES_GROUP_NAME = "templates";
193 static const char *DEFAULTS_GROUP_NAME = "defaults";
194 
200 static IniLoadFile *LoadIniFile(const char *filename)
201 {
202  static const char * const seq_groups[] = {PREAMBLE_GROUP_NAME, POSTAMBLE_GROUP_NAME, nullptr};
203 
204  IniLoadFile *ini = new SettingsIniFile(nullptr, seq_groups);
205  ini->LoadFromDisk(filename, NO_DIRECTORY);
206  return ini;
207 }
208 
214 static void DumpGroup(IniLoadFile *ifile, const char * const group_name)
215 {
216  IniGroup *grp = ifile->GetGroup(group_name, false);
217  if (grp != nullptr && grp->type == IGT_SEQUENCE) {
218  for (IniItem *item = grp->item; item != nullptr; item = item->next) {
219  if (!item->name.empty()) {
220  _stored_output.Add(item->name.c_str());
221  _stored_output.Add("\n", 1);
222  }
223  }
224  }
225 }
226 
234 static const char *FindItemValue(const char *name, IniGroup *grp, IniGroup *defaults)
235 {
236  IniItem *item = grp->GetItem(name, false);
237  if (item == nullptr && defaults != nullptr) item = defaults->GetItem(name, false);
238  if (item == nullptr || !item->value.has_value()) return nullptr;
239  return item->value->c_str();
240 }
241 
246 static void DumpSections(IniLoadFile *ifile)
247 {
248  static const int MAX_VAR_LENGTH = 64;
249  static const char * const special_group_names[] = {PREAMBLE_GROUP_NAME, POSTAMBLE_GROUP_NAME, DEFAULTS_GROUP_NAME, TEMPLATES_GROUP_NAME, nullptr};
250 
251  IniGroup *default_grp = ifile->GetGroup(DEFAULTS_GROUP_NAME, false);
252  IniGroup *templates_grp = ifile->GetGroup(TEMPLATES_GROUP_NAME, false);
253  if (templates_grp == nullptr) return;
254 
255  /* Output every group, using its name as template name. */
256  for (IniGroup *grp = ifile->group; grp != nullptr; grp = grp->next) {
257  const char * const *sgn;
258  for (sgn = special_group_names; *sgn != nullptr; sgn++) if (grp->name == *sgn) break;
259  if (*sgn != nullptr) continue;
260 
261  IniItem *template_item = templates_grp->GetItem(grp->name, false); // Find template value.
262  if (template_item == nullptr || !template_item->value.has_value()) {
263  fprintf(stderr, "settingsgen: Warning: Cannot find template %s\n", grp->name.c_str());
264  continue;
265  }
266 
267  /* Prefix with #if/#ifdef/#ifndef */
268  static const char * const pp_lines[] = {"if", "ifdef", "ifndef", nullptr};
269  int count = 0;
270  for (const char * const *name = pp_lines; *name != nullptr; name++) {
271  const char *condition = FindItemValue(*name, grp, default_grp);
272  if (condition != nullptr) {
273  _stored_output.Add("#", 1);
274  _stored_output.Add(*name);
275  _stored_output.Add(" ", 1);
276  _stored_output.Add(condition);
277  _stored_output.Add("\n", 1);
278  count++;
279  }
280  }
281 
282  /* Output text of the template, except template variables of the form '$[_a-z0-9]+' which get replaced by their value. */
283  const char *txt = template_item->value->c_str();
284  while (*txt != '\0') {
285  if (*txt != '$') {
286  _stored_output.Add(txt, 1);
287  txt++;
288  continue;
289  }
290  txt++;
291  if (*txt == '$') { // Literal $
292  _stored_output.Add(txt, 1);
293  txt++;
294  continue;
295  }
296 
297  /* Read variable. */
298  char variable[MAX_VAR_LENGTH];
299  int i = 0;
300  while (i < MAX_VAR_LENGTH - 1) {
301  if (!(txt[i] == '_' || (txt[i] >= 'a' && txt[i] <= 'z') || (txt[i] >= '0' && txt[i] <= '9'))) break;
302  variable[i] = txt[i];
303  i++;
304  }
305  variable[i] = '\0';
306  txt += i;
307 
308  if (i > 0) {
309  /* Find the text to output. */
310  const char *valitem = FindItemValue(variable, grp, default_grp);
311  if (valitem != nullptr) _stored_output.Add(valitem);
312  } else {
313  _stored_output.Add("$", 1);
314  }
315  }
316  _stored_output.Add("\n", 1); // \n after the expanded template.
317  while (count > 0) {
318  _stored_output.Add("#endif\n");
319  count--;
320  }
321  }
322 }
323 
329 static void CopyFile(const char *fname, FILE *out_fp)
330 {
331  if (fname == nullptr) return;
332 
333  FILE *in_fp = fopen(fname, "r");
334  if (in_fp == nullptr) {
335  fprintf(stderr, "settingsgen: Warning: Cannot open file %s for copying\n", fname);
336  return;
337  }
338 
339  char buffer[4096];
340  size_t length;
341  do {
342  length = fread(buffer, 1, lengthof(buffer), in_fp);
343  if (fwrite(buffer, 1, length, out_fp) != length) {
344  fprintf(stderr, "Error: Cannot copy file\n");
345  break;
346  }
347  } while (length == lengthof(buffer));
348 
349  fclose(in_fp);
350 }
351 
358 static bool CompareFiles(const char *n1, const char *n2)
359 {
360  FILE *f2 = fopen(n2, "rb");
361  if (f2 == nullptr) return false;
362 
363  FILE *f1 = fopen(n1, "rb");
364  if (f1 == nullptr) {
365  fclose(f2);
366  error("can't open %s", n1);
367  }
368 
369  size_t l1, l2;
370  do {
371  char b1[4096];
372  char b2[4096];
373  l1 = fread(b1, 1, sizeof(b1), f1);
374  l2 = fread(b2, 1, sizeof(b2), f2);
375 
376  if (l1 != l2 || memcmp(b1, b2, l1) != 0) {
377  fclose(f2);
378  fclose(f1);
379  return false;
380  }
381  } while (l1 != 0);
382 
383  fclose(f2);
384  fclose(f1);
385  return true;
386 }
387 
389 static const OptionData _opts[] = {
390  GETOPT_NOVAL( 'v', "--version"),
391  GETOPT_NOVAL( 'h', "--help"),
392  GETOPT_GENERAL('h', '?', nullptr, ODF_NO_VALUE),
393  GETOPT_VALUE( 'o', "--output"),
394  GETOPT_VALUE( 'b', "--before"),
395  GETOPT_VALUE( 'a', "--after"),
396  GETOPT_END(),
397 };
398 
419 static void ProcessIniFile(const char *fname)
420 {
421  IniLoadFile *ini_data = LoadIniFile(fname);
422  DumpGroup(ini_data, PREAMBLE_GROUP_NAME);
423  DumpSections(ini_data);
424  DumpGroup(ini_data, POSTAMBLE_GROUP_NAME);
425  delete ini_data;
426 }
427 
433 int CDECL main(int argc, char *argv[])
434 {
435  const char *output_file = nullptr;
436  const char *before_file = nullptr;
437  const char *after_file = nullptr;
438 
439  GetOptData mgo(argc - 1, argv + 1, _opts);
440  for (;;) {
441  int i = mgo.GetOpt();
442  if (i == -1) break;
443 
444  switch (i) {
445  case 'v':
446  puts("$Revision$");
447  return 0;
448 
449  case 'h':
450  puts("settingsgen - $Revision$\n"
451  "Usage: settingsgen [options] ini-file...\n"
452  "with options:\n"
453  " -v, --version Print version information and exit\n"
454  " -h, -?, --help Print this help message and exit\n"
455  " -b FILE, --before FILE Copy FILE before all settings\n"
456  " -a FILE, --after FILE Copy FILE after all settings\n"
457  " -o FILE, --output FILE Write output to FILE\n");
458  return 0;
459 
460  case 'o':
461  output_file = mgo.opt;
462  break;
463 
464  case 'a':
465  after_file = mgo.opt;
466  break;
467 
468  case 'b':
469  before_file = mgo.opt;
470  break;
471 
472  case -2:
473  fprintf(stderr, "Invalid arguments\n");
474  return 1;
475  }
476  }
477 
479 
480  for (int i = 0; i < mgo.numleft; i++) ProcessIniFile(mgo.argv[i]);
481 
482  /* Write output. */
483  if (output_file == nullptr) {
484  CopyFile(before_file, stdout);
485  _stored_output.Write(stdout);
486  CopyFile(after_file, stdout);
487  } else {
488  static const char * const tmp_output = "tmp2.xxx";
489 
490  FILE *fp = fopen(tmp_output, "w");
491  if (fp == nullptr) {
492  fprintf(stderr, "settingsgen: Warning: Cannot open file %s\n", tmp_output);
493  return 1;
494  }
495  CopyFile(before_file, fp);
496  _stored_output.Write(fp);
497  CopyFile(after_file, fp);
498  fclose(fp);
499 
500  if (CompareFiles(tmp_output, output_file)) {
501  /* Files are equal. tmp2.xxx is not needed. */
502  unlink(tmp_output);
503  } else {
504  /* Rename tmp2.xxx to output file. */
505 #if defined(_WIN32)
506  unlink(output_file);
507 #endif
508  if (rename(tmp_output, output_file) == -1) error("rename() failed");
509  }
510  }
511  return 0;
512 }
ProcessIniFile
static void ProcessIniFile(const char *fname)
Process a single INI file.
Definition: settingsgen.cpp:419
CompareFiles
static bool CompareFiles(const char *n1, const char *n2)
Compare two files for identity.
Definition: settingsgen.cpp:358
OutputStore::Write
void Write(FILE *out_fp) const
Write all stored output to the output stream.
Definition: settingsgen.cpp:133
LoadIniFile
static IniLoadFile * LoadIniFile(const char *filename)
Load the INI file.
Definition: settingsgen.cpp:200
IniItem::next
IniItem * next
The next item in this group.
Definition: ini_type.h:26
OutputBuffer::data
char data[OUTPUT_BLOCK_SIZE]
Stored data.
Definition: settingsgen.cpp:89
OutputBuffer::Clear
void Clear()
Prepare buffer for use.
Definition: settingsgen.cpp:48
SettingsIniFile::OpenFile
virtual FILE * OpenFile(const std::string &filename, Subdirectory subdir, size_t *size)
Open the INI file.
Definition: settingsgen.cpp:168
DEFAULTS_GROUP_NAME
static const char * DEFAULTS_GROUP_NAME
Name of the group containing default values for the template variables.
Definition: settingsgen.cpp:193
IniItem
A single "line" in an ini file.
Definition: ini_type.h:25
IniGroup
A group within an ini file.
Definition: ini_type.h:38
GETOPT_VALUE
#define GETOPT_VALUE(shortname, longname)
Short option with value.
Definition: getoptdata.h:76
OutputStore::BufferHasRoom
bool BufferHasRoom() const
Does the buffer have room without adding a new OutputBuffer block?
Definition: settingsgen.cpp:145
_stored_output
OutputStore _stored_output
Temporary storage of the output, until all processing is done.
Definition: settingsgen.cpp:188
MemCpyT
static void MemCpyT(T *destination, const T *source, size_t num=1)
Type-safe version of memcpy().
Definition: mem_func.hpp:23
OutputBuffer::size
size_t size
Number of bytes stored in data.
Definition: settingsgen.cpp:88
IniLoadFile
Ini file that only supports loading.
Definition: ini_type.h:54
GetOptData::argv
char ** argv
Remaining command line arguments.
Definition: getoptdata.h:33
DumpGroup
static void DumpGroup(IniLoadFile *ifile, const char *const group_name)
Dump a IGT_SEQUENCE group into _stored_output.
Definition: settingsgen.cpp:214
POSTAMBLE_GROUP_NAME
static const char * POSTAMBLE_GROUP_NAME
Name of the group containing the post amble.
Definition: settingsgen.cpp:191
OutputStore::Clear
void Clear()
Clear the temporary storage.
Definition: settingsgen.cpp:101
OUTPUT_BLOCK_SIZE
static const size_t OUTPUT_BLOCK_SIZE
Block size of the buffer in OutputBuffer.
Definition: settingsgen.cpp:42
GETOPT_NOVAL
#define GETOPT_NOVAL(shortname, longname)
Short option without value.
Definition: getoptdata.h:69
IniItem::value
std::optional< std::string > value
The value of this item.
Definition: ini_type.h:28
GETOPT_END
#define GETOPT_END()
Option terminator.
Definition: getoptdata.h:107
OutputBuffer::HasRoom
bool HasRoom() const
Does the block have room for more data?
Definition: settingsgen.cpp:83
GETOPT_GENERAL
#define GETOPT_GENERAL(id, shortname, longname, flags)
General macro for creating an option.
Definition: getoptdata.h:62
OutputStore::output_buffer
OutputBufferVector output_buffer
Vector of blocks containing the stored output.
Definition: settingsgen.cpp:152
GetOptData::opt
char * opt
Option value, if available (else nullptr).
Definition: getoptdata.h:31
vseprintf
int CDECL vseprintf(char *str, const char *last, const char *format, va_list ap)
Safer implementation of vsnprintf; same as vsnprintf except:
Definition: string.cpp:61
IniGroup::type
IniGroupType type
type of group
Definition: ini_type.h:40
OptionData
Data of an option.
Definition: getoptdata.h:22
GetOptData
Data storage for parsing command line options.
Definition: getoptdata.h:30
SettingsIniFile
Derived class for loading INI files without going through Fio stuff.
Definition: settingsgen.cpp:157
OutputBuffer::Write
void Write(FILE *out_fp) const
Dump buffer to the output stream.
Definition: settingsgen.cpp:72
OutputStore
Temporarily store output.
Definition: settingsgen.cpp:93
OutputStore::OutputBufferVector
std::vector< OutputBuffer > OutputBufferVector
Vector type for output buffers.
Definition: settingsgen.cpp:151
IniLoadFile::list_group_names
const char *const * list_group_names
nullptr terminated list with group names that are lists
Definition: ini_type.h:58
_opts
static const OptionData _opts[]
Options of settingsgen.
Definition: settingsgen.cpp:389
IniLoadFile::group
IniGroup * group
the first group in the ini
Definition: ini_type.h:55
PREAMBLE_GROUP_NAME
static const char * PREAMBLE_GROUP_NAME
Name of the group containing the pre amble.
Definition: settingsgen.cpp:190
OutputBuffer::Add
size_t Add(const char *text, size_t length)
Add text to the output buffer.
Definition: settingsgen.cpp:59
main
int CDECL main(int argc, char *argv[])
And the main program (what else?)
Definition: settingsgen.cpp:433
FindItemValue
static const char * FindItemValue(const char *name, IniGroup *grp, IniGroup *defaults)
Find the value of a template variable.
Definition: settingsgen.cpp:234
NO_DIRECTORY
@ NO_DIRECTORY
A path without any base directory.
Definition: fileio_type.h:125
error
void NORETURN CDECL error(const char *s,...)
Report a fatal error.
Definition: settingsgen.cpp:31
IGT_SEQUENCE
@ IGT_SEQUENCE
A list of uninterpreted lines, terminated by the next group block.
Definition: ini_type.h:21
Subdirectory
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition: fileio_type.h:108
CopyFile
static void CopyFile(const char *fname, FILE *out_fp)
Copy a file to the output.
Definition: settingsgen.cpp:329
GetOptData::GetOpt
int GetOpt()
Find the next option.
Definition: getoptdata.cpp:22
OutputStore::Add
void Add(const char *text, size_t length=0)
Add text to the output storage.
Definition: settingsgen.cpp:111
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:367
IniGroup::item
IniItem * item
the first item in the group
Definition: ini_type.h:41
OutputBuffer
Output buffer for a block of data.
Definition: settingsgen.cpp:45
IniLoadFile::seq_group_names
const char *const * seq_group_names
nullptr terminated list with group names that are sequences.
Definition: ini_type.h:59
DumpSections
static void DumpSections(IniLoadFile *ifile)
Output all non-special sections through the template / template variable expansion system.
Definition: settingsgen.cpp:246
IniGroup::GetItem
IniItem * GetItem(const std::string &name, bool create)
Get the item with the given name, and if it doesn't exist and create is true it creates a new item.
Definition: ini_load.cpp:95
IniLoadFile::LoadFromDisk
void LoadFromDisk(const std::string &filename, Subdirectory subdir)
Load the Ini file's data from the disk.
Definition: ini_load.cpp:195
SettingsIniFile::ReportFileError
virtual void ReportFileError(const char *const pre, const char *const buffer, const char *const post)
Report an error about the file contents.
Definition: settingsgen.cpp:182
SettingsIniFile::SettingsIniFile
SettingsIniFile(const char *const *list_group_names=nullptr, const char *const *seq_group_names=nullptr)
Construct a new ini loader.
Definition: settingsgen.cpp:163
lastof
#define lastof(x)
Get the last element of an fixed size array.
Definition: stdafx.h:383
ODF_NO_VALUE
@ ODF_NO_VALUE
A plain option (no value attached to it).
Definition: getoptdata.h:15
GetOptData::numleft
int numleft
Number of arguments left in argv.
Definition: getoptdata.h:32
TEMPLATES_GROUP_NAME
static const char * TEMPLATES_GROUP_NAME
Name of the group containing the templates.
Definition: settingsgen.cpp:192
IniLoadFile::GetGroup
IniGroup * GetGroup(const std::string &name, bool create_new=true)
Get the group with the given name.
Definition: ini_load.cpp:143
IniGroup::next
IniGroup * next
the next group within this file
Definition: ini_type.h:39