1 | /*****************************************************************************/ |
2 | /* */ |
3 | /* output.c */ |
4 | /* */ |
5 | /* Output format/file definitions for the sp65 sprite and bitmap utility */ |
6 | /* */ |
7 | /* */ |
8 | /* */ |
9 | /* (C) 2012, Ullrich von Bassewitz */ |
10 | /* Roemerstrasse 52 */ |
11 | /* D-70794 Filderstadt */ |
12 | /* EMail: uz@cc65.org */ |
13 | /* */ |
14 | /* */ |
15 | /* This software is provided 'as-is', without any expressed or implied */ |
16 | /* warranty. In no event will the authors be held liable for any damages */ |
17 | /* arising from the use of this software. */ |
18 | /* */ |
19 | /* Permission is granted to anyone to use this software for any purpose, */ |
20 | /* including commercial applications, and to alter it and redistribute it */ |
21 | /* freely, subject to the following restrictions: */ |
22 | /* */ |
23 | /* 1. The origin of this software must not be misrepresented; you must not */ |
24 | /* claim that you wrote the original software. If you use this software */ |
25 | /* in a product, an acknowledgment in the product documentation would be */ |
26 | /* appreciated but is not required. */ |
27 | /* 2. Altered source versions must be plainly marked as such, and must not */ |
28 | /* be misrepresented as being the original software. */ |
29 | /* 3. This notice may not be removed or altered from any source */ |
30 | /* distribution. */ |
31 | /* */ |
32 | /*****************************************************************************/ |
33 | |
34 | |
35 | |
36 | #include <stdlib.h> |
37 | |
38 | /* common */ |
39 | #include "fileid.h" |
40 | |
41 | /* sp65 */ |
42 | #include "asm.h" |
43 | #include "attr.h" |
44 | #include "bin.h" |
45 | #include "c.h" |
46 | #include "error.h" |
47 | #include "output.h" |
48 | |
49 | |
50 | |
51 | /*****************************************************************************/ |
52 | /* Data */ |
53 | /*****************************************************************************/ |
54 | |
55 | |
56 | |
57 | /* Different types of output formats */ |
58 | enum OutputFormat { |
59 | ofAsm, /* Output assembler source */ |
60 | ofBin, /* Output raw binary format */ |
61 | ofC, /* Output C code */ |
62 | |
63 | ofCount /* Number of output formats without ofAuto */ |
64 | }; |
65 | |
66 | typedef struct OutputFormatDesc OutputFormatDesc; |
67 | struct OutputFormatDesc { |
68 | |
69 | /* Write routine */ |
70 | void (*Write) (const StrBuf*, const Collection*, const Bitmap*); |
71 | |
72 | }; |
73 | |
74 | /* Table with Output formats */ |
75 | static OutputFormatDesc OutputFormatTable[ofCount] = { |
76 | { WriteAsmFile }, |
77 | { WriteBinFile }, |
78 | { WriteCFile }, |
79 | }; |
80 | |
81 | /* Table that maps extensions to Output formats. Must be sorted alphabetically */ |
82 | static const FileId FormatTable[] = { |
83 | /* Upper case stuff for obsolete operating systems */ |
84 | { "A" , ofAsm }, |
85 | { "ASM" , ofAsm }, |
86 | { "BIN" , ofBin }, |
87 | { "C" , ofC }, |
88 | { "INC" , ofAsm }, |
89 | { "S" , ofAsm }, |
90 | |
91 | { "a" , ofAsm }, |
92 | { "asm" , ofAsm }, |
93 | { "bin" , ofBin }, |
94 | { "c" , ofC }, |
95 | { "inc" , ofAsm }, |
96 | { "s" , ofAsm }, |
97 | }; |
98 | |
99 | |
100 | |
101 | /*****************************************************************************/ |
102 | /* Code */ |
103 | /*****************************************************************************/ |
104 | |
105 | |
106 | |
107 | void WriteOutputFile (const StrBuf* Data, const Collection* A, const Bitmap* B) |
108 | /* Write the contents of Data to a file. Format, file name etc. must be given |
109 | ** as attributes in A. If no format is given, the function tries to autodetect |
110 | ** it by using the extension of the file name. The bitmap passed to the |
111 | ** function is the bitmap used as source of the conversion. It may be used to |
112 | ** determine the bitmap properties for documentation purposes. |
113 | */ |
114 | { |
115 | const FileId* F; |
116 | |
117 | /* Get the file format from the command line */ |
118 | const char* Format = GetAttrVal (A, "format" ); |
119 | if (Format != 0) { |
120 | /* Format is given, search for it in the table. */ |
121 | F = bsearch (Format, |
122 | FormatTable, |
123 | sizeof (FormatTable) / sizeof (FormatTable[0]), |
124 | sizeof (FormatTable[0]), |
125 | CompareFileId); |
126 | if (F == 0) { |
127 | Error ("Unknown output format '%s'" , Format); |
128 | } |
129 | } else { |
130 | /* No format given, use file name extension */ |
131 | const char* Name = NeedAttrVal (A, "name" , "write" ); |
132 | F = GetFileId (Name, FormatTable, |
133 | sizeof (FormatTable) / sizeof (FormatTable[0])); |
134 | /* Found? */ |
135 | if (F == 0) { |
136 | Error ("Cannot determine file format of output file '%s'" , Name); |
137 | } |
138 | } |
139 | |
140 | /* Call the format specific write */ |
141 | OutputFormatTable[F->Id].Write (Data, A, B); |
142 | } |
143 | |