Secure CRT INI Perl Module

From Internetworkpro

Jump to: navigation, search
This page is currently in progress and is not completed
Please note that the information on this page is pending completion by the author. You can help contribute by using the edit tab above.
See where else you can help at Category:InProgress
package Config::SecureCRTSession;
 
#
#
#
#
 
use strict;
use Carp;
use Data::Dumper;
 
my $VERSION = '1.0';
my $DEBUG = 1;
 
#--------------------------
# Public Methods
#--------------------------
sub new {
	my $class = shift;
  my $self = { 
  	_filename => undef,  
  	_data     => undef
  };
  bless $self, $class;
  return $self;
}
 
sub read() {
	my ($self, $filename) = @_;
 
	if ($filename) {
#		$self->{$_filename} = $filename;
	  $self->{'_filename'} = $filename; 
	}
 
  print "Opening file " .$self->{'_filename'}. " for reading\n" if ($DEBUG);
  my $fh = $self->__get_fh($self->{'_filename'}, 'O_RDONLY') or return undef;
 
  unless ( seek($fh, 0, 0) ) {
    croak("Couldn't seek($fh, 0, 0): $!");
    return undef;
  }
 
  my $last_key = "";
  my $last_type = "";
  my $line_count = 0;
 
  while (<$fh>) {
 
  	$line_count++;
    print "Reading line $line_count\n" if ($DEBUG);
 
    if (my ($type, $key, $val) = /^(\w):"([^"]+)"=([^\n]*)$/) {
      print "Found key: $key\n  Type:  $type\n  Value: $val\n" if ($DEBUG);
      $self->{_data}{$key}{'value'} = $val;
      $self->{_data}{$key}{'type'}  = $type;
      $last_key = $key;
      $last_type = $type;
    }
    if (my ($bytes) = /^((\s\w{2})+)$/) {
    	if ($last_type != "B") {
    		die "Error: Got hex line but previous value not type 'B'";
    	} else {
    		print "  $bytes\n";
  	  $self->{_data}{$last_key}{'bytes'} =+ "$bytes\n"
  	  }
  	}
  }
 
  close ($fh);
}
 
sub param
{
	my $self = shift;
 
	# return all keys
	unless ( @_ ) {
    return keys %{$self->{'_data'}};
  }
 
}
 
 
 
sub write
{
	my ($self, $filename) = @_;
 
	if ($filename) {
	  $self->{'_filename'} = $filename; 
	}
 
  print "Opening file " . $self->{'_filename'} . " for writing\n" if ($DEBUG);
 
  while( my ($k, $v) = each %{$self->{_data}} ) {
  	print "key: $k\n  value: ". $v->{value} . "\n";
  }
}
 
sub dump {
	my $self = shift;
	return Dumper($self->{_data});
}
 
#---------------------------
# Private Methods
#---------------------------
sub __get_fh {
  my ($self, $arg, $mode) = @_;  
 
  unless ( defined $arg ) {
    croak "__get_fh(): filename is missing";
  }
  if ( ref($arg) && (ref($arg) eq 'GLOB') ) {
    return ($arg, 0);
  }
  unless ( defined $mode ) {
      $mode = "O_RDONLY";
  }
  unless ( sysopen(FH, $arg, $mode) ) {
    croak("couldn't open $arg: $!");
    return undef;
  }
  return \*FH,;
}
 
 
 
 
 
package main;
 
my $conf = Config::SecureCRTSession->new();  
$conf->read("C:\\Docs\\Devel\\Perl\\default-session.ini");
print $conf->dump();
print $conf->param();
#$conf->write("C:\\Docs\\Devel\\Perl\\default-session-new.ini");
Personal tools