Contents ::
|
|
package k2;
;;;;;;;;;;;;; use Jcode;
use Switch;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;sub OutHtml{
my ($fd, $fr, $ic, $tt, $bd, $dt) = @_;
my ($fs, $em, $fn);
$tt = 'K2/IDE:2008' unless($tt);
if(@$bd){
$fn = fixFont(shift(@$bd));
$fs = shift(@$bd);
$em = shift(@$bd);
}else{
$fn = fixFont('g');
$fs = 16;
$em = 1.2;
@$bd = ('margin: 8', 'padding: 8');
}
push(@$bd, 'font: '.$fs.'px/'.$em.'em '.$fn);
print "Content-type: text/html\n\n";
if($fd){
print "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>\n";
}else{
print "<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>\n";
}
print "<HTML lang='ja'>\n";
print "<HEAD>\n";
print "<META http-equiv='Content-Type' content='text/html; charset=Shift_JIS'>\n";
print "<META http-equiv='Content-Style-Type' content='text/css'>\n";
print "<META name='GENERATOR' content='K2/IDE::2008'>\n";
print "<META name='copyright' content='Dr.K'>\n";
if($fr){
print "<META name='robots' content='index,follow'>\n";
}else{
print "<META name='robots' content='noindex,nofollow'>\n";
}
if($ic){
print "<LINK rel='SHORTCUT ICON' href=$ico>\n";
}
print "<TITLE>$tt</TITLE>\n";
print "<style type='text/css'><!--\n";
print "\t"."body{\n";
foreach(@$bd){print "\t\t$_\n"}
print "\t}\n";
print "--></style>\n";
print "</HEAD>\n";
print "<BODY>\n";
my ($fg, $wk, $ct);
my @tag = (
'DIV',
'CENTER',
'FORM', 'SELECT',
'TABLE', 'THEAD', 'TFOOT', 'TBODY', 'TR', 'TD'
);
foreach(@$dt){
$fg = 1;
my $s = cnv2sjis($_);
unless(isTagOnly($s)){
if($s eq ''){
if($wk eq '</UL>' || $wk eq '</OL>'){
$fg = 0;
}
}
$s = "$s<BR>";
}
if($fg){
my $uc = Trim(uc($s));
foreach(@tag){
if(index($uc, '</'.$_) == 0){
$ct--;
last;
}
}
foreach(@tag){
if(index($wk, '<'.$_) == 0){
$ct++;
last;
}
}
if($ct < 0){
$ct = 0;
}else{
my $tb = "\t" x ($ct + 1);
$s = $tb.$s;
}
}
$wk = Trim(uc($s));
print "$s\n";
}
print "</BODY>\n";
print "</HTML>\n";
}
sub Out{
my ($pt, $dt) = @_;
foreach(@{$dt}){push(@{$pt}, $_)}
}
sub HtmlHead{
my ($ttl, $cpy, @css) = @_;
$ttl = 'K2/LIB'if($ttl eq '');
$cpy = 'Dr.K2'if($cpy eq '');
print "Content-type: text/html\n\n";
print "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN' 'http://www.w3.org/TR/html4/strict.dtd'>\n";
print "<HTML lang='ja'>\n";
print "<HEAD>\n";
print "<META http-equiv='Content-Type' content='text/html; charset=shift_jis'>\n";
print "<META http-equiv='Content-Style-Type' content='text/css'>\n";
print "<META name='GENERATOR' content='K2/IDE::2008'>\n";
print "<META name='copyright' content='$cpy'>\n";
print "<META name='robots' content='noindex'>\n";
print "<TITLE>$ttl</TITLE>\n";
print "<style type='text/css'>\n";
if(@css){
foreach(@css){print "$_\n"}
}else{
print " body{\n";
print " margin: 0;\n";
print " padding: 0;\n";
print " background-color: snow;\n";
print " font: 13px MS Pゴシック, MS ゴシック, MSP Gothic, MS Gothic;\n";
print " }\n";
print " a:hover{\n";
print " color: #ff0000;\n";
print " background-color: #ccccff;\n";
print " text-decoration:underline overline;\n";
print " }\n";
print " a:active{\n";
print " color: #000000;\n";
print " background-color: #ddddff;\n";
print " }\n";
print " a{\n";
print " text-decoration: none;\n";
print " }\n";
}
print "</style>\n";
print "</HEAD>\n";
print "<BODY>\n";
print " <CENTER>\n";
}
sub HtmlFoot{
print " </CENTER>\n";
print "</BODY>\n";
print "</HTML>\n";
}
sub fixDoc{
my ($doc, $pre) = @_;
$doc =~ s/^[^\S\n]+//gm;
$doc =~ s/^$pre+//gm;
return($doc);
}
sub fixFont{
my ($fn) = @_;
my $f = lc($fn);
$fn = 'MS 明朝'if($f eq 'm');
$fn = 'MS P明朝'if($f eq 'mp');
$fn = 'MS ゴシック'if($f eq 'g');
$fn = 'MS Pゴシック'if($f eq 'gp');
$fn = 'MS ゴシック'unless($fn);
return($fn);
}
;;;sub isAgent{
my $env = $ENV{'HTTP_USER_AGENT'};
return('d')if($env =~ /DoCoMo/i);
return('a')if($env =~ /^UP.Browser|^KDDI/i);
return('s')if($env =~ /^J-PHONE|^Vodafone|^SoftBank/i);
return('p');
}
sub AgentName{
$_ = $ENV{'HTTP_USER_AGENT'};
s/,/./g;
s/</</g;
s/>/>/g;
return($_);
}
sub IPaddress{
return($ENV{'REMOTE_ADDR'});
}
sub IP2URL{
my($IP) = @_;
my(@add) = split(/\./, $IP);
my($packed) = pack("C4", $add[0], $add[1], $add[2], $add[3]);
my($name, $aliases, $addrtype, $length, @addrs) = gethostbyaddr($packed, 2);
return($name);
}
sub LoginName{
return(getlogin());
}
sub Domain{
my ($sw) = @_;
$sw = MinMax($sw, 1, 2);
my $add = $ENV{'REMOTE_ADDR'};
my $dom = gethostbyaddr(pack("C4",split(/\./,$add)),2);
if($dom eq ''){
$dom = $add;
}else{
if($sw == 2){
if(/.+\.(.+)\.(.+)\.(.+)$/){
$dom = "\*\.$1\.$2\.$3";
}elsif(/.+\.(.+)\.(.+)$/){
$dom = "\*\.$1\.$2";
}elsif (/.+\.(.+)$/){
$dom = "\*\.$1";
}else{
$dom = "on the internet";
}
}
}
return($dom);
}
sub Query{
my (%ret, $buff, @pair, $key, $val);
if($ENV{'REQUEST_METHOD'} eq 'GET'){
$buff = $ENV{'QUERY_STRING'};
}elsif($ENV{'REQUEST_METHOD'} eq 'POST'){
read(STDIN, $buff, $ENV{'CONTENT_LENGTH'});
print STDIN '';
}
if($buff){
foreach(CutOff('&',$buff)){
($key, $val) = CutOff('=', $_);
$key = DecodeURL($key);
$val = DecodeURL($val);
$val =~ s/\n//g;
$key = cnv2sjis($key);
$val = cnv2sjis($val);
$ret{$key} = $val;
}
return(%ret);
}else{
return();
}
}
sub SetCookie{
my ($days, $hour, $mint, $name, @cook) = @_;
my ($cookies, $gmt, @t, @m, @w);
@m = ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
@w = ('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
@t = gmtime(time + $days * 24 * 60 * 60 + $hour * 60 * 60 + $mint * 60);
$gmt = sprintf("%s, %02d-%s-%04d %02d:%02d:%02d GMT",
$w[$t[6]], $t[3], $m[$t[4]], $t[5]+1900, $t[2], $t[1], $t[0]);
foreach(@cook){
s/(\W)/sprintf("%%%02X", unpack("C", $1))/eg;
$cookies .= "$_<>";
}
print "Set-Cookie: $name=$cookies; expires=$gmt\n";
}
sub GetCookie{
my ($name, $sepa) = @_;
my ($key, $val, %id, %cookie);
@dat = CutOff(';', $ENV{'HTTP_COOKIE'});
foreach(@dat){
($key, $val) = CutOff('=', $_);
$key =~ s/\s//g;
$id{$key} = $val;
}
foreach(CutOff('<>', $id{$name})){
tr/\+/ /;
s/%([a-fA-F0-9][a-fA-F0-9])/pack('H2', $1)/eg;
($key, $val) = CutOff($sepa, $_);
$cookie{$key} = $val;
}
return(%cookie);
}
sub ClearCookie{
my ($name) = @_;
my $exp = 'Thu, 01-Jan-1970 00:00:00 GMT';
print "Set-Cookie: $name=clear; expires=$exp;";
}
;;;sub EncodeURL{
my ($s) = @_;
$s =~ s/([^\w ])/'%'.unpack('H2', $1)/eg;
$s =~ tr/ /+/;
return($s);
}
sub DecodeURL{
my ($s) = @_;
$s =~ tr/+/ /;
$s =~ s/%([0-9a-fA-F][0-9a-fA-F])/pack("C", hex($1))/eg;
return($s);
}
sub OmitRetAll{
my ($s) = @_;
$s =~ tr/\x0D\x0A//d;
return($s);
}
sub OmitRetLast{
my ($s) = @_;
$s =~ s/\x0D?\x0A?$//;
return($s);
}
sub ReturnTo1{
my ($s) = @_;
$s =~ s/\x0D\x0A|\x0D|\x0A/\n/g;
return($s);
}
sub Return2br{
my ($s) = @_;
$s =~ s/\x0D\x0A|\x0D|\x0A/<BR>/g;
return($s);
}
sub Space2nbsp{
my ($s) = @_;
my ($p, $l) = (index($s, ' ') + 1);
while($p){
$l = length($s) - $p;
$s = substr($s, 0, $p - 1).' '.substr($s, length($s) - $l, $l);
$p = index($s, ' ') + 1;
}
return($s);
}
;;;sub OmitRetAllEx{
my ($s) = @_;
for(my $i = 0; $i < @$s; $i++){
@$s[$i] =~ tr/\x0D\x0A//d;
}
}
sub OmitRetLastEx{
my ($s) = @_;
for(my $i = 0; $i < @$s; $i++){
@$s[$i] =~ s/\x0D?\x0A?$//;
}
}
sub ReturnTo1Ex{
my ($s) = @_;
for(my $i = 0; $i < @$s; $i++){
@$s[$i] =~ s/\x0D\x0A|\x0D|\x0A/\n/g;
}
}
sub Return2brEx{
my ($s) = @_;
for(my $i = 0; $i < @$s; $i++){
@$s[$i] =~ s/\x0D\x0A|\x0D|\x0A/<BR>/g;
}
}
sub Space2nbspEx{
my ($s) = @_;
for(my $i = 0; $i < @$s; $i++){
@$s[$i] = TextA2B(@$s[$i], ' ', ' ');
}
}
sub Csv2Array{
my ($csv) = @_;
$csv =~ s/(?:\x0D\x0A|[\x0D\x0A])?$/,/;
my @ary = map{/^"(.*)"$/ ? scalar($_ = $1, s/""/"/g, $_) : $_}
($csv =~ /("[^"]*(?:""[^"]*)*"|[^,]*),/g);
return(@ary);
}
sub Array2Csv{
my (@dat) = @_;
return('"'.Joint('","', @dat).'"');
}
;;;sub cnv2euc{
my ($s) = @_;
$s = Jcode->new($s)->euc;
return($s);
}
sub cnv2utf{
my ($s) = @_;
$s = Jcode->new($s)->utf8;
return($s);
}
sub cnv2sjis{
my ($s) = @_;
my $js = jcode($s);
$js->can("fallback") and $js->fallback(Jcode::FB_XMLCREF());
$s = $js->sjis;
return($s);
}
sub cnv2eucEx{
my ($s) = @_;
for(my $i = 0; $i < @$s; $i++){
@$s[$i] = cnv2euc(@$s[$i]);
}
}
sub cnv2utfEx{
my ($s) = @_;
for(my $i = 0; $i < @$s; $i++){
@$s[$i] = cnv2utf(@$s[$i]);
}
}
sub cnv2sjisEx{
my ($s) = @_;
for(my $i = 0; $i < @$s; $i++){
@$s[$i] = cnv2sjis(@$s[$i]);
}
}
;;;my $UTY_HANSP = '[\x20]';
my $UTY_ZENSP = '(?:\x81\x40)';
my $UTY_BYTE1 = '[\x00-\x7F\xA1-\xDF]';
my $UTY_BYTE2 = '(?:[\x81-\x9F\xE0-\xFC][\x40-\x7E\x80-\xFC])';
my $UTY_JIS2B = "(?:$UTY_BYTE1|$UTY_BYTE2)";
my $UTY_EUC2B = '[\xA1-\xFE][\xA1-\xFE]';
sub Trim{
my ($s) = @_;
$s = TrimS($s);
$s =~ s/^\s*(.*?)\s*$/$1/;
return($s);
}
sub TrimA{
my ($s) = @_;
$s = cnv2sjis($s);
$s =~ s/($UTY_JIS2B*?)(?:\s|$UTY_ZENSP)/$1/og;
return($s);
}
sub TrimS{
my ($s) = @_;
$s = cnv2sjis($s);
$s =~ s/^(?:$UTY_HANSP|$UTY_ZENSP)+//o;
$s =~ s/^($UTY_JIS2B*?)(?:$UTY_HANSP|$UTY_ZENSP)+$/$1/o;
return($s);
}
sub TrimL{
my ($s) = @_;
$s = cnv2sjis($s);
$s =~ s/^(?:$UTY_HANSP|$UTY_ZENSP)+//o;
return($s);
}
sub TrimR{
my ($s) = @_;
$s = cnv2sjis($s);
$s =~ s/^($UTY_JIS2B*?)(?:$UTY_HANSP|$UTY_ZENSP)+$/$1/o;
return($s);
}
sub Space1{
my ($s) = @_;
$s = zen2hanSp($s);
do{
$s = TextA2B($s, ' ', ' ');
}while(PosStr(' ', $s) > 0);
return($s);
}
sub LowerCase{
my ($s) = @_;
return(lc($s));
}
sub UpperCase{
my ($s) = @_;
return(uc($s));
}
sub LowerCaseW{
my ($s) = @_;
$s = cnv2sjis($s);
my $ret = '';
my @chr = _get_wideChar($s);
for(my $i = 0; $i < @chr; $i++ ){
if($chr[$i] =~ /$UTY_EUC2B/){
$ret .= han2zen(lc(zen2han(cnv2sjis($chr[$i]))));
}else{
$ret .= cnv2sjis($chr[$i]);
}
}
return($ret);
}
sub UpperCaseW{
my ($s) = @_;
$s = cnv2sjis($s);
my $ret = '';
my @chr = _get_wideChar($s);
for(my $i = 0; $i < @chr; $i++ ){
if($chr[$i] =~ /$UTY_EUC2B/){
$ret .= han2zen(uc(zen2han(cnv2sjis($chr[$i]))));
}else{
$ret .= cnv2sjis($chr[$i]);
}
}
return($ret);
}
sub CutOff{
my ($sep, $s) = @_;
$s = cnv2sjis($s);
$sep = cnv2sjis($sep);
if($sep eq ','){
return(Csv2Array($s));
}else{
return(split(/$sep/, $s));
}
}
sub Joint{
my ($sep, @s) = @_;
my $nn = 0;
my $ret = '';
cnv2sjisEx(\@s);
$sep = cnv2sjis($sep);
$ret = join($sep, @s);
return($ret)
}
sub FillChar{
my ($s, $ln) = @_;
$s = cnv2sjis($s);
$s x= $ln;
return($s);
}
sub FixStr{
my ($s) = @_;
$$s = zen2han(Trim($$s));
}
sub FixInt{
my ($s) = @_;
my $v = zen2han(TrimA($$s));
$$s =~ s/[^\-|\d|\.]//g;
}
sub FixIntEx{
my ($s) = @_;
for(my $i = 0; $i < @$s; $i++){
@$s[$i] = zen2han(Trim(@$s[$i]));
@$s[$i] =~ s/[^\-|\d|\.]//g;
}
}
sub FixStrEx{
my ($s) = @_;
for(my $i = 0; $i < @$s; $i++){
@$s[$i] = zen2han(Trim(@$s[$i]));
}
}
;;;sub PosStr{
my ($a, $s, $n) = @_;
$a = cnv2sjis($a);
$s = cnv2sjis($s);
$n= 1 unless($n);
$n--;
$s = RightStr($s, length($s) - $n);
my $p = index($s, $a) + 1;
return($p);
}
sub MidStr{
my ($s, $st, $ln) = @_;
$s = cnv2sjis($s);
my $ret = substr($s, $st - 1, $ln);
return($ret);
}
sub LeftStr{
my ($s, $ln) = @_;
$s = cnv2sjis($s);
my $ret = substr($s, 0, $ln);
return($ret);
}
sub RightStr{
my ($s, $ln) = @_;
$s = cnv2sjis($s);
my $ret = substr($s, length($s) - $ln, $ln);
return($ret);
}
sub TextA2B{
my ($s, $a, $b) = @_;
$s = cnv2sjis($s);
$a = cnv2sjis($a);
$b = cnv2sjis($b);
my $p;
my $n = LengthW($a);
do{
$p = PosStrW($a, $s);
if($p){
$s = LeftStrW($s, $p-1).$b.RightStrW($s, LengthW($s)-($n + $p)+1);
}
}while($p);
return($s);
}
sub Text2MaxLen{
my ($s, $ln, $sw) = @_;
$s = cnv2sjis($s);
$sw = MinMax($sw, 1, 2);
my $sp = FillChar(' ', $ln);
switch($sw){
case 1 {$s = LeftStr($s.$sp, $ln)}
case 2 {$s = RightStr($sp.$s, $ln)}
}
return($s);
}
sub ChangeStr{
my ($txt, $s, $no) = @_;
$s = cnv2sjis($s);
$txt = cnv2sjis($txt);
my $ln = LengthW($s);
my $lw = LengthW($txt);
$txt = LeftStrW($txt, $no - 1).$s.RightStr($txt, $lw - $ln - $no + 1);
return($txt);
}
sub InsertStr{
my ($txt, $s, $no) = @_;
$s = cnv2sjis($s);
$txt = cnv2sjis($txt);
my $lw = LengthW($txt);
$txt = LeftStr($txt, $no).$s.RightStr($txt, $lw - $no);
return($txt);
}
sub RemoveStr{
my ($txt, $no, $ln) = @_;
$txt = cnv2sjis($txt);
my $lw = LengthW($txt);
$txt = LeftStrW($txt, $no - 1).RightStrW($txt, $lw - $no - $ln + 1);
return($txt);
}
sub Char2CodeStr{
my ($s) = @_;
$s =~ s/(.)/sprintf "%X", ord($1)/eg;
return($s);
}
sub Code2CharStr{
my ($s) = @_;
$s =~ s/([0-9A-Fa-f][0-9A-Fa-f])/pack("C", hex($1))/eg;
return($s);
}
;;;;;;sub LengthW{
$_ = $_[0];
$_ = cnv2sjis($_);
my $lz = s/[\x81-\x9f\xe0-\xfc][\x40-\x7e\x80-\xfc]//g;
my $lh = length($_);
return($lz + $lh);
}
sub PosStrW{
my ($a, $s, $n) = @_;
$a = cnv2sjis($a);
$s = cnv2sjis($s);
$n = 1 unless($n);
my $p = 0;
my $ls = LengthW($s);
my $la = LengthW($a);
for(my $i = $n; $i <= $ls; $i++){
if(MidStrW($s, $i, $la) eq $a){
$p = $i - $n + 1;
last;
}
}
return($p);
}
sub MidStrW{
my ($s, $st, $ln) = @_;
my $ret = '';
my @txt = _get_wideChar($s);
for(my $i = $st - 1; $i < $st + $ln - 1; $i++){
$ret .= $txt[$i];
}
$ret = cnv2sjis($ret);
return($ret);
}
sub LeftStrW{
my ($s, $ln) = @_;
$s = cnv2sjis($s);
my $ret = MidStrW($s, 1, $ln);
return($ret);
}
sub RightStrW{
my ($s, $ln) = @_;
$s = cnv2sjis($s);
my $ret = MidStrW($s, LengthW($s) - $ln + 1, $ln);
return($ret);
}
sub Text2MaxLenW{
my ($dt, $ln, $sw) = @_;
if(isKanji1($dt, $ln) == 1){
$dt = LeftStr($dt, $ln - 1);
}else{
$dt = LeftStr($dt, $ln);
}
$dt = Text2MaxLen($dt, $ln, $sw);
return($dt);
}
sub _get_wideChar{
my ($s) = @_;
$s = cnv2euc($s);
my ($ascii, $UTY_BYTE2, $byte3, @chars);
$ascii = '[\x00-\x7F]';
$UTY_BYTE2 = '[\x8E\xA1-\xFE][\xA1-\xFE]';
$byte3 = '\x8F[\xA1-\xFE][\xA1-\xFE]';
@chars = $s =~ /$ascii|$UTY_BYTE2|$byte3/og;
return(@chars);
}
;;;sub isReal{
my ($s) = @_;
my $ret = ($s =~ /^[+-]?\d+(?:\.?\d+)?$/) ? 1 : 0;
return($ret);
}
sub isFloat{
my ($s) = @_;
my $ret = (! isInteger($s) && isReal($s)) ? 1 : 0;
return($ret);
}
sub isInteger{
my ($s) = @_;
my $ret = ($s =~ /^[+-]?\d+$/) ? 1 : 0;
return($ret);
}
sub isNumeric{
my ($s) = @_;
my $ret = ($s =~ /^\d+$/) ? 1 : 0;
return($ret);
}
sub isFigure{
my ($s) = @_;
$s = k2::TrimA($s);
$s = k2::zen2han($s);
$s =~ s/[\\|\,|円]//g;
my $ret = (k2::isReal($s)) ? 1 : 0;
}
sub isAlphabet{
my ($s) = @_;
$s = cnv2sjis($s);
my $ret = ($s =~ /^[a-z]+$/i) ? 1 : 0;
return($ret);
}
sub isKana{
my ($s) = @_;
$s = cnv2sjis($s);
my $hk = '[\xA6-\xDF]';
$s =~ s/^($hk*?)+$/$1/o;
my $ret = ($s) ? 0 : 1;
return($ret);
}
;;;sub isRealEx{
my (@s) = @_;
my $ret = (@s) ? 1 : 0;
foreach(@s){
unless(/^[+-]?\d+(?:\.?\d+)?$/){
$ret = 0;
last;
}
}
return($ret);
}
sub isFloatEx{
my (@s) = @_;
my (@s) = @_;
my $ret = (@s) ? 1 : 0;
foreach(@s){
unless((/^[+-]?\d+(?:\.?\d+)?$/) && !(/^[+-]?\d+$/)){
$ret = 0;
last;
}
}
return($ret);
}
sub isIntegerEx{
my (@s) = @_;
my $ret = (@s) ? 1 : 0;
foreach(@s){
unless(/^[+-]?\d+$/){
$ret = 0;
last;
}
}
return($ret);
}
sub isNumericEx{
my (@s) = @_;
my $ret = (@s) ? 1 : 0;
foreach(@s){
unless(/^\d+$/){
$ret = 0;
last;
}
}
return($ret);
}
sub isFigureEx{
my (@s) = @_;
my $ret = (@s) ? 1 : 0;
foreach(@s){
unless(isFigure($_)){
$ret = 0;
last;
}
}
return($ret);
}
sub isAlphabetEx{
my (@s) = @_;
my $ret = (@s) ? 1 : 0;
foreach(@s){
unless(/^[a-z]+$/i){
$ret = 0;
last;
}
}
return($ret);
}
sub isKanaEx{
my (@s) = @_;
my $ret = (@s) ? 1 : 0;
foreach(@s){
unless(/^[\xA6-\xDF]+$/){
$ret = 0;
last;
}
}
return($ret);
}
;;;sub isNumericW{
my ($s) = @_;
$s = cnv2sjis($s);
my $zk = '(?:\x82[\x4F-\x58])';
$s =~ s/^($zk*?)+$/$1/o;
my $ret = ($s) ? 0 : 1;
return($ret);
}
sub isAlphabetW{
my ($s) = @_;
$s = cnv2sjis($s);
my $zk = '(?:\x82[\x60-\x79\x81-\x9A])';
$s =~ s/^($zk*?)+$/$1/o;
my $ret = ($s) ? 0 : 1;
return($ret);
}
sub isKanaW{
my ($s) = @_;
$s = cnv2sjis($s);
my $zk = '(?:\x83[\x40-\x96])';
$s =~ s/^($zk*?)+$/$1/o;
my $ret = ($s) ? 0 : 1;
return($ret);
}
sub isHira{
my ($s) = @_;
$s = cnv2sjis($s);
my $zk = '(?:\x82[\x9F-\xF1])';
$s =~ s/^($zk*?)+$/$1/o;
my $ret = ($s) ? 0 : 1;
return($ret);
}
sub isKanji{
my ($s, $sw) = @_;
my $ret = 0;
$sw = MinMax($sw, 1, 2);
my @chr = _get_wideChar($s);
my ($zn, $hn);
for(my $i = 0; $i < @chr; $i++ ){
if($chr[$i] =~ /$UTY_EUC2B/){
$zn++;
}else{
$hn++;
}
}
if($zn){
switch($sw){
case 1{$ret = ($hn == 0) ? 1 : 0}
case 2{$ret = 1}
}
}
return($ret);
}
sub isKanjiIn{
my ($s, $no) = @_;
my $ret = 0;
if( ($s) && ($no >= 1) && ($no <= LengthW($s)) ){
my @chr = _get_wideChar($s);
if($chr[$no - 1] =~ /$UTY_EUC2B/){$ret = 1}
}
return($ret);
}
sub isKanji1{
my ($s, $no) = @_;
my $ret = 0;
my $byte = 0;
my @chr = _get_wideChar($s);
for(my $i = 0; $i < @chr; $i++){
$byte++;
if($chr[$i] =~ /$UTY_EUC2B/){
if($byte == $no){$ret = 1}
$byte++;
if($byte == $no){$ret = 2}
}
if($byte >= $no){last}
}
return($ret);
}
;;;sub zen2hanSp{
my ($s) = @_;
$s = cnv2sjis($s);
$s =~ s/ / /g;
return($s);
}
sub han2zenSp{
my ($s) = @_;
$s = cnv2sjis($s);
$s =~ s/ / /g;
return($s);
}
sub han2zenk{
my ($s) = @_;
$s = cnv2euc($s);
$s = jcode($s, 'euc')->h2z->sjis;
return($s);
}
sub zen2hank{
my ($s) = @_;
$s = cnv2euc($s);
$s = jcode($s, 'euc')->z2h->sjis;
return($s);
}
sub han2zen{
my ($s) = @_;
my $ret = '';
$s = cnv2sjis($s);
my $ls = LengthW($s);
for(my $i = 1; $i <= $ls; $i++){
my $d = MidStrW($s, $i, 1);
unless(isKanji($d)){
$d =~ s/([0-9A-Za-z])/pack("C*", 0xA3, ord($1) + 0x80)/eg;
}
$ret .= Trim($d);
}
return($ret);
}
sub zen2han{
my ($s) = @_;
my ($ln) = length($s);
my ($i, $j, $unpack, $pack);
my (@ASCII) = (
'64-32' , '73-33' , '104-34', '148-35', '144-36' , '147-37', '149-38' ,'102-39',
'105-40', '106-41', '150-42', '123-43', '67-44' , '124-45', '68-46' , '94-47' ,
'70-58' , '71-59' , '131-60', '129-61', '132-62' , '72-63' , '151-64' , '109-91',
'143-92', '110-93', '79-94' , '81-95' , '111-123', '98-124', '112-125', '96-126'
);
local($_);
for($i = 0; $i < $ln; $i++){
$j = substr($s, $i, 1);
$_ .= "!". unpack("C", $j);
}
foreach my $ascii (@ASCII){
($unpack, $pack) = split(/\-/, $ascii);
s/!129!$unpack/!$pack/g;
}
my $st;
while(/(^|!(\d+))!130!(\d+)/){
if( ($3 >= 63 && $3 <= 88)||($3 >= 96 && $3 <= 121) ){
$st = $3 - 31;
$_ =~ s/!130!(\d+)/!$st/;
}elsif($3 >= 129 && $3 <= 154 && $2 < 129){
$st = $3 - 32;
$_ =~ s/!130\!(\d+)/!$st/;
}else{
$_ =~ s/!130!(\d+)/;130!$1/;
}
}
s/;(\d+)/pack("C", $1)/eg;
s/!(\d+)/pack("C", $1)/eg;
s/、/,/g;
return($_);
}
;;;sub isScalar{
my ($dt) = @_;
my $ret = (ref($dt) eq 'SCALAR') ? 1 : 0;
return($ret);
}
sub isArray{
my ($dt) = @_;
my $ret = (ref($dt) eq 'ARRAY') ? 1 : 0;
return($ret);
}
sub isHash{
my ($dt) = @_;
my $ret = (ref($dt) eq 'HASH') ? 1 : 0;
return($ret);
}
sub isRef{
my ($dt) = @_;
my $ret = (ref($dt) eq 'REF') ? 1 : 0;
return($ret);
}
sub isGlob{
my ($dt) = @_;
my $ret = (ref($dt) eq 'GLOB') ? 1 : 0;
return($ret);
}
sub isCode{
my ($dt) = @_;
my $ret = (ref($dt) eq 'CODE') ? 1 : 0;
return($ret);
}
sub isLvalue{
my ($dt) = @_;
my $ret = (ref($dt) eq 'LVALUE') ? 1 : 0;
return($ret);
}
sub isHandle{
my ($HD) = @_;
my $ret = (ref($HD) eq 'IO::Handle') ? 1 : 0;
return($ret);
}
;;;sub HitOf{
my ($s, @dat) = @_;
my $i = 0;
my $ret = -1;
$s = lc(cnv2sjis($s));
foreach(@dat){
if($s eq lc($_)){
$ret = $i;
last;
}
$i++;
}
return($ret);
}
sub IndexOf{
my ($s, @dat) = @_;
my $i = 0;
my $ret = -1;
$s = lc(cnv2sjis($s));
foreach(@dat){
if(index(lc($_), $s) == 0){
$ret = $i;
last;
}
$i++;
}
return($ret);
}
sub IncludeOf{
my ($s, @dat) = @_;
my $i = 0;
$s = lc(cnv2sjis($s));
foreach(@dat){
$_ = lc($_);
if($_ =~ /$s/i){last}
$i++;
}
return($i);
}
sub DeleteOf{
my ($dat, $no, $ln) = @_;
splice(@$dat, $no, $ln);
return(1);
}
sub InsertOf{
my ($dat, $no, $s) = @_;
$s = cnv2sjis($s);
splice(@$dat, $no, 0, $s);
return(1);
}
sub MargeOf{
my ($d1, $d2, $sw) = @_;
my @ret = ();
$sw = MinMax($sw, 1, 2);
switch($sw){
case 1 {@ret = (@$d1, @$d2)} case 2 {@ret = (@$d2, @$d1)} }
return(@ret);
}
sub SortStrOf{
my ($sw, @dat) = @_;
$sw = MinMax($sw, 1, 2);
switch($sw){
case 1 {return(sort{$a cmp $b} @dat)}
case 2 {return(sort{$b cmp $a} @dat)}
}
}
sub SortIntOf{
my ($sw, @dat) = @_;
$sw = MinMax($sw, 1, 2);
switch($sw){
case 1 {return(sort{$a <=> $b} @dat)}
case 2 {return(sort{$b <=> $a} @dat)}
}
}
sub MinLenStrOf{
my $min = 999999999999999;
foreach(@_){
my $ln = length($_);
if($ln < $min){$min = $ln}
}
return($min);
}
sub MaxLenStrOf{
my $max = 0;
foreach(@_){
my $ln = length($_);
if($ln > $max){$max = $ln}
}
return($max);
}
sub MinIntOf{
my (@dt) = @_;
return('')unless(@dt);
my $min = 999999999999999;
foreach my $val (@dt){
next if($val eq '');
if($val < $min){
$min = $val;
}
}
return($min);
}
sub MaxIntOf{
my (@dt) = @_;
return('')unless(@dt);
my $max = -999999999999999;
foreach my $val (@dt){
next if($val eq '');
if($val > $max){
$max = $val;
}
}
return($max);
}
sub MinStrOf{
my (@dt) = @_;
return('')unless(@dt);
my $min = shift(@dt);
foreach my $val (@dt){
if($min gt $val){
$min = $val;
}
}
return($min);
}
sub MaxStrOf{
my (@dt) = @_;
return('')unless(@dt);
my $max = shift(@dt);
foreach my $val (@dt){
if($max lt $val){
$max = $val;
}
}
return($max);
}
sub PickUpOf{
my ($no, $ln, @dt) = @_;
@dt = @dt[$no..$no + $ln - 1];
return(@dt);
}
sub DelDupOf{
my (@dt) = @_;
my %ct;
@dt = grep(!$ct{$_}++, @dt);
return(@dt);
}
sub ShuffleOf{
my (@dt) = @_;
@dt = List::Util::shuffle(@dt);
return(@dt);
}
;;;sub GetHash{
my ($ky, %h) = @_;
$ky = cnv2euc($ky);
if(exists $h{$ky}){
return($h{$ky});
}else{
return(0);
}
}
sub PutHash{
my ($ky, $dt, %h) = @_;
$ky = cnv2euc($ky);
unless(exists $h{$ky}){
$h{$ky} = $dt;
return(%h);
}else{
return(0);
}
}
sub DeleteHash{
my ($ky, %h) = @_;
delete($h{$ky});
return(%h);
}
sub Array2Hash{
my (@dt) = @_;
my %ret = ();
for(my $i = 0; $i < @dt; $i++){
$ret{cnv2euc($dt[0][$i])} = $dt[1][$i];
}
return(%ret);
}
sub Hash2Array{
my (%dt) = @_;
my @ret = ();
foreach my $key (sort keys %dt){
$ret[@ret][0] = cnv2sjis($key);
$ret[@ret][1] = $dt{$key};
}
return(@ret);
}
sub MargeHash{
my ($h1, $h2, $sw) = @_;
my %ret = ();
$sw = MinMax($sw, 1, 2);
switch($sw){
case 1 {%ret = (%$h1, %$h2)}
case 2 {%ret = (%$h2, %$h1)}
}
return(%ret);
}
sub ValsHash{
my (%h) = @_;
my @ret = ();
foreach my $key (sort keys %h){
$ret[@ret] = $h{$key};
}
return(@ret);
}
sub KeysHash{
my (%h) = @_;
my @ret = ();
foreach my $key (sort keys %h){
$ret[@ret] = cnv2sjis($key);
}
return(@ret);
}
sub KeysInHash{
my (%h) = @_;
my $ret = keys(%h);
return($ret);
}
;;;my @UTY_STACK;
sub Push{
my $v = $_[0];
if($v == -1){
@UTY_STACK = ();
return(1);
}else{
return(push(@UTY_STACK, $v));
}
}
sub Pop{
my $v = $_[0];
if($v == -1){
@UTY_STACK = ();
return(1);
}else{
return(pop(@UTY_STACK));
}
}
sub UnShift{
my $v = $_[0];
if($v == -1){
@UTY_STACK = ();
return(1);
}else{
return(unshift(@UTY_STACK, $v));
}
}
sub Shift{
my $v = $_[0];
if($v == -1){
@UTY_STACK = ();
return(1);
}else{
return(shift(@UTY_STACK));
}
}
;;;sub Hi{
my (@v) = @_;
my $ln = @v;
return($ln);
}
sub Lo{
my (@v) = @_;
return($#v);
}
sub Swap{
my ($a, $b) = @_;
($$b, $$a) = ($$a, $$b);
return(1);
}
sub SwapEx{
my ($a, $b) = @_;
return($b, $a);
}
sub Min{
my ($a, $b) = @_;
my $x = ($a < $b) ? $a : $b;
return($x);
}
sub Max{
my ($a, $b) = @_;
my $x = ($a > $b) ? $a : $b;
return($x);
}
sub MinMax{
my ($s, $a, $b) = @_;
$s = 0 unless($s);
Swap(\$a, \$b) if ($a > $b);
$s = ($s < $a) ? $a : $s;
$s = ($s > $b) ? $b : $s;
return($s);
}
;;;sub Round{
my ($v, $r) = @_;
$r = Power(10, $r);
my $x = ($v) ? 0.5 : -0.5;
return(int($v * $r + $x) / $r);
}
sub RoundOff{
my ($v, $r) = @_;
$r = Power(10, $r);
my $x = ($v) ? 0.5 : -0.5;
return(Trunc($v * $r + $x) / $r);
}
sub Trunc{
my ($v, $r) = @_;
$r = Power(10, $r);
my $ret;
if($v){
$ret = int($v * $r) / $r;
}else{
my $x = $v * $r;
if($x == int($x)){
$ret = $x / $r;
}else{
$ret = int($x - 1) / $r;
}
}
return($ret);
}
sub Ceil{
my ($v, $r) = @_;
$r = Power(10, $r);
my $ret;
if($v){
my $x = $v * $r;
if($x == int($x)){
$ret = $x / $r;
}else{
$ret = int($x + 1) / $r;
}
}else{
$ret = int($v * $r) / $r;
}
return($ret);
}
;;;sub Div{
my ($x, $y) = @_;
$x = ($y) ? int($x / $y) : 0;
return($x);
}
sub Mod{
my ($x, $y) = @_;
$x = $x % $y;
return($x);
}
sub Power{
my ($v, $p) = @_;
return($v ** $p);
}
sub Sqr{
my ($v, $n) = @_;
$n = 2 unless($n);
my $i = ($v < 0) ? -1 : 1;
$v = $i * Power(abs($v), (1 / $n));
return($v);
}
;;;sub CommaStr{
my ($v, $ln) = @_;
if($v ne ''){
1 while($v =~ s/(\d+)(\d\d\d)/$1,$2/);
if(isInteger($ln) && $ln != 0){
$v = Text2MaxLen($v, $ln, 2);
}
}
return($v);
}
sub CommaInt{
my ($v, $ln) = @_;
if($v ne ''){
$v = reverse($v);
$v =~ s/(\d{3})(?=\d)(?!\d*\.)/$1,/g;
$v = reverse($v);
if(isInteger($ln) && $ln != 0){
$v = Text2MaxLen($v, $ln, 2);
}
}
return($v);
}
sub CommaFloat{
my ($v, $n, $ln) = @_;
my ($p, $d, $h, $l, $r);
if($v ne ''){
$d = Power(10, $n);
$v = Round($v * $d);
$v = $v / $d;
$p = PosStr('.', $v);
if($p){
$h = LeftStr($v, $p - 1);
$l = RightStr($v, length($v) - $p);
$l = LeftStr($l.FillChar('0', $n), $n);
$r = CommaInt($h).'.'.LeftStr($l, $n);
}else{
$r = CommaInt($v).'.'.FillChar('0', $n);
}
if(isInteger($ln) && $ln != 0){
$r = Text2MaxLen($r, $ln, 2);
}
}else{
$r = '';
}
return($r);
}
1;