Back to index

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