1 /*****************************************************************************/
5 /* C output for the sp65 sprite and bitmap utility */
9 /* (C) 2012, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
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. */
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: */
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 */
32 /*****************************************************************************/
53 /*****************************************************************************/
55 /*****************************************************************************/
59 static int ValidIdentifier (const char* L)
60 /* Check a C identifier for validity */
62 /* Must begin with underscore or alphabetic character */
63 if (*L != '_' && !IsAlpha (*L)) {
68 /* Remainder must be as above plus digits */
70 if (*L != '_' && !IsAlNum (*L)) {
82 static unsigned GetBytesPerLine (const Collection* A)
83 /* Return the number of bytes per line from the attribute collection A */
86 unsigned BytesPerLine = 16;
88 /* Check for a bytesperline attribute */
89 const char* V = GetAttrVal (A, "bytesperline");
90 if ((V && sscanf (V, "%u%c", &BytesPerLine, &C) != 1) ||
91 (BytesPerLine < 1 || BytesPerLine > 64)) {
92 Error ("Invalid value for attribute `bytesperline'");
99 static unsigned GetBase (const Collection* A)
100 /* Return the number base from the attribute collection A */
105 /* Check for a base attribute */
106 const char* V = GetAttrVal (A, "base");
107 if ((V && sscanf (V, "%u%c", &Base, &C) != 1) || (Base != 10 && Base != 16)) {
108 Error ("Invalid value for attribute `base'");
115 static const char* GetIdentifier (const Collection* A)
116 /* Return the variable identifier from the attribute collection A */
118 /* Check for a ident attribute */
119 const char* Ident = NeedAttrVal (A, "ident", "write");
120 if (!ValidIdentifier (Ident)) {
121 Error ("Invalid value for attribute `ident'");
128 void WriteCFile (const StrBuf* Data, const Collection* A)
129 /* Write the contents of Data to a file in C format */
136 /* Get the file name */
137 const char* Name = NeedAttrVal (A, "name", "write");
139 /* Check the number of bytes per line */
140 unsigned BytesPerLine = GetBytesPerLine (A);
142 /* Get the number base */
143 unsigned Base = GetBase (A);
145 /* Get the identifier */
146 const char* Ident = GetIdentifier (A);
148 /* Open the output file */
149 F = fopen (Name, "w");
151 Error ("Cannot open output file `%s': %s", Name, strerror (errno));
154 /* Write a readable header */
157 " * This file was generated by %s %s\n"
161 GetVersionAsString ());
164 /* Output the declaration and identifier */
165 fprintf (F, "const unsigned char %s[] = {\n", Ident);
168 D = SB_GetConstBuf (Data);
169 Size = SB_GetLen (Data);
174 /* Output one line */
175 unsigned Chunk = Size;
176 if (Chunk > BytesPerLine) {
177 Chunk = BytesPerLine;
180 for (I = 0; I < Chunk; ++I) {
183 fprintf (F, "%u,", *D++);
186 fprintf (F, "0x%02X,", *D++);
193 /* Bump the counters */
197 /* Terminate the array */
201 if (fclose (F) != 0) {
202 Error ("Error closing output file `%s': %s", Name, strerror (errno));