Plain.pm untested

filter: Statementstmt 0.0% 0 / 456 Branchbran 0.0% 0 / 88 Conditioncond 0.0% 0 / 36 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 0.0% 0 / 60 Podpod 0.0% 0 / 57 totaltotal 0.0% 0 / 721 CC 45 SCAR 28.1
CC
45
Cov
0%
apply_config
72.0
read_file_into_cache
56.0
new
30.0
CRAP 16.7
1 #####################################################################
2 # Plain.pm
3 # Copyright (c) 1999, 2000 by Markus Winand <mws@fatalmind.com>
4 #
5 # Base class for cached config file reading
6 #
7 # $Id: Plain.pm,v 1.11 2000/06/25 17:08:56 mws Exp $
8 #
9  
10 #####################################################################
11 # History see in pod part of file
12 #
13 # TODO:
14 # -> Translation tables
15 # -> includes
16 # -> time out
17 # -> cgi-script (and related api) for statistics/error reporting...
18  
19 package CONFIG::Plain;
20  
21 use strict;
22 use FileHandle;
23 use Cwd;
24  
25 #####################################################################
26 # initialize global structure for comment styles
27  
28 BEGIN {
29 $CONFIG::Plain::comment_style->{sh}->{COUNT}
30 = \&comment_type_sh_count;
31 $CONFIG::Plain::comment_style->{sh}->{REMOVE}
32 = \&comment_type_sh_remove;
33 $CONFIG::Plain::comment_style->{sh}->{ANTIESCAPE}
34 = \&comment_type_sh_antiescape;
35  
36 $CONFIG::Plain::comment_style->{'C++'}->{COUNT}
37 = \&comment_type_cpp_count;
38 $CONFIG::Plain::comment_style->{'C++'}->{REMOVE}
39 = \&comment_type_cpp_remove;
40 $CONFIG::Plain::comment_style->{'C++'}->{ANTIESCAPE}
41 = \&comment_type_cpp_antiescape;
42  
43 $CONFIG::Plain::comment_style->{'C'}->{COUNT}
44 = \&comment_type_c_count;
45 $CONFIG::Plain::comment_style->{'C'}->{REMOVE}
46 = \&comment_type_c_remove;
47 $CONFIG::Plain::comment_style->{'C'}->{ANTIESCAPE}
48 = \&comment_type_c_antiescape;
49
50 $CONFIG::Plain::comment_style->{asm}->{COUNT}
51 = \&comment_type_asm_count;
52 $CONFIG::Plain::comment_style->{asm}->{REMOVE}
53 = \&comment_type_asm_remove;
54 $CONFIG::Plain::comment_style->{asm}->{ANTIESCAPE}
55 = \&comment_type_asm_antiescape;
56  
57 $CONFIG::Plain::comment_style->{'sql'}->{COUNT}
58 = \&comment_type_sql_count;
59 $CONFIG::Plain::comment_style->{'sql'}->{REMOVE}
60 = \&comment_type_sql_remove;
61 $CONFIG::Plain::comment_style->{'sql'}->{ANTIESCAPE}
62 = \&comment_type_sql_antiescape;
63  
64 $CONFIG::Plain::comment_style->{'pascal'}->{COUNT}
65 = \&comment_type_pascal_count;
66 $CONFIG::Plain::comment_style->{'pascal'}->{REMOVE}
67 = \&comment_type_pascal_remove;
68 $CONFIG::Plain::comment_style->{'pascal'}->{ANTIESCAPE}
69 = \&comment_type_pascal_antiescape;
70  
71 $CONFIG::Plain::comment_style->{'regexp'}->{COUNT}
72 = \&comment_type_regexp_count;
73 $CONFIG::Plain::comment_style->{'regexp'}->{REMOVE}
74 = \&comment_type_regexp_remove;
75 $CONFIG::Plain::comment_style->{'regexp'}->{ANTIESCAPE}
76 = \&comment_type_regexp_antiescape;
77 }
78  
79 #####################################################################
80 # Important data structures used in this file
81 #
82 # the mayor variable is
83 # CONFIG::Plain::already_open_configs
84 # this hash holds a reference for every open file.
85 # the key is the ABSOLUTE filename.
86 # the data structure referenced by the value is called COMMON because
87 # it holds the information which have all instances in common.
88 # NOTE: this variable has got a class (no an object) scope!
89 #
90 # every object has got a referenc to the correspondenting COMMON structure
91 # called $self->{COMMON}
92 # note: one common structure may referenced by more then one object
93 #
94 # and every object has got a private structure to hold the cursors
95 # this structure is referenced by $self->{CURSORS}
96  
97  
98 #####################################################################
99 # new
100 #
101 # constructor
102 #
103 # parameters: 1st -> class
104 # 2nd -> scalar holding filename
105 # OR ref to hash holding config parameters
106 # including the filename in the "FILE" key
107 # or the data itselve in the "DATA" key
108 # 3rd -> ref to hash holding config parameters if the
109 # 2nd arg was a scalar
110 # 4th -> (optional) for internal use only
111 # include path (array ref)
112 # only allowed if 2nd arg is scalar!
113 # return -> undef or object referenc
114 sub new {
115 my $proto = shift;
116 my $class = ref($proto) || $proto;
117 my $self = {};
118 my $filename = shift;
119 my $config = shift;
120 my $include_path = shift;
121 my @include_path;
122 my $pwd;
123 my %COMMON;
124 my %CONFIG;
125 my $key;
126  
127 # check parameters
128 if (ref($filename) eq "HASH") {
129 $config = $filename;
130 $filename = $filename->{FILE};
131 }
132  
133 if (! defined $include_path) {
134 $include_path = [];
135 }
136
137 @include_path = ($include_path);
138  
139 # make absolute path
140 if ($filename !~ /^\//) {
141 $pwd = getcwd;
142 chomp($pwd);
143 $filename = $pwd ."/". $filename;
144 }
145
146 push @include_path, $filename;
147  
148 # if file is unknown
149 if (! defined $CONFIG::Plain::already_open_configs{$filename}) {
150 # create data struct for file
151 $COMMON{FILENAME} = $filename;
152 $COMMON{GLOBALERROR} = ();
153 $COMMON{USED} = 0;
154 $COMMON{ACTIVE} = 0;
155 $COMMON{READS} = 0;
156 $COMMON{CACHELINES} = 0;
157  
158 $CONFIG{COMMENT} = 'sh C++ C';
159 $CONFIG{ESCAPE} = '\\\\';
160 $CONFIG{DELEMPTYLINE} = 1;
161 $CONFIG{REMOVETRAILINGBLANKS} = 1;
162 $CONFIG{INCLUDE} = "include <(.*?)>";
163  
164 foreach $key (keys %$config) {
165 $CONFIG{$key} = $config->{$key};
166 }
167
168 $COMMON{CONFIG} = \%CONFIG;
169 $COMMON{CONFIG}->{COMMENT_FUNCTIONS} =
170 parse_comment_styles($COMMON{CONFIG}->{COMMENT});
171 $CONFIG::Plain::already_open_configs{$filename} = \%COMMON;
172 }
173 bless($self, $class);
174
175 $self->{COMMON} = $CONFIG::Plain::already_open_configs{$filename};
176 $self->{CURSORS}->{getline} = 0;
177 $self->{CURSORS}->{getline_error} = 0;
178 $self->{CURSORS}->{global_error} = 0;
179 $self->{COMMON}->{USED}++;
180 $self->{COMMON}->{ACTIVE}++;
181 $self->{INCLUDEPATH} = \@include_path;
182  
183 $self->{COMMON}->{REPARSE} = '0';
184 $self->read_file;
185  
186 return $self;
187 }
188  
189 sub close{
190 my ($self) = @_;
191 }
192  
193 sub reparse {
194 my ($self) = @_;
195  
196 return $self->{COMMON}->{REPARSE};
197 }
198  
199 sub open_configs {
200 my @list = ();
201 my $key;
202  
203 foreach $key (keys(%CONFIG::Plain::already_open_configs)) {
204 if (defined $CONFIG::Plain::already_open_configs{$key}) {
205 push @list, $key;
206 }
207 }
208 return @list;
209 }
210  
211 sub config_type($) {
212 my ($file) = @_;
213  
214 return $CONFIG::Plain::already_open_configs{$file}->{'_CODE_TYPE'};
215 }
216  
217 sub file_last_changed($) {
218 my ($self) = @_;
219 return $self->{COMMON}->{FILETIME};
220 }
221  
222 sub file_last_read($) {
223 my ($self) = @_;
224 return $self->{COMMON}->{LASTREAD};
225 }
226  
227 sub file_size($) {
228 my ($self) = @_;
229 return $self->{COMMON}->{FILEBYTES};
230 }
231  
232 sub file_lines($) {
233 my ($self) = @_;
234 return $self->{COMMON}->{FILELINES};
235 }
236  
237 sub cache_size($) {
238 my ($self) = @_;
239 return $self->{COMMON}->{CACHEBYTES};
240 }
241  
242 sub cache_lines($) {
243 my ($self) = @_;
244 return $self->{COMMON}->{CACHELINES};
245 }
246 sub file_read($) {
247 my ($self) = @_;
248 return $self->{COMMON}->{USED};
249 }
250  
251 sub file_config($) {
252 my ($self) = @_;
253 my %hash;
254
255 %hash = %{$self->{COMMON}->{CONFIG}};
256  
257 # remove internal stuff
258 delete $hash{COMMENT_FUNCTIONS};
259 return \%hash;
260 }
261  
262  
263 sub DESTROY {
264 my ($self) = @_;
265  
266 $self->{COMMON}->{ACTIVE}--;
267 }
268  
269 #####################################################################
270 # read_file
271 #
272 # check if file has to be (re)read
273 #
274 # parameters: 1st -> object ref
275 sub read_file ($) {
276 my ($self) = @_;
277 my @f_stat;
278  
279 $self->{COMMON}->{LASTCHECKED} = time;
280 $self->{COMMON}->{GLOBALERROR} = ();
281  
282 if (! defined $self->{COMMON}->{LASTREAD}) {
283 return $self->force_read_file;
284 }
285  
286 if (! defined $self->{COMMON}->{CONFIG}->{DATA}) {
287 # no reread required if DATA option was used
288 @f_stat = stat($self->{COMMON}->{FILENAME});
289
290 if ($f_stat[9] > $self->{COMMON}->{LASTREAD}) {
291 $self->{COMMON}->{LASTCHANGED} =
292 scalar(localtime($f_stat[9]));
293 return $self->force_read_file;
294 }
295 }
296 }
297  
298 #####################################################################
299 # force_read_file
300 #
301 # try to open file, (create error if neccessary) and set timestamp
302 sub force_read_file {
303 my ($self) = @_;
304 my $fh = new FileHandle;
305 my @f_stat;
306  
307 if (defined $self->{COMMON}->{CONFIG}->{DATA}) {
308 $self->{COMMON}->{LASTREAD} = time;
309 $self->{COMMON}->{FILETIME} = time;
310 $self->read_file_into_cache();
311 } elsif ($fh->open($self->{COMMON}->{FILENAME})) {
312 $self->{COMMON}->{LASTREAD} = time;
313 @f_stat = stat($fh);
314 $self->{COMMON}->{FILETIME} = $f_stat[9];
315 $self->read_file_into_cache($fh);
316 } else {
317 push(@{$self->{COMMON}->{GLOBALERROR}}, $!);
318 }
319 }
320  
321 #####################################################################
322 # read_file_into_cache
323 #
324 # reads the file linewhise into the memory and applyies the
325 # configuration options (remove comments and so on...)
326 #
327 # parameters: 1st -> object
328 # 2nd -> filehandle
329 sub read_file_into_cache {
330 my ($self, $fh) = @_;
331 my $linenr = undef; # first input_linenr of multi line
332 my $input_linenr = 0;
333 my $line;
334 my $long_line = "";
335 my $cache_bytes = 0;
336 my $file_bytes = 0;
337  
338 $self->{COMMON}->{REPARSE} = '1';
339 $self->{COMMON}->{PLAINFILE} = "";
340 $self->{COMMON}->{LINESFILE} = ();
341 $self->{COMMON}->{LINESFILE_unparsed} = ();
342 $self->{COMMON}->{INPUTLINE} = ();
343 $self->{COMMON}->{FILELINE} = ();
344 $self->{COMMON}->{GLOBALERROR} = ();
345 $self->{COMMON}->{LINEERROR} = ();
346
347 if (defined $fh) {
348 $line = $fh->getline;
349 } else {
350 if ($self->{COMMON}->{CONFIG}->{DATA} !~ m/\n$/s) {
351 $self->{COMMON}->{CONFIG}->{DATA} .= "\n";
352 }
353  
354 $self->{COMMON}->{CONFIG}->{DATA} =~ s/^(.*?\n)(.*)$/$2/s;
355 $line = $1;
356 }
357  
358 while (defined $line) {
359 $input_linenr ++;
360 $self->{COMMON}->{LINESFILE_unparsed}->[$input_linenr] = $line;
361 $file_bytes += length($line);
362 $long_line .= $line;
363 if ($self->apply_config(\$long_line)) {
364 # line contains "\n", so store in cache
365 if (! defined $linenr) {
366 $linenr = $input_linenr;
367 }
368 $self->check_for_include($long_line, $linenr);
369  
370 undef $linenr;
371 $long_line = "";
372 } else {
373 # line contains no "\n", so merge with next line
374 if (! defined $linenr && $long_line ne "") {
375 $linenr = $input_linenr;
376 }
377 }
378 if (defined $fh) {
379 $line = $fh->getline;
380 } else {
381 $self->{COMMON}->{CONFIG}->{DATA} =~
382 s/^(.*?\n)(.*)$/$2/s;
383 $line = $1;
384 }
385 }
386 $self->{COMMON}->{FILELINES} = $input_linenr;
387 $self->{COMMON}->{FILEBYTES} = $file_bytes;
388 $self->{COMMON}->{CACHEBYTES} = length($self->{COMMON}->{PLAINFILE});
389 $self->{COMMON}->{CACHELINES} = $#{$self->{COMMON}->{LINESFILE}}+1;
390 $self->{COMMON}->{READS}++;
391  
392 $self->{COMMON}->{_CODE_TYPE} = 'Plain';
393
394 $self->parse_file;
395 }
396  
397 sub check_for_include($$$) {
398 my ($self, $line, $linenr) = @_;
399 my ($before, $filename, $after);
400 my ($file, $pwd, $error, $src_line, $src_file);
401 my $cursor;
402  
403 my $hlp;
404  
405 $cursor = $#{$self->{COMMON}->{LINESFILE}}+1;
406  
407
408 if ($line =~ m/(.*?)$self->{COMMON}->{CONFIG}->{INCLUDE}(.*)/) {
409 $before = $1;
410 $filename = $2;
411 $after = $3;
412
413 # make absolute path
414 if ($filename !~ /^\//) {
415 $pwd = getcwd;
416 chomp($pwd);
417 $filename = $pwd ."/". $filename;
418 }
419  
420 if (! in_list($filename, $self->{INCLUDEPATH})) {
421 $file = CONFIG::Plain->new($filename,
422 $self->{COMMON}->{CONFIG},
423 $self->{INCLUDEPATH});
424 # global errors
425 while (defined($error = $file->getline_error)) {
426 push @{$self->{COMMON}->{LINEERROR}->
427 [$cursor]},
428 $error;
429 }
430 while (defined ($src_line = $file->getline())) {
431 $self->{COMMON}->{LINESFILE}->[$cursor] =
432 $src_line;
433 # errors to this line
434 while (defined ($error = $file->getline_error)){
435 push @{$self->{COMMON}->{LINEERROR}->
436 [$cursor]}, $error;
437 }
438 while (defined ($src_file = $file->getline_file)) {
439 push @{$self->{COMMON}->{FILELINE}->
440 [$cursor]}, $src_file;
441 push @{$self->{COMMON}->{FILELINE}->
442 [$cursor]}, $self->{COMMON}->{FILENAME};
443 push @{$self->{COMMON}->{INPUTLINE}->
444 [$cursor]}, $file->getline_number;
445 push @{$self->{COMMON}->{INPUTLINE}->
446 [$cursor]}, $linenr;
447 }
448 $cursor++;
449 }
450  
451 $line = $before . $after;
452
453 } else {
454 # CYCLIC include!!
455 push @{$self->{COMMON}->{LINEERROR}->
456 [$cursor]},
457 "Cyclic include ignored";
458
459 $line = $before . $after;
460 }
461  
462 } else {
463 # no include -> normal operation
464 }
465 push @{$self->{COMMON}->{LINESFILE}}, $line;
466 push @{$self->{COMMON}->{INPUTLINE}->[$cursor]}, $linenr;
467 push @{$self->{COMMON}->{FILELINE}->[$cursor]}, $self->{COMMON}->{FILENAME};
468  
469 $self->{COMMON}->{PLAINFILE} .= $line;
470 }
471  
472 #####################################
473 # overload this function..:)
474 #
475 sub parse_file {
476 my ($self) = @_;
477 }
478  
479 #####################################################################
480 # apply_config
481 #
482 # apllies the configuration to a source line
483 #
484 # parameters: 1st -> object-ref
485 # 2nd -> reference to scalar holding the sources
486 # return -> (the scalar referenced by the second argument
487 # may changed)
488 # the return value(boolsch) is true if the line
489 # was finished, the retrun value is false if
490 # the line is incomplete (e.g. multi-line comments)
491 sub apply_config {
492 my ($self, $line_ref) = @_;
493 my $line = $$line_ref;
494  
495 $line = $self->remove_comments($line);
496  
497 # NewLineESCape
498 if (defined $self->{COMMON}->{CONFIG}->{ESCAPE} &&
499 $self->{COMMON}->{CONFIG}->{ESCAPE} ne "") {
500 $line =~ m /($self->{COMMON}->{CONFIG}->{ESCAPE}+)\n/;
501 if ((length ($1) % 2) != 0) {
502 # new line is escaped -> delete it
503 $line =~ s/$self->{COMMON}->{CONFIG}->{ESCAPE}\n//;
504 }
505 }
506  
507 if ($self->{COMMON}->{CONFIG}->{DELEMPTYLINE}) {
508 if ($line eq "\n") {
509 $line = "";
510 }
511 }
512  
513 if ($self->{COMMON}->{CONFIG}->{REMOVETRAILINGBLANKS}) {
514 $line =~ s/^\s*//;
515 $line =~ s/\s*\n/\n/;
516 }
517
518 # now handle escaped escape signs :)
519 if (defined $self->{COMMON}->{CONFIG}->{ESCAPE} &&
520 $self->{COMMON}->{CONFIG}->{ESCAPE} ne "") {
521 $line =~ s/($self->{COMMON}->{CONFIG}->{ESCAPE}){2}/$1/g;
522 }
523  
524 $$line_ref = $line;
525  
526 if ($line =~ /\n/) {
527 return 1;
528 } else {
529 return 0;
530 }
531 }
532  
533 #####################################################################
534 # remove_comments
535 #
536 # delete the comments from the sources
537 #
538 # parameters: 1st -> object-ref
539 # 2nd -> scalar holding source
540 # return -> the scalar which was referenced by the 2nd parameter
541 # without comments OR (if a
542 # multi-line comment starts but doesn´t end) the
543 # source line without the trailing new line char
544 sub remove_comments {
545 my ($self, $line) = @_;
546 my ($type);
547 my ($hit_type, $hit_val, $hit_param, $parameter, $value);
548
549 do {
550 $hit_type = 0;
551 $hit_val = 0;
552 $hit_param = 0;
553 foreach $type (keys %{$self->{COMMON}->
554 {CONFIG}->{COMMENT_FUNCTIONS}}) {
555 foreach $parameter (@{$self->{COMMON}->{CONFIG}->
556 {COMMENT_FUNCTIONS}->{$type}}) {
557 $value = &{$CONFIG::Plain::comment_style->
558 {$type}->{COUNT}}
559 ($self, $line, $parameter);
560  
561 if (($value > 0) && (($value < $hit_val)
562 || ($hit_val == 0))) {
563 $hit_val = $value;
564 $hit_type = $type;
565 $hit_param= $parameter;
566 }
567 }
568 }
569 if ($hit_val > 0) {
570 $line = &{$CONFIG::Plain::comment_style->
571 {$hit_type}->{REMOVE}}
572 ($self, $line, $hit_param);
573 }
574 } while (($hit_val > 0) && ($line =~ /\n$/));
575  
576 if ($line =~ /\n$/) {
577 foreach $type (keys %{$self->{COMMON}->{CONFIG}->
578 {COMMENT_FUNCTIONS}}) {
579 foreach $parameter (@{$self->{COMMON}->{CONFIG}->
580 {COMMENT_FUNCTIONS}->{$type}}) {
581 $line = &{$CONFIG::Plain::comment_style->
582 {$type}->{ANTIESCAPE}}
583 ($self, $line, $parameter);
584 }
585 }
586 }
587 return $line;
588 }
589  
590 #####################################################################
591 # parse_comment_styles
592 #
593 # parses the comment discription line (scalar) and creates a hash
594 # holding the comemnt types as key and the parameters as value
595 #
596 # parameters: 1st -> scalar holding comment style description
597 # return -> reference to hash holding COMMENT_FUNCTION struct
598 sub parse_comment_styles {
599 my ($config) = @_;
600 my ($style, $type, $parameters);
601 my @styles;
602 my %COMMENT_FUNCTIONS = ();
603  
604 while (($config =~ s/^(.*?[^\\]) (.*)$/$2/) || ($config =~ s/(.+)//)) {
605 $style = $1;
606 if ($style =~ /^(.*):(.*)/) {
607 # parameters given
608 $type = $1;
609 $parameters = $2;
610 } else {
611 # no parameters given
612 $type = $style;
613 $parameters = '';
614 }
615 if (defined $CONFIG::Plain::comment_style->{$type}) {
616 push (@{$COMMENT_FUNCTIONS{$type}}, $parameters);
617 }
618 }
619 return \%COMMENT_FUNCTIONS;
620 }
621  
622 sub getline_unparsed($$) {
623 my ($self, $linenr) = @_;
624  
625 return $self->{COMMON}->{LINESFILE_unparsed}->[$linenr];
626 }
627  
628 #####################################################################
629 # get_errors
630 #
631 # returns a scalar holding a preformated string with all error
632 # messages occured while parsing the file.
633 # NOTE: this method uses the api, so the CURSORS are resettet
634 # after this method
635 #
636 # Include support: Full
637 #
638 sub get_errors($) {
639 my ($self) = @_;
640 my $outtext = '';
641 my ($line, $error, $filename);
642  
643 $self->getline_reset();
644  
645 # global errors
646 while (defined ($error = $self->getline_error)) {
647 $outtext .= $error . "\n";
648 }
649  
650 while (defined ($line = $self->getline)) {
651 $error = $self->getline_error;
652 if (defined $error) {
653 $outtext .= sprintf("ERROR in %s:%d\n",
654 $self->getline_file, $self->getline_number);
655  
656 while ($filename = $self->getline_file) {
657 $outtext .= sprintf(" included from %s:%d\n",
658 $filename,
659 $self->getline_number)
660 }
661 do {
662 $outtext .= sprintf(" %s\n", $error);
663 } while (defined ($error = $self->getline_error));
664 }
665 }
666  
667 $self->getline_reset();
668
669 return $outtext;
670 }
671  
672  
673  
674 #####################################################################
675 # getfile
676 #
677 # returns a scalar holding the whole file
678 sub getfile {
679 my ($self) = @_;
680  
681 return $self->{COMMON}->{PLAINFILE};
682 }
683  
684 #####################################################################
685 # getline
686 #
687 # returns the file linewhise, returns undef on end of file
688 sub getline {
689 my ($self) = @_;
690 my $line = "";
691
692 if ($self->{CURSORS}->{getline} >= $self->{COMMON}->{CACHELINES}) {
693 undef $line;
694 } else {
695 $line = $self->{COMMON}->{LINESFILE}->
696 [$self->{CURSORS}->{getline}];
697 $self->{CURSORS}->{getline}++;
698 }
699 $self->{CURSORS}->{getline_error} = 0;
700 $self->{CURSORS}->{getline_number} = 0;
701 $self->{CURSORS}->{getline_file} = 0;
702 return $line;
703 }
704  
705 #####################################################################
706 # getline_cursor
707 #
708 # returns the internal linecursor, usefull only for the setline_error
709 # method
710 sub getline_cursor {
711 my ($self) = @_;
712  
713 return $self->{CURSORS}->{"getline"}-1;
714 }
715  
716 #####################################################################
717 # getline_number
718 #
719 # returns the input line number of the last line got via getline
720 sub getline_number {
721 my ($self) = @_;
722 my $rc;
723  
724 $rc = $self->{COMMON}->{INPUTLINE}->[$self->{CURSORS}->{"getline"}-1]->
725 [$self->{CURSORS}->{"getline_number"}];
726 $self->{CURSORS}->{"getline_number"}++;
727
728 if ((! defined $rc) || ($self->{CURSORS}->{"getline_number"} < 1)) {
729 return undef;
730 } else {
731 return $rc;
732 }
733 }
734  
735 #####################################################################
736 # getline_file
737 #
738 # returns the filename of the file from which the last line got via
739 # getline comes.
740 sub getline_file {
741 my ($self) = @_;
742 my $rc;
743  
744 $rc = $self->{COMMON}->{FILELINE}->[$self->{CURSORS}->{"getline"}-1]->
745 [$self->{CURSORS}->{"getline_file"}];
746 $self->{CURSORS}->{"getline_file"}++;
747  
748 if ((! defined $rc) || ($self->{CURSORS}->{"getline_file"} < 1)) {
749 return undef;
750 } else {
751 return $rc;
752 }
753 }
754  
755 #####################################################################
756 # getline_error
757 #
758 # returns errormessages of this line
759 sub getline_error {
760 my ($self) = @_;
761 my $rc = undef;
762  
763 if ($self->{CURSORS}->{"getline"} == 0) {
764 $rc = $self->{COMMON}->{GLOBALERROR}[
765 $self->{CURSORS}->{global_error}];
766 $self->{CURSORS}->{global_error}++;
767 } else {
768 $rc = $self->{COMMON}->{LINEERROR}->
769 [$self->{CURSORS}->{"getline"}-1]->
770 [$self->{CURSORS}->{"getline_error"}];
771 $self->{CURSORS}->{"getline_error"}++;
772 }
773 return $rc;
774 }
775  
776 #####################################################################
777 # getline_reset
778 #
779 # resets the linepointer for getline
780 sub getline_reset {
781 my ($self) = @_;
782  
783 $self->{CURSORS}->{"getline"} = 0;
784 }
785  
786 #####################################################################
787 # setline_error
788 #
789 # stores a error message to the last line got via getline
790 #
791 # Parameters: 1st -> object
792 # 2nd -> error string
793 # 3rd -> (optional) line nr. (if ommited, error is assigned
794 # to the last line got via getline)
795 sub setline_error {
796 my ($self, $error, $line_cursor) = @_;
797  
798 if (! defined $line_cursor) {
799 if ($self->{CURSORS}->{"getline"} > 0) {
800 push @{$self->{COMMON}->{LINEERROR}->
801 [$self->{CURSORS}->{"getline"}-1]}, $error;
802 }
803 } else {
804 push @{$self->{COMMON}->{LINEERROR}->[$line_cursor]}, $error;
805 }
806 }
807  
808 sub setglobal_error {
809 my ($self, $error) = @_;
810  
811 push @{$self->{COMMON}->{GLOBALERROR}}, $error;
812 }
813  
814 #####################################################################
815 #
816 # stupid translations functions for comment styles ;)
817  
818 ##### sh #####
819  
820 sub comment_type_sh_count {
821 my ($self, $line, $param) = @_;
822 return comment_type_regexp_count($self, $line, "#|\$");
823 }
824  
825 sub comment_type_sh_remove {
826 my ($self, $line, $param) = @_;
827 return comment_type_regexp_remove($self, $line, "#|\$");
828 }
829  
830 sub comment_type_sh_antiescape {
831 my ($self, $line, $param) = @_;
832 return comment_type_regexp_antiescape($self, $line, "#|\$");
833 }
834  
835 ##### C++ #####
836  
837 sub comment_type_cpp_count {
838 my ($self, $line, $param) = @_;
839 return comment_type_regexp_count($self, $line, "\/\/|\$");
840 }
841  
842 sub comment_type_cpp_remove {
843 my ($self, $line, $param) = @_;
844 return comment_type_regexp_remove($self, $line, "\/\/|\$");
845 }
846  
847 sub comment_type_cpp_antiescape {
848 my ($self, $line, $param) = @_;
849 return comment_type_regexp_antiescape($self, $line, "\/\/|\$");
850 }
851  
852 ##### C #####
853  
854 sub comment_type_c_count {
855 my ($self, $line, $param) = @_;
856 return comment_type_regexp_count($self, $line, "\/[*]|[*]\nn/");
857 }
858  
859 sub comment_type_c_remove {
860 my ($self, $line, $param) = @_;
861 return comment_type_regexp_remove($self, $line, "\/[*]|[*]\/");
862 }
863  
864 sub comment_type_c_antiescape {
865 my ($self, $line, $param) = @_;
866 return comment_type_regexp_antiescape($self, $line, "\/[*]|[*]\/");
867 }
868  
869 ##### asm #####
870  
871 sub comment_type_asm_count {
872 my ($self, $line, $param) = @_;
873 return comment_type_regexp_count($self, $line, ";|\$");
874 }
875  
876 sub comment_type_asm_remove {
877 my ($self, $line, $param) = @_;
878 return comment_type_regexp_remove($self, $line, ";|\$");
879 }
880  
881 sub comment_type_asm_antiescape {
882 my ($self, $line, $param) = @_;
883 return comment_type_regexp_antiescape($self, $line, ";|\$");
884 }
885  
886 ##### sql #####
887  
888 sub comment_type_sql_count {
889 my ($self, $line, $param) = @_;
890 return comment_type_regexp_count($self, $line, "--|\$");
891 }
892  
893 sub comment_type_sql_remove {
894 my ($self, $line, $param) = @_;
895 return comment_type_regexp_remove($self, $line, "--|\$");
896 }
897  
898 sub comment_type_sql_antiescape {
899 my ($self, $line, $param) = @_;
900 return comment_type_regexp_antiescape($self, $line, "--|\$");
901 }
902  
903 ##### pascal #####
904  
905 sub comment_type_pascal_count {
906 my ($self, $line, $param) = @_;
907 return comment_type_regexp_count($self, $line, "\{|\}");
908 }
909  
910 sub comment_type_pascal_remove {
911 my ($self, $line, $param) = @_;
912 return comment_type_regexp_remove($self, $line, "\{|\}");
913 }
914  
915 sub comment_type_pascal_antiescape {
916 my ($self, $line, $param) = @_;
917 return comment_type_regexp_antiescape($self, $line, "\{|\}");
918 }
919  
920 sub comment_type_regexp_count {
921 my ($self, $line, $param) = @_;
922 my ($start_re, $stop_re) = split /\|/, $param;
923 my $before_comment = "";;
924 my $comment_found;
925
926 ($before_comment, $comment_found, undef) =
927 search_unescaped_regexp($line, $start_re,
928 $self->{COMMON}->{CONFIG}->{ESCAPE});
929
930 if ($comment_found) {
931 return length($before_comment)+1;
932 } else {
933 return 0;
934 }
935 }
936  
937 sub comment_type_regexp_remove {
938 my ($self, $line, $param) = @_;
939 my ($start_re, $stop_re) = split /\|/, $param;
940 my $before_comment = "";;
941 my ($comment, $matched_start_re, $matched_stop_re);
942  
943 ($before_comment, $matched_start_re, $line) =
944 search_unescaped_regexp($line, $start_re,
945 $self->{COMMON}->{CONFIG}->{ESCAPE});
946
947 ($comment, $matched_stop_re, $line) =
948 search_unescaped_regexp($line, $stop_re,
949 $self->{COMMON}->{CONFIG}->{ESCAPE});
950  
951 if (! defined $matched_stop_re) {
952 chomp $line;
953 } else {
954 $matched_start_re = "";
955 $comment = "";
956 $matched_stop_re = "";
957 }
958  
959 return $before_comment . $matched_start_re . $comment .
960 $matched_stop_re . $line;
961 }
962  
963 sub comment_type_regexp_antiescape {
964 my ($self, $line, $param) = @_;
965 my ($start_re, $stop_re) = split /\|/, $param;
966 my ($before_comment) = ("");
967  
968 ($before_comment, undef, $line) =
969 search_unescaped_regexp($line, $start_re,
970 $self->{COMMON}->{CONFIG}->{ESCAPE}, 1);
971  
972 return $before_comment . $line;
973 }
974  
975 sub in_list {
976 my ($scalar, $list) = @_;
977 my $element;
978 my $count = 0;
979 my @list = @{$list};
980  
981 foreach $element (@list) {
982 if ($element eq $scalar) {
983 $count ++;
984 }
985 }
986 return $count;
987 }
988  
989  
990 sub search_unescaped_regexp {
991 my ($line, $regexp, $esc, $remove_escapes) = @_;
992 my ($before_regexp, $matched_regexp) = ("", "");
993 my ($len, $found) = (0, 0);;
994  
995 do {
996 $before_regexp .= $matched_regexp;
997 if ($found = ($line =~ /^(.*?)($regexp)(.*)$/s)) {
998 $before_regexp .= $1;
999 $matched_regexp = $2;
1000 $line = $3;
1001 $before_regexp =~ m/($esc*)$/;
1002 $len = length($1);
1003 if ((($len % 2) != 0) && $remove_escapes) {
1004 $before_regexp =~ s/$esc$//;
1005 }
1006 }
1007 } while ((($len % 2) != 0) && ($found));
1008  
1009 if (! $found) {
1010 $matched_regexp = undef;
1011 }
1012 return ($before_regexp, $matched_regexp, $line);
1013 }
1014  
1015 1;
1016  
1017  
1018 __END__