/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ /* reflow.c by alan partis, thundernet development group */ /* (c) copyright 2002. all rights reserved. */ /* */ /* Reflows long lines in the input stream to a given column. Lines are */ /* split on the last white space before the 'wrap' column. If no white */ /* space appears on a line, it will be split at the wrap column. */ /* */ /* args: */ /* n reflow column */ /* */ /* usage: reflow */ /* */ /* Functions: */ /* main() program entry point */ /* */ /* Date Who Comment */ /* ----------------------------------------------------------------------- */ /* 22feb02 acp initial creation */ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ #include #include #include #include void reflow(FILE *in, FILE *out, int wrap); /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ /* main() by alan partis, thundernet development group */ /* (c) copyright 2002. all rights reserved. */ /* */ /* Program entry point. Executes program algorithm. */ /* */ /* args: argc number of command line arguments */ /* argv array of command line argument strings */ /* */ /* return: int program exit status -- 0 on success, */ /* */ /* Date who Comments */ /* ----------------------------------------------------------------------- */ /* 22feb02 acp initial creation */ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ int main (int argc, char *argv[]) { int rc = EXIT_SUCCESS; if (argc == 2) { reflow(stdin, stdout, atoi(argv[1])); } else { printf("usage\n\tentab \n"); } return rc; } /* end of function main() */ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ /* reflow() by alan partis, thundernet development group */ /* (c) copyright 2002. all rights reserved. */ /* */ /* From the given input stream, reflows long lines in the input stream on */ /* the given column and writes them to the output stream. */ /* */ /* args: in FILE input stream */ /* out FILE output stream */ /* wrap column at which to wrap */ /* */ /* return: none */ /* */ /* Date who Comments */ /* ----------------------------------------------------------------------- */ /* 22feb02 acp initial creation */ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ void reflow(FILE *in, FILE *out, int wrap) { char *buffer; char c; int column; int i, j; int last_whitespace; int leftover; // // initial setup // buffer = malloc(wrap + 1); memset(buffer, 0, wrap + 1); last_whitespace = column = 0; while ((c = getc(in)) != EOF) { buffer[column] = c; column++; if ((c == LF) || (c == CR)) { // // end of line found ... go ahead and dump // out the buffer and go to the next line. // i = 0; while (buffer[i] != 0) { putc(buffer[i++], out); } memset(buffer, 0, wrap + 1); last_whitespace = column = 0; } else if (column >= wrap) { // // time to execute a wrap // if (last_whitespace > 0) { for (i = 0; i < last_whitespace; i++) { putc(buffer[i], out); } leftover = column - i; putc(LF, out); for (j = 0; j < (wrap + 1); j++) { buffer[j] = (i <= column) ? buffer[i++] : 0; } last_whitespace = 0; column = leftover; } else { i = 0; while (buffer[i]) putc(buffer[i++], out); putc(LF, out); memset(buffer, 0, wrap + 1); last_whitespace = column = 0; } } else if (isspace(c)) { // update the whitespace pointer last_whitespace = column; } } free(buffer); } /* end of function reflow() */
Copyright © Thundernet Development Group Inc., 2003. All rights reserved. |