summaryrefslogtreecommitdiff
path: root/support/ochem/streambuf.pm
blob: 50d9720454cbc52916540ea913c1076678aa18bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
package streambuf;
#
#	history
#
#	09-JAN-2000 IK	new bond shapes >> and >. and b
#	19-JAN-2000 IK	new bond shape ~, bond length now determined by number of type markers,
#			LaTeX packages can be given to be preloaded
#

#
# creates new buffer
#
sub new
{
    $r_buffer = { "Ptr" => 0,
		  "Anz" => 0,
		  "Buffer" => ""
		};
    bless $r_buffer, 'streambuf';
    return $r_buffer;
}


#
#  $buf->LoadBuffer($file)
#
sub LoadBuffer
{
    my ($r_buff, $file, $texthash) = @_;
    my ($text, $l, $c, $bOutText);

    open(FILE, $file);
    while ($line = <FILE>)
    {
	chop($line);
	$l = ""; 
	$bOutText = $::true;
	for ($i=$j=0; $i<length($line); $i++)
	{
	    $c = substr($line, $i, 1);
	    if ($c eq '"')
	    {
		if (substr($line, $i+1, 1) eq '"')
		{
		    $c = '"';
		    $i++;
		}
		else
		{
		    $bOutText = !$bOutText;
		    $c = ($bOutText ? "</text>" : "<text>");
		}
	    }
	    last if ($c =~ /%/ && $bOutText);  
	    next if ($c =~ /\s/ && $bOutText);  
	    substr($l, $j) = $c;
	    $j += length($c);
	}
	$line = $l;
##+	$line =~ s/\s//g;		# delete white spaces
##+	$line =~ /^([^%]*)%*.*$/;	# cut off comment
##+	$line = $1;
	# write out all TeX text strings
	@textext = ($line =~ m{<text>(.*?)</text>}g);
	if (@textext)
	{
	    foreach $text (@textext)
	    {
		$texthash->{$text} = "";
	    }
	}
	substr($r_buff->{"Buffer"}, $r_buff->{"Anz"}, 0) = $line;
	$r_buff->{"Anz"} += length($line);
    }
    close(FILE);
}


#
#  $buf->GetTextSize(...)
#
sub GetTextSize
{
    my ($r_buff, $tmptex, $texthash, $be_type, $latexcmd, $cFont, $packages) = @_;
    my $text;
    my ($opt, $package, $packagename);

    # get dimensions of text fragments
    if ($be_type == $be::BE_PSLATEX || $be_type == $be::BE_LATEX)
    {
	open(OUT, ">".$tmptex);
	print OUT "\\documentclass[a4paper,12pt]{article}\n";
	print OUT "\\usepackage{german}\n";
	# write each desired LaTeX package in preamble
	while ($package = pop(@$packages))
	{
	    ($opt, $packagename) = $package =~ /(\[\S*\])*(\S*)/;
	    print OUT "\\usepackage$opt\{$packagename\}\n";
	}
	print OUT "\\newsavebox{\\testbox}\n";
	print OUT "\\newcommand{\\atom}[1]\n";
	print OUT "{\\sbox{\\testbox}{#1}\n";
	print OUT " \\typeout{A:\\the\\wd\\testbox,\\the\\ht\\testbox,\\the\\dp\\testbox}\n";
	print OUT " }\n";
	print OUT "\\newcommand{\\testfont}{$cFont}\n" if $cFont;
	print OUT "\n";
	print OUT "\\begin{document}\n";
	print OUT "\\testfont\n" if $cFont;
	foreach $text (keys %$texthash)
	{
	    print OUT "\\atom{$text}\n";
	}
	print OUT "\\end{document}\n";
	close(OUT);

	# build array of TeX text dimensions: LaTeX it!
	my @dimen = grep(/^A:/, `$latexcmd $tmptex`);
	my $i = 0;
	foreach $text (keys %$texthash)
	{
	    $texthash->{$text} = $dimen[$i++];
	}
    }
    elsif ($be_type == $be::BE_PS)
    {
	foreach $text (keys %$texthash)
	{
	    print "warning: cannot determine text size for $text\n";
	}
    }
}


#
#
#
sub GetPtr
{
    my $r_buff = shift;
    return $r_buff->{"Ptr"};
}

sub SetPtr
{
    my ($r_buff, $ptr) = @_;
    $r_buff->{"Ptr"} = $ptr;
}


#
#  check if next token in buffer is $token, and adjusts buffer pointer
#  Returns zero if there's not this token.
#  BOOL $buf->NextToken($token)
#
sub NextToken
{
    my ($r_buff, $text) = @_;
    my $Ptr = $r_buff->{"Ptr"};
    my $Anz = $r_buff->{"Anz"};
    my $len = length($text);

    return 0 if $len+$Ptr > $Anz;
    if (substr($r_buff->{"Buffer"}, $Ptr, $len) eq $text)
    {
	$r_buff->{"Ptr"} += $len;
	return 1;
    }
    return 0;
}


