| 1 | package Alvis::AinoDump; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | use Data::Dumper; | ||
| 7 | |||
| 8 | $Alvis::AinoDump::VERSION = '0.1'; | ||
| 9 | |||
| 10 | ############################################################################## | ||
| 11 | # | ||
| 12 | # Error messages | ||
| 13 | # | ||
| 14 | ############################################################################# | ||
| 15 | |||
| 16 | my $ErrStr; | ||
| 17 | my ($ERR_OK, | ||
| 18 | $ERR_BYTE_LENGTH, | ||
| 19 | $ERR_TOO_SHORT_DOC, | ||
| 20 | $ERR_SPLIT, | ||
| 21 | $ERR_PROCESS, | ||
| 22 | $ERR_READ | ||
| 23 | )=(0..5); | ||
| 24 | my %ErrMsgs=($ERR_OK=>"", | ||
| 25 | $ERR_BYTE_LENGTH=>"No byte length given for a doc.", | ||
| 26 | $ERR_TOO_SHORT_DOC=>"Too short a document.", | ||
| 27 | $ERR_SPLIT=>"Splitting the doc to text and header failed", | ||
| 28 | $ERR_PROCESS=>"Processing the text and the header failed.", | ||
| 29 | $ERR_READ=>"Reading from the filehandle failed." | ||
| 30 | ); | ||
| 31 | |||
| 32 | sub _set_err_state | ||
| 33 | { | ||
| 34 | my $self=shift; | ||
| 35 | my $errcode=shift; | ||
| 36 | my $errmsg=shift; | ||
| 37 | |||
| 38 | if (!defined($errcode)) | ||
| 39 | { | ||
| 40 | confess("set_err_state() called with an undefined argument."); | ||
| 41 | } | ||
| 42 | |||
| 43 | if (exists($ErrMsgs{$errcode})) | ||
| 44 | { | ||
| 45 | if ($errcode==$ERR_OK) | ||
| 46 | { | ||
| 47 | $ErrStr=""; | ||
| 48 | } | ||
| 49 | else | ||
| 50 | { | ||
| 51 | $ErrStr.=" " . $ErrMsgs{$errcode}; | ||
| 52 | if (defined($errmsg)) | ||
| 53 | { | ||
| 54 | $ErrStr.=" " . $errmsg; | ||
| 55 | } | ||
| 56 | } | ||
| 57 | } | ||
| 58 | else | ||
| 59 | { | ||
| 60 | confess("Internal error: set_err_state() called with an " . | ||
| 61 | "unrecognized argument ($errcode).") | ||
| 62 | } | ||
| 63 | } | ||
| 64 | |||
| 65 | sub errmsg | ||
| 66 | { | ||
| 67 | return $ErrStr; | ||
| 68 | } | ||
| 69 | |||
| 70 | ############################################################################# | ||
| 71 | # | ||
| 72 | # Methods | ||
| 73 | # | ||
| 74 | ############################################################################## | ||
| 75 | |||
| 76 | sub new | ||
| 77 | { | ||
| 78 | my $proto=shift; | ||
| 79 | |||
| 80 | my $class=ref($proto)||$proto; | ||
| 81 | my $parent=ref($proto)&&$proto; | ||
| 82 | my $self={}; | ||
| 83 | bless($self,$class); | ||
| 84 | |||
| 85 | |||
| 86 | $self->_init(@_); | ||
| 87 | |||
| 88 | $self->_set_err_state($ERR_OK); | ||
| 89 | |||
| 90 | return $self; | ||
| 91 | } | ||
| 92 | |||
| 93 | sub _init | ||
| 94 | { | ||
| 95 | my $self=shift; | ||
| 96 | |||
| 97 | if (defined(@_)) | ||
| 98 | { | ||
| 99 | my %args=@_; | ||
| 100 | @$self{ keys %args }=values(%args); | ||
| 101 | } | ||
| 102 | } | ||
| 103 | |||
| 104 | ####################################################################3 | ||
| 105 | # | ||
| 106 | # Public methods | ||
| 107 | # | ||
| 108 | #################################################################### | ||
| 109 | |||
| 110 | # | ||
| 111 | # Has to be a ':raw' stream for the length to work properly | ||
| 112 | # | ||
| 113 | sub process_dump | ||
| 114 | { | ||
| 115 | my $self=shift; | ||
| 116 | my $fh=shift; | ||
| 117 | my $process_cb=shift; | ||
| 118 | |||
| 119 | while (my $l=<$fh>) | ||
| 120 | { | ||
| 121 | chomp $l; | ||
| 122 | if ($l!~/^\d+$/) | ||
| 123 | { | ||
| 124 | $self->_set_err_state($ERR_BYTE_LENGTH, | ||
| 125 | "Supposed byte length info line:\"$l\"."); | ||
| 126 | return 0; | ||
| 127 | } | ||
| 128 | my $nof_bytes=$l; | ||
| 129 | |||
| 130 | my $doc; | ||
| 131 | my $nof_read=sysread($fh,$doc,$nof_bytes); | ||
| 132 | if (!defined($nof_read)) | ||
| 133 | { | ||
| 134 | $self->_set_err_state($ERR_READ); | ||
| 135 | return 0; | ||
| 136 | } | ||
| 137 | if ($nof_read!=$nof_bytes) | ||
| 138 | { | ||
| 139 | $self->_set_err_state($ERR_TOO_SHORT_DOC, | ||
| 140 | "Supposed to be " . | ||
| 141 | "$nof_bytes, filehandle exhausted after " . | ||
| 142 | "$nof_read bytes."); | ||
| 143 | return 0; | ||
| 144 | } | ||
| 145 | |||
| 146 | my ($header,$text)=split(/\n---\n/,$doc); | ||
| 147 | if (!defined($header) || !defined($text)) | ||
| 148 | { | ||
| 149 | $self->_set_err_state($ERR_SPLIT,"Doc:\"$doc\"."); | ||
| 150 | return 0; | ||
| 151 | } | ||
| 152 | |||
| 153 | my %header=(); | ||
| 154 | if ($header=~/^ID\s*(.*)$/mo) | ||
| 155 | { | ||
| 156 | $header{id}=$1; | ||
| 157 | } | ||
| 158 | if ($header=~/^URL\s*(.*)$/mo) | ||
| 159 | { | ||
| 160 | $header{url}=$1; | ||
| 161 | } | ||
| 162 | if ($header=~/^TIME\s*(.*)$/mo) | ||
| 163 | { | ||
| 164 | $header{time}=$1; | ||
| 165 | } | ||
| 166 | |||
| 167 | my ($cb,@args); | ||
| 168 | if (ref($process_cb) eq 'ARRAY') | ||
| 169 | { | ||
| 170 | ($cb,@args)=@$process_cb; | ||
| 171 | } | ||
| 172 | else | ||
| 173 | { | ||
| 174 | $cb=$process_cb; | ||
| 175 | } | ||
| 176 | |||
| 177 | if (!&{$cb}(@args,$text,\%header)) | ||
| 178 | { | ||
| 179 | $self->_set_err_state($ERR_PROCESS,"Text:\"$text\",header:", | ||
| 180 | Dumper(%header),"."); | ||
| 181 | return 0; | ||
| 182 | } | ||
| 183 | } | ||
| 184 | |||
| 185 | return 1; | ||
| 186 | } | ||
| 187 | |||
| 188 | 1; |