1 /*****************************************************************************/
5 /* Output file handling */
9 /* (C) 2009, 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 /*****************************************************************************/
54 /*****************************************************************************/
56 /*****************************************************************************/
60 /* Name of the output file. Dynamically allocated and read only. */
61 const char* OutputFilename = 0;
63 /* Output file handle */
68 /*****************************************************************************/
70 /*****************************************************************************/
74 void SetOutputName (const char* Name)
75 /* Sets the name of the output file. */
77 OutputFilename = Name;
82 void MakeDefaultOutputName (const char* InputFilename)
83 /* If the name of the output file is empty or NULL, the name of the output
84 * file is derived from the input file by adjusting the file name extension.
87 if (OutputFilename == 0 || *OutputFilename == '\0') {
88 /* We don't have an output file for now */
89 const char* Ext = PreprocessOnly? ".i" : ".s";
90 OutputFilename = MakeFilename (InputFilename, Ext);
96 void OpenOutputFile ()
97 /* Open the output file. Will call Fatal() in case of failures. */
99 /* Output file must not be open and we must have a name*/
100 PRECONDITION (OutputFile == 0 && OutputFilename != 0);
103 OutputFile = fopen (OutputFilename, "w");
104 if (OutputFile == 0) {
105 Fatal ("Cannot open output file `%s': %s", OutputFilename, strerror (errno));
107 Print (stdout, 1, "Opened output file `%s'\n", OutputFilename);
112 void CloseOutputFile ()
113 /* Close the output file. Will call Fatal() in case of failures. */
115 /* Output file must be open */
116 PRECONDITION (OutputFile != 0);
118 /* Close the file, check for errors */
119 if (fclose (OutputFile) != 0) {
120 remove (OutputFilename);
121 Fatal ("Cannot write to output file (disk full?)");
123 Print (stdout, 1, "Closed output file `%s'\n", OutputFilename);
128 int WriteOutput (const char* Format, ...)
129 /* Write to the output file using printf like formatting. Returns the number
136 /* Must have an output file */
137 PRECONDITION (OutputFile != 0);
139 /* Output formatted */
140 va_start (ap, Format);
141 CharCount = vfprintf (OutputFile, Format, ap);
144 /* Return the number of chars written */