#
#  Tries to get an signed integer value.
#  BOOL $buf->GetInt($iVar)
#
sub GetInt
{
    my $r_buff = shift;

    if ( $iAnz = substr($r_buff->{"Buffer"}, $r_buff->{"Ptr"}) =~ 
			/^([+-]?\d+)/)
    {
	$_[0] = $1;
	$r_buff->{"Ptr"} += length($1);
    }
    return ($iAnz);
}


#
#  Tries to get an signed fix float value.
#  BOOL $buf->GetReal($rVar)
#
sub GetReal
{
    my $r_buff = shift;

    if ( $iAnz = substr($r_buff->{"Buffer"}, $r_buff->{"Ptr"}) =~ 
			/^([+-]?(\d+(\.\d*)*)|(\d*\.\d*))/)
    {
	$_[0] = $1;
	$r_buff->{"Ptr"} += length($1);
    }
    return ($iAnz);
}


#
#  Tries to get a text enclosed in <text>...</text>
#  BOOL $buf->GetText($cVar)
#
sub GetText
{
    my $r_buff = shift;

    if ( $iAnz = substr($r_buff->{"Buffer"}, $r_buff->{"Ptr"}) =~ 
			m{^<text>(.*?)</text>})
    {
	$_[0] = $1;
	$r_buff->{"Ptr"} += length($1) + 13;
    }
    return ($iAnz);
}


#
#  decodes a type marker like X or Y
#  BOOL $buf->GetType($iType)
#
($T_X, $T_Y) = (0, 1);
sub GetType
{
    my $r_buff = shift;
    my $code = substr($r_buff->{"Buffer"}, $r_buff->{"Ptr"}, 1);
    my $l;
    $_[0] = -1;
    SWITCH: {
	$l = 1;
    	$_[0] = $T_X, last SWITCH if $code eq "X";
    	$_[0] = $T_Y, last SWITCH if $code eq "Y";
	$l = 0;
    }
    $r_buff->{"Ptr"} += $l;
    return ($_[0] != -1);
}

#
#  decodes a position marker like T, TR, T, BL, C, ...
#  BOOL $buf->GetPos($iPos)
#
sub GetPos
{
    my $r_buff = shift;
    my $code2 = substr($r_buff->{"Buffer"}, $r_buff->{"Ptr"}, 2);
    my $code1 = substr($r_buff->{"Buffer"}, $r_buff->{"Ptr"}, 1);
    my $l;
    $_[0] = -1;
    SWITCH: {
	$l = 2;
    	$_[0] = $bbox::SB_BL, last SWITCH if $code2 eq "BL";
    	$_[0] = $bbox::SB_BR, last SWITCH if $code2 eq "BR";
    	$_[0] = $bbox::SB_TL, last SWITCH if $code2 eq "TL";
    	$_[0] = $bbox::SB_TR, last SWITCH if $code2 eq "TR";
	$l = 1;
    	$_[0] = $bbox::SB_L, last SWITCH if $code1 eq "L";
    	$_[0] = $bbox::SB_R, last SWITCH if $code1 eq "R";
    	$_[0] = $bbox::SB_T, last SWITCH if $code1 eq "T";
    	$_[0] = $bbox::SB_B, last SWITCH if $code1 eq "B";
    	$_[0] = $bbox::SB_C, last SWITCH if $code1 eq "C";
	$l = 0;
    }
    $r_buff->{"Ptr"} += $l;
    return ($_[0] != -1);
}


#
#  decodes a bond length marker like S, N, L and returns length
#  BOOL $buf->GetBondLen($rLen)
#
sub GetBondLen
{
    my $r_buff = shift;
    my $l;
    my $bFound = 0;
    $_[0] = 0;
    while(1)
    {
	my $code = substr($r_buff->{"Buffer"}, $r_buff->{"Ptr"}, 1);
    	$_[0] += $main::rLenN, next if $code eq "N";
    	$_[0] += $main::rLenL, next if $code eq "L";
    	$_[0] += $main::rLenS, next if $code eq "S";
    	$_[0] += $main::rLenN/2, next if $code eq "n";
    	$_[0] += $main::rLenL/2, next if $code eq "l";
    	$_[0] += $main::rLenS/2, next if $code eq "s";
    	next if $code eq "0";
	last;
    } continue { $r_buff->{"Ptr"} += 1; $bFound = 1; }
    return ($bFound);
}
sub GetBondLenOrig
{
    my $r_buff = shift;
    my $code2 = substr($r_buff->{"Buffer"}, $r_buff->{"Ptr"}, 2);
    my $code1 = substr($r_buff->{"Buffer"}, $r_buff->{"Ptr"}, 1);
    my $l;
    $_[0] = -1;
    SWITCH: {
	$l = 2;
    	$_[0] = 2*$main::rLenN, last SWITCH if $code2 eq "NN";
    	$_[0] = 2*$main::rLenS, last SWITCH if $code2 eq "SS";
    	$_[0] = 2*$main::rLenL, last SWITCH if $code2 eq "LL";
	$l = 1;
    	$_[0] = $main::rLenN, last SWITCH if $code1 eq "N";
    	$_[0] = $main::rLenL, last SWITCH if $code1 eq "L";
    	$_[0] = $main::rLenS, last SWITCH if $code1 eq "S";
    	$_[0] = $main::rLenN/2, last SWITCH if $code1 eq "n";
    	$_[0] = $main::rLenL/2, last SWITCH if $code1 eq "l";
    	$_[0] = $main::rLenS/2, last SWITCH if $code1 eq "s";
    	$_[0] = 0, last SWITCH if $code1 eq "0";
	$l = 0;
    }
    $r_buff->{"Ptr"} += $l;
    return ($_[0] != -1);
}


