/[ascend]/trunk/base/generic/utilities/ascPrint.c
ViewVC logotype

Contents of /trunk/base/generic/utilities/ascPrint.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 588 - (show annotations) (download) (as text)
Thu May 11 02:06:46 2006 UTC (14 years, 2 months ago) by johnpye
File MIME type: text/x-csrc
File size: 7053 byte(s)
This fixes the new Driver.c implementation. Now, default values of the environment variables
are specified in the file base/generic/utilities/config.h.in, which should be tooled up with
you configuration process. These default values will be subject to environment substiution,
so that if you set the default ASCENDTK=$ASCENDDIST/TK, that will be expanded *at runtime*.
This is to help with the 'application folder' style of installation off-root.
1 /*
2 * ASCEND Printf Substitutes
3 * by Mark Thomas
4 * Created: 27.May.1997
5 * Version: $Revision: 1.6 $
6 * Version control file: $RCSfile: ascPrint.c,v $
7 * Date last modified: $Date: 1997/10/29 13:08:49 $
8 * Last modified by: $Author: mthomas $
9 *
10 * This file is part of the ASCEND utilities.
11 *
12 * Copyright 1997, Carnegie Mellon University
13 *
14 * The ASCEND utilities is free software; you can redistribute
15 * it and/or modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
18 *
19 * The ASCEND utilities is distributed in hope that it will be
20 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with the program; if not, write to the Free Software Foundation,
26 * Inc., 675 Mass Ave, Cambridge, MA 02139 USA. Check the file named
27 * COPYING. COPYING is found in ../compiler.
28 */
29
30 /* ChangeLog
31 *
32 * 10/13/2005 Added Asc_PrintHasVTable() so user can tell if a vtable
33 * has already been registered. (J.D. St.Clair)
34 */
35 #include <stdlib.h>
36 #include <stdarg.h>
37 #include <stdio.h>
38 #include "ascConfig.h"
39 #include "ascPrint.h"
40
41 static struct Asc_PrintVTable *g_Asc_printVtables = NULL;
42
43 #define PRINT_BUFFER_SIZE 16380
44
45 int Asc_PrintPushVTable(struct Asc_PrintVTable *vtable)
46 {
47 if ((vtable == NULL) ||
48 (vtable->name == NULL) ||
49 (vtable->print == NULL) ||
50 (vtable->fflush == NULL) ||
51 (vtable->next != NULL)) {
52 return 1;
53 }
54 /* push it on the stack */
55 vtable->next = g_Asc_printVtables;
56 g_Asc_printVtables = vtable;
57 return 0;
58 }
59
60 struct Asc_PrintVTable *Asc_PrintRemoveVTable(CONST char *name)
61 {
62 struct Asc_PrintVTable *prev;
63 struct Asc_PrintVTable *vt;
64
65 /* skip notables cases */
66 if ((g_Asc_printVtables == NULL) || (NULL == name)) {
67 return NULL;
68 }
69 /* LIFO is easy */
70 vt = g_Asc_printVtables;
71 if ( strcmp(vt->name,name) == 0 ) {
72 g_Asc_printVtables = vt->next;
73 return vt;
74 }
75 /* middle chain is worse */
76 prev = g_Asc_printVtables;
77 vt = prev->next;
78 while (vt != NULL) {
79 if ( strcmp(vt->name,name) == 0 ) {
80 prev->next = vt->next;
81 return vt;
82 }
83 vt = vt->next;
84 prev = prev->next;
85 }
86 return NULL;
87 }
88
89 int Asc_PrintHasVTable(CONST char *name)
90 {
91 struct Asc_PrintVTable *vt;
92
93 if ((g_Asc_printVtables == NULL) ||
94 (NULL == name)) {
95 return FALSE;
96 }
97 vt = g_Asc_printVtables;
98 while (vt != NULL) {
99 if (strcmp(vt->name, name) == 0) {
100 return TRUE;
101 }
102 vt = vt->next;
103 }
104 return FALSE;
105 }
106
107
108 /*
109 * int Asc_Printf(format, variable_number_args)
110 * CONST char *format;
111 * variable_number_args;
112 *
113 * Using the sprintf-style format string `format', print the
114 * `variable_number_args' to an approximation of stdout.
115 *
116 * This function just initializes the variable_number_args into a
117 * va_list, and then calls AscPrint to actually do the work.
118 */
119 int Asc_Printf(CONST char *format, ...)
120 {
121 va_list args; /* the variable number of arguments */
122 int result = 0; /* the result of the call to AscPrint; our return value */
123
124 struct Asc_PrintVTable * vt = g_Asc_printVtables;
125 while (vt != NULL) {
126 /* create the va_list */
127 va_start( args, format );
128 result = vt->print( stdout, format, args );
129 /* cleanup */
130 va_end( args );
131 vt = vt->next;
132 }
133 /* only the result of the last printer makes it out */
134 return result;
135 }
136
137
138 /*
139 * int Asc_FPrintf(fp, format, variable_number_args)
140 * FILE *fp;
141 * CONST char *format;
142 * variable_number_args;
143 *
144 * Using the sprintf-style format string `format', print the
145 * `variable_number_args' to the file pointer `fp'.
146 *
147 * This function just initializes the variable_number_args into a
148 * va_list, and then calls AscPrint to actually do the work.
149 */
150 int Asc_FPrintf(FILE *fp, CONST char *format, ...)
151 {
152 va_list args; /* the variable number of arguments */
153 int result=0; /* the result of the call to AscPrint; our return value */
154
155 struct Asc_PrintVTable * vt = g_Asc_printVtables;
156 while (vt != NULL) {
157 /* create the va_list */
158 va_start( args, format );
159 result = vt->print( fp, format, args );
160 /* cleanup and return */
161 va_end( args );
162 vt = vt->next;
163 }
164 /* only the result of the last printer makes it out */
165 return result;
166 }
167
168 /**
169 Var-arg version of the above, used by error.h
170 */
171 int Asc_VFPrintf(FILE *fp, CONST char *format, va_list args){
172 int result=0; /* the result of the call to AscPrint; our return value */
173
174 struct Asc_PrintVTable * vt;
175 for(vt = g_Asc_printVtables; vt != NULL; vt = vt->next) {
176 result = vt->print(fp, format, args);
177 }
178 return result;
179 }
180
181 /*
182 * int Asc_FFlush(fileptr)
183 * FILE *fileptr;
184 *
185 * Flush output to the file pointed to by the file pointer `fileptr';
186 * return 0 for success and EOF for failure.
187 *
188 * This is needed for consistency with Asc_FPrintf() and Asc_Printf().
189 */
190 int Asc_FFlush( FILE *fileptr )
191 {
192 int result = 0;
193 struct Asc_PrintVTable * vt = g_Asc_printVtables;
194 while (vt != NULL) {
195 result = vt->fflush(fileptr);
196 vt = vt->next;
197 }
198 /* only the result of the last printer makes it out */
199 return result;
200 }
201
202
203 /*
204 * int Asc_FPutc( c, fileptr );
205 * int c;
206 * FILE *fileptr;
207 *
208 * Print the character `c' to the output file pointed to by the
209 * file pointer `fileptr'; return 0 for success and EOF for failure.
210 *
211 * This is needed for consistency with Asc_FPrintf() and Asc_Printf().
212 */
213 int Asc_FPutc( int c, FILE *fileptr )
214 {
215 /*
216 * Call vtable list for output to stdout and stderr, or the real putc
217 * for output to other file handles
218 */
219 if(( fileptr == stdout ) || ( fileptr == stderr )) {
220 return Asc_FPrintf( fileptr, "%c", c );
221 } else {
222 return fputc( c, fileptr );
223 }
224 }
225
226
227 /*
228 * int Asc_Putchar( c );
229 * int c;
230 *
231 * Print the character `c' to `stdout'; return 0 for success and
232 * EOF for failure.
233 *
234 * This is needed for consistency with Asc_FPrintf() and Asc_Printf().
235 */
236 int Asc_Putchar( int c )
237 {
238 return Asc_Printf( "%c", c );
239 }
240
241 static int use_xterm_color = 0;
242
243 /**
244 Little routine to aid output of XTERM colour codes where supported.
245 Not very efficient, so use sparingly.
246 */
247 int color_on(FILE *f, const char *colorcode){
248 char *term;
249 if(!use_xterm_color){
250 term = getenv("TERM");
251 if(term!=NULL){
252 if(strcmp(term,"msys")==0 || strcmp(term,"xterm")==0){
253 use_xterm_color=1;
254 }else{
255 use_xterm_color=-1;
256 }
257 }else{
258 use_xterm_color=-1;
259 }
260 }
261
262 if(colorcode!=NULL && use_xterm_color==1){
263 return fprintf(f,"\033[%sm",colorcode);
264 }
265 return 0;
266 }
267
268 /**
269 Little routine to aid output of XTERM colour codes where supported.
270 Not very efficient, so use sparingly.
271 */
272 int color_off(FILE *f){
273 if(use_xterm_color==1){
274 return fprintf(f,"\033[0m");
275 }
276 return 0;
277 }

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22