_________________________
12 dd
DH: mm struct{
int a;
char b;
m1 struct {
int c;
int d;
};
m2 struct {
int e;
int f;
};
}
2 dfdkk
df
dd
}
1
LOCAL_PARAM: ssi_struct {
ref = 0;
header = {
req = 0xa;
};
}
DH: mm struct{
int a;
char b;
m1 struct {
int c;
int d;
};
}
____________________________________
####
sub get_record_from_file {
my $filein = shift;
my $regex = shift;
my $pos = shift
open FILEIN, "<$filein" or die "!$\n";
seek(FILEIN, $pos, 0);
my $in_block = 0;
my $last = "}";
my @array = {};
foreach my $line () {
if ($line =~ /$regex/) {
$in_block = 1;
}
if ($in_block) {
push @array, $line;
}
if ($line =~ m/$last$/) {
$in_block = 0;
}
}
$pos = tell FILEIN;
return (\@array, $pos);
}
$record = get_record_from_file("input.txt","DH:",0);
####
sub get_record_from_file {
my $filein = shift;
my $regex = shift;
my $pos = shift
open FILEIN, "<$filein" or die "!$\n";
seek(FILEIN, $pos, 0);
my $in_block = 0;
my $last = "}";
my @array = {};
foreach my $line () {
if ($line =~ /$regex/) {
$in_block = 1;
}
if ($in_block) {
push @array, $line;
}
if ($line =~ m/$last$/) {
$in_block = 0;
last;
}
}
$pos = tell FILEIN;
return (\@array, $pos);
}
my $curpos = 0;
($record,$curpos) = get_record_from_file("input.txt","DH:",$curpos);
####
#!/usr/bin/env perl
use strict;
use warnings;
my $sep = '=' x 60 . "\n";
(my $dh_file = $0) =~ s/pl$/dat/;
open_dh($dh_file);
while (1) {
print 'Get next DH record (y/n): ';
my $reply = <>;
if ($reply =~ /^y/i) {
print "Next DH record:\n";
my $dh_record = read_dh();
print "$sep$dh_record\n$sep";
if (eof_dh()) {
print "No more DH records!\n";
last;
}
}
else {
print "Stopped reading DH records.\n";
last;
}
}
close_dh();
{
my $dh_fh;
sub open_dh {
my $dh_file = shift;
open $dh_fh, '<', $dh_file or die "Can't read $dh_file: $!";
my $discard = read_dh();
return;
}
sub read_dh {
local $/ = 'DH: ';
my $dh_trim_re = qr{\A \s* (.*?) \s* (?: DH: \s )? \z}msx;
my $dh_record = <$dh_fh>;
$dh_record =~ s/$dh_trim_re/$1/;
return $dh_record;
}
sub close_dh {
close $dh_fh;
return;
}
sub eof_dh {
return eof $dh_fh;
}
}
####
$ pm_get_multi_rec.pl
Get next DH record (y/n): y
Next DH record:
============================================================
mm struct{
int a;
char b;
m1 struct {
int c;
int d;
};
m2 struct {
int e;
int f;
};
}
2 dfdkk
df
dd
}
1
LOCAL_PARAM: ssi_struct {
ref = 0;
header = {
req = 0xa;
};
}
============================================================
Get next DH record (y/n): y
Next DH record:
============================================================
mm struct{
int a;
char b;
m1 struct {
int c;
int d;
};
}
============================================================
No more DH records!