CVCode.pas

2018-10-31

cvcode.pas
// GB2312/BIG5 convert unit for Delphi 3
// ver 1.01
// By Tom Lee  1997/9/5
// E-Mail Address : Tomm.bbs@csie.nctu.edu.tw
// It is Freeware !

unit CVCode;

interface

uses
  sysutils, Forms, IniFiles;

function GBtoBIG5(value: string): string;
function BIG5toGB(value: string): string;
function GetGBOrder(value: string): string;
function GetBIG5Order(value: string): string;
function WriteGBOrder(value, aBIG5: string): Boolean;
function WriteBIG5Order(value, aGBK: string): Boolean;

implementation

function GetGBOrder(value: string): string;
var
  i, j: LongInt;
begin
  for i := $A1 to $A9 do
  for j := $A1 to $FE do
  begin
   if (chr(i) + chr(j))= value then
   begin
   Result := inttohex(i, 2) + inttohex(j, 2);
   break;
   end;
  end;

  for i := $B0 to $F7 do
  for j := $A1 to $FE do
  begin
   if (chr(i) + chr(j))= value then
   begin
   Result := inttohex(i, 2) + inttohex(j, 2);
   break;
   end;
  end;

  for i := $81 to $A0 do
  for j := $40 to $FE do
  begin
   if (chr(i) + chr(j))= value then
   begin
   Result := inttohex(i, 2) + inttohex(j, 2);
   break;
   end;
  end;

  for i := $AA to $FE do
  for j := $40 to $A0 do
  begin
   if (chr(i) + chr(j))= value then
   begin
   Result := inttohex(i, 2) + inttohex(j, 2);
   break;
   end;
  end;

  for i := $A8 to $A9 do
  for j := $40 to $A0 do
  begin
   if (chr(i) + chr(j))= value then
   begin
   Result := inttohex(i, 2) + inttohex(j, 2);
   break;
   end;
  end;
 
end;

function GetBIG5Order(value: string): string;
var
  i, j: LongInt;
begin
  for i := $A1 to $F9 do
  for j := $40 to $FE do
  begin
   if (chr(i) + chr(j)) = value then
   begin
   Result := inttohex(i, 2) + inttohex(j, 2);
   break;
   end;
  end;
end;

function GBOffset(value: string): integer;
begin
  if length(value) >= 2 then
   Result := (Ord(value[1]) - 161) * 94 + (Ord(value[2]) - 161)
//    Result := (Ord(value[1]) - 129) * 190 + (Ord(value[2]) - 64) - Ord(Value[2]) div 128
  else
   Result := -1;
end;

function BIG5Offset(value: string): integer;
begin
  Result := -1;
  if length(value) >= 2 then
  begin
   if (Ord(value[2]) >= 64) and (Ord(value[2]) <= 126) then
   Result := (Ord(value[1]) - 161) * 157 + (Ord(value[2]) - 64);
   if (Ord(value[2]) >= 161) and (Ord(value[2]) <= 254) then
   Result := (Ord(value[1]) - 161) * 157 + 63 + (Ord(value[2]) - 161);
  end
end;

function WordToString(value: Word): string;
begin
  Result := Chr(Hi(value)) + Chr(Lo(Value));
end;

function isBIG5(value: string): Boolean;
begin
  if (length(value)>=2) then
  begin
  if (value[1] < #161) then
   Result := false
  else
   if ((value[2] >= #64) and (value[2] <= #126)) or ((value[2] >= #161) and (value[2] <= #254)) then
   Result := true
   else
   Result := false
  end
  else
   Result := false
end;

function isGB(value: string): Boolean;
begin
  if (length(value)>=2) then
  begin
   if (value[1] <= #161) and (value[1] >= #247) then
//    if (value[1] <= #129) and (value[1] >= #254) then
   Result := false
   else
   if (value[2] <= #64) and (value[2] >= #254) then
   Result := false
   else
   Result := true
  end
  else
   Result := true;
end;

function GBtoBIG5(value: string): string;
var
  leng, idx    : integer;
  tmpStr    : string[2];
  Offset    : integer;
  output    : string;
  IniFile    : TIniFile;
begin
  IniFile := TIniFile.Create(ExtractFilePath(Application.ExeName) + 'Language');
  try
  output := '';
  leng := length(value);
  idx := 1;
  while idx <= leng do
  begin
   tmpStr := value[idx]+ value[idx + 1];
   if isGB(tmpStr) then
   begin
   offset:=GBOffset(tmpStr);
   if (offset >= 0) and (offset <= 22704) then
   begin
   output := output + WordToString(IniFile.ReadInteger('GB', 'GBOrder[' + inttostr(offset) + ']', 0));
   inc(idx);
   end
   else
   output := output + value[idx] ;
   end
   else
   output := output + value[idx] ;

   inc(idx, 1);
  end;
  Result := output;
  finally
   IniFile.Free;
  end;
end;

function BIG5toGB(value: string): string;
var
  leng, idx    : integer;
  tmpStr    : string[2];
  output    : string;
  offset    : integer;
  IniFile    : TIniFile;
begin
  IniFile := TIniFile.Create(ExtractFilePath(Application.ExeName) + 'Language');
  try
  output := '';
  leng := length(value);
  idx := 1;
  while idx <= leng do
  begin
   tmpStr := value[idx]+ value[idx + 1];
   if isBIG5(tmpStr) then
   begin
   offset:=BIG5Offset(tmpStr);
   if (offset >= 0) and (offset <= 13972) then
   begin
   output := output + WordToString(IniFile.ReadInteger('BIG5', 'BIG5Order[' + inttostr(offset) + ']', 0));
   inc(idx);
   end
   else
   output := output + value[idx];
   end
   else
   output := output + value[idx];

   inc(idx);
  end;
  Result := output;
  finally
   IniFile.Free;
  end;
end;

function WriteGBOrder(value, aBIG5: string): Boolean;
var
  tmpStr    : string[2];
  Offset    : integer;
  IniFile    : TIniFile;
begin
  IniFile := TIniFile.Create(ExtractFilePath(Application.ExeName) + 'Language');
  Result := false;
  try
   tmpStr := chr(strtoint('$' + copy(value, 1, 2))) + chr(strtoint('$' + copy(value, 3, 2)));
   if isGB(tmpStr) then
   begin
   offset:=GBOffset(tmpStr);
   if (offset >= 0) and (offset <= 22704) then
   begin
   IniFile.WriteString('GB', 'GBOrder[' + inttostr(offset) + ']', '$' + aBIG5);
   Result := true;
   end
   else begin
   Application.MessageBox('编码顺序有小于0,不能存档.', PChar(Application.Title), 0);
   end;
   end;
  finally
   IniFile.Free;
  end;
end;

function WriteBIG5Order(value, aGBK: string): Boolean;
var
  tmpStr    : string[2];
  Offset    : integer;
  IniFile    : TIniFile;
begin
  IniFile := TIniFile.Create(ExtractFilePath(Application.ExeName) + 'Language');
  Result := false;
  try
   tmpStr := chr(strtoint('$' + copy(value, 1, 2))) + chr(strtoint('$' + copy(value, 3, 2)));
   if isBIG5(tmpStr) then
   begin
   offset:=BIG5Offset(tmpStr);
   if (offset >= 0) and (offset <= 13972) then
   begin
   IniFile.WriteString('BIG5', 'BIG5Order[' + inttostr(offset) + ']', '$' + aGBK);
   Result := true;
   end
   else begin
   Application.MessageBox('编码顺序有小于0,不能存档.', PChar(Application.Title), 0);
   end;
   end;
  finally
   IniFile.Free;
  end;
end;

end.


阅读16