| 1 | #!/usr/bin/perl | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use File::Basename; | ||
| 6 | use oEdtk; | ||
| 7 | use oEdtk::libDev qw ( lastCourt ); | ||
| 8 | |||
| 9 | sub usage() { | ||
| 10 | my $PROG = basename( $0, "" ); | ||
| 11 | print STDERR <<"EOF"; | ||
| 12 | |||
| 13 | Usage: | ||
| 14 | $PROG recorder_descriptor.cob > record_ID.pm | ||
| 15 | |||
| 16 | Structure of descriptor file SHOULD start with record number 01 and SHOULD be : | ||
| 17 | 01 VECTOR-RECORD-ID-ROW_NAME-1. | ||
| 18 | * comment for row | ||
| 19 | nn NOM-COLONNE1 PIC X(50). | ||
| 20 | * comment for row | ||
| 21 | nn NOM-COLONNE2 PIC S9(10). | ||
| 22 | nn NOM-COLONNEn PIC S9(12)V9(2). | ||
| 23 | * comment for row | ||
| 24 | 01 VECTOR-RECORD_ID-ROW_NAME-2. | ||
| 25 | ... | ||
| 26 | 01 VECTOR-RECORD_ID-ROW_NAME-n. | ||
| 27 | ... | ||
| 28 | |||
| 29 | NB : nn is a number | ||
| 30 | ATT: each line should stop with a point '.' | ||
| 31 | each line is : comment starting with '*' | ||
| 32 | or record descriptor starting with nn | ||
| 33 | each record line structure is : nn name [data PIC]. | ||
| 34 | nn level | ||
| 35 | name (with no space no underscore) | ||
| 36 | for data in record, data PIC | ||
| 37 | note, in the recard name the shortest part would be considered as record key | ||
| 38 | |||
| 39 | $PROG release 24/03/2011 15:33:48 | ||
| 40 | this util builds records modules from copygroups descriptors | ||
| 41 | |||
| 42 | EOF | ||
| 43 | } | ||
| 44 | |||
| 45 | sub type2field { | ||
| 46 | my ($type) = @_; | ||
| 47 | |||
| 48 | $type =~ s/(.)\((\d+)\)/$1 x $2/ge; | ||
| 49 | |||
| 50 | if ( $type =~ /^X+$/ ) { | ||
| 51 | |||
| 52 | # Alpha-numeric field. | ||
| 53 | return ( "Field", length($type) ); | ||
| 54 | } | ||
| 55 | if ( $type !~ /^(S)?(9+)(V9+)?$/ ) { | ||
| 56 | die "ERROR: Unexpected type declaration: \"$type\"\n"; | ||
| 57 | } | ||
| 58 | my ( $sign, $int, $frac ) = ( $1, $2, $3 ); | ||
| 59 | |||
| 60 | if ( defined($frac) ) { | ||
| 61 | |||
| 62 | # Floating-point number. oEdtk::SignedField->new('D1-NUMRLV', 9), | ||
| 63 | # return ("FPField", length($int), length($frac) - 1); | ||
| 64 | return ( "SignedField", length($int), length($frac) - 1 ); | ||
| 65 | } | ||
| 66 | |||
| 67 | # Integral number. | ||
| 68 | return ( "Field", length($int) ); | ||
| 69 | } | ||
| 70 | |||
| 71 | sub unwind { | ||
| 72 | my ( $levels, $goal, $cur ) = @_; | ||
| 73 | |||
| 74 | while ( my ( $lvl, $ref, $refid, $occurs ) = @{ shift(@$levels) } ) { | ||
| 75 | if ( $lvl < $goal ) { | ||
| 76 | die "ERROR: Unexpected level $goal line $.\n"; | ||
| 77 | } | ||
| 78 | add( $ref, $refid, $cur, $occurs ); | ||
| 79 | $cur = $ref; | ||
| 80 | last if $lvl == $goal; | ||
| 81 | } | ||
| 82 | |||
| 83 | # XXX Error handling missing. | ||
| 84 | return $cur; | ||
| 85 | } | ||
| 86 | |||
| 87 | my %identifiers; | ||
| 88 | |||
| 89 | sub get_unique_id { | ||
| 90 | my ($id) = @_; | ||
| 91 | |||
| 92 | # Ensure unicity of identifier names. | ||
| 93 | my $i = $identifiers{$id}; | ||
| 94 | if ( defined($i) ) { | ||
| 95 | $i++; | ||
| 96 | $identifiers{$id} = $i; | ||
| 97 | $id .= "-$i"; | ||
| 98 | } | ||
| 99 | else { | ||
| 100 | $identifiers{$id} = 1; | ||
| 101 | } | ||
| 102 | return $id; | ||
| 103 | } | ||
| 104 | |||
| 105 | sub dup { | ||
| 106 | my ($data) = @_; | ||
| 107 | |||
| 108 | if ( ref($data) ne 'ARRAY' ) { | ||
| 109 | die "ARG! ($.)\n"; | ||
| 110 | } | ||
| 111 | my $new = []; | ||
| 112 | foreach (@$data) { | ||
| 113 | my ( $id, $val ) = %$_; | ||
| 114 | $id = get_unique_id($id); | ||
| 115 | if ( ref($val) ne '' ) { | ||
| 116 | $val = dup($val); | ||
| 117 | } | ||
| 118 | push( @$new, { $id => $val } ); | ||
| 119 | } | ||
| 120 | return $new; | ||
| 121 | } | ||
| 122 | |||
| 123 | sub add { | ||
| 124 | my ( $rec, $id, $val, $occurs ) = @_; | ||
| 125 | |||
| 126 | my $count = 1; | ||
| 127 | $count = $occurs if defined($occurs); | ||
| 128 | |||
| 129 | $id = get_unique_id($id); | ||
| 130 | push( @$rec, { $id => $val } ); | ||
| 131 | |||
| 132 | my $id2 = $id; | ||
| 133 | my $val2 = $val; | ||
| 134 | while ( $count > 1 ) { | ||
| 135 | $id2 = get_unique_id($id); | ||
| 136 | $val2 = dup($val); | ||
| 137 | push( @$rec, { $id2 => $val2 } ); | ||
| 138 | $count--; | ||
| 139 | } | ||
| 140 | } | ||
| 141 | |||
| 142 | sub perlify { | ||
| 143 | my ( $out, $indent, $ref ) = @_; | ||
| 144 | |||
| 145 | foreach my $item (@$ref) { | ||
| 146 | my ( $id, $type ) = %$item; | ||
| 147 | if ( ref($type) eq '' ) { | ||
| 148 | my ( $obj, @args ) = type2field($type); | ||
| 149 | print $out " " x $indent . "oEdtk::$obj->new('$id', " . join( ', ', @args ) . "),\n"; | ||
| 150 | } | ||
| 151 | else { | ||
| 152 | print $out " " x $indent . "# $id\n"; | ||
| 153 | perlify( $out, $indent + 4, $type ); | ||
| 154 | } | ||
| 155 | } | ||
| 156 | } | ||
| 157 | |||
| 158 | if ( @ARGV < 1 ) { | ||
| 159 | usage(); #"usage: $0 <det-file> [out-file]\n"; | ||
| 160 | exit 1; | ||
| 161 | } | ||
| 162 | |||
| 163 | # The regular expression that matches an identifier. | ||
| 164 | # my $idre = qr/[a-zA-Z\d-]{1,31}/; | ||
| 165 | my $idre = qr/[a-zA-Z\d-]{1,}/; | ||
| 166 | my $picre = qr/PIC(?:TURE)?(?:\s+IS)?/; | ||
| 167 | my $typre = qr/S?(?:[9XAV](?:\(\d+\))?)+/; | ||
| 168 | |||
| 169 | open( my $in, "<", $ARGV[0] ) or die "Cannot open \"$ARGV[0]\": $!\n"; | ||
| 170 | my $out = \*STDOUT; | ||
| 171 | if ( @ARGV > 1 ) { | ||
| 172 | open( $out, ">", $ARGV[1] ) or die "Cannot open \"$ARGV[1]\": $!\n"; | ||
| 173 | } | ||
| 174 | |||
| 175 | my @levels; | ||
| 176 | my @record = (); | ||
| 177 | my $cur = \@record; | ||
| 178 | my $curlvl = 0; | ||
| 179 | my $recid; | ||
| 180 | |||
| 181 | while (<$in>) { | ||
| 182 | chomp; | ||
| 183 | |||
| 184 | #s/^.{6}//; # Remove the first six characters. | ||
| 185 | s/^(\*\D*)(\d{2}\.*)$/$2/; # Strip first col comments. | ||
| 186 | s/\*.*$//; # Strip comments. | ||
| 187 | s/\_/\-/g; # replace '_' with '-'. | ||
| 188 | s/\s+$//g; # remove end line white spaces | ||
| 189 | next if m/^\s*$/; # Ignore empty lines. | ||
| 190 | |||
| 191 | if ( $_ !~ /^\s*(\d{2})\s+($idre)(?:\s+OCCURS\s+(\d+)(?:\s+TIMES)?)?/ ) { | ||
| 192 | die "ERROR: Unexpected line format (1) (line $. :'$_')\n"; | ||
| 193 | } | ||
| 194 | my ( $level, $id, $occurs, $rest ) = ( $1, $2, $3, $' ); | ||
| 195 | |||
| 196 | if ( !defined($recid) && $id =~ /^([^-]+)/ ) { | ||
| 197 | $recid = lastCourt($id); | ||
| 198 | |||
| 199 | # $recid = $1; | ||
| 200 | } | ||
| 201 | |||
| 202 | if ( $level < $curlvl ) { | ||
| 203 | $cur = unwind( \@levels, $level, $cur ); | ||
| 204 | } | ||
| 205 | $curlvl = $level; | ||
| 206 | |||
| 207 | if ( $rest =~ /^\s*\.\s*$/ ) { | ||
| 208 | unshift( @levels, [ $curlvl, $cur, $id, $occurs ] ); | ||
| 209 | $cur = []; | ||
| 210 | } | ||
| 211 | elsif ( $rest =~ /^\s+$picre\s+($typre)\s*\.\s*$/ ) { | ||
| 212 | add( $cur, $id, $1, $occurs ); | ||
| 213 | } | ||
| 214 | else { | ||
| 215 | die | ||
| 216 | "ERROR: Unexpected line format (2) (line $. - rest '$rest' - recid $recid - curlvl $curlvl - id $id)\n"; | ||
| 217 | } | ||
| 218 | } | ||
| 219 | |||
| 220 | unwind( \@levels, 01, $cur ); | ||
| 221 | close($in); | ||
| 222 | |||
| 223 | print $out <<EOF; | ||
| 224 | package oEUser::Descriptor::$recid; | ||
| 225 | |||
| 226 | use strict; | ||
| 227 | use warnings; | ||
| 228 | |||
| 229 | use oEdtk::Record; | ||
| 230 | use oEdtk::AddrField; | ||
| 231 | use oEdtk::SignedField; | ||
| 232 | use oEdtk::Field; | ||
| 233 | |||
| 234 | sub get { | ||
| 235 | return oEdtk::Record->new( | ||
| 236 | EOF | ||
| 237 | perlify( $out, 8, \@record ); | ||
| 238 | print $out <<EOF; | ||
| 239 | ); | ||
| 240 | } | ||
| 241 | |||
| 242 | 1; | ||
| 243 | EOF | ||
| 244 | close($out); |