Storage/Textutil.pm untested

filter: Statementstmt 0.0% 0 / 147 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 24 Podpod 0.0% 0 / 23 totaltotal 0.0% 0 / 252 CC 25 SCAR 33.7
CC
25
Cov
0%
_column
110.0
next_white
20.0
new
12.0
CRAP 29.2
1 #
2 # $Id: Textutil.pm,v 1.1.1.1 1998/02/25 21:13:00 schwartz Exp $
3 #
4 # *Experimentary* package, handles text format documents.
5 #
6 # Don't use it in its current state! No documentation, therefore!
7 #
8 # It is actually part of Elser, a program to handle word 6 documents, but
9 # Elser is not yet ported to perl 5.
10 #
11 # Copyright (C) 1996, 1997, 1998 Martin Schwartz
12 #
13 # This program is free software; you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation; either version 2 of the License, or
16 # (at your option) any later version.
17 #
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 # GNU General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, you should find it at:
25 #
26 # http://wwwwbs.cs.tu-berlin.de/~schwartz/pmh/COPYING
27 #
28 # You can contact me via schwartz@cs.tu-berlin.de
29 #
30  
31 package OLE::Storage::Textutil;
32 use strict;
33  
34 sub new {
35 my ($proto, $parH) = @_;
36 my $S = bless ({}, ref($proto) || $proto);
37 $S->width (-1);
38 $S->white ([" "]);
39 $S->hyphen ("-");
40 $S->newline ("\n");
41 $S->newpar ("\n");
42 $S->pardel ("\x0d");
43 $S->tabdel ("\x09");
44 $S->mode (0);
45 if ($parH) {
46 $S -> Startup ($parH->{"STARTUP"}) if $parH->{"STARTUP"};
47 }
48 $S;
49 }
50
51 sub _member { my $S=shift; my $n=shift if @_; $S->{$n}=shift if @_; $S->{$n}}
52  
53 sub width { shift->_member("WIDTH", @_) }
54 sub white { shift->_member("WHITE", @_) }
55 sub hyphen { shift->_member("HYPHEN", @_) }
56 sub newline { shift->_member("NL", @_) }
57 sub newpar { shift->_member("NP", @_) }
58 sub pardel { shift->_member("PDEL", @_) }
59 sub tabdel { shift->_member("TDEL", @_) }
60 sub mode { shift->_member("MODE", @_) }
61  
62 ##
63 ## --- Module Interfaces --------------------------------------------------
64 ##
65  
66 sub Startup { shift->_member("STARTUP", @_) }
67  
68 sub _error { my $S=shift; $S->{STARTUP} ? $S->{STARTUP}->error(@_) : 0 }
69 sub _msg { my $S=shift; $S->Startup ? $S->Startup->msg(@_) : 0 }
70  
71 ##
72 ## --- Code ---------------------------------------------------------------
73 ##
74  
75 sub wrap {
76 #
77 # 1||0 == wrap(\$buf)
78 #
79 # mode 0 Keep a long line, if a word is longer than a line.
80 # 1 Break a word with a $hyphen, if it is longer than a line.
81 #
82 my ($S, $bufR, $mode) = @_;
83 my $par = $S -> pardel();
84 my $len;
85 my @Tab = ();
86 my @Len = ();
87 for (0 .. split(/$par/, $$bufR, -1)) {
88 push (@Len, $len=length($_[$_]));
89 push (@Tab, $S->tab_pos(\$_[$_], $len));
90 # missing: tab handling
91 $S->_column(\$_[$_], $S->width, $len);
92 }
93 #$S->print_statistic(\@Tab, \@Len);
94 $$bufR = join($S->newpar, @_);
95 1}
96  
97 sub _column {
98 #
99 my ($S, $bufR, $w, $l) = @_;
100  
101 if ($w==1) {
102 # Special case: width == 1
103 $$bufR = join ($S->newline(), split(//, $$bufR));
104 return 1;
105 } elsif ($w<0) {
106 # Special case: invalid width
107 return $S->_error ("Cannot handle negative width.");
108 }
109 my ($mpos, $mlen, $l1);
110 my $pos = 0;
111 while (($pos+$w)<$l) {
112 my $status = 0;
113 ($mpos, $mlen) = $S->match_white($bufR, $pos, $w);
114 if ($mpos < 0) {
115 my $sep = $S->mode();
116 if ($sep==1) {
117 ($mpos, $status) = $S->sep_lite($bufR, $pos, $w);
118 $mpos = $l if $mpos > $l;
119 } elsif (ref($sep)) {
120 # 2do
121 }
122 if ((!$sep) || ($mpos<0)) {
123 # No line breaks made. Leave overlong lines.
124 $pos = $S->next_white($bufR, $pos)-$w+1;
125 next;
126 }
127 }
128 $l1 = "";
129 if ($status) {
130 $l1 .= $S->hyphen() if $status == 1;
131 $mlen = 0;
132 };
133 $l1 .= $S->newline();
134 substr($$bufR, $mpos, $mlen) = $l1;
135 $l += length($l1) - $mlen;
136 $pos = $mpos+length($l1);
137 }
138 1}
139  
140 sub sep_lite {
141 #
142 # 0||1||2 = $S->sep_lite($bufR, $pos, $free)
143 #
144 my ($S, $bufR, $pos, $free) = @_;
145 my $mpos = $pos+$free-1;
146 if (substr($$bufR, $mpos-1, 1) =~ /[a-zA-ZÄÖÜäöüß]/) {
147 return ($mpos, 1);
148 } else {
149 return ($mpos, 2);
150 }
151 }
152  
153 sub next_paragraph {
154 my ($S, $bufR, $pos) = @_;
155 index($$bufR, $S->pardel(), $pos);
156 }
157  
158 sub next_tab {
159 my ($S, $bufR, $pos) = @_;
160 index($$bufR, $S->tabdel(), $pos);
161 }
162  
163 sub next_word {
164 my ($S, $bufR, $pos) = @_;
165 my $end = $S->next_white($bufR, $pos);
166 substr($$bufR, $pos, $end-$pos);
167 }
168  
169 sub next_white {
170 #
171 # $pos = $S -> next_white ($bufR, $pos)
172 #
173 my ($S, $bufR, $pos) = @_;
174 my $us = 0;
175 my $os = 0xffffffff;
176 for (@{$S->white()}) {
177 $us = index($$bufR, $_, $pos);
178 next if $us == -1;
179 $os = $us if $us < $os;
180 }
181 $os = length($$bufR) if $os == -1;
182 $os;
183 }
184  
185 sub match_white {
186 #
187 # ($breakpos, $breaklen) = $S -> match_white ($bufR, $pos, $width)
188 #
189 my ($S, $bufR, $pos, $w) = @_;
190  
191 my $min = $pos;
192 my $max = $min + $w -1;
193  
194 my $us;
195 my $os = $min;
196 for (@{$S->white()}) {
197 $us = rindex($$bufR, $_, $max);
198 $os = $us if $us > $os;
199 }
200 return -1 if $os <= $min;
201  
202 my ($left, $right, $l, $flag);
203 $right = $os;
204 $flag=0;
205 while (!$flag) {
206 $flag = 1;
207 for (@{$S->white()}) {
208 $l = length($_);
209 while (substr($$bufR, $right, $l) =~ /$_/) {
210 $right += $l; $flag=0;
211 }
212 }
213 }
214  
215 $left=$os;
216 $flag=0;
217 while (!$flag) {
218 $flag = 1;
219 for (@{$S->white()}) {
220 $l = length($_);
221 while (substr($$bufR, $left-$l, $l) =~ /$_/) {
222 $left -= $l; $flag=0;
223 }
224 }
225 }
226  
227 ($left, $right-$left);
228 }
229  
230 ##
231 ## --- Tabulators ----------------------------------------------------------
232 ##
233  
234 sub tab_pos {
235 my ($S, $bufR, $l) = @_;
236  
237 my $tpos = -1;
238 my @tabs = ();
239  
240 while( $tpos < $l ) {
241 $tpos = $S -> next_tab ($bufR, $tpos+1);
242 last if ($tpos > $l);
243 last if ($tpos < 0);
244 push (@tabs, $tpos);
245 }
246  
247 \@tabs;
248 }
249  
250 sub print_statistic {
251 my ($S, $LT, $LL) = @_;
252 print "\nTabulator statistic:\n";
253 for (0 .. $#$LT) {
254 if (defined $LT->[$_]) {
255 printf " %03d (%3d): " . "%d " x ($#{$LT->[$_]}+1) . "\n",
256 $_, $LL->[$_], @{$LT->[$_]}
257 ;
258 } else {
259 printf(" %03d (%03d)\n", $_, $LL->[$_]);
260 }
261 }
262 print "\n";
263 }
264  
265 "Atomkraft? Nein, danke!";
266  
267 __END__