| 1 | package Clarion; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use FileHandle; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 4 | our $VERSION = '1.02'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | =head1 NAME | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | Clarion - Perl module for reading CLARION 2.1 data files | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | This is a perl module to access CLARION 2.1 files. | ||||||||||||||||||||||||||||
| 18 | At the moment only read access to the files is implemented. | ||||||||||||||||||||||||||||
| 19 | "Encrypted" (owned) files are processed transparently, | ||||||||||||||||||||||||||||
| 20 | there is no need to specify the password of a file. | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | use Clarion; | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | my $dbh=new Clarion "customer.dat"; | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | print $dbh->file_struct; | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | for ( 1 .. $dbh->last_record ) { | ||||||||||||||||||||||||||||
| 31 | my $r=$dbh->get_record_hash($_); | ||||||||||||||||||||||||||||
| 32 | next if $r->{_DELETED}; | ||||||||||||||||||||||||||||
| 33 | print $r->{CODE}." ".$r->{NAME}." ".$r->{PHONE}."\n"; | ||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | $dbh->close(); | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | =head1 METHODS | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | =over 4 | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =cut | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 0 | ▶ | sub FILLOCK { 0x01; } # file is locked | ||||||||||||||||||||||||||
|
Subroutine
FILLOCK: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 45 | 16 | ▶ | sub FILOWN { 0x02; } # file is owned | ||||||||||||||||||||||||||
|
Subroutine
FILOWN: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 46 | 48 | ▶ | sub FILCRYP { 0x04; } # records are encrypted | ||||||||||||||||||||||||||
|
Subroutine
FILCRYP: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 47 | 13 | ▶ | sub FILMEMO { 0x08; } # memo file exists | ||||||||||||||||||||||||||
|
Subroutine
FILMEMO: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 48 | 0 | ▶ | sub FILCOMP { 0x10; } # file is compressed | ||||||||||||||||||||||||||
|
Subroutine
FILCOMP: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 49 | 6 | ▶ | sub FILRCLM { 0x20; } # reclaim deleted records | ||||||||||||||||||||||||||
|
Subroutine
FILRCLM: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 50 | 6 | ▶ | sub FILREAD { 0x40; } # file is read only | ||||||||||||||||||||||||||
|
Subroutine
FILREAD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 51 | 6 | ▶ | sub FILCRET { 0x80; } # file may be created | ||||||||||||||||||||||||||
|
Subroutine
FILCRET: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 0 | ▶ | sub RECNEW { 0x01; } # bit 0 - new record | ||||||||||||||||||||||||||
|
Subroutine
RECNEW: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 54 | 0 | ▶ | sub RECOLD { 0x02; } # bit 1 - old record | ||||||||||||||||||||||||||
|
Subroutine
RECOLD: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 55 | 0 | ▶ | sub RECREV { 0x04; } # bit 2 - revised record | ||||||||||||||||||||||||||
|
Subroutine
RECREV: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 56 | 49 | ▶ | sub RECDEL { 0x10; } # bit 4 - deleted record | ||||||||||||||||||||||||||
|
Subroutine
RECDEL: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 57 | 0 | ▶ | sub RECHLD { 0x40; } # bit 6 - record held | ||||||||||||||||||||||||||
|
Subroutine
RECHLD: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | =item $h=new Clarion ["file.dat" [, 1]] | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | Create object for reading Clarion file. If file name is specified then | ||||||||||||||||||||||||||||
| 62 | associate the DAT file with the object. "Encrypted" files are processed | ||||||||||||||||||||||||||||
| 63 | transparently, you do not need to specify the password of a file. | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | If the third argument (skipMemo) specified, memo field will not be | ||||||||||||||||||||||||||||
| 66 | processed at all. | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =cut | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 11 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 71 | 11 | my $self={}; | |||||||||||||||||||||||||||
| 72 | 11 | bless $self, shift; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 11 | ▶ | $self->open(@_) if @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 11 | return $self; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =item $h->close | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | Close all open file handles. | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | =cut | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 17 | ▶ | sub close { | ||||||||||||||||||||||||||
|
Subroutine
close: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 85 | 17 | my $self=shift; | |||||||||||||||||||||||||||
| 86 | 17 | ▶ | if($self->{fh}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 10 | $self->{fh}->close(); | |||||||||||||||||||||||||||
| 88 | 10 | delete $self->{fh}; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | 17 | ▶ | if($self->{fhMemo}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 4 | $self->{fhMemo}->close(); | |||||||||||||||||||||||||||
| 92 | 4 | delete $self->{fhMemo}; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 11 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 97 | 11 | shift->close; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | =item $h->open('file.dat' [, 1]) | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | Read and parse header of Clarion file. | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | If second argument given, skip processing of memo field. | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =cut | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 10 | ▶ | sub open { | ||||||||||||||||||||||||||
|
Subroutine
open: called CC=19 SCAR=29.5
Pod
documented
| |||||||||||||||||||||||||||||
| 109 | 10 | my ($self, $fileName, $skipMemo)=@_; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 10 | ▶ | my $fh=new FileHandle $fileName | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | or die("Cannot open '$fileName': $!\n"); | ||||||||||||||||||||||||||||
| 113 | 10 | binmode($fh); | |||||||||||||||||||||||||||
| 114 | 10 | $self->{fh}=$fh; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | # Read file signature & header | ||||||||||||||||||||||||||||
| 117 | 10 | my ($filesig, $sfatr)=unpack('a2 S', $self->readData(4, 'header')); | |||||||||||||||||||||||||||
| 118 | 10 | ▶ | die "Not a Clarion 2.1 file '$fileName'!\n" if $filesig ne 'C3'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 10 | $self->{name}=$fileName; | |||||||||||||||||||||||||||
| 120 | 10 | $self->{sfatr}=$sfatr; | |||||||||||||||||||||||||||
| 121 | 10 | my $header=$self->readData(2*9+31+9*4-4, 'header'); | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | # File is encrypted? | ||||||||||||||||||||||||||||
| 124 | 10 | ▶ | if($sfatr & FILOWN) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | # Looking for key; 4 variants exist | ||||||||||||||||||||||||||||
| 126 | 7 | $self->{Key}=[unpack('x8 CX2C', $header)]; # numdels, high word | |||||||||||||||||||||||||||
| 127 | # $self->{Key}=[unpack('x68 CX2C', $header)]; # reserved, low word | ||||||||||||||||||||||||||||
| 128 | # $self->{Key}=[unpack('x70 CX2C', $header)]; # reserved, high word | ||||||||||||||||||||||||||||
| 129 | # $self->{Key}=[unpack('x68 CC', $header)]; # reserved, middle word | ||||||||||||||||||||||||||||
| 130 | 7 | $header=$self->decrypt($header); | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | # Parse header itself | ||||||||||||||||||||||||||||
| 134 | 10 | my @X=unpack('C L L S S S S L L L L A12 A12 A3 A3 S S L L L S', $header); | |||||||||||||||||||||||||||
| 135 | 10 | foreach my $f(qw(numbkeys numrecs numdels numflds numpics nummars reclen offset | |||||||||||||||||||||||||||
| 136 | logeof logbof freerec recname memnam filpre recpre memolen memowid | ||||||||||||||||||||||||||||
| 137 | reserved chgtime chgdate reserved2)) { | ||||||||||||||||||||||||||||
| 138 | 210 | $self->{header}{$f}=shift @X; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | # Read field descriptions & build record template | ||||||||||||||||||||||||||||
| 142 | 10 | $self->{fields}=[]; | |||||||||||||||||||||||||||
| 143 | 10 | $self->{decimal_fields}=[]; | |||||||||||||||||||||||||||
| 144 | 10 | $self->{record}{unpack}=''; | |||||||||||||||||||||||||||
| 145 | 10 | $self->{record}{No}=0; | |||||||||||||||||||||||||||
| 146 | 10 | ▶ | for(my $i=0; $i<$self->{header}{numflds}; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | 74 | @X=unpack('C A16 S S C C S S', $self->readData(3+16+2*4, 'field descriptor', 1)); | |||||||||||||||||||||||||||
| 148 | 74 | my $fd={}; | |||||||||||||||||||||||||||
| 149 | 74 | foreach my $f(qw(fldtype fldname foffset length decsig decdec arrnum picnum)) { | |||||||||||||||||||||||||||
| 150 | 592 | $fd->{$f}=shift @X; | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | 74 | push @{$self->{fields}}, $fd; | |||||||||||||||||||||||||||
| 153 | 74 | ▶ | push @{$self->{decimal_fields}}, $fd if 8==$fd->{fldtype}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 74 | my $n=$fd->{fldname}; | |||||||||||||||||||||||||||
| 155 | 74 | $n=~s/^.+?://; | |||||||||||||||||||||||||||
| 156 | 74 | $fd->{Name}=$n; | |||||||||||||||||||||||||||
| 157 | 74 | $self->{field_map}{$n}=$fd->{No}=$i; | |||||||||||||||||||||||||||
| 158 | 74 | my $c=qw(a l d A A C s G)[$fd->{fldtype}]; | |||||||||||||||||||||||||||
| 159 | 74 | ▶ | $c='a' unless $c; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | 74 | ▶ | $c.=$fd->{length} if uc($c)eq 'A'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | 74 | ▶ | $c='a'.$fd->{length}.' X'.$fd->{length}.' ' if 'G' eq $c; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 74 | $self->{record}{unpack}.=$c.' '; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | # Read key descriptions | ||||||||||||||||||||||||||||
| 166 | 10 | $self->{keys}=[]; | |||||||||||||||||||||||||||
| 167 | 10 | ▶ | for(my $i=$self->{header}{numbkeys}; $i>0; $i--) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | 21 | @X=unpack('C A16 C C', $self->readData(1+16+1+1, 'key descriptor', 1)); | |||||||||||||||||||||||||||
| 169 | 21 | my $kd={}; | |||||||||||||||||||||||||||
| 170 | 21 | foreach my $f(qw(numcomps keynams comptype complen)) { | |||||||||||||||||||||||||||
| 171 | 84 | $kd->{$f}=shift @X; | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | 21 | push @{$self->{keys}}, $kd; | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | # Read key parts | ||||||||||||||||||||||||||||
| 176 | 21 | $kd->{parts}=[]; | |||||||||||||||||||||||||||
| 177 | 21 | ▶ | for(my $j=$kd->{numcomps}; $j>0; $j--) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | 28 | @X=unpack('C S S C', $self->readData(1+2+2+1, 'key element', 1)); | |||||||||||||||||||||||||||
| 179 | 28 | my $kp={}; | |||||||||||||||||||||||||||
| 180 | 28 | foreach my $f(qw(fldtype fldnum elmoff elmlen)) { | |||||||||||||||||||||||||||
| 181 | 112 | $kp->{$f}=shift @X; | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | 28 | push @{$kd->{parts}}, $kp; | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 10 | ▶ | return if defined($skipMemo) or !($sfatr & FILMEMO); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $skipMemo or not $sfatr & FILMEMO()
MC/DC100%2/2
defined $skipMemo or not $sfatr & FILMEMO()
defined $skipMemo
not $sfatr & FILMEMO()
Truth table100%3/3
A or not(B)
A= defined $skipMemoB= not $sfatr & FILMEMO()
| |||||||||||||||||||||||||||||
| 188 | # Reading memo... | ||||||||||||||||||||||||||||
| 189 | 4 | $fileName=~s/\.[^\.\\\/]*$//; | |||||||||||||||||||||||||||
| 190 | 4 | $fileName.='.mem'; | |||||||||||||||||||||||||||
| 191 | 4 | ▶ | $fh=new FileHandle $fileName | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | or die("Cannot open memo '$fileName': $!\n"); | ||||||||||||||||||||||||||||
| 193 | 4 | binmode($fh); | |||||||||||||||||||||||||||
| 194 | 4 | $self->{fhMemo}=$fh; | |||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | # Read memo file signature | ||||||||||||||||||||||||||||
| 197 | 4 | read($fh, $filesig, 2); | |||||||||||||||||||||||||||
| 198 | 4 | ▶ | die "Not a Clarion 2.1 memo '$fileName'!\n" if $filesig ne 'M3'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 199 | my $m={ | ||||||||||||||||||||||||||||
| 200 | isMemo=>1, | ||||||||||||||||||||||||||||
| 201 | 4 | No=>scalar @{$self->{fields}}, | |||||||||||||||||||||||||||
| 202 | Name=>$self->{header}{memnam}, | ||||||||||||||||||||||||||||
| 203 | fldname=>$self->{header}{memnam}.':'.$self->{header}{filpre}, | ||||||||||||||||||||||||||||
| 204 | length=>$self->{header}{memolen}, | ||||||||||||||||||||||||||||
| 205 | 4 | }; | |||||||||||||||||||||||||||
| 206 | 4 | push @{$self->{fields}}, $m; | |||||||||||||||||||||||||||
| 207 | 4 | $self->{field_map}{$m->{Name}}=$m->{No}; | |||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | =item $n=$dbh->last_record; | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | Returns the number of records in the database file. | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | =cut | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 3 | ▶ | sub last_record { | ||||||||||||||||||||||||||
|
Subroutine
last_record: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 217 | 3 | return shift->{header}{numrecs}; | |||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | =item $n=$dbh->bof; | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | Returns the physical number of first logical record. | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | =cut | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | 0 | ▶ | sub bof { | ||||||||||||||||||||||||||
|
Subroutine
bof: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 227 | 0 | return shift->{header}{logbof}; | |||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | =item $n=$dbh->eof; | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | Returns the physical number of last logical record. | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | =cut | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | 0 | ▶ | sub eof { | ||||||||||||||||||||||||||
|
Subroutine
eof: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 237 | 0 | return shift->{header}{logeof}; | |||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | # Internal function to read a record | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | 38 | ▶ | sub readRecord { | ||||||||||||||||||||||||||
|
Subroutine
readRecord: called CC=11 SCAR=24.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 243 | 38 | my ($self, $n)=@_; | |||||||||||||||||||||||||||
| 244 | 38 | ▶ | $n||=$self->{record}{No}+1; | ||||||||||||||||||||||||||
|
Condition66%2/3
$n ||= $self->{"record"}{"No"} + 1
MC/DC0%0/2
$n ||= $self->{"record"}{"No"} + 1
$n
$self->{"record"}{"No"} + 1
Truth table66%2/3
A ||= B
A= $nB= $self->{"record"}{"No"} + 1
| |||||||||||||||||||||||||||||
| 245 | 38 | ▶ | return if $n<1 or $n>$self->{header}{numrecs}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$n < 1 or $n > $self->{"header"}{"numrecs"}
MC/DC50%1/2
$n < 1 or $n > $self->{"header"}{"numrecs"}
$n < 1
$n > $self->{"header"}{"numrecs"}
Truth table66%2/3
A or B
A= $n < 1B= $n > $self->{"header"}{"numrecs"}
| |||||||||||||||||||||||||||||
| 246 | 35 | $self->{record}{data}=[]; | |||||||||||||||||||||||||||
| 247 | 35 | $self->{record}{No}=$n; | |||||||||||||||||||||||||||
| 248 | 35 | seek($self->{fh}, $self->{header}{offset}+$self->{header}{reclen}*($n-1), 0); | |||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | 35 | ($self->{record}{rhd}, $self->{record}{rptr})=unpack('C L', $self->readData(5, 'record')); | |||||||||||||||||||||||||||
| 251 | my @Data=unpack($self->{record}{unpack}, | ||||||||||||||||||||||||||||
| 252 | 35 | $self->readData($self->{header}{reclen}-5, 'record', $self->{sfatr} & FILCRYP)); | |||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | # Convert decimal() fields, if any | ||||||||||||||||||||||||||||
| 255 | 35 | foreach my $f(@{$self->{decimal_fields}}) { | |||||||||||||||||||||||||||
| 256 | 49 | $Data[$f->{No}]=unpackBCD($Data[$f->{No}], $f->{decsig}, $f->{decdec}); | |||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||
| 258 | 35 | $self->{record}{data}=\@Data; | |||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | 35 | ▶ | return 1 unless $self->{fhMemo}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | # Read memo... | ||||||||||||||||||||||||||||
| 263 | 14 | my $memo; | |||||||||||||||||||||||||||
| 264 | 14 | ▶ | $n=($self->{record}{rhd} & RECDEL)? 0 : $self->{record}{rptr}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 265 | 14 | ▶ | while($n) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 266 | 7 | seek($self->{fhMemo}, ($n-1)*256+6, 0); | |||||||||||||||||||||||||||
| 267 | 7 | $n=unpack('L', $self->readMemo(4)); | |||||||||||||||||||||||||||
| 268 | 7 | my $m=$self->readMemo(252); | |||||||||||||||||||||||||||
| 269 | 7 | ▶ | $m=$self->decrypt($m) if $self->{sfatr} & FILCRYP; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 270 | 7 | ▶ | $memo='' unless defined($memo); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 271 | 7 | $memo.=$m; | |||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||
| 273 | 14 | ▶ | $memo=~s/( +|\00+)\z// if $memo; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 274 | 14 | push @Data, $memo; | |||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | 14 | return 1; | |||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | =item @r=$dbh->get_record([ $n [, @fields]]); | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | Returns a list of data (field values) from the specified record. | ||||||||||||||||||||||||||||
| 282 | The first parameter in the call is the number of the physical | ||||||||||||||||||||||||||||
| 283 | record. If you do not specify any other parameters, all fields are | ||||||||||||||||||||||||||||
| 284 | returned in the same order as they appear in the file. You can also | ||||||||||||||||||||||||||||
| 285 | put list of field names after the record number and then only those | ||||||||||||||||||||||||||||
| 286 | will be returned. The first value of the returned list is always the | ||||||||||||||||||||||||||||
| 287 | logical (0 or not 0) value saying whether the record is deleted or not. | ||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | If first argument is omited (or undef) then reads next record from file. | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | =cut | ||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | 36 | ▶ | sub get_record { | ||||||||||||||||||||||||||
|
Subroutine
get_record: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||||||||||
| 294 | 36 | my ($self, $n, @fields)=@_; | |||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | 36 | ▶ | $self->readRecord($n) or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | 33 | ▶ | return ($self->{record}{rhd} & RECDEL, @{$self->{record}{data}}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 299 | unless @fields; | ||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | return | ||||||||||||||||||||||||||||
| 302 | $self->{record}{rhd} & RECDEL, | ||||||||||||||||||||||||||||
| 303 | 0 | map($self->{record}{data}[$self->{field_map}{$_}], @fields); | |||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | =item $r=$dbh->get_record_hash([ $n [, @fields]]); | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | Returns reference to hash containing field values indexed by field names. | ||||||||||||||||||||||||||||
| 309 | The name of the deleted flag is C<_DELETED>. The first parameter in the call | ||||||||||||||||||||||||||||
| 310 | is the number of the physical record (can be omited to read next record if | ||||||||||||||||||||||||||||
| 311 | avaialable). If you do not specify any other parameters, all fields are returned. | ||||||||||||||||||||||||||||
| 312 | You can also put list of field names after the record number and then only those | ||||||||||||||||||||||||||||
| 313 | will be returned. | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | =cut | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | 2 | ▶ | sub get_record_hash { | ||||||||||||||||||||||||||
|
Subroutine
get_record_hash: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||
| 318 | 2 | my ($self, $n, @fields)=@_; | |||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | 2 | ▶ | $self->readRecord($n) or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | my %res= @fields ? | ||||||||||||||||||||||||||||
| 323 | map(($_, $self->{record}{data}[$self->{field_map}{$_}]), @fields) : | ||||||||||||||||||||||||||||
| 324 | 2 | ▶ | map(($_->{Name}, $self->{record}{data}[$_->{No}]), @{$self->{fields}}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | 2 | $res{_DELETED}=$self->{record}{rhd} & RECDEL; | |||||||||||||||||||||||||||
| 327 | 2 | return \%res; | |||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | =item $struct = $dbh->file_struct; | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | This returns CLARION file structure as a string. | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | =cut | ||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | 6 | ▶ | sub file_struct { | ||||||||||||||||||||||||||
|
Subroutine
file_struct: called CC=14 SCAR=26.6
Pod
documented
| |||||||||||||||||||||||||||||
| 337 | 6 | my $self=shift; | |||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | 6 | my $res=$self->{name}; | |||||||||||||||||||||||||||
| 340 | 6 | $res=~s/\.dat$//i; | |||||||||||||||||||||||||||
| 341 | 6 | $res=~s/^.*[\/\\]//; | |||||||||||||||||||||||||||
| 342 | 6 | $res=uc($res); | |||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | 6 | $res.="\tFILE,NAME('$res'),PRE('$self->{header}{filpre}')"; | |||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | 6 | ▶ | $res.=",OWNER('???')" if $self->{sfatr} & FILOWN; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 347 | 6 | ▶ | $res.=",ENCRYPT" if $self->{sfatr} & FILCRYP; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 348 | 6 | ▶ | $res.=",CREATE" if $self->{sfatr} & FILCRET; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 349 | 6 | ▶ | $res.=",RECLAIM" if $self->{sfatr} & FILRCLM; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 350 | 6 | ▶ | $res.=",PROTECT" if $self->{sfatr} & FILREAD; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 351 | $res.="\n$self->{header}{memnam}\tMEMO($self->{header}{memolen})" | ||||||||||||||||||||||||||||
| 352 | 6 | ▶ | if $self->{sfatr} & FILMEMO; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | 6 | $res.="\n$self->{header}{recname}\tRECORD\n"; | |||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | 6 | for my $f(@{$self->{fields}}) { | |||||||||||||||||||||||||||
| 357 | 45 | ▶ | next if $f->{isMemo}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 358 | 42 | $res.=$f->{Name}."\t"; | |||||||||||||||||||||||||||
| 359 | 42 | my $t=qw(? LONG REAL . . BYTE SHORT . DECIMAL)[$f->{fldtype}]; | |||||||||||||||||||||||||||
| 360 | 42 | ▶ | if(!$t or '?' eq $t) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $t or "?" eq $t
MC/DC0%0/2
not $t or "?" eq $t
not $t
"?" eq $t
Truth table33%1/3
not(A) or B
A= not $tB= "?" eq $t
| |||||||||||||||||||||||||||||
| 361 | 0 | $t='UNKNOWN TYPE'; | |||||||||||||||||||||||||||
| 362 | 0 | $res.='!'; | |||||||||||||||||||||||||||
| 363 | } | ||||||||||||||||||||||||||||
| 364 | 42 | ▶ | if('.' eq $t){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 365 | 9 | $res.="STRING($f->{length})"; | |||||||||||||||||||||||||||
| 366 | 9 | ▶ | $res.="\t!GROUP" if 7==$f->{fldtype}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 367 | } else { | ||||||||||||||||||||||||||||
| 368 | 33 | $res.=$t; | |||||||||||||||||||||||||||
| 369 | $res.="(".($f->{decsig}+$f->{decdec}).",$f->{decdec})" | ||||||||||||||||||||||||||||
| 370 | 33 | ▶ | if 8==$f->{fldtype}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 371 | } | ||||||||||||||||||||||||||||
| 372 | 42 | $res.="\n"; | |||||||||||||||||||||||||||
| 373 | } | ||||||||||||||||||||||||||||
| 374 | 6 | return $res."\t. .\n"; | |||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | # Clarion "decryption" | ||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | 146 | ▶ | sub decrypt { | ||||||||||||||||||||||||||
|
Subroutine
decrypt: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 380 | 146 | my ($self, $str)=@_; | |||||||||||||||||||||||||||
| 381 | 146 | ▶ | return $str unless defined($self->{Key}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 382 | 110 | my $res=''; | |||||||||||||||||||||||||||
| 383 | 110 | do{ | |||||||||||||||||||||||||||
| 384 | 1858 | my($c1, $c2)=unpack('C2', $str); | |||||||||||||||||||||||||||
| 385 | 1858 | ▶ | defined($c2) or return $res.$str; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 386 | 1748 | $res.=pack('C2', $c1^$self->{Key}[0], $c2^$self->{Key}[1]); | |||||||||||||||||||||||||||
| 387 | 1748 | $str=unpack('x2 a*', $str); | |||||||||||||||||||||||||||
| 388 | }while(1); | ||||||||||||||||||||||||||||
| 389 | } | ||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | 213 | ▶ | sub readData { | ||||||||||||||||||||||||||
|
Subroutine
readData: called CC=4 SCAR=14.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 392 | 213 | my ($self, $len, $what, $decrypt)=@_; | |||||||||||||||||||||||||||
| 393 | 213 | ▶ | my $rc=read($self->{fh}, my $buf, $len)||0; | ||||||||||||||||||||||||||
|
Condition50%1/2
read($self->{'fh'}, my $buf, $len) || 0
MC/DC0%0/1
read($self->{'fh'}, my $buf, $len) || 0
read($self->{'fh'}, my $buf, $len)
Truth table50%1/2
A
A= read($self->{'fh'}, my $buf, $len)
| |||||||||||||||||||||||||||||
| 394 | 213 | ▶ | die "Read error Clarion file ($what) ($rc bytes read instead of $len)!\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 395 | if $rc!=$len; | ||||||||||||||||||||||||||||
| 396 | 213 | ▶ | return $decrypt? $self->decrypt($buf) : $buf; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 397 | } | ||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | 14 | ▶ | sub readMemo { | ||||||||||||||||||||||||||
|
Subroutine
readMemo: called CC=3 SCAR=11.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 400 | 14 | my ($self, $len)=@_; | |||||||||||||||||||||||||||
| 401 | 14 | ▶ | my $rc=read($self->{fhMemo}, my $buf, $len)||0; | ||||||||||||||||||||||||||
|
Condition50%1/2
read($self->{'fhMemo'}, my $buf, $len) || 0
MC/DC0%0/1
read($self->{'fhMemo'}, my $buf, $len) || 0
read($self->{'fhMemo'}, my $buf, $len)
Truth table50%1/2
A
A= read($self->{'fhMemo'}, my $buf, $len)
| |||||||||||||||||||||||||||||
| 402 | 14 | ▶ | die "Read error Clarion memo ($rc bytes read instead of $len)!\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 403 | if $rc!=$len; | ||||||||||||||||||||||||||||
| 404 | 14 | return $buf; | |||||||||||||||||||||||||||
| 405 | } | ||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||
| 407 | # Convert BCD to string | ||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | 49 | ▶ | sub unpackBCD { | ||||||||||||||||||||||||||
|
Subroutine
unpackBCD: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 410 | 49 | my ($bcd, $decsig, $decdec)=@_; | |||||||||||||||||||||||||||
| 411 | 49 | $bcd=unpack('H*', $bcd); | |||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||
| 413 | 49 | ▶ | my $sign=substr($bcd, 0, 1) eq '0' ? '' : '-'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 414 | 49 | $bcd=substr($bcd, 1); | |||||||||||||||||||||||||||
| 415 | 49 | ▶ | $bcd=~s/\D/9/g and | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 416 | warn "Incorrect DECIMAL value!\n"; | ||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | 49 | my $sig=substr($bcd, 0, $decsig); | |||||||||||||||||||||||||||
| 419 | 49 | $sig=~s/^0+//; | |||||||||||||||||||||||||||
| 420 | 49 | ▶ | $sig='0' if !length($sig); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | 49 | my $dec=substr($bcd, $decsig, $decdec); | |||||||||||||||||||||||||||
| 423 | 49 | $dec=~s/0+$//; | |||||||||||||||||||||||||||
| 424 | 49 | ▶ | $sig.='.' if length($dec); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | 49 | return $sign.$sig.$dec; | |||||||||||||||||||||||||||
| 427 | } | ||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||
| 429 | 4 | 1; | |||||||||||||||||||||||||||
| 430 | __END__ | ||||||||||||||||||||||||||||