| verzia 1.1, 2002/03/19 21:03:12 |
verzia 1.2, 2002/04/13 22:49:48 |
|
|
| <? |
<?
|
| /* |
/*
|
| Module: frloprint.php |
Module: frloprint.php
|
| |
|
| Author: Martin Karas a.k.a. Wolcano |
Author: Martin Karas a.k.a. Wolcano
|
| Date: 19.03.2002 |
Date: 13.04.2002
|
| Mail: wolcano@pobox.sk |
Mail: wolcano@pobox.sk
|
| Version: 3.0 |
Version: 3.1
|
| License: GNU-GPL |
License: GNU-GPL
|
| Description: Processes input, to be more printable/readable. Wraps the |
Description: Processes input, to be more printable/readable. Wraps the
|
| text and flushes it into several columns on one page. Based on idea of |
text and flushes it into several columns per page. Based on idea of
|
| FRLOs program frloprint (DOS-exe). |
FRLOs program frloprint (DOS-exe).
|
| |
|
| TODO: |
TODO:
|
| * implement multiline page-separator |
* remove in_line_separator EREG pattern restrictions
|
| * remove in_line_separator restrictions |
* add printer handling
|
| * add printer-writing |
|
| */ |
Change Log:
|
| |
3.1: Minor bug-fix in processing empty lines
|
| |
Added multi-line page separator
|
| ////////////// |
Now user-definable column padding character
|
| // Settings // |
3.0: Public release (via CVS - see www.platon.sk)
|
| ////////////// |
*/
|
| |
|
| /* INPUT/OUTPUT SETTINGS |
|
| use "php://stdin", "php://stdout", "php://stderr" |
//////////////
|
| "http://host/path/file" or "ftp://user:pass@host/file" |
// Settings //
|
| */ |
//////////////
|
| $in_file = "in.txt"; |
|
| $out_file = "out.txt"; |
/* INPUT/OUTPUT SETTINGS
|
| // in_line_separator is used in ereg [] ranges |
use "php://stdin", "php://stdout", "php://stderr"
|
| // should not include '-' character |
"http://host/path/file" or "ftp://user:pass@host/file"
|
| // should be backslashed |
*/
|
| $in_line_separator = chr(13).chr(10); |
$in_file = "in.txt";
|
| $out_line_separator = chr(13).chr(10); |
$out_file = "out.txt";
|
| |
|
| /* PAGE SIZE SETTINGS */ |
/* LINE SEPARATOR SETTINGS */
|
| $rows = 66; |
/*
|
| $cols = 2; |
in_line_separator is used in ereg [] ranges,
|
| $append_space = 4; // (text is more readible on blocks of dense text input) |
should not include '-' character, should be backslashed
|
| $col_width = 40; |
*/
|
| |
$in_line_separator = chr(13).chr(10);
|
| /* SUB-PAGE SEPARATORS */ |
$out_line_separator = chr(13).chr(10);
|
| $use_col_separator = false; |
|
| $use_row_separator = false; |
/* PAGE SIZE SETTINGS */
|
| $col_separator = "|"; |
$rows = 66;
|
| $row_separator = "-"; |
$cols = 2;
|
| |
$col_width = 80;
|
| /* INPUT AUTO-CONVERT */ |
$col_padding_width = 4; // (text is more readible on blocks of dense text input)
|
| $wrap = 1; |
$col_padding_char = " ";
|
| $replace_non_chars = true; |
|
| $non_chars_to = " "; |
/* SUB-PAGE SEPARATORS */
|
| $replace_tab = true; |
$use_col_separator = false;
|
| $tabs_to_space = " "; |
$use_row_separator = false;
|
| |
$col_separator = "|";
|
| /////////////////////// |
$row_separator = array("-End of page-");
|
| // Support functions // |
$row_separator_padding_char = "-"; //appended to row_separator if its shorter than col width
|
| /////////////////////// |
|
| |
/* INPUT AUTO-CONVERT */
|
| function add_char($string, $char, $final_width) { |
$auto_wrap = 1;
|
| while(strlen($string) < $final_width) |
$replace_non_chars = true;
|
| $string .= $char; |
$non_chars_to = " ";
|
| return $string; |
$replace_tab = true;
|
| } // add_char($string, $char, $final_width) |
$tabs_to_space = " ";
|
| |
|
| function add_spaces($string, $width) { |
///////////////////////
|
| while(strlen($string) < $width) |
// Support functions //
|
| $string .= " "; |
///////////////////////
|
| return $string; |
|
| } // add_spaces($string, $width) |
function add_char($string, $char, $final_width) {
|
| |
while(strlen($string) < $final_width)
|
| function wrap_lines($a, $width) { |
$string .= $char;
|
| $j = 0; |
return $string;
|
| $ret = array(); |
} // add_char($string, $char, $final_width)
|
| for($i = 0; $i < sizeof($a); $i++) { |
|
| $tmp = rtrim($a[$i]); |
function add_spaces($string, $width) {
|
| $left = 0; |
while(strlen($string) < $width)
|
| while ($left < (strlen($tmp) - 1)) { |
$string .= " ";
|
| $ret[$j] = rtrim(substr($tmp, $left, $width)); |
return $string;
|
| $left += $width; |
} // add_spaces($string, $width)
|
| $j++; |
|
| } |
function wrap_lines($a, $width) {
|
| } |
$j = 0;
|
| return $ret; |
$ret = array();
|
| } // wrap_lines($a, $width) |
for($i = 0; $i < sizeof($a); $i++) {
|
| |
$tmp = $a[$i];
|
| |
for ($left = 0;;) {
|
| /////////////////// |
$ret[$j] = rtrim(substr($tmp, $left, $width));
|
| // MAIN FUNCTION // |
$left += $width;
|
| /////////////////// |
$j++;
|
| |
if ($left >= (strlen($tmp) - 1))
|
| /* Initialise */ |
break;
|
| |
}
|
| if ($use_col_separator) |
}
|
| $col_width - strlen($col_separator); |
return $ret;
|
| if ($use_row_separator) |
} // wrap_lines($a, $width)
|
| $rows--; |
|
| $text_width = $col_width - $append_space; |
|
| $out = array(); |
///////////////////
|
| |
// MAIN FUNCTION //
|
| $fin = fopen($in_file, "rb"); |
///////////////////
|
| $fout = fopen($out_file, "w"); |
|
| |
/* Initialise */
|
| $row = 0; |
|
| $col = 0; |
if ($use_col_separator)
|
| |
$col_width - strlen($col_separator);
|
| while (!feof($fin)) { |
if ($use_row_separator)
|
| // Here should be used explode(), but that function creates |
$rows - substr_count($row_separator, $in_line_separator);
|
| // greater array (by one item). Now we've got a problem if the |
$text_width = $col_width - $col_padding_width;
|
| // line is longer than 4096 chars |
|
| //$in = explode($in_line_separator, $in); |
$fin = fopen($in_file, "rb");
|
| $in = fgets($fin, 4096); |
$fout = fopen($out_file, "w");
|
| |
|
| if ($replace_tab) |
$out = array();
|
| $in = str_replace(chr(9), $tabs_to_space, $in); |
$row = 0;
|
| if ($replace_non_chars) |
$col = 0;
|
| $in = ereg_replace("[^ -\t$in_line_separator]", $non_chars_to, $in); |
|
| |
while (!feof($fin)) {
|
| $in = array($in); |
// Here should be used explode(), but that function creates
|
| if ($wrap) |
// greater array (by one item). Now we've got a problem if the
|
| $in = wrap_lines($in, $text_width); |
// line is longer than 4096 chars
|
| |
//$in = explode($in_line_separator, $in);
|
| for($i = 0; $i < sizeof($in); $i++) { |
$in = fgets($fin, 4096);
|
| if ($col != 0) { |
|
| $out[$row] = rtrim(add_spaces($out[$row], $col * $col_width) . $in[$i]); |
if ($replace_tab)
|
| } |
$in = str_replace(chr(9), $tabs_to_space, $in);
|
| else { |
if ($replace_non_chars)
|
| // first column |
$in = ereg_replace("[^ -\t$in_line_separator]", $non_chars_to, $in);
|
| $out[$row] = $in[$i]; |
|
| } |
$in = array($in);
|
| if ($use_col_separator) |
if ($auto_wrap)
|
| $out[$row] .= add_spaces("", |
$in = wrap_lines($in, $text_width);
|
| ($col + 1) * $col_width - strlen($out[$row]) - strlen($col_separator) |
|
| ) . $col_separator; |
for($i = 0; $i < sizeof($in); $i++) {
|
| $row++; |
if ($col == 0) {
|
| $row = $row % $rows; |
// first column
|
| |
$out[$row] = $in[$i];
|
| if ($row == 0) { |
}
|
| if ($use_row_separator) |
else {
|
| $out[$rows] .= add_char("", $row_separator, $col_width); |
$out[$row] = rtrim(
|
| |
add_char($out[$row], $col_padding_char, $col * $col_width) .
|
| $col++; |
$in[$i]);
|
| $col = $col % $cols; |
}
|
| if ($col == 0) { |
if ($use_col_separator)
|
| fwrite($fout, implode($out, $out_line_separator) . |
$out[$row] .= add_char("", $col_padding_char,
|
| $out_line_separator); |
($col + 1) * $col_width - strlen($out[$row]) - strlen($col_separator)
|
| $out = array(); |
) . $col_separator;
|
| } |
$row++;
|
| } |
$row = $row % $rows;
|
| } |
|
| } |
if ($row == 0) {
|
| |
if ($use_row_separator)
|
| fclose($fin); |
for ($j = 0; $j < sizeof($row_separator); $j++)
|
| fwrite($fout, implode($out, $out_line_separator) . |
$out[$rows + $j] .= add_char($row_separator[$j], $row_separator_padding_char, $col_width);
|
| $out_line_separator); |
|
| fclose($fout); |
$col++;
|
| ?> |
$col = $col % $cols;
|
| |
if ($col == 0) {
|
| |
fwrite($fout, implode($out, $out_line_separator) . $out_line_separator);
|
| |
$out = array();
|
| |
}
|
| |
}
|
| |
}
|
| |
}
|
| |
|
| |
/*
|
| |
finally flush the accumulator, if anything was left in it and
|
| |
close all open files
|
| |
*/
|
| |
fclose($fin);
|
| |
fwrite($fout, implode($out, $out_line_separator) . $out_line_separator);
|
| |
fclose($fout);
|
| |
?>
|