1 /* Word-wrapping and line-truncating streams.
2 Copyright (C) 1996 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If
17 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18 Cambridge, MA 02139, USA. */
20 #ifndef __LINEWRAP_H__
21 #define __LINEWRAP_H__
29 #include <string.h> /* Need size_t. */
33 /* We keep this data for each line-wrapping stream. */
36 size_t lmargin
, rmargin
; /* Left and right margins. */
37 size_t wmargin
; /* Margin to wrap to, or -1 to truncate. */
38 size_t point
; /* Current column of last chars flushed. */
40 /* Original cookie and hooks from the stream. */
42 void (*output
) (FILE *, int);
44 __io_fileno_fn
*fileno
;
48 /* Modify STREAM so that it prefixes lines written on it with LMARGIN spaces
49 and limits them to RMARGIN columns total. If WMARGIN >= 0, words that
50 extend past RMARGIN are wrapped by replacing the whitespace before them
51 with a newline and WMARGIN spaces. Otherwise, chars beyond RMARGIN are
52 simply dropped until a newline. Returns STREAM after modifying it, or
53 NULL if there was an error. */
54 FILE *line_wrap_stream (FILE *stream
,
55 size_t lmargin
, size_t rmargin
, size_t wmargin
);
57 /* Remove the hooks placed in STREAM by `line_wrap_stream'. */
58 void line_unwrap_stream (FILE *stream
);
60 /* Returns true if STREAM is line wrapped. */
61 extern inline int line_wrapped (FILE *stream
);
63 /* If STREAM is not line-wrapped return -1, else return its left margin. */
64 extern size_t line_wrap_lmargin (FILE *stream
);
66 /* If STREAM is not line-wrapped return -1, else set its left margin to
67 LMARGIN and return the old value. */
68 extern size_t line_wrap_set_lmargin (FILE *stream
, size_t lmargin
);
70 /* If STREAM is not line-wrapped return -1, else return its left margin. */
71 extern size_t line_wrap_rmargin (FILE *stream
);
73 /* If STREAM is not line-wrapped return -1, else set its right margin to
74 RMARGIN and return the old value. */
75 extern size_t line_wrap_set_rmargin (FILE *stream
, size_t rmargin
);
77 /* If STREAM is not line-wrapped return -1, else return its wrap margin. */
78 extern size_t line_wrap_wmargin (FILE *stream
);
80 /* If STREAM is not line-wrapped return -1, else set its left margin to
81 WMARGIN and return the old value. */
82 extern size_t line_wrap_set_wmargin (FILE *stream
, size_t wmargin
);
84 /* If STREAM is not line-wrapped return -1, else return the column number of
85 the current output point. */
86 extern size_t line_wrap_point (FILE *stream
);
91 extern void __line_wrap_output (FILE *, int); /* private */
93 /* Returns true if STREAM is line wrapped. */
95 line_wrapped (FILE *stream
)
97 return (stream
->__room_funcs
.__output
== &__line_wrap_output
);
100 /* If STREAM is not line-wrapped return -1, else return its left margin. */
102 line_wrap_lmargin (FILE *stream
)
104 if (! line_wrapped (stream
))
106 return ((struct line_wrap_data
*)stream
->__cookie
)->lmargin
;
109 /* If STREAM is not line-wrapped return -1, else set its left margin to
110 LMARGIN and return the old value. */
112 line_wrap_set_lmargin (FILE *stream
, size_t lmargin
)
114 if (! line_wrapped (stream
))
118 struct line_wrap_data
*d
= stream
->__cookie
;
119 size_t old
= d
->lmargin
;
120 d
->lmargin
= lmargin
;
125 /* If STREAM is not line-wrapped return -1, else return its left margin. */
127 line_wrap_rmargin (FILE *stream
)
129 if (! line_wrapped (stream
))
131 return ((struct line_wrap_data
*)stream
->__cookie
)->rmargin
;
134 /* If STREAM is not line-wrapped return -1, else set its right margin to
135 RMARGIN and return the old value. */
137 line_wrap_set_rmargin (FILE *stream
, size_t rmargin
)
139 if (! line_wrapped (stream
))
143 struct line_wrap_data
*d
= stream
->__cookie
;
144 size_t old
= d
->rmargin
;
145 d
->rmargin
= rmargin
;
150 /* If STREAM is not line-wrapped return -1, else return its wrap margin. */
152 line_wrap_wmargin (FILE *stream
)
154 if (! line_wrapped (stream
))
156 return ((struct line_wrap_data
*)stream
->__cookie
)->wmargin
;
159 /* If STREAM is not line-wrapped return -1, else set its left margin to
160 WMARGIN and return the old value. */
162 line_wrap_set_wmargin (FILE *stream
, size_t wmargin
)
164 if (! line_wrapped (stream
))
168 struct line_wrap_data
*d
= stream
->__cookie
;
169 size_t old
= d
->wmargin
;
170 d
->wmargin
= wmargin
;
175 /* If STREAM is not line-wrapped return -1, else return the column number of
176 the current output point. */
178 line_wrap_point (FILE *stream
)
180 if (! line_wrapped (stream
))
182 return ((struct line_wrap_data
*)stream
->__cookie
)->point
;
185 #endif /* Optimizing. */
189 #endif /* __LINEWRAP_H__ */