#
#  decodes a bond type marker like -, =, L and returns type code
#  BOOL $buf->GetBondType($iType)
#
sub GetBondType
{
    my $r_buff = shift;
    my %codes = ( "->" => 10,  "-" => 0,    "<-" => 11,  "s" => 12,
		  "<<" => 2,   "<." => 3,   "o" => 4, 
   		  "=C" => 5,   "=U" => 6,   "=" => 7,    "3" => 8,
		  "p" =>1,     "t" => 9,    ">>" => 13,  ">." => 14,
		  "b" => 15,   "~" => 16
		);
    $_[0] = -1;
    
    foreach $elem (keys %codes)
    {
	if (substr($r_buff->{"Buffer"}, $r_buff->{"Ptr"}, length($elem)) eq
		$elem)
	{
	    $_[0] = $codes{$elem};
	    $r_buff->{"Ptr"} += length($elem);
	    last;
	}
    }
    return ($_[0] != -1);
}


#
#  decodes a arrow type marker like -, =, ... and returns type code
#  BOOL $buf->GetArrowType($iType)
#
sub GetArrowType
{
    my $r_buff = shift;
    my $code3 = substr($r_buff->{"Buffer"}, $r_buff->{"Ptr"}, 3);
    my $code2 = substr($r_buff->{"Buffer"}, $r_buff->{"Ptr"}, 2);
    my $code1 = substr($r_buff->{"Buffer"}, $r_buff->{"Ptr"}, 1);
    my $l;
    $_[0] = -1;
    SWITCH: {
	$l = 3;
    	$_[0] = 3, last SWITCH if $code3 eq "<=>";
    	$_[0] = 4, last SWITCH if $code3 eq "<->";
    	$_[0] = 5, last SWITCH if $code3 eq "-|>";
    	$_[0] = 6, last SWITCH if $code3 eq "<|-";
	$l = 2;
    	$_[0] = 1, last SWITCH if $code2 eq "->";
    	$_[0] = 2, last SWITCH if $code2 eq "<-";
	$l = 1;
    	$_[0] = 0, last SWITCH if $code1 eq "-";
	$l = 0;
    }
    $r_buff->{"Ptr"} += $l;
    return ($_[0] != -1);
}

#
#  decodes a marker type marker like -, =, ... and returns type code
#  BOOL $buf->GetMarkerType($iType)
#
sub GetMarkerType
{
    my $r_buff = shift;
    my $code2 = substr($r_buff->{"Buffer"}, $r_buff->{"Ptr"}, 2);
    my $code1 = substr($r_buff->{"Buffer"}, $r_buff->{"Ptr"}, 1);
    my $l;
    $_[0] = -1;
    SWITCH: {
	$l = 2;
    	$_[0] = 3, last SWITCH if $code2 eq "4f";
    	$_[0] = 1, last SWITCH if $code2 eq "of";
    	$_[0] = 5, last SWITCH if $code2 eq "rf";
    	$_[0] = 7, last SWITCH if $code2 eq "3f";
	$l = 1;
    	$_[0] = 0, last SWITCH if $code1 eq "o";
    	$_[0] = 2, last SWITCH if $code1 eq "4";
    	$_[0] = 4, last SWITCH if $code1 eq "r";
    	$_[0] = 6, last SWITCH if $code1 eq "3";
    	$_[0] = 8, last SWITCH if $code1 eq "+";
    	$_[0] = 9, last SWITCH if $code1 eq "x";
    	$_[0] = 10, last SWITCH if $code1 eq "*";
    	$_[0] = 11, last SWITCH if $code1 eq "n";
	$l = 0;
    }
    $r_buff->{"Ptr"} += $l;
    return ($_[0] != -1);
}


1;