1 /*****************************************************************************/
5 /* Output file handling */
9 /* (C) 2009-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 /*****************************************************************************/
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 OpenDebugOutputFile (const char* Name)
113 /* Open an output file for debugging purposes. Will call Fatal() in case of
117 /* Output file must not be open and we must have a name*/
118 PRECONDITION (OutputFile == 0);
121 OutputFile = fopen (Name, "w");
122 if (OutputFile == 0) {
123 Fatal ("Cannot open debug output file '%s': %s", Name, strerror (errno));
125 Print (stdout, 1, "Opened debug output file '%s'\n", Name);
130 void CloseOutputFile ()
131 /* Close the output file. Will call Fatal() in case of failures. */
133 /* Output file must be open */
134 PRECONDITION (OutputFile != 0);
136 /* Close the file, check for errors */
137 if (fclose (OutputFile) != 0) {
138 remove (OutputFilename);
139 Fatal ("Cannot write to output file (disk full?)");
141 Print (stdout, 1, "Closed output file '%s'\n", OutputFilename);
148 int WriteOutput (const char* Format, ...)
149 /* Write to the output file using printf like formatting. Returns the number
156 /* Must have an output file */
157 PRECONDITION (OutputFile != 0);
159 /* Output formatted */
160 va_start (ap, Format);
161 CharCount = vfprintf (OutputFile, Format, ap);
164 /* Return the number of chars written */