package IniFile;
use JFile;
@ISA = qw(JFile);
#公開したいサブルーチン
#@EXPORT = qw();
use strict;
use Jcode;
use File::Path;
use File::Basename;
use File::Find;
use Utils;
use Deps;
#===============================================
# デバッグ関係変数
#===============================================
my $Debug = 0;
#===============================================
# 文字コード関係変数
#===============================================
# sjis, euc, jis, noconv, utf8
my $FileSystemCharCode = Deps::FileSystemCharCode();
my $FileCharCode = Deps::FileCharCode();
my $PerlCharCode = Deps::PerlCharCode();
my $MySQLCharCode = Deps::MySQLCharCode();
my $WebCharCode = Deps::WebCharCode();
my $WebCharSet = Deps::WebCharSet();
#===============================================
# スクリプト大域変数
#===============================================
my $LF = Deps::LF();
my $DirectorySeparator = Deps::DirectorySeparator();
my $OS = Deps::OS();
#===============================================
# パス(読み込みDB)
#===============================================
sub new
{
my ($module, $path, $CreateIniFile, $UseAsIniFile) = @_;
$UseAsIniFile = 1 if(!defined $UseAsIniFile);
my $this = {};
bless $this;
my $keyhead = $this->{SystemKeyHead} = "System/";
$this->{"${keyhead}UseAsIniFile"} = $UseAsIniFile;
if($path) {
$this->SetIniFile($path);
}
$CreateIniFile = 0 unless(defined $CreateIniFile);
$this->{"$this->{'SystemKeyHead'}CreateIniFile"} = $CreateIniFile;
return $this;
}
sub DESTROY
{
my $this = shift;
$this->SUPER::DESTROY(@_);
}
sub SetIniFile
{
my ($this, $ProgramPath, $keyhead, $UseIniFileName) = @_;
$keyhead = 'System/' if(!defined $keyhead);
if(!$this->{"${keyhead}UseAsIniFile"}) {
if($UseIniFileName) {
$this->{"${keyhead}IniFile"} = $ProgramPath;
}
return;
}
$this->{"SystemKeyHead"} = $keyhead;
$this->{"$this->{'SystemKeyHead'}ProgramFile"} = $ProgramPath;
#ファイル名を(ベース名, ディレクトリ名, 拡張子)に分解
my ($drive, $dir, $filename, $ext, $lastdir, $filebody)
= Deps::SplitFilePath($ProgramPath);
$dir =~ s/[\\\/]$//;
$this->{"${keyhead}MyDir"} = "$drive$dir";
if($UseIniFileName) {
$this->{"${keyhead}IniFile"} = Deps::MakePath("$drive$dir", "$filebody$ext", 0);
}
else {
$this->{"${keyhead}IniFile"} = Deps::MakePath("$drive$dir", "$filebody.ini", 0);
}
$this->{"${keyhead}WinDir"} = $ENV{'windir'};
$this->{"${keyhead}TempDir"} = $ENV{'temp'};
$this->{"${keyhead}TempDir"} = $ENV{'TMP_DIR'} unless($this->{'TempDir'});
$this->{"${keyhead}CPU"} = $ENV{'CPU'};
$this->{"${keyhead}ComSpec"} = $ENV{'ComSpec'};
$this->{"${keyhead}ComSpec"} = $ENV{'SHELL'} unless($this->{'ComSpec'});
$this->{"${keyhead}MachineType"} = $ENV{'MACHTYPE'};
$this->{"${keyhead}MachineType"} = 'i386' if($OS eq 'MSWin32');
$this->{"${keyhead}Vendor"} = $ENV{'VENDOR'};
$this->{"${keyhead}Vendor"} = 'intel' if($OS eq 'MSWin32');
$this->{"${keyhead}HostName"} = $ENV{'HOSTNAME'};
$this->{"${keyhead}HostName"} = $ENV{'COMPUTERNAME'} unless($this->{'HostName'});
$this->{"${keyhead}OSType"} = $ENV{'OSTYPE'};
$this->{"${keyhead}OSType"} = $OS unless($this->{'OSType'});
$this->{"${keyhead}Lang"} = $ENV{'LANG'};
$this->{"${keyhead}Lang"} = '' unless($this->{'Lang'});
if($Debug) {
print "ProgramFile: ", $this->ProgramFile(), "\n";
print "MyDir : ", $this->MyDir(), "\n";
print "IniFile : ", $this->IniFile(), "\n";
print "WinDir : ", $this->WinDir(), "\n";
}
}
sub IniFile
{
my ($this) = @_;
return $this->{"$this->{'SystemKeyHead'}IniFile"};
}
sub ProgramFile
{
my ($this) = @_;
return $this->{"$this->{'SystemKeyHead'}ProgramFile"};
}
sub MyDir
{
my ($this) = @_;
return $this->{"$this->{'SystemKeyHead'}MyDir"};
}
sub WinDir
{
my ($this) = @_;
return $this->{"$this->{'SystemKeyHead'}WinDir"};
}
sub TempDir
{
my ($this) = @_;
my $path = $this->{"$this->{'SystemKeyHead'}TempDir"};
return $path if($path);
return $ENV{temp} if($ENV{temp});
return $ENV{TMP_DIR} if($ENV{'TMP_DIR'});
}
sub Shell
{
my ($this) = @_;
return $this->{"$this->{'SystemKeyHead'}ComSpec"};
}
sub ComSpec
{
my ($this) = @_;
return $this->Shell();
}
sub HostName
{
my ($this) = @_;
return $this->{"$this->{'SystemKeyHead'}HostName"};
}
sub DeleteAllArrayItems
{
my ($this, $key) = @_;
for(my $i = 0 ; ; $i++) {
last if(!defined $this->{"$key$i"});
delete $this->{"$key$i"};
}
}
sub nArrayItems
{
my ($this, $key) = @_;
for(my $i = 0 ; ; $i++) {
if(!defined $this->{"$key$i"}) {
return $i;
}
}
return 0;
}
sub GetArray
{
my ($this, $key) = @_;
my @a;
for(my $i = 0 ; ; $i++) {
if(!defined $this->{"$key$i"}) {
return @a;
}
$a[$i] = $this->{"$key$i"};
}
}
sub AddArrayItems
{
my ($this, $key, @a) = @_;
my $n = $this->nArrayItems();
for(my $i = 0 ; $i < @a ; $i++) {
my $k = $n + $i;
$this->{"$key$k"} = $a[$i];
}
}
sub WriteAll
{
my ($this, $path, $separator) = @_;
if($path) {
$this->SetIniFile($path, "System/", 1);
}
else {
$path = $this->IniFile();
}
$separator = $this->{"$this->{'SystemKeyHead'}Separator"} if(!$separator);
$separator = '=' if(!$separator);
$this->{"$this->{'SystemKeyHead'}Separator"} = $separator;
my $keyhead = $this->{'SystemKeyHead'};
#print "write to [", $this->IniFile(), "]\n";
my $outfile = new JFile();
my $ret = $outfile->Open($this->IniFile(), "w");
#print "ret=$ret File[", $this->IniFile(), "]\n";
return undef if(!$ret);
for my $key (sort keys %$this) {
next if($key =~ /^$keyhead/i);
next if($key =~ /^SystemKeyHead$/i);
next if($key =~ /\//);
my $val = $this->{$key};
$outfile->print("$key$separator$val\n");
}
my $section = '';
for my $key (sort keys %$this) {
next if($key =~ /^$keyhead/i);
next if($key =~ /^SystemKeyHead$/i);
next if($key !~ /\//);
my $val = $this->{$key};
my ($sec, $key1) = ($key =~ /^([^\/]*)\/(.*)$/);
#print "sec: $sec, $key1\n";
if($sec ne $section) {
$section = $sec;
$outfile->print("\n");
$outfile->print("[$sec]\n");
}
$outfile->print("$key1$separator$val\n");
}
$outfile->Close();
return 1;
}
sub ReadAll
{
my ($this, $path, $separator, $RemoveQuotation) = @_;
$RemoveQuotation = 0 if(!defined $RemoveQuotation);
if($path) {
#print "path=$path\n";
$this->SetIniFile($path, "System/", 1);
}
$separator = $this->{"$this->{'SystemKeyHead'}Separator"} if(!$separator);
$separator = '=' if(!$separator);
$this->{"$this->{'SystemKeyHead'}Separator"} = $separator;
my $seqnum = 1;
my $infile = new JFile();
my $ret = $infile->Open($this->IniFile(), "r");
return undef if(!$ret);
my $section = '';
while(!$infile->eof()) {
my $line = $infile->ReadLine();
last if(!defined $line);
Utils::DelSpace($line);
next if($line eq '');
if($line =~ /^\[(.*)\]/) {
$section = $1;
#print "section=$section\n";
next;
}
my ($key, $val) = ($line =~ /^([^$separator]*)$separator(.*?)$/);
if($key) {
}
else {
$key = "Line$seqnum";
$val = $line;
$seqnum++;
}
if($section ne '') {
$key = "$section/$key";
}
if($RemoveQuotation) {
Utils::DelQuote($val);
}
$this->{$key} = $val;
#print "Read [$key]: $val\n";
}
$infile->Close();
return 1;
}
sub GetString
{
my ($this, $section, $key, $defstr) = @_;
#print "key: $section : $key\n";
return undef if(!defined $section);
return $this->GetSection() if(!defined $key);
$section = "[$section]";
# Jcode::convert(\$section, $PerlCharCode);
$section = Utils::RegExpQuote($section);
#print " sectoin: $section\n";
Jcode::convert(\$key, $PerlCharCode) if($key and $PerlCharCode);
$key = Utils::RegExpQuote($key);
#print " key: $key\n";
my $infile = new JFile;
my $ret = $infile->Open($this->IniFile(), "r", $PerlCharCode, 0);
unless($ret) {
if($this->{"$this->{'SystemKeyHead'}CreateIniFile"}) {
$infile->Open($this->IniFile(), "w", $PerlCharCode, 0);
$ret = $infile->Open($this->IniFile(), "r", $PerlCharCode, 0);
}
else {
print "Can not open [" . $this->IniFile() . "].
\n";
}
return undef;
}
#print "sec: $section
\n";
$infile->SkipTo("^$section", 0);
#my $l = $infile->ReadLine();
#print "next: $l
\n";
my $pos = $infile->tell(); #($infile->HANDLE());
my $line;
while($line = $infile->ReadLine()) {
#print "LINE2: $line
\n";
if($line =~ /^\[/) {
$infile->seek($pos, 0); #(INFILE, $pos, 0);
last;
}
#print " test: $line
\n";
unless($line =~ /^$key\s*=(.*)$/i) {
$pos = $infile->tell(); #(INFILE);
next;
}
#print " Found: $line
\n";
$infile->Close();
my $str = $1;
chomp($str);
$str =~ s/[\r\n]//g;
return $str;
}
$infile->Close();
#print "Def: $defstr
";
return $defstr;
}
sub GetSection
{
my ($this, $section) = @_;
return undef if(!defined $section);
$section = "[$section]";
$section = Utils::RegExpQuote($section);
my $infile = new JFile;
my $ret = $infile->Open($this->IniFile(), "r", $PerlCharCode, 0);
unless($ret) {
if($this->{"$this->{'SystemKeyHead'}CreateIniFile"}) {
$infile->Open($this->IniFile(), "w", $PerlCharCode, 0);
$ret = $infile->Open($this->IniFile(), "r", $PerlCharCode, 0);
}
else {
print "Can not open [" . $this->IniFile() . "].
\n";
}
return undef;
}
my %val;
$infile->SkipTo("^$section", 0);
my $line;
while($line = $infile->ReadLine()) {
last if($line =~ /^\[/);
Utils::DelSpace($line);
last if($line eq '');
my ($key, $value) = ($line =~ /^\s*(.*?)=(.*)\s*$/);
$val{$key} = $value if(defined $key and defined $value);
}
$infile->Close();
return %val;
}
sub WriteString
{
my ($this, $section, $key, $val) = @_;
return if(!defined $section or !defined $key or !defined $val);
my $originalsection = $section = "[$section]";
Jcode::convert(\$section, $PerlCharCode) if($section and $PerlCharCode);
my $RegSection = Utils::RegExpQuote($section);
Jcode::convert(\$key, $PerlCharCode) if($key and $PerlCharCode);
my $RegKey = Utils::RegExpQuote($key);
my $fname = $this->IniFile();
#print "fname: $fname\n";
my $infile = new JFile;
my $ret = $infile->Open($fname, "r", $PerlCharCode);
unless($ret) {
$ret = $infile->Open($fname, "w", $PerlCharCode);
if($ret) {
$infile->Close();
$ret = $infile->Open($fname, "r", $PerlCharCode);
}
if(!$ret) {
unless($this->{"$this->{'SystemKeyHead'}CreateIniFile"}) {
print "Can not read [$fname].\n";
return undef;
}
}
}
my @lines;
my $line;
my $IsSectionFound = 0;
while($line = $infile->ReadLine()) {
push(@lines, $line);
#sectionを見つけたら次へ行く
if($line =~ /^$RegSection/) {
$IsSectionFound = 1;
last;
}
}
#$fnameの中にsectionがない場合
unless($IsSectionFound) {
push(@lines, "$originalsection\n");
push(@lines, "$key=$val\n");
push(@lines, "\n");
}
#$fnameの中にsectionが見つかった場合
else {
my $pos = $infile->tell();
my $IsKeyFound = 0;
while($line = $infile->ReadLine()) {
if($line =~ /^\s*$RegKey=(.*)$/i) {
push(@lines, "$key=$val\n");
$IsKeyFound = 1;
last;
}
if($line =~ /^\s*\[/) {
$infile->seek($pos, 0);
last;
}
push(@lines, $line);
$pos = $infile->tell();
}
#sectionの中に$keyが見つからなかった場合
unless($IsKeyFound) {
my $s = $lines[@lines-1];
Utils::DelSpace($s);
pop(@lines) if($s eq '');
push(@lines, "$key=$val\n");
push(@lines, "\n") if($s eq '');
}
}
#残りの行を追加する
while($line = $infile->ReadLine()) {
push(@lines, $line);
}
$infile->Close();
my $ofile = new JFile;
unless($ofile->Open($fname, "w", $FileCharCode)) {
print "Can not write to [$fname].\n";
return "";
}
for(my $i = 0 ; $i < @lines ; $i++) {
$ofile->print($lines[$i]);
}
$ofile->Close();
return $val;
}
#int SaveWindowPosition(CWnd *pWnd, LPCSTR section, LPCSTR key);
#int GetWindowPosition(CREATESTRUCT *pcs, LPCSTR section, LPCSTR key);
#int ReadWindowPos(LPCSTR section, CREATESTRUCT& cs);
#int SaveWindowPos(LPCSTR section, CWnd *pWnd);
#int GetTempFileName(CString& filename);
#int RemoveTempDir(CString tempdir = "");
#LPCSTR GetTempDir() { return (LPCSTR)m_TempDir; };
#int CreateTempDir(LPCSTR prefix, LPCSTR dir = NULL);
#int LoadWindowPlacement(LPCSTR lpszSection, LPCSTR lpszKey, CREATESTRUCT& cs);
#int SaveWindowPlacement(LPCSTR lpszSection, LPCSTR lpszKey, CWnd *pWnd);
#int DeleteLogFont(LPCSTR lpszSection, LPCSTR lpszKey);
#int LoadLogFont(LPCSTR lpszSection, LPCSTR lpszKey, LOGFONT* logfont);
#int SaveLogFont(LPCSTR lpszSection, LPCSTR lpszKey, LOGFONT *logfont);
#BOOL IsValid();
#BOOL CreateIniFileName(HINSTANCE hInstance);
#BOOL WriteString(LPCSTR lpszSection, LPCSTR lpszKey, LPCSTR lpszString)
#BOOL WriteQuotedString(LPCSTR lpszSection, LPCSTR lpszKey, CString String, LPCSTR quote);
#BOOL WriteQuotedString(LPCSTR lpszSection, LPCSTR lpszKey, int index,
# CString String, LPCSTR quote);
#BOOL WriteString(LPCSTR lpszSection, LPCSTR lpszKey, CString String)
#BOOL WriteString(LPCSTR lpszSection, LPCSTR lpszKey, int index, LPCSTR lpszString)
#BOOL WriteInt(LPCSTR lpszSection, LPCSTR lpszKey, int i)
#BOOL WriteDouble(LPCSTR lpszSection, LPCSTR lpszKey, double f)
#BOOL WriteBool(LPCSTR lpszSection, LPCSTR lpszKey, BOOL b)
#BOOL Writebool(LPCSTR lpszSection, LPCSTR lpszKey, bool b)
#BOOL GetString(LPCSTR lpszSection, LPCSTR lpszKey, char *DefStr,
# char *lpszString, int len = 256)
#BOOL GetQuotedString(LPCSTR lpszSection, LPCSTR lpszKey, char *DefStr,
# CString& string, LPCSTR quote, int len = 256);
#BOOL GetQuotedString(LPCSTR lpszSection, LPCSTR lpszKey, int index, char *DefStr,
# CString& string, LPCSTR quote, int len = 256);
#BOOL GetString(LPCSTR lpszSection, LPCSTR lpszKey, char *DefStr,
# CString& string, int len = 256)
#BOOL GetString(LPCSTR lpszSection, LPCSTR lpszKey, int index, char *DefStr,
# char *lpszString, int len = 256)
#BOOL GetString(LPCSTR lpszSection, LPCSTR lpszKey, int index, char *DefStr,
# CString& strString, int len = 256)
#BOOL GetFileName(LPCSTR lpszSection, LPCSTR lpszKey, char *DefStr,
# char *lpszString, int len = 256)
#UINT GetInt(LPCSTR lpszSection, LPCSTR lpszKey, int dwDefault)
#double GetDouble(LPCSTR lpszSection, LPCSTR lpszKey, double dwDefault)
#BOOL GetBool(LPCSTR lpszSection, LPCSTR lpszKey, BOOL dwDefault);
#bool Getbool(LPCSTR lpszSection, LPCSTR lpszKey, bool dwDefault);
#BOOL SaveCurrentDir(LPCSTR pszSection, LPCSTR lpszKey, char *fname);
1;