12 #include "../stdafx.h"
13 #include "../string_func.h"
14 #include "../strings_type.h"
15 #include "../misc/getoptdata.h"
16 #include "../ini_type.h"
17 #include "../core/smallvec_type.hpp"
21 #if (!defined(WIN32) && !defined(WIN64)) || defined(__CYGWIN__)
33 #include "../safeguards.h"
40 void NORETURN CDECL
error(
const char *s, ...)
47 fprintf(stderr,
"FATAL: %s\n", buf);
68 int Add(
const char *text,
int length)
71 assert(store_size >= 0);
74 this->
size += store_size;
84 if (fwrite(this->
data, 1, this->
size, out_fp) != (
size_t)this->
size) {
85 fprintf(stderr,
"Error: Cannot write output\n");
121 void Add(
const char *text,
int length = 0)
123 if (length == 0) length = strlen(text);
127 length -= stored_size;
133 int stored_size = block->
Add(text, length);
134 length -= stored_size;
146 out_data->Write(out_fp);
158 return num_blocks > 0 && this->
output_buffer[num_blocks - 1].HasRoom();
182 FILE *in = fopen(filename,
"rb");
183 if (in == NULL)
return NULL;
185 fseek(in, 0L, SEEK_END);
188 fseek(in, 0L, SEEK_SET);
192 virtual void ReportFileError(
const char *
const pre,
const char *
const buffer,
const char *
const post)
194 error(
"%s%s%s", pre, buffer, post);
231 _stored_output.
Add(item->name);
232 _stored_output.
Add(
"\n", 1);
248 if (item == NULL && defaults != NULL) item = defaults->
GetItem(name,
false);
249 if (item == NULL || item->
value == NULL)
return NULL;
259 static const int MAX_VAR_LENGTH = 64;
264 if (templates_grp == NULL)
return;
268 const char *
const *sgn;
269 for (sgn = special_group_names; *sgn != NULL; sgn++)
if (strcmp(grp->name, *sgn) == 0)
break;
270 if (*sgn != NULL)
continue;
272 IniItem *template_item = templates_grp->
GetItem(grp->name,
false);
273 if (template_item == NULL || template_item->
value == NULL) {
274 fprintf(stderr,
"settingsgen: Warning: Cannot find template %s\n", grp->name);
279 static const char *
const pp_lines[] = {
"if",
"ifdef",
"ifndef", NULL};
281 for (
const char *
const *name = pp_lines; *name != NULL; name++) {
282 const char *condition =
FindItemValue(*name, grp, default_grp);
283 if (condition != NULL) {
284 _stored_output.
Add(
"#", 1);
285 _stored_output.
Add(*name);
286 _stored_output.
Add(
" ", 1);
287 _stored_output.
Add(condition);
288 _stored_output.
Add(
"\n", 1);
294 const char *txt = template_item->
value;
295 while (*txt !=
'\0') {
297 _stored_output.
Add(txt, 1);
303 _stored_output.
Add(txt, 1);
309 char variable[MAX_VAR_LENGTH];
311 while (i < MAX_VAR_LENGTH - 1) {
312 if (!(txt[i] ==
'_' || (txt[i] >=
'a' && txt[i] <=
'z') || (txt[i] >=
'0' && txt[i] <=
'9')))
break;
313 variable[i] = txt[i];
321 const char *valitem =
FindItemValue(variable, grp, default_grp);
322 if (valitem != NULL) _stored_output.
Add(valitem);
324 _stored_output.
Add(
"$", 1);
327 _stored_output.
Add(
"\n", 1);
329 _stored_output.
Add(
"#endif\n");
340 static void CopyFile(
const char *fname, FILE *out_fp)
342 if (fname == NULL)
return;
344 FILE *in_fp = fopen(fname,
"r");
346 fprintf(stderr,
"settingsgen: Warning: Cannot open file %s for copying\n", fname);
353 length = fread(buffer, 1,
lengthof(buffer), in_fp);
354 if (fwrite(buffer, 1, length, out_fp) != length) {
355 fprintf(stderr,
"Error: Cannot copy file\n");
358 }
while (length ==
lengthof(buffer));
371 FILE *f2 = fopen(n2,
"rb");
372 if (f2 == NULL)
return false;
374 FILE *f1 = fopen(n1,
"rb");
375 if (f1 == NULL)
error(
"can't open %s", n1);
381 l1 = fread(b1, 1,
sizeof(b1), f1);
382 l2 = fread(b2, 1,
sizeof(b2), f2);
384 if (l1 != l2 || memcmp(b1, b2, l1) != 0) {
441 int CDECL
main(
int argc,
char *argv[])
443 const char *output_file = NULL;
444 const char *before_file = NULL;
445 const char *after_file = NULL;
458 puts(
"settingsgen - $Revision$\n"
459 "Usage: settingsgen [options] ini-file...\n"
461 " -v, --version Print version information and exit\n"
462 " -h, -?, --help Print this help message and exit\n"
463 " -b FILE, --before FILE Copy FILE before all settings\n"
464 " -a FILE, --after FILE Copy FILE after all settings\n"
465 " -o FILE, --output FILE Write output to FILE\n");
469 output_file = mgo.
opt;
473 after_file = mgo.
opt;
477 before_file = mgo.
opt;
481 fprintf(stderr,
"Invalid arguments\n");
486 _stored_output.
Clear();
491 if (output_file == NULL) {
493 _stored_output.
Write(stdout);
496 static const char *
const tmp_output =
"tmp2.xxx";
498 FILE *fp = fopen(tmp_output,
"w");
500 fprintf(stderr,
"settingsgen: Warning: Cannot open file %s\n", tmp_output);
504 _stored_output.
Write(fp);
513 #if defined(WIN32) || defined(WIN64)
516 if (rename(tmp_output, output_file) == -1)
error(
"rename() failed");