Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/preferences/src/lib/argp/argp-fmtstream.h @ 6394

Last change on this file since 6394 was 6393, checked in by rennerc, 19 years ago

added libargp to orxonox

File size: 11.0 KB
Line 
1/* Word-wrapping and line-truncating streams.
2   Copyright (C) 1997, 2003 Free Software Foundation, Inc.
3   This file is part of the GNU C Library.
4   Written by Miles Bader <miles@gnu.ai.mit.edu>.
5
6   The GNU C Library is free software; you can redistribute it and/or
7   modify it under the terms of the GNU Library General Public License as
8   published by the Free Software Foundation; either version 2 of the
9   License, or (at your option) any later version.
10
11   The GNU C Library is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   Library General Public License for more details.
15
16   You should have received a copy of the GNU Library General Public
17   License along with the GNU C Library; see the file COPYING.LIB.  If not,
18   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19   Boston, MA 02111-1307, USA.  */
20
21/* This package emulates glibc `line_wrap_stream' semantics for systems that
22   don't have that.  If the system does have it, it is just a wrapper for
23   that.  This header file is only used internally while compiling argp, and
24   shouldn't be installed.  */
25
26#ifndef _ARGP_FMTSTREAM_H
27#define _ARGP_FMTSTREAM_H
28
29#include <stdio.h>
30#include <string.h>
31#include <unistd.h>
32
33#if _LIBC || (defined (HAVE_FLOCKFILE) && defined(HAVE_PUTC_UNLOCKED) \
34     && defined (HAVE_FPUTS_UNLOCKED) && defined (HAVE_FWRITE_UNLOCKED) )
35/* Use locking funxtions */
36# define FLOCKFILE(f) flockfile(f)
37# define FUNLOCKFILE(f) funlockfile(f)
38# define PUTC_UNLOCKED(c, f) putc_unlocked((c), (f))
39# define FPUTS_UNLOCKED(s, f) fputs_unlocked((s), (f))
40# define FWRITE_UNLOCKED(b, s, n, f) fwrite_unlocked((b), (s), (n), (f))
41#else
42/* Disable stdio locking */
43# define FLOCKFILE(f)
44# define FUNLOCKFILE(f)
45# define PUTC_UNLOCKED(c, f) putc((c), (f))
46# define FPUTS_UNLOCKED(s, f) fputs((s), (f))
47# define FWRITE_UNLOCKED(b, s, n, f) fwrite((b), (s), (n), (f))
48#endif /* No thread safe i/o */
49
50#if    (_LIBC - 0 && !defined (USE_IN_LIBIO)) \
51    || (defined (__GNU_LIBRARY__) && defined (HAVE_LINEWRAP_H))
52/* line_wrap_stream is available, so use that.  */
53#define ARGP_FMTSTREAM_USE_LINEWRAP
54#endif
55
56#ifdef ARGP_FMTSTREAM_USE_LINEWRAP
57/* Just be a simple wrapper for line_wrap_stream; the semantics are
58   *slightly* different, as line_wrap_stream doesn't actually make a new
59   object, it just modifies the given stream (reversibly) to do
60   line-wrapping.  Since we control who uses this code, it doesn't matter.  */
61
62#include <linewrap.h>
63
64typedef FILE *argp_fmtstream_t;
65
66#define argp_make_fmtstream line_wrap_stream
67#define __argp_make_fmtstream line_wrap_stream
68#define argp_fmtstream_free line_unwrap_stream
69#define __argp_fmtstream_free line_unwrap_stream
70
71#define __argp_fmtstream_putc(fs,ch) putc(ch,fs)
72#define argp_fmtstream_putc(fs,ch) putc(ch,fs)
73#define __argp_fmtstream_puts(fs,str) fputs(str,fs)
74#define argp_fmtstream_puts(fs,str) fputs(str,fs)
75#define __argp_fmtstream_write(fs,str,len) fwrite(str,1,len,fs)
76#define argp_fmtstream_write(fs,str,len) fwrite(str,1,len,fs)
77#define __argp_fmtstream_printf fprintf
78#define argp_fmtstream_printf fprintf
79
80#define __argp_fmtstream_lmargin line_wrap_lmargin
81#define argp_fmtstream_lmargin line_wrap_lmargin
82#define __argp_fmtstream_set_lmargin line_wrap_set_lmargin
83#define argp_fmtstream_set_lmargin line_wrap_set_lmargin
84#define __argp_fmtstream_rmargin line_wrap_rmargin
85#define argp_fmtstream_rmargin line_wrap_rmargin
86#define __argp_fmtstream_set_rmargin line_wrap_set_rmargin
87#define argp_fmtstream_set_rmargin line_wrap_set_rmargin
88#define __argp_fmtstream_wmargin line_wrap_wmargin
89#define argp_fmtstream_wmargin line_wrap_wmargin
90#define __argp_fmtstream_set_wmargin line_wrap_set_wmargin
91#define argp_fmtstream_set_wmargin line_wrap_set_wmargin
92#define __argp_fmtstream_point line_wrap_point
93#define argp_fmtstream_point line_wrap_point
94
95#else /* !ARGP_FMTSTREAM_USE_LINEWRAP */
96/* Guess we have to define our own version.  */
97
98#ifndef __const
99#define __const const
100#endif
101
102
103struct argp_fmtstream
104{
105  FILE *stream;     /* The stream we're outputting to.  */
106
107  size_t lmargin, rmargin;  /* Left and right margins.  */
108  ssize_t wmargin;    /* Margin to wrap to, or -1 to truncate.  */
109
110  /* Point in buffer to which we've processed for wrapping, but not output.  */
111  size_t point_offs;
112  /* Output column at POINT_OFFS, or -1 meaning 0 but don't add lmargin.  */
113  ssize_t point_col;
114
115  char *buf;      /* Output buffer.  */
116  char *p;      /* Current end of text in BUF. */
117  char *end;      /* Absolute end of BUF.  */
118};
119
120typedef struct argp_fmtstream *argp_fmtstream_t;
121
122/* Return an argp_fmtstream that outputs to STREAM, and which prefixes lines
123   written on it with LMARGIN spaces and limits them to RMARGIN columns
124   total.  If WMARGIN >= 0, words that extend past RMARGIN are wrapped by
125   replacing the whitespace before them with a newline and WMARGIN spaces.
126   Otherwise, chars beyond RMARGIN are simply dropped until a newline.
127   Returns NULL if there was an error.  */
128extern argp_fmtstream_t __argp_make_fmtstream (FILE *__stream,
129                 size_t __lmargin,
130                 size_t __rmargin,
131                 ssize_t __wmargin);
132extern argp_fmtstream_t argp_make_fmtstream (FILE *__stream,
133               size_t __lmargin,
134               size_t __rmargin,
135               ssize_t __wmargin);
136
137/* Flush __FS to its stream, and free it (but don't close the stream).  */
138extern void __argp_fmtstream_free (argp_fmtstream_t __fs);
139extern void argp_fmtstream_free (argp_fmtstream_t __fs);
140
141extern ssize_t __argp_fmtstream_printf (argp_fmtstream_t __fs,
142               __const char *__fmt, ...)
143     /*PRINTF_STYLE(2,3)*/;
144extern ssize_t argp_fmtstream_printf (argp_fmtstream_t __fs,
145              __const char *__fmt, ...)
146     /*PRINTF_STYLE(2,3)*/;
147
148extern int __argp_fmtstream_putc (argp_fmtstream_t __fs, int __ch);
149extern int argp_fmtstream_putc (argp_fmtstream_t __fs, int __ch);
150
151extern int __argp_fmtstream_puts (argp_fmtstream_t __fs, __const char *__str);
152extern int argp_fmtstream_puts (argp_fmtstream_t __fs, __const char *__str);
153
154extern size_t __argp_fmtstream_write (argp_fmtstream_t __fs,
155              __const char *__str, size_t __len);
156extern size_t argp_fmtstream_write (argp_fmtstream_t __fs,
157            __const char *__str, size_t __len);
158
159/* Access macros for various bits of state.  */
160#define argp_fmtstream_lmargin(__fs) ((__fs)->lmargin)
161#define argp_fmtstream_rmargin(__fs) ((__fs)->rmargin)
162#define argp_fmtstream_wmargin(__fs) ((__fs)->wmargin)
163#define __argp_fmtstream_lmargin argp_fmtstream_lmargin
164#define __argp_fmtstream_rmargin argp_fmtstream_rmargin
165#define __argp_fmtstream_wmargin argp_fmtstream_wmargin
166
167/* Set __FS's left margin to LMARGIN and return the old value.  */
168extern size_t argp_fmtstream_set_lmargin (argp_fmtstream_t __fs,
169            size_t __lmargin);
170extern size_t __argp_fmtstream_set_lmargin (argp_fmtstream_t __fs,
171              size_t __lmargin);
172
173/* Set __FS's right margin to __RMARGIN and return the old value.  */
174extern size_t argp_fmtstream_set_rmargin (argp_fmtstream_t __fs,
175            size_t __rmargin);
176extern size_t __argp_fmtstream_set_rmargin (argp_fmtstream_t __fs,
177              size_t __rmargin);
178
179/* Set __FS's wrap margin to __WMARGIN and return the old value.  */
180extern size_t argp_fmtstream_set_wmargin (argp_fmtstream_t __fs,
181            size_t __wmargin);
182extern size_t __argp_fmtstream_set_wmargin (argp_fmtstream_t __fs,
183              size_t __wmargin);
184
185/* Return the column number of the current output point in __FS.  */
186extern size_t argp_fmtstream_point (argp_fmtstream_t __fs);
187extern size_t __argp_fmtstream_point (argp_fmtstream_t __fs);
188
189/* Internal routines.  */
190extern void _argp_fmtstream_update (argp_fmtstream_t __fs);
191extern void __argp_fmtstream_update (argp_fmtstream_t __fs);
192extern int _argp_fmtstream_ensure (argp_fmtstream_t __fs, size_t __amount);
193extern int __argp_fmtstream_ensure (argp_fmtstream_t __fs, size_t __amount);
194
195#ifdef __OPTIMIZE__
196/* Inline versions of above routines.  */
197
198#if !_LIBC
199#define __argp_fmtstream_putc argp_fmtstream_putc
200#define __argp_fmtstream_puts argp_fmtstream_puts
201#define __argp_fmtstream_write argp_fmtstream_write
202#define __argp_fmtstream_set_lmargin argp_fmtstream_set_lmargin
203#define __argp_fmtstream_set_rmargin argp_fmtstream_set_rmargin
204#define __argp_fmtstream_set_wmargin argp_fmtstream_set_wmargin
205#define __argp_fmtstream_point argp_fmtstream_point
206#define __argp_fmtstream_update _argp_fmtstream_update
207#define __argp_fmtstream_ensure _argp_fmtstream_ensure
208#endif
209
210#ifndef ARGP_FS_EI
211#define ARGP_FS_EI extern inline
212#endif
213
214ARGP_FS_EI size_t
215__argp_fmtstream_write (argp_fmtstream_t __fs,
216      __const char *__str, size_t __len)
217{
218  if (__fs->p + __len <= __fs->end || __argp_fmtstream_ensure (__fs, __len))
219    {
220      memcpy (__fs->p, __str, __len);
221      __fs->p += __len;
222      return __len;
223    }
224  else
225    return 0;
226}
227
228ARGP_FS_EI int
229__argp_fmtstream_puts (argp_fmtstream_t __fs, __const char *__str)
230{
231  size_t __len = strlen (__str);
232  if (__len)
233    {
234      size_t __wrote = __argp_fmtstream_write (__fs, __str, __len);
235      return __wrote == __len ? 0 : -1;
236    }
237  else
238    return 0;
239}
240
241ARGP_FS_EI int
242__argp_fmtstream_putc (argp_fmtstream_t __fs, int __ch)
243{
244  if (__fs->p < __fs->end || __argp_fmtstream_ensure (__fs, 1))
245    return *__fs->p++ = __ch;
246  else
247    return EOF;
248}
249
250/* Set __FS's left margin to __LMARGIN and return the old value.  */
251ARGP_FS_EI size_t
252__argp_fmtstream_set_lmargin (argp_fmtstream_t __fs, size_t __lmargin)
253{
254  size_t __old;
255  if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
256    __argp_fmtstream_update (__fs);
257  __old = __fs->lmargin;
258  __fs->lmargin = __lmargin;
259  return __old;
260}
261
262/* Set __FS's right margin to __RMARGIN and return the old value.  */
263ARGP_FS_EI size_t
264__argp_fmtstream_set_rmargin (argp_fmtstream_t __fs, size_t __rmargin)
265{
266  size_t __old;
267  if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
268    __argp_fmtstream_update (__fs);
269  __old = __fs->rmargin;
270  __fs->rmargin = __rmargin;
271  return __old;
272}
273
274/* Set FS's wrap margin to __WMARGIN and return the old value.  */
275ARGP_FS_EI size_t
276__argp_fmtstream_set_wmargin (argp_fmtstream_t __fs, size_t __wmargin)
277{
278  size_t __old;
279  if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
280    __argp_fmtstream_update (__fs);
281  __old = __fs->wmargin;
282  __fs->wmargin = __wmargin;
283  return __old;
284}
285
286/* Return the column number of the current output point in __FS.  */
287ARGP_FS_EI size_t
288__argp_fmtstream_point (argp_fmtstream_t __fs)
289{
290  if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
291    __argp_fmtstream_update (__fs);
292  return __fs->point_col >= 0 ? __fs->point_col : 0;
293}
294
295#if !_LIBC
296#undef __argp_fmtstream_putc
297#undef __argp_fmtstream_puts
298#undef __argp_fmtstream_write
299#undef __argp_fmtstream_set_lmargin
300#undef __argp_fmtstream_set_rmargin
301#undef __argp_fmtstream_set_wmargin
302#undef __argp_fmtstream_point
303#undef __argp_fmtstream_update
304#undef __argp_fmtstream_ensure
305#endif
306
307#endif /* __OPTIMIZE__ */
308
309#endif /* ARGP_FMTSTREAM_USE_LINEWRAP */
310
311#endif /* argp-fmtstream.h */
Note: See TracBrowser for help on using the repository browser.