r_kar
asked on
Windows type ini file modification
Hi,
I have some code, which is used to get the values for a windows type ini file.
My code works fine. I would like to Modify the program according to the option or brand choice.
This is my code ..........
--------------
#!/usr/bin/perl
use strict;
use warnings;
my $brand = shift || die "Must give a brand\n";
my %data;
while (<DATA>) {
chomp;
next unless /^\[$brand\]$/ .. !/\S/;
next if /^\[/;
next unless /\S/;
my ($k, $v) = split /\s*=\s*/, $_, 2;
$data{$k} = $v;
}
foreach (keys %data) {
print "$_ => $data{$_}\n";
}
This is my config file.... (logins_test_1.cfg)
-------------------------- ----------
[NEWBRAND]
#LOGIN FOR THAT PARTICULAR BRAND
#Get the LOgin and Password according to Brand
[AN]
LOGIN = H006698
PASSWORD = $"?Y??
[AZ]
LOGIN = H006699
PASSWORD = ???S?
[CL]
LOGIN = H007985
PASSWORD = 8?|?E?0
[CM]
LOGIN = H006897
PASSWORD = $"?Y??
[CU]
LOGIN = H006701
PASSWORD = $"?Y??
[GM]
LOGIN = H006703
PASSWORD = 8?|?E?0
-------------------------- ----
Suppose if i want to Edit the values for brand or option say [CM] with another set of LOGIN and PASSWORD.... How can i do that?
My next Question is , How to add New Brand say[ZZ] in this file, by making use of two functiions .
1. function to Add LOGIN (will pass 2 arguments say brand and login)
2, function to Add PASSWORD ((will pass 2 arguments say brand and password)
I tried as follows and its working:
$brand = "MO";
$userid="john";
$password="test";
&add_user_id($brand,$useri d);
&add_password($brand,$pass word);
sub add_user_id
{
#------------------------- ---------- ---------- ---------- ---------- ---------- ---
my ($brand,$uid) = @_;
my ($ok,$ans,$flag);
$ans = read_config_file($brand);
print "Here ... $ans\n";
if($ans > 0)
{
#$ok = "Can not Add userid... Already Exists!!";
$flag = 0;
}
else
{
$ok = $uid;
$flag = 1;
open(FH,">>logins_test_1.c fg");
flock(FH,2);
print FH "[".$brand."]";
print FH "\n";
print FH "LOGIN = ".$ok;
print FH "\n";
flock(FH,8);
close(FH);
}
#return ($ok,$flag,$brand);
return $ok;
}
#------------------------- ---------- ---------- ---------- ---------- ---------- ---
#------------------------- ---------- ---------- ---------- ---------- ---------- ---
sub add_password {
#------------------------- ---------- ---------- ---------- ---------- ---------- ---
my ($brand,$pwd) = @_;
my $ok1;
if($ok1)
{
$flag = 1;
open(FH,">>logins_test_1.c fg");
flock(FH,2);
print FH "PASSWORD = ".$ok1;
print FH "\n";
flock(FH,8);
close(FH);
}
else
{
print "Error ...Can not encrypt the Password\n";
$flag = 0;
}
return $ok1;
}
I would like to Store the Password under the brand. Please help me to do that
Regards,
r_k
I have some code, which is used to get the values for a windows type ini file.
My code works fine. I would like to Modify the program according to the option or brand choice.
This is my code ..........
--------------
#!/usr/bin/perl
use strict;
use warnings;
my $brand = shift || die "Must give a brand\n";
my %data;
while (<DATA>) {
chomp;
next unless /^\[$brand\]$/ .. !/\S/;
next if /^\[/;
next unless /\S/;
my ($k, $v) = split /\s*=\s*/, $_, 2;
$data{$k} = $v;
}
foreach (keys %data) {
print "$_ => $data{$_}\n";
}
This is my config file.... (logins_test_1.cfg)
--------------------------
[NEWBRAND]
#LOGIN FOR THAT PARTICULAR BRAND
#Get the LOgin and Password according to Brand
[AN]
LOGIN = H006698
PASSWORD = $"?Y??
[AZ]
LOGIN = H006699
PASSWORD = ???S?
[CL]
LOGIN = H007985
PASSWORD = 8?|?E?0
[CM]
LOGIN = H006897
PASSWORD = $"?Y??
[CU]
LOGIN = H006701
PASSWORD = $"?Y??
[GM]
LOGIN = H006703
PASSWORD = 8?|?E?0
--------------------------
Suppose if i want to Edit the values for brand or option say [CM] with another set of LOGIN and PASSWORD.... How can i do that?
My next Question is , How to add New Brand say[ZZ] in this file, by making use of two functiions .
1. function to Add LOGIN (will pass 2 arguments say brand and login)
2, function to Add PASSWORD ((will pass 2 arguments say brand and password)
I tried as follows and its working:
$brand = "MO";
$userid="john";
$password="test";
&add_user_id($brand,$useri
&add_password($brand,$pass
sub add_user_id
{
#-------------------------
my ($brand,$uid) = @_;
my ($ok,$ans,$flag);
$ans = read_config_file($brand);
print "Here ... $ans\n";
if($ans > 0)
{
#$ok = "Can not Add userid... Already Exists!!";
$flag = 0;
}
else
{
$ok = $uid;
$flag = 1;
open(FH,">>logins_test_1.c
flock(FH,2);
print FH "[".$brand."]";
print FH "\n";
print FH "LOGIN = ".$ok;
print FH "\n";
flock(FH,8);
close(FH);
}
#return ($ok,$flag,$brand);
return $ok;
}
#-------------------------
#-------------------------
sub add_password {
#-------------------------
my ($brand,$pwd) = @_;
my $ok1;
if($ok1)
{
$flag = 1;
open(FH,">>logins_test_1.c
flock(FH,2);
print FH "PASSWORD = ".$ok1;
print FH "\n";
flock(FH,8);
close(FH);
}
else
{
print "Error ...Can not encrypt the Password\n";
$flag = 0;
}
return $ok1;
}
I would like to Store the Password under the brand. Please help me to do that
Regards,
r_k
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.