/[ascend]/trunk/tcltk98/generic/interface/AscPrintTcl.c
ViewVC logotype

Contents of /trunk/tcltk98/generic/interface/AscPrintTcl.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 539 - (show annotations) (download) (as text)
Tue Apr 25 23:11:59 2006 UTC (19 years ago) by johnpye
File MIME type: text/x-csrc
File size: 4262 byte(s)
Removed references to ascPrintType.h
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 #include <stdarg.h>
31 #include <tcl.h>
32 #include <utilities/ascConfig.h>
33 #include <utilities/ascPrint.h>
34
35 /*
36 * Only compile this file if we are using Asc_Printf()
37 */
38 #ifdef USE_ASC_PRINTF
39
40 #define PRINT_BUFFER_SIZE 16380
41
42 /*
43 * The Tcl channels for stdout and stderr.
44 *
45 * These need to be initialized by calling Asc_PrintInit() before
46 * calls to Asc_Printf() will write to the Tcl Channels.
47 */
48 static Tcl_Channel g_tclout = NULL; /* the Tcl channel for stdout */
49 static Tcl_Channel g_tclerr = NULL; /* the Tcl channel for stderr */
50
51 static struct Asc_PrintVTable g_Asc_PrintVTable_Tcl;
52 static int Asc_PrintTcl(FILE *fp, CONST char *format, va_list args);
53 static int Asc_FFlushTcl(FILE *fp);
54 static char *g_AscTclPrintName = "tclPrintChannels";
55
56 int Asc_PrintInit_TclVtable(void)
57 {
58 g_Asc_PrintVTable_Tcl.name = g_AscTclPrintName;
59 g_Asc_PrintVTable_Tcl.print = Asc_PrintTcl;
60 g_Asc_PrintVTable_Tcl.fflush = Asc_FFlushTcl;
61 g_Asc_PrintVTable_Tcl.next = 0;
62 return Asc_PrintPushVTable(&g_Asc_PrintVTable_Tcl);
63 }
64
65 int Asc_PrintInit_Tcl(void)
66 {
67
68 /*
69 * Initialize ASCEND's ideas of Tcl's idea of stdout and stderr.
70 */
71 g_tclout = Tcl_GetStdChannel( TCL_STDOUT );
72 g_tclerr = Tcl_GetStdChannel( TCL_STDERR );
73
74 if(( g_tclout == NULL ) || ( g_tclerr == NULL )) {
75 return 1;
76 }
77 return 0;
78 }
79
80 void Asc_PrintFinalize_Tcl(void)
81 {
82 Asc_PrintRemoveVTable(g_AscTclPrintName);
83 }
84
85
86 /*
87 * int AscPrint(fp, format, args)
88 * FILE *fp;
89 * CONST char *format;
90 * va_list args;
91 *
92 * Using the sprintf-style format string `format', print the arguments in
93 * the va_list `args' to the file pointer `fp'. Return the number of
94 * bytes printed.
95 *
96 * NOTE: The last argument to this function is a VA_LIST, NOT a variable
97 * number of arguments. You must initialize the va_list before calling
98 * this function, and cleanup the va_list afterwards.
99 */
100 static
101 int Asc_PrintTcl(FILE *fp, CONST char *format, va_list args)
102 {
103 static char buf[PRINT_BUFFER_SIZE]; /* the buffer that holds the output */
104
105 if(( fp == stdout ) && ( g_tclout != NULL )) {
106 vsprintf( buf, format, args );
107 return Tcl_Write( g_tclout, buf, -1 );
108 }
109 else if(( fp == stderr ) && ( g_tclerr != NULL )) {
110 vsprintf( buf, format, args );
111 return Tcl_Write( g_tclerr, buf, -1 );
112 }
113 /* TODO: should this fail loudly when a NULL fp is passed in? */
114 else if (fp != NULL) {
115 return vfprintf( fp, format, args );
116 }
117 else {
118 return 0;
119 }
120 }
121
122
123 /*
124 * int Asc_FFlushTcl(fileptr)
125 * FILE *fileptr;
126 *
127 * Flush output to the file pointed to by the file pointer `fileptr';
128 * return 0 for success and EOF for failure.
129 *
130 * This is needed for consistency with Asc_FPrintf() and Asc_Printf().
131 */
132 extern
133 int Asc_FFlushTcl( FILE *fileptr )
134 {
135 if(( fileptr == stdout ) && ( g_tclout != NULL )) {
136 if( Tcl_Flush( g_tclout ) != TCL_OK ) {
137 return EOF;
138 }
139 return 0;
140 }
141 else if(( fileptr == stdout ) && ( g_tclerr )) {
142 if( Tcl_Flush( g_tclerr ) != TCL_OK ) {
143 return EOF;
144 }
145 return 0;
146 }
147 else {
148 return fflush(fileptr);
149 }
150 }
151
152 #endif /* USE_ASC_PRINTF */

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