package MemoryFile; use Common; @ISA = qw(Common); #公開したいサブルーチン #@EXPORT = qw(); use strict; use Jcode; use Deps; use JFile; #=============================================== # デバッグ関係変数 #=============================================== my $Debug = 0; #=============================================== # 文字コード関係変数 #=============================================== # sjis, euc, jis, noconv, utf8 my $FileSystemCharCode = Deps::FileSystemCharCode(); 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(); #=============================================== # コンストラクタ・デストラクタ #=============================================== sub new { my ($module, $buff) = @_; my $this = {}; bless $this; $this->Open($buff); return $this; } sub DESTROY { my $this = shift; $this->Close(); } sub Initialize { my ($this) = @_; } #=============================================== # 変数取得関数 #=============================================== sub SetSourceCharCode { my ($this, $c) = @_; return $this->{SoruceCharCode} = $c; } sub SourceCharCode { return shift->{SoruceCharCode}; } sub CharCode { my ($this) = @_; return $this->{'CharCode'}; } sub SetBuffer { my ($this,$s)=@_; reutrn $this->Open($s); } sub Buffer { return shift->{Buffer}; } #=============================================== # 一般メンバ関数 #=============================================== sub Open { my ($this, $buff) = @_; $this->{Pos} = 0; return $this->{Buffer} = $buff; } sub ReadFromFile { my ($this, $path) = @_; my $s = JFile->new()->ReadFile($path, "sjis"); $this->{Pos} = 0; return $this->{Buffer} = $s; } sub Close { my ($this) = @_; delete $this->{Buffer}; $this->{Pos} = 0; } sub SetSourceCharCode { my ($this, $c) = @_; return $this->{SoruceCharCode} = $c; } sub SourceCharCode { return shift->{SoruceCharCode}; } sub read { my ($this, $s, $len, $offset) = @_; return $_[1] = substr($this->{Buffer}, $offset, $len); } sub sysread { my ($this, $s, $len, $offset) = @_; return $this->read($s, $len, $offset); } sub syswrite { my ($this, $s, $len, $offset) = @_; my $s0 = substr($this->{Buffer}, 0, $offset); retturn $this->{Buffer} = "$s0$s"; } sub getc { my ($this) = @_; my $c = substr($this->{Buffer}, $this->{Pos}, 1); $this->{Pos}++; return $c; } sub truncate { my ($this, $length) = @_; return $this->{Buffer} = substr($this->{Buffer}, 0, $length); } sub eof { my ($this) = @_; my $len = length($this->{Buffer}); #print "l: $len - $this->{Pos}\n"; return 1 if($len <= $this->{Pos}); return 0; } sub ReadLine { my ($this) = @_; my $s = substr($this->{Buffer}, $this->{Pos}); my ($l) = ($s =~ /^([^\r\n]*?[$\r\n])/); if(!defined $l) { ($l) = ($s =~ /^([^\r\n]*?)$/); } $this->{Pos} += length($l); return $l; } sub PreReadLine { my ($this) = @_; my $s = substr($this->{Buffer}, $this->{Pos}); my ($l) = ($s =~ /^([^\r\n]*?)[\r\n$]/); return $l; } sub Write { my ($this, $str) = @_; Jcode::convert(\$str, $this->{'CharCode'}) if($str and $this->{'CharCode'}); my $s0 = substr($this->{Buffer}, 0, $this->{Pos}); retturn $this->{Buffer} = "$s0$str"; } sub print { my ($this, @args) = @_; for(my $i = 0 ; $i < @args ; $i++) { next if($args[$i] eq ''); if($this->{CharCode} and $this->{CharCode} ne 'ascii') { if($this->{SourceCharCode} and $this->{SourceCharCode} ne 'ascii') { Jcode::convert(\$args[$i], $this->{CharCode}, $this->{SourceCharCode}) if($this->{SourceCharCode} ne $this->{CharCode}); } else { Jcode::convert(\$args[$i], $this->{CharCode}); } } $this->Write($args[$i]); } } sub printf { my ($this, @args) = @_; for(my $i = 0 ; $i < @args ; $i++) { Jcode::convert(\$args[$i], $this->{'CharCode'}) if($args[$i] and $this->{'CharCode'}); $this->Write($args[$i]); } } sub SkipTo { my ($this, $pattern, $origin) = @_; $this->seek($origin, 0) if(defined $origin and $origin ne ''); Jcode::convert(\$pattern, $PerlCharCode) if($pattern and $PerlCharCode); my $line; my $IsFound = 0; while($line = $this->ReadLine()) { #print "LINE1: $line"; last if($this->eof()); Jcode::convert(\$line, $PerlCharCode) if($line and $PerlCharCode); if($line =~ /$pattern/i) { $IsFound = 1; last; } } return undef unless($IsFound); return $line; } sub tell { my ($this) = @_; return $this->{Pos}; } sub rewind { return shift->seek(0,0); } sub seek { my ($this, $pos, $origin) = @_; return $this->{Pos} = $pos; } 1;