~ubuntu-branches/ubuntu/precise/code-saturne/precise

« back to all changes in this revision

Viewing changes to src/bft/bft_printf.c

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2011-11-24 00:00:08 UTC
  • mfrom: (6.1.9 sid)
  • Revision ID: package-import@ubuntu.com-20111124000008-2vo99e38267942q5
Tags: 2.1.0-3
Install a missing file

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*============================================================================
 
2
 * Base user-definable printf() wrapper or replacement.
 
3
 *============================================================================*/
 
4
 
 
5
/*
 
6
  This file is part of Code_Saturne, a general-purpose CFD tool.
 
7
 
 
8
  Copyright (C) 1998-2011 EDF S.A.
 
9
 
 
10
  This program is free software; you can redistribute it and/or modify it under
 
11
  the terms of the GNU General Public License as published by the Free Software
 
12
  Foundation; either version 2 of the License, or (at your option) any later
 
13
  version.
 
14
 
 
15
  This program is distributed in the hope that it will be useful, but WITHOUT
 
16
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
17
  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 
18
  details.
 
19
 
 
20
  You should have received a copy of the GNU General Public License along with
 
21
  this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
 
22
  Street, Fifth Floor, Boston, MA 02110-1301, USA.
 
23
*/
 
24
 
 
25
/*----------------------------------------------------------------------------*/
 
26
 
 
27
#if defined(HAVE_CONFIG_H)
 
28
#include "cs_config.h"
 
29
#endif
 
30
 
 
31
#include "bft_config_defs.h"
 
32
 
 
33
/*
 
34
 * Standard C library headers
 
35
 */
 
36
 
 
37
#include <assert.h>
 
38
#include <stdarg.h>
 
39
#include <stdio.h>
 
40
#include <stdlib.h>
 
41
#include <string.h>
 
42
 
 
43
/*
 
44
 * Optional library and BFT headers
 
45
 */
 
46
 
 
47
#include "bft_printf.h"
 
48
 
 
49
/*-----------------------------------------------------------------------------*/
 
50
 
 
51
#ifdef __cplusplus
 
52
extern "C" {
 
53
#if 0
 
54
} /* Fake brace to force back Emacs auto-indentation back to column 0 */
 
55
#endif
 
56
#endif /* __cplusplus */
 
57
 
 
58
/*-----------------------------------------------------------------------------*/
 
59
 
 
60
/*-----------------------------------------------------------------------------
 
61
 * Local type definitions
 
62
 *-----------------------------------------------------------------------------*/
 
63
 
 
64
/* Associated typedef documentation (for bft_printf.h) */
 
65
 
 
66
/*!
 
67
 * \typedef bft_printf_proxy_t
 
68
 *
 
69
 * \brief Function pointer for printf() type functions.
 
70
 *
 
71
 * \param [in] format       format string, as printf() and family.
 
72
 * \param [in, out] arg_ptr pointer to variable argument list based on format
 
73
 *                          string.
 
74
 */
 
75
 
 
76
/*!
 
77
 * \typedef bft_printf_flush_proxy_t
 
78
 *
 
79
 * \brief Function pointer for fflush(stdout) type functions.
 
80
 */
 
81
 
 
82
/*-----------------------------------------------------------------------------
 
83
 * Local function prototypes
 
84
 *-----------------------------------------------------------------------------*/
 
85
 
 
86
/*
 
87
 * Default bft_printf_flush() proxy.
 
88
 *
 
89
 * returns:
 
90
 *   return code of fflush(stdout).
 
91
 */
 
92
 
 
93
static int
 
94
_bft_printf_flush_proxy_default(void);
 
95
 
 
96
/*-----------------------------------------------------------------------------
 
97
 * Local static variable definitions
 
98
 *-----------------------------------------------------------------------------*/
 
99
 
 
100
static bft_printf_proxy_t        *_bft_printf_proxy = vprintf;
 
101
static bft_printf_flush_proxy_t  *_bft_printf_flush_proxy
 
102
                                    = _bft_printf_flush_proxy_default;
 
103
 
 
104
/*-----------------------------------------------------------------------------
 
105
 * Local function definitions
 
106
 *-----------------------------------------------------------------------------*/
 
107
 
 
108
/*
 
109
 * Default bft_printf_flush() proxy.
 
110
 *
 
111
 * returns:
 
112
 *   return code of fflush(stdout).
 
113
 */
 
