argp-fmtstream.h

00001 /* Word-wrapping and line-truncating streams.
00002    Copyright (C) 1997 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Written by Miles Bader <miles@gnu.ai.mit.edu>.
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU Lesser General Public License as published by
00008    the Free Software Foundation; either version 2.1, or (at your option)
00009    any later version.
00010 
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014    GNU Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public License along
00017    with this program; if not, write to the Free Software Foundation,
00018    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
00019 
00020 /* This package emulates glibc `line_wrap_stream' semantics for systems that
00021    don't have that.  If the system does have it, it is just a wrapper for
00022    that.  This header file is only used internally while compiling argp, and
00023    shouldn't be installed.  */
00024 
00025 #ifndef _ARGP_FMTSTREAM_H
00026 #define _ARGP_FMTSTREAM_H
00027 
00028 #include <stdio.h>
00029 #include <string.h>
00030 #include <unistd.h>
00031 
00032 #ifndef __attribute__
00033 /* This feature is available in gcc versions 2.5 and later.  */
00034 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5) || __STRICT_ANSI__
00035 #  define __attribute__(Spec) /* empty */
00036 # endif
00037 /* The __-protected variants of `format' and `printf' attributes
00038    are accepted by gcc versions 2.6.4 (effectively 2.7) and later.  */
00039 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7) || __STRICT_ANSI__
00040 #  define __format__ format
00041 #  define __printf__ printf
00042 # endif
00043 #endif
00044 
00045 #if    (_LIBC - 0 && !defined (USE_IN_LIBIO)) \
00046     || (defined (__GNU_LIBRARY__) && defined (HAVE_LINEWRAP_H))
00047 /* line_wrap_stream is available, so use that.  */
00048 #define ARGP_FMTSTREAM_USE_LINEWRAP
00049 #endif
00050 
00051 #ifdef ARGP_FMTSTREAM_USE_LINEWRAP
00052 /* Just be a simple wrapper for line_wrap_stream; the semantics are
00053    *slightly* different, as line_wrap_stream doesn't actually make a new
00054    object, it just modifies the given stream (reversibly) to do
00055    line-wrapping.  Since we control who uses this code, it doesn't matter.  */
00056 
00057 #include <linewrap.h>
00058 
00059 typedef FILE *argp_fmtstream_t;
00060 
00061 #define argp_make_fmtstream line_wrap_stream
00062 #define __argp_make_fmtstream line_wrap_stream
00063 #define argp_fmtstream_free line_unwrap_stream
00064 #define __argp_fmtstream_free line_unwrap_stream
00065 
00066 #define __argp_fmtstream_putc(fs,ch) putc(ch,fs)
00067 #define argp_fmtstream_putc(fs,ch) putc(ch,fs)
00068 #define __argp_fmtstream_puts(fs,str) fputs(str,fs)
00069 #define argp_fmtstream_puts(fs,str) fputs(str,fs)
00070 #define __argp_fmtstream_write(fs,str,len) fwrite(str,1,len,fs)
00071 #define argp_fmtstream_write(fs,str,len) fwrite(str,1,len,fs)
00072 #define __argp_fmtstream_printf fprintf
00073 #define argp_fmtstream_printf fprintf
00074 
00075 #define __argp_fmtstream_lmargin line_wrap_lmargin
00076 #define argp_fmtstream_lmargin line_wrap_lmargin
00077 #define __argp_fmtstream_set_lmargin line_wrap_set_lmargin
00078 #define argp_fmtstream_set_lmargin line_wrap_set_lmargin
00079 #define __argp_fmtstream_rmargin line_wrap_rmargin
00080 #define argp_fmtstream_rmargin line_wrap_rmargin
00081 #define __argp_fmtstream_set_rmargin line_wrap_set_rmargin
00082 #define argp_fmtstream_set_rmargin line_wrap_set_rmargin
00083 #define __argp_fmtstream_wmargin line_wrap_wmargin
00084 #define argp_fmtstream_wmargin line_wrap_wmargin
00085 #define __argp_fmtstream_set_wmargin line_wrap_set_wmargin
00086 #define argp_fmtstream_set_wmargin line_wrap_set_wmargin
00087 #define __argp_fmtstream_point line_wrap_point
00088 #define argp_fmtstream_point line_wrap_point
00089 
00090 #else /* !ARGP_FMTSTREAM_USE_LINEWRAP */
00091 /* Guess we have to define our own version.  */
00092 
00093 #ifndef __const
00094 #define __const const
00095 #endif
00096 
00097 struct argp_fmtstream
00098 {
00099   FILE *stream;                 /* The stream we're outputting to.  */
00100 
00101   size_t lmargin, rmargin;      /* Left and right margins.  */
00102   ssize_t wmargin;              /* Margin to wrap to, or -1 to truncate.  */
00103 
00104   /* Point in buffer to which we've processed for wrapping, but not output.  */
00105   size_t point_offs;
00106   /* Output column at POINT_OFFS, or -1 meaning 0 but don't add lmargin.  */
00107   ssize_t point_col;
00108 
00109   char *buf;                    /* Output buffer.  */
00110   char *p;                      /* Current end of text in BUF. */
00111   char *end;                    /* Absolute end of BUF.  */
00112 };
00113 
00114 typedef struct argp_fmtstream *argp_fmtstream_t;
00115 
00116 /* Return an argp_fmtstream that outputs to STREAM, and which prefixes lines
00117    written on it with LMARGIN spaces and limits them to RMARGIN columns
00118    total.  If WMARGIN >= 0, words that extend past RMARGIN are wrapped by
00119    replacing the whitespace before them with a newline and WMARGIN spaces.
00120    Otherwise, chars beyond RMARGIN are simply dropped until a newline.
00121    Returns NULL if there was an error.  */
00122 extern argp_fmtstream_t __argp_make_fmtstream (FILE *__stream,
00123                                                size_t __lmargin,
00124                                                size_t __rmargin,
00125                                                ssize_t __wmargin);
00126 extern argp_fmtstream_t argp_make_fmtstream (FILE *__stream,
00127                                              size_t __lmargin,
00128                                              size_t __rmargin,
00129                                              ssize_t __wmargin);
00130 
00131 /* Flush __FS to its stream, and free it (but don't close the stream).  */
00132 extern void __argp_fmtstream_free (argp_fmtstream_t __fs);
00133 extern void argp_fmtstream_free (argp_fmtstream_t __fs);
00134 
00135 extern ssize_t __argp_fmtstream_printf (argp_fmtstream_t __fs,
00136                                        __const char *__fmt, ...)
00137      __attribute__ ((__format__ (printf, 2, 3)));
00138 extern ssize_t argp_fmtstream_printf (argp_fmtstream_t __fs,
00139                                       __const char *__fmt, ...)
00140      __attribute__ ((__format__ (printf, 2, 3)));
00141 
00142 extern int __argp_fmtstream_putc (argp_fmtstream_t __fs, int __ch);
00143 extern int argp_fmtstream_putc (argp_fmtstream_t __fs, int __ch);
00144 
00145 extern int __argp_fmtstream_puts (argp_fmtstream_t __fs, __const char *__str);
00146 extern int argp_fmtstream_puts (argp_fmtstream_t __fs, __const char *__str);
00147 
00148 extern size_t __argp_fmtstream_write (argp_fmtstream_t __fs,
00149                                       __const char *__str, size_t __len);
00150 extern size_t argp_fmtstream_write (argp_fmtstream_t __fs,
00151                                     __const char *__str, size_t __len);
00152 
00153 /* Access macros for various bits of state.  */
00154 #define argp_fmtstream_lmargin(__fs) ((__fs)->lmargin)
00155 #define argp_fmtstream_rmargin(__fs) ((__fs)->rmargin)
00156 #define argp_fmtstream_wmargin(__fs) ((__fs)->wmargin)
00157 #define __argp_fmtstream_lmargin argp_fmtstream_lmargin
00158 #define __argp_fmtstream_rmargin argp_fmtstream_rmargin
00159 #define __argp_fmtstream_wmargin argp_fmtstream_wmargin
00160 
00161 /* Set __FS's left margin to LMARGIN and return the old value.  */
00162 extern size_t argp_fmtstream_set_lmargin (argp_fmtstream_t __fs,
00163                                           size_t __lmargin);
00164 extern size_t __argp_fmtstream_set_lmargin (argp_fmtstream_t __fs,
00165                                             size_t __lmargin);
00166 
00167 /* Set __FS's right margin to __RMARGIN and return the old value.  */
00168 extern size_t argp_fmtstream_set_rmargin (argp_fmtstream_t __fs,
00169                                           size_t __rmargin);
00170 extern size_t __argp_fmtstream_set_rmargin (argp_fmtstream_t __fs,
00171                                             size_t __rmargin);
00172 
00173 /* Set __FS's wrap margin to __WMARGIN and return the old value.  */
00174 extern size_t argp_fmtstream_set_wmargin (argp_fmtstream_t __fs,
00175                                           size_t __wmargin);
00176 extern size_t __argp_fmtstream_set_wmargin (argp_fmtstream_t __fs,
00177                                             size_t __wmargin);
00178 
00179 /* Return the column number of the current output point in __FS.  */
00180 extern size_t argp_fmtstream_point (argp_fmtstream_t __fs);
00181 extern size_t __argp_fmtstream_point (argp_fmtstream_t __fs);
00182 
00183 /* Internal routines.  */
00184 extern void _argp_fmtstream_update (argp_fmtstream_t __fs);
00185 extern void __argp_fmtstream_update (argp_fmtstream_t __fs);
00186 extern int _argp_fmtstream_ensure (argp_fmtstream_t __fs, size_t __amount);
00187 extern int __argp_fmtstream_ensure (argp_fmtstream_t __fs, size_t __amount);
00188 
00189 #ifdef __OPTIMIZE__
00190 /* Inline versions of above routines.  */
00191 
00192 #if !_LIBC
00193 #define __argp_fmtstream_putc argp_fmtstream_putc
00194 #define __argp_fmtstream_puts argp_fmtstream_puts
00195 #define __argp_fmtstream_write argp_fmtstream_write
00196 #define __argp_fmtstream_set_lmargin argp_fmtstream_set_lmargin
00197 #define __argp_fmtstream_set_rmargin argp_fmtstream_set_rmargin
00198 #define __argp_fmtstream_set_wmargin argp_fmtstream_set_wmargin
00199 #define __argp_fmtstream_point argp_fmtstream_point
00200 #define __argp_fmtstream_update _argp_fmtstream_update
00201 #define __argp_fmtstream_ensure _argp_fmtstream_ensure
00202 #endif
00203 
00204 #ifndef ARGP_FS_EI
00205 #define ARGP_FS_EI extern inline
00206 #endif
00207 
00208 ARGP_FS_EI size_t
00209 __argp_fmtstream_write (argp_fmtstream_t __fs,
00210                         __const char *__str, size_t __len)
00211 {
00212   if (__fs->p + __len <= __fs->end || __argp_fmtstream_ensure (__fs, __len))
00213     {
00214       memcpy (__fs->p, __str, __len);
00215       __fs->p += __len;
00216       return __len;
00217     }
00218   else
00219     return 0;
00220 }
00221 
00222 ARGP_FS_EI int
00223 __argp_fmtstream_puts (argp_fmtstream_t __fs, __const char *__str)
00224 {
00225   size_t __len = strlen (__str);
00226   if (__len)
00227     {
00228       size_t __wrote = __argp_fmtstream_write (__fs, __str, __len);
00229       return __wrote == __len ? 0 : -1;
00230     }
00231   else
00232     return 0;
00233 }
00234 
00235 ARGP_FS_EI int
00236 __argp_fmtstream_putc (argp_fmtstream_t __fs, int __ch)
00237 {
00238   if (__fs->p < __fs->end || __argp_fmtstream_ensure (__fs, 1))
00239     return *__fs->p++ = __ch;
00240   else
00241     return EOF;
00242 }
00243 
00244 /* Set __FS's left margin to __LMARGIN and return the old value.  */
00245 ARGP_FS_EI size_t
00246 __argp_fmtstream_set_lmargin (argp_fmtstream_t __fs, size_t __lmargin)
00247 {
00248   size_t __old;
00249   if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
00250     __argp_fmtstream_update (__fs);
00251   __old = __fs->lmargin;
00252   __fs->lmargin = __lmargin;
00253   return __old;
00254 }
00255 
00256 /* Set __FS's right margin to __RMARGIN and return the old value.  */
00257 ARGP_FS_EI size_t
00258 __argp_fmtstream_set_rmargin (argp_fmtstream_t __fs, size_t __rmargin)
00259 {
00260   size_t __old;
00261   if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
00262     __argp_fmtstream_update (__fs);
00263   __old = __fs->rmargin;
00264   __fs->rmargin = __rmargin;
00265   return __old;
00266 }
00267 
00268 /* Set FS's wrap margin to __WMARGIN and return the old value.  */
00269 ARGP_FS_EI size_t
00270 __argp_fmtstream_set_wmargin (argp_fmtstream_t __fs, size_t __wmargin)
00271 {
00272   size_t __old;
00273   if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
00274     __argp_fmtstream_update (__fs);
00275   __old = __fs->wmargin;
00276   __fs->wmargin = __wmargin;
00277   return __old;
00278 }
00279 
00280 /* Return the column number of the current output point in __FS.  */
00281 ARGP_FS_EI size_t
00282 __argp_fmtstream_point (argp_fmtstream_t __fs)
00283 {
00284   if ((size_t) (__fs->p - __fs->buf) > __fs->point_offs)
00285     __argp_fmtstream_update (__fs);
00286   return __fs->point_col >= 0 ? __fs->point_col : 0;
00287 }
00288 
00289 #if !_LIBC
00290 #undef __argp_fmtstream_putc
00291 #undef __argp_fmtstream_puts
00292 #undef __argp_fmtstream_write
00293 #undef __argp_fmtstream_set_lmargin
00294 #undef __argp_fmtstream_set_rmargin
00295 #undef __argp_fmtstream_set_wmargin
00296 #undef __argp_fmtstream_point
00297 #undef __argp_fmtstream_update
00298 #undef __argp_fmtstream_ensure
00299 #endif
00300 
00301 #endif /* __OPTIMIZE__ */
00302 
00303 #endif /* ARGP_FMTSTREAM_USE_LINEWRAP */
00304 
00305 #endif /* argp-fmtstream.h */

Generated on Fri Oct 5 18:20:25 2007 for WvStreams by  doxygen 1.5.3