1 /* Word-wrapping and line-truncating streams.
2 Copyright (C) 1996, 1997 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 not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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 ssize_t wmargin
; /* Margin to wrap to, or -1 to truncate. */
39 /* Point in stdio buffer to which we've processed for wrapping, but
42 /* Output column at POINT_OFFS, or -1 meaning 0 but don't add lmargin. */
45 /* Original cookie and hooks from the stream. */
47 void (*output
) (FILE *, int);
49 __io_fileno_fn
*fileno
;
53 /* Modify STREAM so that it prefixes lines written on it with LMARGIN spaces
54 and limits them to RMARGIN columns total. If WMARGIN >= 0, words that
55 extend past RMARGIN are wrapped by replacing the whitespace before them
56 with a newline and WMARGIN spaces. Otherwise, chars beyond RMARGIN are
57 simply dropped until a newline. Returns STREAM after modifying it, or
58 NULL if there was an error. */
59 FILE *line_wrap_stream (FILE *stream
,
60 size_t lmargin
, size_t rmargin
, ssize_t wmargin
);
62 /* Remove the hooks placed in STREAM by `line_wrap_stream'. */
63 void line_unwrap_stream (FILE *stream
);
65 /* Returns true if STREAM is line wrapped. */
66 extern inline int line_wrapped (FILE *stream
);
68 /* If STREAM is not line-wrapped return -1, else return its left margin. */
69 extern size_t line_wrap_lmargin (FILE *stream
);
71 /* If STREAM is not line-wrapped return -1, else set its left margin to
72 LMARGIN and return the old value. */
73 extern size_t line_wrap_set_lmargin (FILE *stream
, size_t lmargin
);
75 /* If STREAM is not line-wrapped return -1, else return its left margin. */
76 extern size_t line_wrap_rmargin (FILE *stream
);
78 /* If STREAM is not line-wrapped return -1, else set its right margin to
79 RMARGIN and return the old value. */
80 extern size_t line_wrap_set_rmargin (FILE *stream
, size_t rmargin
);
82 /* If STREAM is not line-wrapped return -1, else return its wrap margin. */
83 extern size_t line_wrap_wmargin (FILE *stream
);
85 /* If STREAM is not line-wrapped return -1, else set its left margin to
86 WMARGIN and return the old value. */
87 extern size_t line_wrap_set_wmargin (FILE *stream
, size_t wmargin
);
89 /* If STREAM is not line-wrapped return -1, else return the column number of
90 the current output point. */
91 extern size_t line_wrap_point (FILE *stream
);
95 extern void __line_wrap_output (FILE *, int); /* private */
97 /* If STREAM is not line-wrapped, return 0. Otherwise all pending text
98 buffered text in STREAM so that the POINT_OFFS field refers to the last
99 position in the stdio buffer, and return the line wrap state object for
100 STREAM. Since all text has been processed, this means that (1) the
101 POINT_COL field refers to the column at which any new text would be added,
102 and (2) any changes to the margin parameters will only affect new text. */
103 extern struct line_wrap_data
*__line_wrap_update (FILE *stream
); /* private */
105 /* Returns true if STREAM is line wrapped. */
107 line_wrapped (FILE *stream
)
109 return (stream
->__room_funcs
.__output
== &__line_wrap_output
);
112 /* If STREAM is not line-wrapped return -1, else return its left margin. */
114 line_wrap_lmargin (FILE *stream
)
116 if (! line_wrapped (stream
))
118 return ((struct line_wrap_data
*)stream
->__cookie
)->lmargin
;
121 /* If STREAM is not line-wrapped return -1, else set its left margin to
122 LMARGIN and return the old value. */
124 line_wrap_set_lmargin (FILE *stream
, size_t lmargin
)
126 struct line_wrap_data
*d
= __line_wrap_update (stream
);
129 size_t old
= d
->lmargin
;
130 d
->lmargin
= lmargin
;
137 /* If STREAM is not line-wrapped return -1, else return its left margin. */
139 line_wrap_rmargin (FILE *stream
)
141 if (! line_wrapped (stream
))
143 return ((struct line_wrap_data
*)stream
->__cookie
)->rmargin
;
146 /* If STREAM is not line-wrapped return -1, else set its right margin to
147 RMARGIN and return the old value. */
149 line_wrap_set_rmargin (FILE *stream
, size_t rmargin
)
151 struct line_wrap_data
*d
= __line_wrap_update (stream
);
154 size_t old
= d
->rmargin
;
155 d
->rmargin
= rmargin
;
162 /* If STREAM is not line-wrapped return -1, else return its wrap margin. */
164 line_wrap_wmargin (FILE *stream
)
166 if (! line_wrapped (stream
))
168 return ((struct line_wrap_data
*)stream
->__cookie
)->wmargin
;
171 /* If STREAM is not line-wrapped return -1, else set its left margin to
172 WMARGIN and return the old value. */
174 line_wrap_set_wmargin (FILE *stream
, size_t wmargin
)
176 struct line_wrap_data
*d
= __line_wrap_update (stream
);
179 size_t old
= d
->wmargin
;
180 d
->wmargin
= wmargin
;
187 /* If STREAM is not line-wrapped return -1, else return the column number of
188 the current output point. */
190 line_wrap_point (FILE *stream
)
192 struct line_wrap_data
*d
= __line_wrap_update (stream
);
193 return d
? (d
->point_col
>= 0 ? d
->point_col
: 0) : -1;
196 #endif /* Optimizing. */
200 #endif /* __LINEWRAP_H__ */