PropertySet.pm untested

filter: Statementstmt 0.0% 0 / 160 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 33 Podpod 0.0% 0 / 30 totaltotal 0.0% 0 / 316 CC 31 SCAR 28.6
CC
31
Cov
0%
_load_ppset_05
72.0
load
30.0
_load_dictionary
20.0
CRAP 17.4
1 #
2 # $Id: PropertySet.pm,v 1.1.1.1 1998/02/25 21:13:00 schwartz Exp $
3 #
4 # OLE::PropertySet
5 #
6 # Copyright (C) 1996, 1997, 1998 Martin Schwartz
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, you should find it at:
20 #
21 # http://wwwwbs.cs.tu-berlin.de/~schwartz/pmh/COPYING
22 #
23 # Contact: schwartz@cs.tu-berlin.de
24 #
25  
26 package OLE::PropertySet;
27 use strict;
28 my $VERSION=do{my@R=('$Revision: 1.1.1.1 $'=~/\d+/g);sprintf"%d."."%d"x$#R,@R};
29  
30 use OLE::Storage::Std;
31 use OLE::Storage::Var();
32  
33 sub Startup { my $S=shift; $S->{STARTUP} = shift if @_; $S->{STARTUP} }
34 sub Var { my $S=shift; $S->{VAR} = shift if @_; $S->{VAR} }
35 sub NewVar { OLE::Storage::Var::new qw(OLE::Storage::Var) }
36  
37 # package globals
38 my %PPSET;
39  
40 sub dictionary { ##
41 #
42 # 1||0 = dictionary(\%dictionary [,$nodefault]);
43 #
44 my ($S, $dictR, $forbid) = @_;
45 $S->_load_dictionary($forbid);
46 %$dictR = %{$S->{DICT}};
47 1}
48  
49 sub exists {
50 my ($S, $id) = @_;
51 defined $S->_prop($id);
52 }
53  
54 sub idset { ##
55 #
56 # 1||0 = idset(\%ppset_idset [,$nodefault]);
57 #
58 my ($S, $idsetR, $forbid) = @_;
59 $S->_load_dictionary($forbid);
60 %{$idsetR}=();
61 for (keys %{$S->_prop}) {
62 $idsetR->{$_} = $S->{DICT}{$_};
63 }
64 1}
65  
66 sub idstr {
67 #
68 # $str||undef = idstr($id [,$nodefault])
69 #
70 my ($S, $id, $forbid) = @_;
71 $S->_load_dictionary($forbid);
72 $S->{DICT}{$id};
73 }
74  
75 sub load {
76 #
77 # $self||0 = 1. load ($Startup, $Var, $pps, $Doc [,$filter])
78 # = 2. load ($Startup, $Var, $name, \$buf [,$filter])
79 #
80 my ($proto, $Startup, $Var, $pps, $Doc, $filter) = @_;
81 my $class = ref($proto) || $proto;
82  
83 my $name;
84 my $Scalar = (ref($Doc) =~ /SCALAR/);
85 if ($Scalar) {
86 $name = $pps;
87 } else {
88 $name = $Doc->name($pps)->string;
89 }
90  
91 my $S = _init($Startup, $Var, $name, _type($name), $filter);
92 bless ($S, $class);
93  
94 return $S->_error("This is not a PropertySet handle.") if !$S->{TYPE};
95  
96 if ($Scalar) {
97 my $bufR = $Doc;
98 $S->{BUF} = $$bufR;
99 } else {
100 $S->{PPS} = $pps;
101 $S->{DOC} = $Doc;
102 return 0 if !$Doc->read($pps, \$S->{BUF});
103 }
104  
105 $S->_load() && $S;
106 }
107  
108 sub property {
109 #
110 # 1. $Property = property($id)
111 # 2. @Properties = property($id1 [,$id2...])
112 #
113 # If an error occurs, an internal error $Property is returned.
114 #
115 my $S = shift;
116 if (!@_) {
117 return $S->_var()->error("No property id given.");
118 } elsif (!wantarray) {
119 return $S->_property(shift());
120 } else {
121 map ($S->_property($_), @_);
122 }
123 }
124  
125 sub type {
126 #
127 # type || 0 = type e {1, 0x10}
128 # 1. type ($Doc, $pps)
129 # 2. type ($name)
130 #
131 my ($Quatsch, $Doc, $pps) = @_;
132 if (defined $pps) {
133 # case 1
134 return 0 if ! $Doc->is_file($pps);
135 return _type($Doc->name($pps)->string());
136 } else {
137 # case 2
138 my $name = $Doc;
139 return _type($name);
140 }
141 }
142  
143 #
144 # --------------------------- private ------------------------------
145 #
146  
147 sub _error { my $S = shift; $S->{STARTUP}->error(@_) if defined $S->{STARTUP} }
148  
149 sub _filter {
150 shift->{FILTER};
151 }
152  
153 sub _init {
154 #
155 # $self||0 = _init ($Startup, $Var, $name, $type [,$filter])
156 #
157 my ($Startup, $Var, $name, $type, $filter) = @_;
158 {
159 STARTUP => $Startup, # Startup object
160 VAR => $Var, # Var object
161 FILTER => $filter, # property filter method
162 # aggregated data
163 PROP => {}, # Property {id}
164 DICT_LOAD => undef,
165 DICT_PROP => {}, # Dictionary of property {id}
166 DICT_DEF => {}, # Default dictionary property {id}
167 DICT => {}, # DICT_PROP || DICT_PROP+DICT_DEF
168 HEADER => {}, # Header data.
169 # IO
170 NAME => $name, # name of propertyset
171 TYPE => $type, # type of PropertySet
172 BUF => undef, # Buffer for PropertySet stream.
173 DOC => undef, # Laola document object
174 PPS => undef, # Property storage handle
175 };
176 }
177  
178 sub _load {
179 #
180 # 1||0 = _load();
181 #
182 my $S = shift;
183 if ($S->{TYPE} == 1) {
184 $S->_load_ppset_05();
185 } elsif ($S->{TYPE} == 0x10) {
186 $S->_load_ppset_CompObj();
187 } else {
188 $S->_error("Unknown property set type!");
189 }
190 }
191  
192 sub _load_dictionary {
193 my ($S, $forbid) = @_;
194  
195 $forbid = 0 if ! defined $forbid;
196 return if defined $S->{DICT_LOAD} && ($S->{DICT_LOAD} == $forbid);
197  
198 if (!$forbid && $PPSET{$S->{NAME}}) {
199 $S->{DICT} = $PPSET{$S->{NAME}}->{DICT};
200 }
201 my ($k, $v);
202 while (($k, $v) = each %{$S->{DICT_PROP}} ) {
203 $S->{DICT}{$k} = $v;
204 }
205 $S->{DICT_LOAD} = $forbid;
206 }
207  
208 sub _property {
209 #
210 # $Property || $ErrorProperty = _property($id)
211 #
212 my ($S, $id) = @_;
213 return $S->_var()->error("Property not available!") if !$S->exists($id);
214 $S->_prop($id);
215 }
216  
217 sub _type {
218 #!
219 my $name = shift;
220 return $PPSET{$name}->{TYPE} if $PPSET{$name};
221 return 1 if $name =~ /^\05/;
222 0}
223  
224 sub _var {
225 shift->{VAR};
226 }
227 sub _prop {
228 my ($S, $key, $val) = @_;
229 $S->{PROP}{$key} = $val if defined $val;
230 return $S->{PROP}{$key} if defined $key;
231 $S->{PROP};
232 }
233  
234 #
235 # ---------------- Flat stream data into system -------------------
236 #
237  
238 sub _load_ppset_CompObj {
239 #
240 # true||0 = _load_ppset_CompObj ()
241 #
242 # Structure:
243 # 00: word uk1 0x0001
244 # 02: word byteorder 0xfffe
245 # 04: word osver lbyte=version hbyte=revision
246 # 06: word os 0=win16 1=mac 2=win32
247 # 08: ff ff ff ff 00 09 02 00 00 00 00 00
248 # c0 00 00 00 00 00 00 46
249 # 1c: word() offsets
250 #
251 my $S = shift;
252 my $bufR = \$S->{BUF};
253 my ($l, $n, $o, $var, $vl);
254  
255 my $H = {
256 BYTEORDER => get_word($bufR, 2),
257 OSVER => get_word($bufR, 4),
258 OS => get_word($bufR, 6),
259 };
260 return $S->_error ("I don't understand the property set.")
261 if ($H->{BYTEORDER} != 0xfffe)
262 ;
263 $S->{HEADER} = $H;
264  
265 get_it: {
266 $o = 0x1c;
267 for (0,1,2) {
268 $S->_prop($_, $S->_var->property($bufR, \$o, 0x1e));
269 }
270 }
271 1}
272  
273 sub _load_ppset_05 {
274 #
275 # true||0 = _load_ppset_05 ()
276 #
277 # Structure:
278 # 00: word byteorder 0xfffe
279 # 02: word format 0
280 # 04: word osver lbyte=version hbyte=revision
281 # 06: word os 0=win16 1=mac 2=win32
282 # 08: clsid class identifier {e.g. @0}
283 # 18: long reserved >=1
284 # 1c: fmtid format identifier
285 # 2c: word offset0 offset of first property chunk
286 #
287 my $S = shift;
288 my $bufR = \$S->{BUF};
289 my ($did, $dname, $fido, $id, $l, $n, $num, $o, $type, @var, $vid, $vo);
290  
291 my $H = {
292 BYTEORDER => get_word($bufR, 0x00),
293 FORMAT => get_word($bufR, 0x02),
294 OSVER => get_word($bufR, 0x04),
295 OS => get_word($bufR, 0x06),
296 CLSID => $S->_var->property($bufR, 0x08, 0x48),
297 RESERVED => get_long($bufR, 0x18),
298 FMTID => $S->_var->property($bufR, 0x1c, 0x48),
299 OFFSET => [ get_word($bufR, 0x2c) ],
300 CODEPAGE => {},
301 LOCALE => {},
302 };
303  
304 return $S->_error("I don't understand the property set.") if
305 ($H->{BYTEORDER} != 0xfffe)
306 || ($H->{FORMAT} != 0)
307 || ($H->{RESERVED} < 1)
308 || (${$H->{OFFSET}}[0] < 0x30)
309 ;
310
311 get_it: {
312 $o = $H->{OFFSET}->[0];
313  
314 for ($n=0; $n < $H->{RESERVED}; $n++) {
315 # default dictionary and codepage
316 $H->{CODEPAGE}->{$n*0x1000+1} = 0x4e4;
317  
318 $num=get_word($bufR, $o+4);
319 for (0 .. $num-1) {
320 $id = get_long($bufR, $o+8+$_*8);
321 if ($n) {
322 $id = $_ if $id>1;
323 }
324  
325 $fido = get_long($bufR, $o+8+$_*8+4);
326 $vid = $n*0x1000 + $id;
327 $vo = $H->{OFFSET}->[$n]+$fido;
328  
329 if ($id==0x80000000) {
330 # locale
331 $H->{LOCALE}->{$vid} = $S->_var->property($bufR, \$vo)
332 -> int()
333 ;
334 } elsif ($id>1) {
335 # read variable
336 $S->_prop($vid, $S->_var->property($bufR, $vo));
337 } elsif ($id==1) {
338 $H->{CODEPAGE}->{$vid} = $S->_var->property($bufR, \$vo)
339 -> int()
340 ;
341 #printf "Codepage %d\n", $H->{CODEPAGE}->{$vid};
342 } elsif ($id==0) {
343 # read dictionary
344 for (1..get_long($bufR, \$vo)) {
345 $did = get_long($bufR, \$vo);
346 $S->{DICT_PROP}{$did+$n*0x1000} =
347 $S->_var->property($bufR, \$vo, 0x1e) -> string()
348 ;
349 }
350 }
351 }
352 $o+=get_word($bufR, $o);
353 $H->{OFFSET}->[$n+1]=$o;
354 }
355 }
356 $S->{HEADER} = $H;
357 1}
358  
359 ##
360 ## ---------------------------- Tie Support -------------------------------
361 ##
362  
363 sub TIEHASH {
364 #
365 # $PropertySet||0 = TIEHASH $classname, $Startup, $Var, $Doc, $pps [,$filter]
366 #
367 goto &load;
368 }
369  
370 sub CLEAR{0}
371 sub DELETE{0}
372 sub DESTROY{0}
373 sub STORE{0}
374  
375 sub FETCH {
376 #
377 # FETCH this, key
378 #
379 my $S = shift;
380 if (my $filter = $S->_filter()) {
381 $S -> property (@_) -> $filter();
382 } else {
383 $S -> property (@_);
384 }
385 }
386  
387 sub EXISTS {
388 goto &exists;
389 }
390  
391 sub FIRSTKEY {
392 my $S = shift;
393 keys %{$S->_prop};
394 $S->NEXTKEY();
395 }
396  
397 sub NEXTKEY {
398 #
399 # NEXTKEY this, lastkey
400 #
401 scalar each %{$_[0]->_prop};
402 }
403  
404 #
405 # -------------------------------- Defines --------------------------------
406 #
407  
408 # global_definitions
409 $[=0;
410  
411 # \05
412 %PPSET = (
413 "\05SummaryInformation" => {
414 TYPE => 1,
415 NAME => "Summary Info",
416 DICT => {
417 2 => "Title",
418 3 => "Subject",
419 4 => "Authress",
420 5 => "Keywords",
421 6 => "Comments",
422 7 => "Template",
423 8 => "LastAuthress",
424 9 => "Revision",
425 10 => "EditTime",
426 11 => "LastPrinted",
427 12 => "Created",
428 13 => "LastSaved",
429 14 => "Pages",
430 15 => "Words",
431 16 => "Chars",
432 17 => "Thumbnail",
433 18 => "Application",
434 19 => "Security"
435 },
436 GUID => [
437 0xf29f85e0, 0x4ff9, 0x1068,
438 "\0xab\0x91\0x08\0x00\0x2b\0x27\0xb3\0xd9"
439 ]
440 },
441 "\05DocumentSummaryInformation" => {
442 TYPE => 1,
443 NAME => "Document Summary Info",
444 DICT => {
445 2 => "Category",
446 3 => "PresentationTarget",
447 4 => "Bytes",
448 5 => "Lines",
449 6 => "Paragraphs",
450 7 => "Slides",
451 8 => "Notes",
452 9 => "HiddenSlides",
453 10 => "MMClips",
454 11 => "ScaleCrop",
455 12 => "HeadingPairs",
456 13 => "TitlesOfParts",
457 14 => "Manager",
458 15 => "Company",
459 16 => "LinksUpToDate",
460 },
461 GUID => [
462 0xd5cdd502, 0x2e9c, 0x101b,
463 "\0x93\0x97\0x08\0x00\0x2b\0x2c\0xf9\0xae"
464 ],
465 USERGUID => [
466 0xd5cdd505, 0x2e9c, 0x101b,
467 "\0x93\0x97\0x08\0x00\0x2b\0x2c\0xf9\0xae"
468 ]
469 },
470 "\01CompObj" => {
471 TYPE => 0x10,
472 NAME => "Compound Object Info",
473 DICT => {
474 0 => "doc_long",
475 1 => "doc_class",
476 2 => "doc_spec"
477 }
478 }
479 );
480  
481 "Atomkraft? Nein, danke!"
482  
483 __END__