]> git.sur5r.net Git - cc65/blob - src/sp65/output.c
Move attribute lookup into the output functions. Allow a bytesperline
[cc65] / src / sp65 / output.c
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 "error.h"
46 #include "output.h"
47
48
49
50 /*****************************************************************************/
51 /*                                   Data                                    */
52 /*****************************************************************************/
53
54
55
56 typedef struct OutputFormatDesc OutputFormatDesc;
57 struct OutputFormatDesc {
58
59     /* Write routine */
60     void (*Write) (const StrBuf*, const Collection*);
61
62 };
63
64 /* Table with Output formats */
65 static OutputFormatDesc OutputFormatTable[ofCount] = {
66     {   WriteAsmFile    },
67     {   WriteBinFile    },
68 };
69
70 /* Table that maps extensions to Output formats. Must be sorted alphabetically */
71 static const FileId FormatTable[] = {
72     /* Upper case stuff for obsolete operating systems */
73     {   "A",    ofAsm           },
74     {   "ASM",  ofAsm           },
75     {   "BIN",  ofBin           },
76     {   "INC",  ofAsm           },
77     {   "S",    ofAsm           },
78
79     {   "a",    ofAsm           },
80     {   "asm",  ofAsm           },
81     {   "bin",  ofBin           },
82     {   "inc",  ofAsm           },
83     {   "s",    ofAsm           },
84 };
85
86
87
88 /*****************************************************************************/
89 /*                                   Code                                    */
90 /*****************************************************************************/
91
92
93
94 void WriteOutputFile (const StrBuf* Data, const Collection* A)
95 /* Write the contents of Data to a file. Format, file name etc. must be given
96  * as attributes in A. If no format is given, the function tries to autodetect
97  * it by using the extension of the file name.
98  */
99 {
100     const FileId* F;
101
102     /* Get the file format from the command line */
103     const char* Format = GetAttrVal (A, "format");
104     if (Format != 0) {
105         /* Format is given, search for it in the table. */
106         F = bsearch (Format,
107                      FormatTable,
108                      sizeof (FormatTable) / sizeof (FormatTable[0]),
109                      sizeof (FormatTable[0]),
110                      CompareFileId);
111         if (F == 0) {
112             Error ("Unknown output format `%s'", Format);
113         }
114     } else {
115         /* No format given, use file name extension */
116         const char* Name = NeedAttrVal (A, "name", "write");
117         F = GetFileId (Name, FormatTable,
118                        sizeof (FormatTable) / sizeof (FormatTable[0]));
119         /* Found? */
120         if (F == 0) {
121             Error ("Cannot determine file format of output file `%s'", Name);
122         }
123     }
124
125     /* Call the format specific write */
126     OutputFormatTable[F->Id].Write (Data, A);
127 }
128
129
130