annotate tools/data2inc.c @ 2574:3feca4682680

Move functions around to get rid of the unknown-size dmFormatList array forward declaration (without the need to give it a size), so that we compile cleanly with -pedantic.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 11 Mar 2022 14:48:56 +0200
parents d75431bf1a7d
children 9807ae37ad69
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
2566
d75431bf1a7d Bump copyright year.
Matti Hamalainen <ccr@tnsp.org>
parents: 2565
diff changeset
4 * (C) Copyright 2003,2009-2022 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]);
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
75
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
76
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
77 static void dmPrintIndentation(FILE *fh)
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
78 {
2427
f07e3e66ca58 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 2421
diff changeset
79 int level;
f07e3e66ca58 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 2421
diff changeset
80 const char *str;
f07e3e66ca58 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 2421
diff changeset
81
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
82 if (optIndentation < 0)
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
83 {
2427
f07e3e66ca58 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 2421
diff changeset
84 level = - optIndentation;
f07e3e66ca58 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 2421
diff changeset
85 str = "\t";
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
86 }
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
87 else
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
88 {
2427
f07e3e66ca58 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 2421
diff changeset
89 level = optIndentation;
f07e3e66ca58 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 2421
diff changeset
90 str = " ";
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
91 }
2427
f07e3e66ca58 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 2421
diff changeset
92
f07e3e66ca58 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 2421
diff changeset
93 for (int i = 0; i < level; i++)
f07e3e66ca58 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 2421
diff changeset
94 fputs(str, fh);
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
95 }
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
96
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
97
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
98 static int dmHandleError(const int res)
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
99 {
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
100 return res >= 0 ? DMERR_OK : dmGetErrno();
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
101 }
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
102
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
103
2574
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
104 off_t dmGetFileSize(FILE *fh)
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
105 {
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
106 off_t len, pos = ftello(fh);
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
107 fseeko(fh, 0, SEEK_END);
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
108 len = ftello(fh);
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
109 fseeko(fh, pos, SEEK_SET);
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
110 return len;
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
111 }
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
112
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
113
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
114 /* 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
115 */
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
116 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
117 {
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
118 int res = 0;
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
119 (void) ctx;
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
120
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
121 if (name)
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
122 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
123 else
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
124 res = fprintf(fh, "; Generated");
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
125
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
126 if (res >= 0)
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
127 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
128 dmProgName, dmProgVersion);
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
129
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
130 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
131 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
132
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
133
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
134 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
135 {
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
136 int res = 0;
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
137 (void) ctx;
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
138
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
139 if (optExtraData)
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
140 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
141
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
142 if (res >= 0)
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
143 res = fprintf(fh, "%s:\n", name);
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
144
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
145 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
146 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
147
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
148
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
149 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
150 {
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
151 int res;
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
152 (void) ctx;
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
153
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
154 dmPrintIndentation(fh);
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
155 res = fprintf(fh, "%s ", optDataType);
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
156
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
157 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
158 {
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
159 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
160 {
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
161 if (optHexMode)
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
162 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
163 else
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
164 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
165 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
166 else
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
167 {
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
168 if (optHexMode)
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
169 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
170 else
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
171 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
172 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
173
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
174 if (res >= 0 && i + 1 < len)
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
175 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
176 }
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
177
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
178 if (res >= 0)
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
179 res = fprintf(fh, "\n");
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
180
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
181 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
182 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
183
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
184
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
185 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
186 {
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
187 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
188 (void) len;
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
189 (void) ctx;
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
190
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
191 if (optExtraData)
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
192 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
193 else
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
194 res = fprintf(fh, "\n");
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
195
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
196 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
197 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
198
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
199
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
200 /* 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
201 */
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
202 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
203 {
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
204 int res;
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
205 (void) ctx;
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
206
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
207 if (name)
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
208 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
209 else
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
210 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
211
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
212 if (res >= 0)
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
213 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
214 dmProgName, dmProgVersion);
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
215
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
216 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
217 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
218
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
219
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
220 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
221 {
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
222 int res;
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
223 (void) ctx;
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
224
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
225 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
226 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
227
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
228 if (res >= 0)
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
229 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
230 optDataType, name, len);
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
231
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
232 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
233 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
234
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
235
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
236 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
237 {
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
238 int res = 0;
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
239 (void) ctx;
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
240
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
241 dmPrintIndentation(fh);
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
242
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
243 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
244 {
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
245 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
246 {
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
247 if (optHexMode)
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
248 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
249 else
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
250 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
251 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
252 else
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
253 {
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
254 if (optHexMode)
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
255 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
256 else
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
257 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
258 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
259
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
260 if (res >= 0)
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
261 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
262 }
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
263
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
264 if (res >= 0)
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
265 res = fprintf(fh, "\n");
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
266
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
267 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
268 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
269
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
270
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
271 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
272 {
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
273 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
274 (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
275 (void) name;
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
276 (void) ctx;
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
277
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
278 res = fprintf(fh, "};\n");
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
279 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
280 }
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
2405
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
283 /*
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
284 * List of formats
1f74cc842d71 Add some error checking to data2inc.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
285 */
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
286 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
287 {
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
288 {
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
289 "asm",
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
290 "XA65 compatible assembler",
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
291 { "s", "asm", NULL },
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
292 ".byte",
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
293
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
294 NULL,
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
295 NULL,
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
296
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
297 writeHeader_ASM,
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
298 writeDecl_ASM,
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
299 writeData_ASM,
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
300 writeFooter_ASM,
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
301 },
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
302
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
303 {
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
304 "c",
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
305 "ANSI C array",
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
306 { "c", "h", "cc", "cpp", "hpp", "c++", NULL },
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
307 "unsigned char",
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
308
2421
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
309 NULL,
1361e3883dd8 Make things more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
310 NULL,
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
311
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
312 writeHeader_C,
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
313 writeDecl_C,
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
314 writeData_C,
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
315 writeFooter_C,
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
316 },
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
317 };
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
318
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
319 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
320
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
321
2574
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
322
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
323
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
324 void argShowHelp()
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
325 {
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
326 dmPrintBanner(stdout, dmProgName,
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
327 "[options] [sourcefile] [destfile]");
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
328
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
329 dmArgsPrintHelp(stdout, optList, optListN, 0, 80 - 2);
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
330
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
331 fprintf(stdout,
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
332 "\n"
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
333 "Available output formats (-f <frmt>):\n"
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
334 " frmt | Description (filename extensions)\n"
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
335 "------+------------------------------------------\n");
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
336
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
337 for (int i = 0; i < ndmFormatList; i++)
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
338 {
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
339 const DMOutputFormat *fmt = &dmFormatList[i];
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
340 fprintf(stdout, "%-5s | %s (",
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
341 fmt->name, fmt->desc);
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
342
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
343 for (int n = 0; n < SET_MAX_FEXTS && fmt->fexts[n] != NULL; n++)
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
344 {
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
345 fprintf(stdout, ".%s%s",
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
346 fmt->fexts[n],
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
347 (n + 1 < SET_MAX_FEXTS &&
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
348 fmt->fexts[n + 1] != NULL) ? " " : "");
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
349 }
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
350 fprintf(stdout, ")\n");
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
351 }
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
352
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
353 fprintf(stdout,
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
354 "\n"
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
355 "To convert a data file to a C structure using 'Uint8' as type:\n"
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
356 "$ data2inc -n variable_name -t Uint8 input.bin output.h\n"
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
357 "\n"
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
358 );
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
359 }
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
360
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
361
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
362 BOOL argHandleOpt(const int optN, char *optArg, char *currArg)
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
363 {
2574
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
364 switch (optN)
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
365 {
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
366 case 0:
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
367 argShowHelp();
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
368 exit(0);
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
369 break;
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
370
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
371 case 1:
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
372 dmPrintLicense(stdout);
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
373 exit(0);
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
374 break;
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
375
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
376 case 10:
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
377 optObjName = optArg;
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
378 break;
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
379
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
380 case 12:
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
381 optDataType = optArg;
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
382 break;
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
383
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
384 case 14:
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
385 for (int i = 0; i < ndmFormatList; i++)
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
386 {
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
387 const DMOutputFormat *fmt = &dmFormatList[i];
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
388 if (strcasecmp(fmt->name, optArg) == 0)
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
389 {
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
390 setFormat = fmt;
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
391 return TRUE;
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
392 }
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
393 }
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
394 dmErrorMsg("Invalid format name '%s'.\n",
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
395 optArg);
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
396 return FALSE;
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
397
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
398 case 16:
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
399 optAddLine = optArg;
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
400 break;
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
401
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
402 case 18:
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
403 optLineLen = atoi(optArg);
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
404 if (optLineLen < 1)
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
405 {
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
406 dmErrorMsg("Invalid line length / number of items per line '%s'.\n",
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
407 optArg);
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
408 return FALSE;
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
409 }
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
410 break;
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
411
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
412 case 20:
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
413 optHexMode = TRUE;
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
414 break;
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
415
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
416 case 22:
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
417 optQuiet = TRUE;
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
418 break;
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
419
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
420 case 24:
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
421 optFormatting = FALSE;
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
422 break;
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
423
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
424 case 26:
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
425 optIndentation = atoi(optArg);
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
426 break;
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
427
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
428 case 28:
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
429 optExtraData = TRUE;
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
430 break;
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
431
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
432 default:
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
433 dmErrorMsg("Unimplemented option argument '%s'.\n", currArg);
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
434 return FALSE;
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
435 }
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
436
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
437 return TRUE;
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
438 }
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
439
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
440
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
441 BOOL argHandleFile(char * currArg)
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
442 {
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
443 if (optInFilename == NULL)
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
444 optInFilename = currArg;
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
445 else
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
446 if (optOutFilename == NULL)
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
447 optOutFilename = currArg;
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
448 else
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
449 dmErrorMsg("Source and destination filenames already specified, extraneous argument '%s'.\n", currArg);
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
450
3feca4682680 Move functions around to get rid of the unknown-size dmFormatList array
Matti Hamalainen <ccr@tnsp.org>
parents: 2566
diff changeset
451 return TRUE;
407
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 }