annotate tools/data2inc.c @ 2565:d56a0e86067a

Improve error handling.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 28 Feb 2022 11:49:58 +0200
parents c6ee41fd98dd
children d75431bf1a7d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 /*
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 * data2inc - Convert binary data to "C"-source or XA-compatible include file
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3 * Programmed and designed by Matti 'ccr' Hamalainen
2539
c6ee41fd98dd Bump copyright years.
Matti Hamalainen <ccr@tnsp.org>
parents: 2427
diff changeset
4 * (C) Copyright 2003,2009-2021 Tecnic Software productions (TNSP)
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
5 *
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6 * Please read file 'COPYING' for information on license and distribution.
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 */
1432
a9516570cc26 Improve build, so that we can build the tools and tests with minimal
Matti Hamalainen <ccr@tnsp.org>
parents: 1429
diff changeset
8 #include "dmtool.h"
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
9 #include "dmlib.h"
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10 #include "dmargs.h"
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
11 #include "dmmutex.h"
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
12
1433
d8a83582f78f Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 1432
diff changeset
13
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
14 #define SET_DEF_LINELEN 16
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
15 #define SET_MAX_FEXTS 16
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
16
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
17
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
18 //
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
19 // Types etc
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
20 //
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
21 typedef struct
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
22 {
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
23 char *name;
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
24 char *desc;
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
25 char *fexts[SET_MAX_FEXTS];
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
26
2250
956db0095f82 Change struct member name.
Matti Hamalainen <ccr@tnsp.org>
parents: 2246
diff changeset
27 char *defDataType;
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
28
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
29 int (*initContext) (void **pctx);
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
30 int (*closeContext) (void *pctx);
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
31
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
32 int (*writeHeader) (FILE *fh, void *ctx, const char *name);
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
33 int (*writeDecl) (FILE *fh, void *ctx, const size_t len, const char *name);
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
34 int (*writeData) (FILE *fh, void *ctx, const Uint8 *buf, const size_t len);
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
35 int (*writeFooter) (FILE *fh, void *ctx, const size_t len, const char *name);
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
36 } DMOutputFormat;
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
37
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
38
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
39 //
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
40 // Options
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
41 //
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
42 char *optInFilename = NULL,
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
43 *optOutFilename = NULL,
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
44 *optObjName = "default_object",
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
45 *optDataType = NULL,
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
46 *optAddLine = NULL;
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
47
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
48 const DMOutputFormat *setFormat = NULL;
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
49 int optIndentation = -1,
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
50 optLineLen = SET_DEF_LINELEN;
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
51 BOOL optHexMode = FALSE,
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
52 optQuiet = FALSE,
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
53 optExtraData = FALSE,
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
54 optFormatting = TRUE;
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
55
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
56
860
daebbf28953d The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 652
diff changeset
57 static const DMOptArg optList[] =
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
58 {
2408
60e119262c67 Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents: 2406
diff changeset
59 { 0, '?', "help" , "Show this help", OPT_NONE },
2410
bc05bcfc4598 Add a C file with the generic BSD license text and a function for
Matti Hamalainen <ccr@tnsp.org>
parents: 2408
diff changeset
60 { 1, 0, "license" , "Print out this program's license agreement", OPT_NONE },
2408
60e119262c67 Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents: 2406
diff changeset
61
60e119262c67 Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents: 2406
diff changeset
62 { 10, 'n', "name" , "Set object name", OPT_ARGREQ },
60e119262c67 Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents: 2406
diff changeset
63 { 12, 't', "type" , "Set datatype (unsigned char/byte)", OPT_ARGREQ },
60e119262c67 Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents: 2406
diff changeset
64 { 14, 'f', "format" , "Set output format (see list below)", OPT_ARGREQ },
60e119262c67 Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents: 2406
diff changeset
65 { 16, 'a', "add-line" , "Add this line to start of file", OPT_ARGREQ },
60e119262c67 Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents: 2406
diff changeset
66 { 18, 'l', "line-items" , "Set number of items per line", OPT_ARGREQ },
60e119262c67 Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents: 2406
diff changeset
67 { 20, 'x', "hexadecimal" , "Use hexadecimal output", OPT_NONE },
60e119262c67 Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents: 2406
diff changeset
68 { 22, 'q', "quiet" , "Do not add comments", OPT_NONE },
60e119262c67 Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents: 2406
diff changeset
69 { 24, 'N', "no-formatting" , "Disable additional output formatting", OPT_NONE },
60e119262c67 Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents: 2406
diff changeset
70 { 26, 'i', "indentation" , "Set indentation (negative value = tab)", OPT_ARGREQ },
60e119262c67 Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents: 2406
diff changeset
71 { 28, 'e', "extra-data" , "Add object end labels and size in asm output", OPT_NONE },
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
72 };
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
73
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74 static const int optListN = sizeof(optList) / sizeof(optList[0]);
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
75 static const DMOutputFormat dmFormatList[];
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
76 static const int ndmFormatList;
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
77
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
78
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
79 void argShowHelp()
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
80 {
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81 dmPrintBanner(stdout, dmProgName,
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
82 "[options] [sourcefile] [destfile]");
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
83
2402
b7cd5dd0b82e Merge one more change from th-libs args processing.
Matti Hamalainen <ccr@tnsp.org>
parents: 2286
diff changeset
84 dmArgsPrintHelp(stdout, optList, optListN, 0, 80 - 2);
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1107
diff changeset
85
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
86 fprintf(stdout,
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
87 "\n"
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
88 "Available output formats (-f <frmt>):\n"
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
89 " frmt | Description (filename extensions)\n"
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
90 "------+------------------------------------------\n");
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
91
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
92 for (int i = 0; i < ndmFormatList; i++)
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
93 {
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
94 const DMOutputFormat *fmt = &dmFormatList[i];
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
95 fprintf(stdout, "%-5s | %s (",
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
96 fmt->name, fmt->desc);
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
97
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
98 for (int n = 0; n < SET_MAX_FEXTS && fmt->fexts[n] != NULL; n++)
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
99 {
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
100 fprintf(stdout, ".%s%s",
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
101 fmt->fexts[n],
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
102 (n + 1 < SET_MAX_FEXTS &&
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
103 fmt->fexts[n + 1] != NULL) ? " " : "");
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
104 }
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
105 fprintf(stdout, ")\n");
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
106 }
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
107
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
108 fprintf(stdout,
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
109 "\n"
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
110 "To convert a data file to a C structure using 'Uint8' as type:\n"
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
111 "$ data2inc -n variable_name -t Uint8 input.bin output.h\n"
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
112 "\n"
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
113 );
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
114 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
115
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
116
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
117 BOOL argHandleOpt(const int optN, char *optArg, char *currArg)
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
118 {
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
119 switch (optN)
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
120 {
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
121 case 0:
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
122 argShowHelp();
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
123 exit(0);
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
124 break;
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
125
2410
bc05bcfc4598 Add a C file with the generic BSD license text and a function for
Matti Hamalainen <ccr@tnsp.org>
parents: 2408
diff changeset
126 case 1:
bc05bcfc4598 Add a C file with the generic BSD license text and a function for
Matti Hamalainen <ccr@tnsp.org>
parents: 2408
diff changeset
127 dmPrintLicense(stdout);
bc05bcfc4598 Add a C file with the generic BSD license text and a function for
Matti Hamalainen <ccr@tnsp.org>
parents: 2408
diff changeset
128 exit(0);
bc05bcfc4598 Add a C file with the generic BSD license text and a function for
Matti Hamalainen <ccr@tnsp.org>
parents: 2408
diff changeset
129 break;
bc05bcfc4598 Add a C file with the generic BSD license text and a function for
Matti Hamalainen <ccr@tnsp.org>
parents: 2408
diff changeset
130
2408
60e119262c67 Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents: 2406
diff changeset
131 case 10:
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
132 optObjName = optArg;
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
133 break;
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
134
2408
60e119262c67 Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents: 2406
diff changeset
135 case 12:
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
136 optDataType = optArg;
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
137 break;
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
138
2408
60e119262c67 Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents: 2406
diff changeset
139 case 14:
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
140 for (int i = 0; i < ndmFormatList; i++)
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
141 {
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
142 const DMOutputFormat *fmt = &dmFormatList[i];
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
143 if (strcasecmp(fmt->name, optArg) == 0)
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
144 {
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
145 setFormat = fmt;
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
146 return TRUE;
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
147 }
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
148 }
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
149 dmErrorMsg("Invalid format name '%s'.\n",
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
150 optArg);
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
151 return FALSE;
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
152
2408
60e119262c67 Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents: 2406
diff changeset
153 case 16:
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
154 optAddLine = optArg;
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
155 break;
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
156
2408
60e119262c67 Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents: 2406
diff changeset
157 case 18:
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
158 optLineLen = atoi(optArg);
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
159 if (optLineLen < 1)
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
160 {
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
161 dmErrorMsg("Invalid line length / number of items per line '%s'.\n",
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
162 optArg);
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
163 return FALSE;
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
164 }
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
165 break;
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
166
2408
60e119262c67 Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents: 2406
diff changeset
167 case 20:
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
168 optHexMode = TRUE;
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
169 break;
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
170
2408
60e119262c67 Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents: 2406
diff changeset
171 case 22:
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
172 optQuiet = TRUE;
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
173 break;
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
174
2408
60e119262c67 Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents: 2406
diff changeset
175 case 24:
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
176 optFormatting = FALSE;
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
177 break;
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
178
2408
60e119262c67 Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents: 2406
diff changeset
179 case 26:
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
180 optIndentation = atoi(optArg);
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
181 break;
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
182
2408
60e119262c67 Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents: 2406
diff changeset
183 case 28:
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
184 optExtraData = TRUE;
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
185 break;
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
186
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
187 default:
2183
e3f0eaf23f4f Change the error message for unimplemented option argument.
Matti Hamalainen <ccr@tnsp.org>
parents: 1433
diff changeset
188 dmErrorMsg("Unimplemented option argument '%s'.\n", currArg);
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
189 return FALSE;
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
190 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
191
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
192 return TRUE;
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
193 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
194
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
195
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
196 BOOL argHandleFile(char * currArg)
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
197 {
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
198 if (optInFilename == NULL)
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
199 optInFilename = currArg;
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
200 else
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
201 if (optOutFilename == NULL)
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
202 optOutFilename = currArg;
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
203 else
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 866
diff changeset
204 dmErrorMsg("Source and destination filenames already specified, extraneous argument '%s'.\n", currArg);
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
205
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
206 return TRUE;
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
207 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
208
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
209
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
210 static void dmPrintIndentation(FILE *fh)
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
211 {
2427
f07e3e66ca58 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 2421
diff changeset
212 int level;
f07e3e66ca58 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 2421
diff changeset
213 const char *str;
f07e3e66ca58 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 2421
diff changeset
214
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
215 if (optIndentation < 0)
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
216 {
2427
f07e3e66ca58 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 2421
diff changeset
217 level = - optIndentation;
f07e3e66ca58 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 2421
diff changeset
218 str = "\t";
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
219 }
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
220 else
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
221 {
2427
f07e3e66ca58 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 2421
diff changeset
222 level = optIndentation;
f07e3e66ca58 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 2421
diff changeset
223 str = " ";
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
224 }
2427
f07e3e66ca58 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 2421
diff changeset
225
f07e3e66ca58 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 2421
diff changeset
226 for (int i = 0; i < level; i++)
f07e3e66ca58 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 2421
diff changeset
227 fputs(str, fh);
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
228 }
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
229
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
230
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
231 static int dmHandleError(const int res)
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
232 {
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
233 return res >= 0 ? DMERR_OK : dmGetErrno();
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
234 }
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
235
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
236
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
237 /* Assembler include data output functions
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
238 */
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
239 int writeHeader_ASM(FILE *fh, void *ctx, const char *name)
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
240 {
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
241 int res = 0;
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
242 (void) ctx;
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
243
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
244 if (name)
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
245 res = fprintf(fh, "; '%s'", name);
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
246 else
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
247 res = fprintf(fh, "; Generated");
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
248
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
249 if (res >= 0)
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
250 res = fprintf(fh, " by %s v%s\n",
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
251 dmProgName, dmProgVersion);
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
252
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
253 return dmHandleError(res);
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
254 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
255
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
256
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
257 int writeDecl_ASM(FILE *fh, void *ctx, const size_t len, const char *name)
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
258 {
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
259 int res = 0;
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
260 (void) ctx;
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
261
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
262 if (optExtraData)
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
263 res = fprintf(fh, "%s_size = %" DM_PRIu_SIZE_T "\n", name, len);
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
264
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
265 if (res >= 0)
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
266 res = fprintf(fh, "%s:\n", name);
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
267
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
268 return dmHandleError(res);
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
269 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
270
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
271
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
272 int writeData_ASM(FILE *fh, void *ctx, const Uint8 * buf, const size_t len)
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
273 {
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
274 int res;
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
275 (void) ctx;
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
276
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
277 dmPrintIndentation(fh);
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
278 res = fprintf(fh, "%s ", optDataType);
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
279
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
280 for (size_t i = 0; res >= 0 && i < len; i++)
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
281 {
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
282 if (optFormatting)
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
283 {
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
284 if (optHexMode)
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
285 res = fprintf(fh, "$%.2x", buf[i]);
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
286 else
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
287 res = fprintf(fh, "%3d", buf[i]);
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
288 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
289 else
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
290 {
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
291 if (optHexMode)
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
292 res = fprintf(fh, "$%x", buf[i]);
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
293 else
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
294 res = fprintf(fh, "%d", buf[i]);
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
295 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
296
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
297 if (res >= 0 && i + 1 < len)
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
298 res = fprintf(fh, optFormatting ? ", " : ",");
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
299 }
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
300
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
301 if (res >= 0)
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
302 res = fprintf(fh, "\n");
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
303
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
304 return dmHandleError(res);
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
305 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
306
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
307
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
308 int writeFooter_ASM(FILE *fh, void *ctx, const size_t len, const char *name)
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
309 {
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
310 int res;
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
311 (void) len;
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
312 (void) ctx;
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
313
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
314 if (optExtraData)
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
315 res = fprintf(fh, "%s_end: \n", name);
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
316 else
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
317 res = fprintf(fh, "\n");
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
318
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
319 return dmHandleError(res);
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
320 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
321
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
322
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
323 /* ANSI-C include data output functions
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
324 */
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
325 int writeHeader_C(FILE *fh, void *ctx, const char *name)
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
326 {
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
327 int res;
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
328 (void) ctx;
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
329
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
330 if (name)
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
331 res = fprintf(fh, "/* '%s' generated", name);
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
332 else
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
333 res = fprintf(fh, "/* Generated");
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
334
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
335 if (res >= 0)
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
336 res = fprintf(fh, " by %s v%s\n" " */\n",
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
337 dmProgName, dmProgVersion);
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
338
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
339 return dmHandleError(res);
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
340 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
341
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
342
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
343 int writeDecl_C(FILE *fh, void *ctx, const size_t len, const char *name)
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
344 {
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
345 int res;
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
346 (void) ctx;
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
347
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
348 res = fprintf(fh, "%s %s[%" DM_PRIu_SIZE_T "] = {\n",
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
349 optDataType, name, len);
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
350
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
351 if (res >= 0)
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
352 res = printf("extern %s %s[%" DM_PRIu_SIZE_T "];\n",
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
353 optDataType, name, len);
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
354
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
355 return dmHandleError(res);
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
356 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
357
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
358
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
359 int writeData_C(FILE *fh, void *ctx, const Uint8 *buf, const size_t len)
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
360 {
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
361 int res = 0;
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
362 (void) ctx;
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
363
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
364 dmPrintIndentation(fh);
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
365
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
366 for (size_t i = 0; res >= 0 && i < len; i++)
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
367 {
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
368 if (optFormatting)
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
369 {
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
370 if (optHexMode)
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
371 res = fprintf(fh, "0x%.2x", buf[i]);
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
372 else
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
373 res = fprintf(fh, "%3d", buf[i]);
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
374 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
375 else
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
376 {
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
377 if (optHexMode)
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
378 res = fprintf(fh, "0x%x", buf[i]);
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
379 else
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
380 res = fprintf(fh, "%d", buf[i]);
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
381 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
382
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
383 if (res >= 0)
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
384 res = fprintf(fh, optFormatting ? ", " : ",");
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
385 }
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
386
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
387 if (res >= 0)
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
388 res = fprintf(fh, "\n");
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
389
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
390 return dmHandleError(res);
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
391 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
392
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
393
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
394 int writeFooter_C(FILE *fh, void *ctx, const size_t len, const char *name)
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
395 {
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
396 int res;
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
397 (void) len;
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
398 (void) name;
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
399 (void) ctx;
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
400
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
401 res = fprintf(fh, "};\n");
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
402 return dmHandleError(res);
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
403 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
404
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
405
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
406 /*
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
407 * List of formats
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
408 */
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
409 static const DMOutputFormat dmFormatList[] =
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
410 {
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
411 {
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
412 "asm",
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
413 "XA65 compatible assembler",
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
414 { "s", "asm", NULL },
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
415 ".byte",
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
416
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
417 NULL,
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
418 NULL,
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
419
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
420 writeHeader_ASM,
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
421 writeDecl_ASM,
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
422 writeData_ASM,
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
423 writeFooter_ASM,
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
424 },
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
425
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
426 {
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
427 "c",
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
428 "ANSI C array",
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
429 { "c", "h", "cc", "cpp", "hpp", "c++", NULL },
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
430 "unsigned char",
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
431
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
432 NULL,
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
433 NULL,
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
434
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
435 writeHeader_C,
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
436 writeDecl_C,
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
437 writeData_C,
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
438 writeFooter_C,
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
439 },
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
440 };
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
441
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
442 static const int ndmFormatList = sizeof(dmFormatList) / sizeof(dmFormatList[0]);
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
443
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
444
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
445 off_t dmGetFileSize(FILE *fh)
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
446 {
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
447 off_t len, pos = ftello(fh);
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
448 fseeko(fh, 0, SEEK_END);
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
449 len = ftello(fh);
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
450 fseeko(fh, pos, SEEK_SET);
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
451 return len;
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
452 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
453
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
454
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
455 const DMOutputFormat *dmGuessFormatFromName(const char *filename)
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
456 {
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
457 const char *fext;
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
458
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
459 if ((fext = strrchr(filename, '.')) == NULL)
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
460 return NULL;
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
461
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
462 for (int i = 0; i < ndmFormatList; i++)
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
463 {
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
464 const DMOutputFormat *fmt = &dmFormatList[i];
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
465
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
466 for (int n = 0; n < SET_MAX_FEXTS && fmt->fexts[n] != NULL; n++)
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
467 {
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
468 if (strcasecmp(fext + 1, fmt->fexts[n]) == 0)
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
469 return fmt;
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
470 }
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
471 }
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
472
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
473 return NULL;
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
474 }
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
475
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
476
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
477 int main(int argc, char *argv[])
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
478 {
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
479 FILE *inFile = NULL, *outFile = NULL;
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
480 Uint8 *dataBuf = NULL;
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
481 void *ctx = NULL;
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
482 size_t dataSize;
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
483 off_t totalSize;
2565
d56a0e86067a Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2539
diff changeset
484 int res = DMERR_OK;
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
485
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
486 // Initialize
2408
60e119262c67 Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents: 2406
diff changeset
487 dmInitProg("data2inc", "Data to include file converter", "0.7", NULL, NULL);
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
488 dmVerbosity = 0;
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
489
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
490 // Parse arguments
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
491 if (!dmArgsProcess(argc, argv, optList, optListN,
860
daebbf28953d The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 652
diff changeset
492 argHandleOpt, argHandleFile, OPTH_BAILOUT))
2565
d56a0e86067a Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2539
diff changeset
493 goto out;
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
494
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
495 // Determine output type, if not specified
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
496 if (setFormat == NULL)
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
497 {
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
498 if (optOutFilename == NULL)
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
499 {
2565
d56a0e86067a Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2539
diff changeset
500 argShowHelp();
d56a0e86067a Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2539
diff changeset
501 res = dmError(DMERR_INVALID_ARGS,
d56a0e86067a Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2539
diff changeset
502 "Output format not specified and no output filename given.\n");
d56a0e86067a Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2539
diff changeset
503 goto out;
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
504 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
505
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
506 if ((setFormat = dmGuessFormatFromName(optOutFilename)) == NULL)
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
507 {
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
508 dmErrorMsg("Could not guess output format from filename '%s'.\n",
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
509 optOutFilename);
2565
d56a0e86067a Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2539
diff changeset
510 goto out;
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
511 }
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
512
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
513 dmMsg(0, "Guessed output format: %s (%s)\n",
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
514 setFormat->desc, setFormat->name);
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
515 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
516
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
517 // Set some option defaults
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
518 if (optDataType == NULL)
2250
956db0095f82 Change struct member name.
Matti Hamalainen <ccr@tnsp.org>
parents: 2246
diff changeset
519 optDataType = setFormat->defDataType;
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
520
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
521 // Open the files
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
522 if (optInFilename == NULL)
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
523 inFile = stdin;
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
524 else
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
525 if ((inFile = fopen(optInFilename, "rb")) == NULL)
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
526 {
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
527 res = dmGetErrno();
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 866
diff changeset
528 dmErrorMsg("Error opening input file '%s'. (%s)\n",
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
529 optInFilename, dmErrorStr(res));
2565
d56a0e86067a Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2539
diff changeset
530 goto out;
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
531 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
532
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
533 if (optOutFilename == NULL)
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
534 outFile = stdout;
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
535 else
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
536 if ((outFile = fopen(optOutFilename, "wa")) == NULL)
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
537 {
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
538 res = dmGetErrno();
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 866
diff changeset
539 dmErrorMsg("Error creating output file '%s'. (%s)\n",
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
540 optOutFilename, dmErrorStr(res));
2565
d56a0e86067a Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2539
diff changeset
541 goto out;
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
542 }
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
543
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
544 // Allocate linebuffer
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
545 dataSize = optLineLen * sizeof(Uint8);
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
546 if ((dataBuf = dmMalloc(dataSize)) == NULL)
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
547 {
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
548 dmErrorMsg("Could not allocate %" DM_PRIu_SIZE_T " byte buffer.\n",
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
549 dataSize);
2565
d56a0e86067a Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2539
diff changeset
550 goto out;
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
551 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
552
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
553 // Get sourcefile size
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
554 totalSize = dmGetFileSize(inFile);
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
555
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
556 // Call context init
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
557 if (setFormat->initContext != NULL &&
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
558 (res = setFormat->initContext(&ctx)) != DMERR_OK)
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
559 {
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
560 dmErrorMsg("Error initializing format %s (%s) context: %s\n",
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
561 setFormat->name, setFormat->desc,
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
562 dmErrorStr(res));
2565
d56a0e86067a Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2539
diff changeset
563 goto out;
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
564 }
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
565
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
566 // Output header
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
567 if (!optQuiet &&
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
568 (res = setFormat->writeHeader(outFile, ctx, optOutFilename)) != DMERR_OK)
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
569 {
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
570 dmErrorMsg("Error writing output header: %s\n",
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
571 dmErrorStr(res));
2565
d56a0e86067a Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2539
diff changeset
572 goto out;
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
573 }
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
574
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
575 if (optAddLine)
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
576 fprintf(outFile, "%s\n", optAddLine);
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
577
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
578 // Output declaration
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
579 if (setFormat->writeDecl != NULL &&
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
580 (res = setFormat->writeDecl(outFile, ctx, totalSize, optObjName)) != DMERR_OK)
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
581 {
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
582 dmErrorMsg("Error writing output declaration: %s\n",
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
583 dmErrorStr(res));
2565
d56a0e86067a Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2539
diff changeset
584 goto out;
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
585 }
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
586
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
587 // Output data
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
588 while (!feof(inFile))
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
589 {
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
590 res = fread(dataBuf, 1, dataSize, inFile);
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
591 if (res > 0 &&
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
592 (res = setFormat->writeData(outFile, ctx, dataBuf, res)) != DMERR_OK)
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
593 {
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
594 dmErrorMsg("Error writing output data: %s\n",
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
595 dmErrorStr(res));
2565
d56a0e86067a Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2539
diff changeset
596 goto out;
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
597 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
598 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
599
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
600 // Output footer
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
601 if (setFormat->writeFooter != NULL &&
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
602 (res = setFormat->writeFooter(outFile, ctx, totalSize, optObjName)) != DMERR_OK)
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
603 {
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
604 dmErrorMsg("Error writing output footer: %s\n",
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
605 dmErrorStr(res));
2565
d56a0e86067a Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2539
diff changeset
606 goto out;
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
607 }
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
608
2565
d56a0e86067a Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2539
diff changeset
609 out:
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
610 // Cleanup
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
611 if (inFile != NULL)
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
612 fclose(inFile);
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
613
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
614 if (outFile != NULL)
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
615 fclose(outFile);
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
616
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
617 dmFree(dataBuf);
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
618
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
619 if (setFormat != NULL &&
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
620 setFormat->closeContext != NULL &&
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
621 (res = setFormat->closeContext(ctx)) != DMERR_OK)
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
622 {
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
623 dmErrorMsg("Error closing format %s (%s) context: %s\n",
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
624 setFormat->name, setFormat->desc,
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
625 dmErrorStr(res));
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
626 }
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
627
2565
d56a0e86067a Improve error handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 2539
diff changeset
628 return res;
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
629 }