Debian Codegraph @

scripts/CG3_External.pm

raw
1 #!/usr/bin/perl
2 # -*- mode: cperl; indent-tabs-mode: nil; tab-width: 3; cperl-indent-level: 3; -*-
3 package CG3_External;
4 use strict;
5 use warnings;
6 use utf8;
7 #use feature 'unicode_strings';
8
9 our $VERSION = 7226;
10 our @EXPORT_OK;
11
12 BEGIN {
13 $| = 1;
14 use Exporter ();
15 our (@ISA, @EXPORT_OK);
16
17 @ISA = qw(Exporter);
18
19 @EXPORT_OK = qw(&check_protocol &read_window &write_window &write_null_response);
20 }
21
22 sub read_uint16_t {
23 my ($fh,$msg) = @_;
24
25 my $n = 0;
26 if (read($fh, $n, 2) != 2) {
27 return undef;
28 }
29 $n = unpack('S', $n);
30 if ($ENV{'DEBUG'}) { print STDERR "Read $msg uint16_t: $n\n"; }
31 return $n;
32 }
33
34 sub write_uint16_t {
35 my ($fh,$n,$msg) = @_;
36
37 if ($ENV{'DEBUG'}) { print STDERR "Writing $msg uint16_t: $n\n"; }
38 $n = pack('S', $n);
39 print $fh $n;
40 }
41
42 sub read_uint32_t {
43 my ($fh,$msg) = @_;
44
45 my $n = 0;
46 if (read($fh, $n, 4) != 4) {
47 return undef;
48 }
49 $n = unpack('L', $n);
50 if ($ENV{'DEBUG'}) { print STDERR "Read $msg uint32_t: $n\n"; }
51 return $n;
52 }
53
54 sub write_uint32_t {
55 my ($fh,$n,$msg) = @_;
56
57 if ($ENV{'DEBUG'}) { print STDERR "Writing $msg uint32_t: $n\n"; }
58 $n = pack('L', $n);
59 print $fh $n;
60 }
61
62 sub read_utf8_string {
63 my ($fh,$msg) = @_;
64
65 my $n = read_uint16_t($fh, $msg.' length');
66 my $s = '';
67 if (read($fh, $s, $n) != $n) {
68 return undef;
69 }
70
71 utf8::decode($s);
72
73 if ($ENV{'DEBUG'}) { print STDERR "Read $msg UTF-8 string: $s\n"; }
74 return $s;
75 }
76
77 sub write_utf8_string {
78 my ($fh,$s,$msg) = @_;
79
80 utf8::encode($s);
81
82 my $n = length($s);
83 write_uint16_t($fh, $n, $msg.' length');
84 if ($ENV{'DEBUG'}) { utf8::decode($s); print STDERR "Writing $msg UTF-8 string: $s\n"; utf8::encode($s); }
85 print $fh $s;
86 }
87
88 sub check_protocol {
89 my ($fh) = @_;
90
91 my $protocol = read_uint32_t($fh, 'protocol revision');
92 if ($protocol == $VERSION) {
93 return $VERSION;
94 }
95
96 return undef;
97 }
98
99 sub read_window {
100 my ($fh) = @_;
101
102 my $len = read_uint32_t($fh, 'window packet length');
103 if (!$len) {
104 return undef;
105 }
106
107 my %window = ();
108 $window{'num'} = read_uint32_t($fh, 'window number');
109
110 my @cohorts;
111 my $clen = read_uint32_t($fh, 'num cohorts');
112 for (my $c=0 ; $c<$clen ; $c++) {
113 read_uint32_t($fh, 'cohort packet length');
114 my %cohort = ();
115 $cohort{'num'} = read_uint32_t($fh, 'cohort number');
116 $cohort{'flags'} = read_uint32_t($fh, 'cohort flags');
117 if ($cohort{'flags'} & (1 << 1)) {
118 $cohort{'parent'} = read_uint32_t($fh, 'cohort parent');
119 }
120 $cohort{'wordform'} = read_utf8_string($fh, 'cohort wordform');
121
122 my @readings;
123 my $rlen = read_uint32_t($fh, 'num readings');
124 for (my $r=0 ; $r<$rlen ; $r++) {
125 read_uint32_t($fh, 'reading packet length');
126 my %reading = ();
127 $reading{'flags'} = read_uint32_t($fh, 'reading flags');
128
129 if ($reading{'flags'} & (1 << 3)) {
130 $reading{'baseform'} = read_utf8_string($fh, 'reading baseform');
131 }
132
133 my @tags;
134 my $tlen = read_uint32_t($fh, 'num tags');
135 for (my $t=0 ; $t<$tlen ; $t++) {
136 my $tag = read_utf8_string($fh, 'tag');
137 push @tags, $tag;
138 }
139 $reading{'tags'} = \@tags;
140
141 push @readings, \%reading;
142 }
143 $cohort{'readings'} = \@readings;
144
145 if ($cohort{'flags'} & (1 << 0)) {
146 $cohort{'text'} = read_utf8_string($fh, 'cohort text');
147 }
148
149 push @cohorts, \%cohort;
150 }
151 $window{'cohorts'} = \@cohorts;
152
153 return \%window;
154 }
155
156 sub write_window {
157 my ($fh,$w) = @_;
158
159 if ($ENV{'DEBUG'} && $ENV{'DEBUG'} > 1) { write_uint32_t($fh, $VERSION, 'debug version'); }
160
161 open(my $fw, '>', \my $wo);
162 write_uint32_t($fw, $w->{'num'}, 'window number');
163
164 my $clen = @{$w->{'cohorts'}};
165 write_uint32_t($fw, $clen, 'num cohorts');
166
167 foreach my $c (@{$w->{'cohorts'}}) {
168 open(my $fc, '>', \my $co);
169
170 write_uint32_t($fc, $c->{'num'}, 'cohort number');
171 if ($c->{'text'}) {
172 $c->{'flags'} |= (1 << 0);
173 }
174 if (exists($c->{'parent'})) {
175 $c->{'flags'} |= (1 << 1);
176 }
177 write_uint32_t($fc, $c->{'flags'}, 'cohort flags');
178 if (exists($c->{'parent'})) {
179 write_uint32_t($fc, $c->{'parent'}, 'cohort parent');
180 }
181 write_utf8_string($fc, $c->{'wordform'}, 'cohort wordform');
182
183 my $rlen = @{$c->{'readings'}};
184 write_uint32_t($fc, $rlen, 'num readings');
185
186 foreach my $r (@{$c->{'readings'}}) {
187 open(my $fr, '>', \my $ro);
188 if ($r->{'baseform'}) {
189 $r->{'flags'} |= (1 << 3);
190 }
191 write_uint32_t($fr, $r->{'flags'}, 'reading flags');
192 if ($r->{'baseform'}) {
193 write_utf8_string($fr, $r->{'baseform'}, 'reading baseform');
194 }
195 my $tlen = @{$r->{'tags'}};
196 write_uint32_t($fr, $tlen, 'num tags');
197 foreach my $t (@{$r->{'tags'}}) {
198 write_utf8_string($fr, $t, 'tag');
199 }
200
201 write_uint32_t($fc, length($ro), 'reading packet length');
202 print $fc $ro;
203 }
204
205 if ($c->{'text'}) {
206 write_utf8_string($fc, $c->{'text'}, 'cohort text');
207 }
208
209 write_uint32_t($fw, length($co), 'cohort packet length');
210 print $fw $co;
211 }
212
213 write_uint32_t($fh, length($wo), 'window packet length');
214 print $fh $wo;
215
216 if ($ENV{'DEBUG'} && $ENV{'DEBUG'} > 1) { write_uint32_t($fh, 0, 'debug null'); }
217 }
218
219 sub write_null_response {
220 my ($fh) = @_;
221 write_uint32_t($fh, 0, 'null response');
222 }
223
224 1;