2N/A/* Word-wrapping and line-truncating streams 2N/A Copyright (C) 1997-1999, 2001-2003, 2005, 2009-2010 Free Software 2N/A This file is part of the GNU C Library. 2N/A Written by Miles Bader <miles@gnu.ai.mit.edu>. 2N/A This program is free software: you can redistribute it and/or modify 2N/A it under the terms of the GNU General Public License as published by 2N/A the Free Software Foundation; either version 3 of the License, or 2N/A (at your option) any later version. 2N/A This program is distributed in the hope that it will be useful, 2N/A but WITHOUT ANY WARRANTY; without even the implied warranty of 2N/A MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 2N/A GNU General Public License for more details. 2N/A You should have received a copy of the GNU General Public License 2N/A/* This package emulates glibc `line_wrap_stream' semantics for systems that 2N/A/* Return an argp_fmtstream that outputs to STREAM, and which prefixes lines 2N/A written on it with LMARGIN spaces and limits them to RMARGIN columns 2N/A total. If WMARGIN >= 0, words that extend past RMARGIN are wrapped by 2N/A replacing the whitespace before them with a newline and WMARGIN spaces. 2N/A Otherwise, chars beyond RMARGIN are simply dropped until a newline. 2N/A Returns NULL if there was an error. */ 2N/A/* Flush FS to its stream, and free it (but don't close the stream). */ 2N/A/* Process FS's buffer so that line wrapping is done from POINT_OFFS to the 2N/A /* Scan the buffer for newlines. */ 2N/A /* We are starting a new line. Print spaces to the left margin. */ 2N/A /* We can fit in them in the buffer by moving the 2N/A buffer text up and filling in the beginning. */ 2N/A fs->p +=
pad;
/* Compensate for bigger buffer. */ 2N/A buf +=
pad;
/* Don't bother searching them. */ 2N/A /* No buffer space for spaces. Must flush. */ 2N/A /* The buffer ends in a partial line. */ 2N/A /* The remaining buffer text is a partial line and fits 2N/A within the maximum line width. Advance point for the 2N/A characters to be written and stop scanning. */ 2N/A /* Set the end-of-line pointer for the code below to 2N/A the end of the buffer. */ 2N/A /* The buffer contains a full line that fits within the maximum 2N/A line width. Reset point and scan the next line. */ 2N/A /* This line is too long. */ 2N/A /* Truncate the line by overwriting the excess with the 2N/A newline and anything after it in the buffer. */ 2N/A /* Reset point for the next line and start scanning it. */ 2N/A buf += r +
1;
/* Skip full line plus \n. */ 2N/A /* The buffer ends with a partial line that is beyond the 2N/A maximum line width. Advance point for the characters 2N/A written, and discard those past the max from the buffer. */ 2N/A /* Do word wrap. Go to the column just past the maximum line 2N/A width and scan back for the beginning of the word there. 2N/A Then insert a line break. */ 2N/A /* Swallow separating blanks. */ 2N/A nl = p +
1;
/* The newline will replace the first blank. */ 2N/A /* A single word that is greater than the maximum line width. 2N/A Oh well. Put it on an overlong line by itself. */ 2N/A /* Find the end of the long word. */ 2N/A /* It already ends a line. No fussing required. */ 2N/A /* We will move the newline to replace the first blank. */ 2N/A /* Swallow separating blanks. */ 2N/A /* The next line will start here. */ 2N/A /* Note: There are a bunch of tests below for 2N/A NEXTLINE == BUF + LEN + 1; this case is where NL happens to fall 2N/A at the end of the buffer, and NEXTLINE is in fact empty (and so 2N/A we need not be careful to maintain its contents). */ 2N/A /* The margin needs more blanks than we removed. */ 2N/A /* Make some space for them. */ 2N/A /* Output the first line so we can use the space. */ 2N/A /* We can fit the newline and blanks in before 2N/A /* Add blanks up to the wrap margin column. */ 2N/A /* Copy the tail of the original buffer into the current buffer 2N/A /* Continue the scan on the remaining lines in the buffer. */ 2N/A /* Restore bufp to include all the remaining text. */ 2N/A /* Reset the counter of what has been output this line. If wmargin 2N/A is 0, we want to avoid the lmargin getting added, so we set 2N/A point_col to a magic value of -1 in that case. */ 2N/A /* Remember that we've scanned as far as the end of the buffer. */ 2N/A/* Ensure that FS has space for AMOUNT more bytes in its buffer, either by 2N/A growing the buffer, or by flushing it. True is returned iff we succeed. */ 2N/A /* Flush FS's buffer. */ 2N/A /* Gotta grow the buffer. */ 2N/A#
endif /* !ARGP_FMTSTREAM_USE_LINEWRAP */