- 論壇徽章:
- 0
|
unpack和pack不是很難理解的!
不知道是自己的習慣還是真的閱讀能力上有問題。我認為好的/有可讀性的代碼是從變量命名就開始做起的。大量無意義的變量名,令人很頭痛。即使我能像機器一樣分析出代碼執(zhí)行的結(jié)果,但那時對于代碼的物理理解,不是對于它解決問題的算法的理解。最近我在用perl重寫CNPRINT,一個中國人寫得很好的一個解決UNIX下漢字/日文/韓文打印的程序。他的源代碼是公布出來的,但是可讀性很差。我不能責備他,因為這只是一個醫(yī)學博士的業(yè)余愛好而已。不過,這么好的解決方法沒有被推廣和被主流linux用于解決CJK打印很大程度上由于代碼的可讀性。我之所以要用perl重寫是想有人能看懂我代碼后再用C改寫成功能更強大的代碼。下面是我代碼的片斷?赡芎懿籶erl化。但是很容易讀懂。
<CODE>
#!/usr/bin/perl -w
our @ParameterList;
our $ValidParameters="SioHtc";
our $DEBUG=1;
our $SILENT=0;
our @Input;
our @InputBuff;
our @Output;
our @OutputBuff;
our $Language="auto";
our @CodeTypes=("shiftjis","gb2312","big5","gbk","jis");
our @CharUsage;
our $InUseCode;
sub convert2Unicode
{
my $code=shift;
my $code_type=shift;
local $FileName;
local $CodeType;
foreach $CodeType (@CodeTypes)
{
$FileName="/CNPRN/CODE/".$CodeType.".txt";
next if (($code_type ne "auto") and ($CodeType ne $code_type));
if (!(open(CodeFile,$FileName)))
{
print STDERR "Unicode covert file open error of codetype $CodeType!\nPlease check the file path $FileName!\n";
return 0 if ($code_type ne "auto");
}
my @FileBuff=<CodeFile>;
close(CodeFile);
foreach my $LineBuff (@FileBuff)
{
next if ($LineBuff!~/^0[xX]/);
chomp $LineBuff;
($Code,$Unicode)=(split(/\t/,$LineBuff));
if ($code==hex($Code))
{
$InUseCode=$CodeType;
close(CodeFile);
return hex($Unicode);
}
}
}
return 0;
}
sub updateCharUsage
{
my $Code=shift;
local $i;
for($i=0;$i<@CharUsage;$i++)
{
last if ($CharUsage[$i]{Code}==$Code);
}
$CharUsage[$i]{Code}=$Code;
$CharUsage[$i]{Freq}++;
$CharUsage[$i]{CodeType}=$InUseCode;
}
sub getCharFreq
{
my $Code=shift;
local $i;
for($i=0;$i<@CharUsage;$i++)
{
return $CharUsage[$i]{Freq} if ($CharUsage[$i]{Code}==$Code);
}
return 0;
}
sub getCharOrd
{
my $Code=shift;
local $i;
for($i=0;$i<@CharUsage;$i++)
{
return $i if ($CharUsage[$i]{Code}==$Code);
}
return -1;
}
sub errorHandler
{
my $ErrorMsg=shift;
my @ErrorPrg=caller;
my $DisplayMsg=($SILENT==0)?$ErrorMsg."\n".($DEBUG?"In function $ErrorPrg[0]() of file $ErrorPrg[1] at line #$ErrorPrg[2].\n":""):"";
system("time /T");
die($DisplayMsg);
}
sub getParameterValue
{
my $Item=shift;
for(my $i=0;$i<@ParameterList;$i++)
{
return $ParameterList[$i]{"Value"} if ($ParameterList[$i]{"Parameter"} eq $Item);
}
return -1;
}
sub getInput
{
my $FileName=getParameterValue("i");
if (length($FileName)==0)
{
errorHandler("Can not open the STANDARD or PIPE\n") if (!(open(INPUT,STDIN)));
}
else
{
errorHandler("Can not open the input file $FileName\n") if (!(open(INPUT,$FileName)));
}
@InputBuff=<INPUT>;
close(INPUT);
}
sub unicodeFile
{
local $Index;
for($Index=0;$Index<@InputBuff;$Index++)
{
local $LineBuff=$InputBuff[$Index];
chomp $LineBuff;
local $Counter=1;
for(my $SubIndex=0;$SubIndex<length($LineBuff);$SubIndex++)
{
local $BinaryCode=ord(substr($LineBuff,$SubIndex,1));
local $Unicode;
$Unicode=convert2Unicode($BinaryCode,$Language);
updateCharUsage($Unicode) if (($Unicode!=$BinaryCode) and ($Unicode!=0));
if ($Unicode==0)
{
$SubIndex++;
$BinaryCode=$BinaryCode*256+ord(substr($LineBuff,$SubIndex,1)) ;
$Unicode=convert2Unicode($BinaryCode,$Language);
}
errorHandler("Can not convert $Language code $BinaryCode to Unicode, which is located in line $Index column $SubIndex!\n") if ($Unicode==0);
$Input[$Index][$Counter]=$Unicode;
updateCharUsage($Unicode);
$Counter++;
}
$Input[$Index][0]=$Counter-1;
}
}
</CODE> |
|