annotate tools/data2inc.c @ 2298:b5abfff07ca9

Add new DMGrowBuf helper functions dmGrowBufCopyOffsSize() and dmGrowBufConstCopyOffsSize().
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 04 Jul 2019 10:54:16 +0300
parents e771185db600
children b7cd5dd0b82e
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
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
4 * (C) Copyright 2003,2009-2019 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
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
29 void (*writeHeader) (FILE *fh, const char *name);
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
30 void (*writeDecl) (FILE *fh, const size_t len, const char *name);
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
31 void (*writeData) (FILE *fh, const Uint8 *buf, const size_t len);
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
32 void (*writeFooter) (FILE *fh, const size_t len, const char *name);
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
33 } DMOutputFormat;
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
34
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
35
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
36 //
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
37 // Options
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
38 //
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
39 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
40 *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
41 *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
42 *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
43 *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
44
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
45 const DMOutputFormat *setFormat = NULL;
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
46 int optIndentation = -1,
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
47 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
48 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
49 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
50 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
51 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
52
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
53
860
daebbf28953d The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 652
diff changeset
54 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
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 { 0, '?', "help", "Show this help", OPT_NONE },
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
57 { 1, 'n', "name", "Set object name", OPT_ARGREQ },
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
58 { 2, 't', "type", "Set datatype (unsigned char/byte)", OPT_ARGREQ },
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
59 { 3, 'f', "format", "Set output format (see list below)", OPT_ARGREQ },
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
60 { 4, 'a', "add-line", "Add this line to start of file", OPT_ARGREQ },
2251
d736ff74663b Oops, we had two '-n' short options. Change the line items one to '-l'.
Matti Hamalainen <ccr@tnsp.org>
parents: 2250
diff changeset
61 { 5, 'l', "line-items", "Set number of items per line", OPT_ARGREQ },
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
62 { 6, 'x', "hexadecimal", "Use hexadecimal output", OPT_NONE },
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
63 { 7, 'q', "quiet", "Do not add comments", OPT_NONE },
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
64 { 8, 'N', "no-formatting", "Disable additional output formatting", 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
65 { 9, 'i', "indentation", "Set indentation (negative value = tab)", OPT_ARGREQ },
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
66 { 10, 'e', "extra-data", "Add object end labels and size in asm output", OPT_NONE },
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
67 };
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
68
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
69 static const int optListN = sizeof(optList) / sizeof(optList[0]);
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
70 static const DMOutputFormat dmFormatList[];
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
71 static const int ndmFormatList;
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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 void argShowHelp()
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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 dmPrintBanner(stdout, dmProgName,
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
77 "[options] [sourcefile] [destfile]");
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
78
860
daebbf28953d The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 652
diff changeset
79 dmArgsPrintHelp(stdout, optList, optListN, 0);
1235
5b8245e5f785 Remove trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 1107
diff changeset
80
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
81 fprintf(stdout,
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
82 "\n"
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
83 "Available output formats (-f <frmt>):\n"
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
84 " frmt | Description (filename extensions)\n"
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
85 "------+------------------------------------------\n");
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
86
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
87 for (int i = 0; i < ndmFormatList; i++)
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
88 {
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
89 const DMOutputFormat *fmt = &dmFormatList[i];
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
90 fprintf(stdout, "%-5s | %s (",
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
91 fmt->name, fmt->desc);
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
92
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
93 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
94 {
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
95 fprintf(stdout, ".%s%s",
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
96 fmt->fexts[n],
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
97 (n + 1 < SET_MAX_FEXTS &&
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
98 fmt->fexts[n + 1] != NULL) ? " " : "");
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
99 }
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
100 fprintf(stdout, ")\n");
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
101 }
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
102
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
103 fprintf(stdout,
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
104 "\n"
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
105 "To convert a data file to a C structure using 'Uint8' as type:\n"
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
106 "$ data2inc -n variable_name -t Uint8 input.bin output.h\n"
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
107 "\n"
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
108 );
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
109 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
110
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
111
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
112 BOOL argHandleOpt(const int optN, char *optArg, char *currArg)
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
113 {
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
114 switch (optN)
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
115 {
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
116 case 0:
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
117 argShowHelp();
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
118 exit(0);
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
119 break;
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
120
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
121 case 1:
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
122 optObjName = optArg;
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
123 break;
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
124
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
125 case 2:
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
126 optDataType = optArg;
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
127 break;
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
128
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
129 case 3:
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
130 for (int i = 0; i < ndmFormatList; i++)
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
131 {
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
132 const DMOutputFormat *fmt = &dmFormatList[i];
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
133 if (strcasecmp(fmt->name, optArg) == 0)
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
134 {
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
135 setFormat = fmt;
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
136 return TRUE;
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
137 }
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
138 }
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
139 dmErrorMsg("Invalid format name '%s'.\n",
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
140 optArg);
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
141 return FALSE;
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
142
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
143 case 4:
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
144 optAddLine = optArg;
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
145 break;
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
146
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
147 case 5:
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
148 optLineLen = atoi(optArg);
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
149 if (optLineLen < 1)
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
150 {
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
151 dmErrorMsg("Invalid line length / number of items per line '%s'.\n",
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
152 optArg);
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
153 return FALSE;
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
154 }
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
155 break;
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
156
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
157
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 case 6:
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
159 optHexMode = TRUE;
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
160 break;
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
161
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
162 case 7:
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
163 optQuiet = TRUE;
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
164 break;
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
165
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
166 case 8:
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
167 optFormatting = FALSE;
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
168 break;
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
169
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
170 case 9:
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
171 optIndentation = atoi(optArg);
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
172 break;
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
173
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
174 case 10:
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
175 optExtraData = TRUE;
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
176 break;
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
177
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
178 default:
2183
e3f0eaf23f4f Change the error message for unimplemented option argument.
Matti Hamalainen <ccr@tnsp.org>
parents: 1433
diff changeset
179 dmErrorMsg("Unimplemented option argument '%s'.\n", currArg);
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
180 return FALSE;
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
181 }
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 return TRUE;
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
184 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
185
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
186
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
187 BOOL argHandleFile(char * currArg)
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
188 {
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
189 if (optInFilename == NULL)
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
190 optInFilename = currArg;
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
191 else
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
192 if (optOutFilename == NULL)
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
193 optOutFilename = currArg;
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
194 else
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 866
diff changeset
195 dmErrorMsg("Source and destination filenames already specified, extraneous argument '%s'.\n", currArg);
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
196
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
197 return TRUE;
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
201 /* 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
202 */
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
203 void writeHeader_ASM(FILE *fh, 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
204 {
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
205 if (name)
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
206 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
207 else
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
208 fprintf(fh, "; Generated");
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
209
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
210 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
211 dmProgName, dmProgVersion);
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
212 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
213
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
214
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
215 void writeDecl_ASM(FILE *fh, 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
216 {
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
217 if (optExtraData)
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
218 fprintf(fh, "%s_size = %" DM_PRIu_SIZE_T "\n", name, len);
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
219
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
220 fprintf(fh, "%s:\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
221 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
222
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
223
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
224 void writeData_ASM(FILE *fh, 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
225 {
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
226 fprintf(fh, "%s ", optDataType);
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
227
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
228 for (size_t i = 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
229 {
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
230 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
231 {
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
232 if (optHexMode)
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
233 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
234 else
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
235 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
236 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
237 else
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
238 {
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
239 if (optHexMode)
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
240 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
241 else
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
242 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
243 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
244
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
245 if (i + 1 < len)
2286
e771185db600 Add extra formatting to C and ASM outputs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2251
diff changeset
246 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
247 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
248 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
249
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
250
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
251 void writeFooter_ASM(FILE *fh, 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
252 {
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
253 (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
254 if (optExtraData)
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
255 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
256 else
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
257 fprintf(fh, "\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
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
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
260
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
261 /* 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
262 */
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
263 void writeHeader_C(FILE *fh, 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
264 {
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
265 if (name)
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
266 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
267 else
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
268 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
269
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
270 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
271 dmProgName, dmProgVersion);
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
272 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
273
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
274
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
275 void writeDecl_C(FILE *fh, 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
276 {
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
277 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
278 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
279
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
280 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
281 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
282 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
283
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
284
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
285 void writeData_C(FILE *fh, 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
286 {
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
287 for (size_t i = 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
288 {
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
289 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
290 {
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
291 if (optHexMode)
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
292 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
293 else
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
294 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
295 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
296 else
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
297 {
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
298 if (optHexMode)
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
299 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
300 else
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
301 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
302 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
303
2286
e771185db600 Add extra formatting to C and ASM outputs.
Matti Hamalainen <ccr@tnsp.org>
parents: 2251
diff changeset
304 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
305 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
306 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
307
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
308
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
309 void writeFooter_C(FILE *fh, 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
310 {
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
311 (void) len;
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
312 (void) name;
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
313
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
314 fprintf(fh, "};\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
315 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
316
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
317
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
318 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
319 {
2246
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 "asm",
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
322 "XA65 compatible assembler",
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
323 { "s", "asm", NULL },
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
324 ".byte",
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
325
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
326 writeHeader_ASM,
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
327 writeDecl_ASM,
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
328 writeData_ASM,
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
329 writeFooter_ASM,
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
330 },
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
331
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
332 {
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
333 "c",
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
334 "ANSI C array",
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
335 { "c", "h", "cc", "cpp", "hpp", "c++", NULL },
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
336
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
337 "unsigned char",
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
338
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
339 writeHeader_C,
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
340 writeDecl_C,
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
341 writeData_C,
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
342 writeFooter_C,
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
343 },
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
344 };
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
345
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
346 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
347
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
348
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
349 off_t dmGetFileSize(FILE *fh)
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
350 {
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
351 off_t len, pos = ftello(fh);
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
352 fseeko(fh, 0, SEEK_END);
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
353 len = ftello(fh);
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
354 fseeko(fh, pos, SEEK_SET);
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
355 return len;
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
356 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
357
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
358
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
359 const DMOutputFormat *dmGuessFormatFromName(const char *filename)
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
360 {
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
361 const char *fext;
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
362
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
363 if ((fext = strrchr(filename, '.')) == NULL)
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
364 return NULL;
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
365
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
366 for (int i = 0; i < ndmFormatList; i++)
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
367 {
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
368 const DMOutputFormat *fmt = &dmFormatList[i];
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
369
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
370 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
371 {
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
372 if (strcasecmp(fext + 1, fmt->fexts[n]) == 0)
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
373 return fmt;
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
374 }
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
375 }
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
376
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
377 return NULL;
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
378 }
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
379
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
380
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
381 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
382 {
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
383 FILE *inFile = NULL, *outFile = NULL;
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
384 Uint8 *dataBuf = NULL;
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
385 size_t dataSize;
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
386 off_t totalSize;
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
387 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
388
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
389 // Initialize
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
390 dmInitProg("data2inc", "Data to include 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
391 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
392
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
393 // 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
394 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
395 argHandleOpt, argHandleFile, OPTH_BAILOUT))
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
396 exit(1);
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
397
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
398 // Determine output type, if not specified
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
399 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
400 {
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
401 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
402 {
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 866
diff changeset
403 dmErrorMsg("Output format not specified and no output filename given (try --help)\n");
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
404 goto exit;
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
405 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
406
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
407 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
408 {
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
409 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
410 optOutFilename);
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
411 goto exit;
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
412 }
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
413
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
414 dmMsg(0, "Guessed output format: %s (%s)\n",
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
415 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
416 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
417
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
418 // Set some option defaults
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
419 if (optDataType == NULL)
2250
956db0095f82 Change struct member name.
Matti Hamalainen <ccr@tnsp.org>
parents: 2246
diff changeset
420 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
421
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
422 // 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
423 if (optInFilename == NULL)
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
424 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
425 else
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
426 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
427 {
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
428 res = dmGetErrno();
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 866
diff changeset
429 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
430 optInFilename, dmErrorStr(res));
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
431 goto exit;
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
432 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
433
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
434 if (optOutFilename == NULL)
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
435 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
436 else
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
437 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
438 {
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
439 res = dmGetErrno();
958
985225a93aeb Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents: 866
diff changeset
440 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
441 optOutFilename, dmErrorStr(res));
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
442 goto exit;
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
443 }
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
444
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
445 // Allocate linebuffer
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
446 dataSize = optLineLen * sizeof(Uint8);
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
447 if ((dataBuf = dmMalloc(dataSize)) == NULL)
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
448 {
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
449 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
450 dataSize);
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
451 goto exit;
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
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
454 // Get sourcefile size
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
455 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
456
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
457 // Output header
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
458 if (!optQuiet)
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
459 setFormat->writeHeader(outFile, optOutFilename);
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
460
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
461 if (optAddLine)
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
462 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
463
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
464 // Output declaration
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
465 setFormat->writeDecl(outFile, totalSize, optObjName);
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
466
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
467 // Output data
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
468 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
469 {
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
470 res = fread(dataBuf, 1, dataSize, inFile);
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
471 if (res > 0)
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
472 {
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
473 if (optIndentation < 0)
2246
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 for (int i = 0; i < -optIndentation; i++)
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
476 fputs("\t", outFile);
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
477 }
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
478 else
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
479 if (optIndentation > 0)
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
480 {
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
481 for (int i = 0; i < optIndentation; i++)
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
482 fputs(" ", outFile);
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
483 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
484
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
485 setFormat->writeData(outFile, dataBuf, 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
486
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
487 fprintf(outFile, "\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
488 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
489 }
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
490
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
491 // Output footer
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
492 setFormat->writeFooter(outFile, totalSize, optObjName);
407
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
493
2246
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
494 exit:
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
495 // Cleanup
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
496 if (inFile != NULL)
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
497 fclose(inFile);
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
498
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
499 if (outFile != NULL)
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
500 fclose(outFile);
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
501
d76b0c92769d Refactor data2inc to be more flexible.
Matti Hamalainen <ccr@tnsp.org>
parents: 2183
diff changeset
502 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
503
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
504 return 0;
59244a7ae37f Move c64 utilities to the engine lib, as we benefit from a common framework.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
505 }