114
 
 
115
static int
 
116
_bft_printf_flush_proxy_default(void)
 
117
{
 
118
  return fflush(stdout);
 
119
}
 
120
 
 
121
/*============================================================================
 
122
 * Public function definitions
 
123
 *============================================================================*/
 
124
 
 
125
/*!
 
126
 * \brief Replacement for printf() with modifiable behavior.
 
127
 *
 
128
 * This function calls vprintf() by default, or a function with similar
 
129
 * arguments indicated by bft_printf_proxy_set().
 
130
 *
 
131
 * \param [in] format format string, as printf() and family.
 
132
 * \param [in] ...    variable arguments based on format string.
 
133
 *
 
134
 * \return number of characters printed, not counting the trailing '\\0'
 
135
 *         used to end output strings
 
136
 */
 
137
 
 
138
int
 
139
bft_printf(const char *const format,
 
140
           ...)
 
141
{
 
142
  int  retval;
 
143
  va_list  arg_ptr;
 
144
 
 
145
  va_start(arg_ptr, format);
 
146
 
 
147
  retval = _bft_printf_proxy(format, arg_ptr);
 
148
 
 
149
  va_end(arg_ptr);
 
150
 
 
151
  return retval;
 
152
}
 
153
 
 
154
/*!
 
155
 * \brief Flush for output of bft_printf() with modifiable behavior.
 
156
 *
 
157
 * This function calls fflush(stdout) if bft_printf()'s default behavior is
 
158
 * used. If bft_printf's behavior is modified with bft_printf_proxy_set(),
 
159
 * bft_printf_flush()'s behavior may have to be also adjusted with
 
160
 * bft_printf_flush_proxy_set().
 
161
 *
 
162
 * \return using the default behavior, the return value is that of
 
163
 *         fflush(stdout): O upon successful completion, EOF otherwise
 
164
 *         (with errno set to indicate the error).
 
165
 */
 
166
 
 
167
int
 
168
bft_printf_flush(void)
 
169
{
 
170
  return _bft_printf_flush_proxy();
 
171
}
 
172
 
 
173
/*!
 
174
 * \brief Returns function associated with the bft_printf() function.
 
175
 *
 
176
 * \return pointer to the vprintf() or replacement function.
 
177
 */
 
178
 
 
179
bft_printf_proxy_t *
 
180
bft_printf_proxy_get(void)
 
181
{
 
182
  return _bft_printf_proxy;
 
183
}
 
184
 
 
185
/*!
 
186
 * \brief Associates a vprintf() type function with the bft_printf() function.
 
187
 *
 
188
 * \param [in] fct pointer to a vprintf() type function.
 
189
 */
 
190
 
 
191
void
 
192
bft_printf_proxy_set(bft_printf_proxy_t *const fct)
 
193
{
 
194
  _bft_printf_proxy = fct;
 
195
}
 
196
 
 
197
/*!
 
198
 * \brief Returns function associated with bft_printf_flush().
 
199
 *
 
200
 * \return pointer to the bft_printf_flush() proxy.
 
201
 */
 
202
 
 
203
bft_printf_flush_proxy_t *
 
204
bft_printf_flush_proxy_get(void)
 
205
{
 
206
  return _bft_printf_flush_proxy;
 
207
}
 
208
 
 
209
/*!
 
210
 * \brief Associates a proxy function with bft_printf_flush().
 
211
 *
 
212
 * \warning
 
213
 *   bft_printf() is called by the default bft_error() error handler
 
214
 *   (so as to ensure that the error text appears at the end of the
 
215
 *   program output), so a bft_print_flush replacement must not itself
 
216
 *   call (directly or indirectly) bft_error() if the default error
 
217
 *   handler is used.
 
218
 *
 
219
 * \param [in] fct pointer to a function similar to {return fflush(stdout)}.
 
220
 */
 
221
 
 
222
void
 
223
bft_printf_flush_proxy_set(bft_printf_flush_proxy_t *const fct)
 
224
{
 
225
  _bft_printf_flush_proxy = fct;
 
226
}
 
227
 
 
228
/*-----------------------------------------------------------------------------*/
 
229
 
 
230
#ifdef __cplusplus
 
231
}
 
232
#endif /* __cplusplus */