4 # Copyright (C) 2002 Jan Kratochvil <short@ucw.cz>
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 use vars qw($VERSION);
23 $VERSION=do { my @r=(q$Revision$=~/\d+/g); sprintf "%d.".("%03d"x$#r),@r; };
40 my($self,$data,@headers)=@_;
42 my $re=join("(.*?)(\r\n)?",map("\Q[$_]\E\r\n",@headers));
45 while (my @items=($data=~/$re/s)) {
47 for my $i (0..$#headers) {
48 $h{$headers[$i]}=(!$items[2*$i+1] ? undef : $items[2*$i+0])
51 $data=substr($data,length $&);
53 die "Unrecognized data (parsing ".join("/",@headers)."): $data" if $data;
61 $self=$self->new() if !ref $self;
62 push @{$self->{"data"}},map({
63 [ $self->_parse($_->{"RECORD"}."\r\n","FIELD","FIELD-NAME","FIELD-DATA","FIELD-END") ];
64 } $self->_parse($data,"RECORD","RECORD-END"));
65 return (wantarray() ? @{$self->{"data"}} : $self->{"data"});
70 my($self,$href,@headers)=@_;
72 my $trailer=pop @headers;
73 return join("",map(("[$_]\r\n".(!defined $href->{$_} ? "" : $href->{$_}."\r\n")),@headers))."[$trailer]\r\n";
80 $data=$self->{"data"} if !$data;
82 "[RECORD]\r\n".join("",map({
83 $self->_join($_,"FIELD","FIELD-NAME","FIELD-DATA","FIELD-END");
84 } @$_))."[RECORD-END]\r\n";