type TForm1 = class(TForm) Memo1: TMemo; Memo2: TMemo; SpeedButton1: TSpeedButton; SpeedButton2: TSpeedButton; OpenDialog1: TOpenDialog; procedure SpeedButton1Click(Sender: TObject); procedure SpeedButton2Click(Sender: TObject); private { Private declarations } function UnicodeToAnsi(Unicode: string):string; public { Public declarations } end;
var Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.SpeedButton1Click(Sender: TObject); var s,a:string; i,j,k:integer; b:boolean; begin s:=memo1.Lines.Text; for i:=1 to length(s) do begin if s[i]='#' then begin j:=i; repeat j:=j+1; until not(s[j] in ['0','1','2','3','4','5','6','7','8','9']); a:=copy(s,i+1,j-i-1); delete(s,i,j-i); insert(''+UnicodeToAnsi(IntToHex(StrToInt(a), 4))+'',s,i); end; end; memo2.Lines.Text:=s; //memo2.Lines.SaveToFile(OpenDialog1.FileName); end;
function TForm1.UnicodeToAnsi(Unicode: string):string; var s:string; i:integer; j,k:string[2];
function ReadHex(AString:string):integer; begin Result:=StrToInt('$'+AString) end;
begin i:=1; s:='; while i<Length(Unicode)+1 do begin j:=Copy(Unicode,i+2,2); k:=Copy(Unicode,i,2); i:=i+4; s:=s+Char(ReadHex(j))+Char(ReadHex(k)); end; if s<>' then s:=WideCharToString(PWideChar(s+#0#0#0#0)) else s:='; Result:=s; end;
procedure TForm1.SpeedButton2Click(Sender: TObject); begin OpenDialog1.Execute; memo1.Lines.LoadFromFile(OpenDialog1.FileName); end;
procedure TForm1.Button1Click(Sender: TObject); var I,J,K,L,P,Q: integer; F,S,T,R: string; H: TextFile; begin for I := 0 to FileListBox1.Items.Count-1 do begin F := FileListBox1.Items[I]; if pos('.~',F) > 0 then continue; if pos('.exe',F) > 0 then continue; if pos('.dfm',F) = 0 then begin CopyFileTo(DirectoryListBox1.Directory+'\'+FileListBox1.Items[I],DirectoryListBox2.Directory+'\'+FileListBox1.Items[I]); Continue; end; AssignFile(H,DirectoryListBox1.Directory+'\'+FileListBox1.Items[I]); Reset(H); F := '; while True do begin ReadLn(H,S); if S = ' then break; if pos('#',S) = 0 then begin F := F+S+#13#10; continue; end; while pos(#39,S) > 0 do Delete(S,Pos(#39,S),1); T := '; J := 0; L := 0; while S <> ' do begin P := pos('#',S); if P = 0 then begin T := T+#39+S; L := 1; break; end; if P <> 1 then T := T+copy(S,1,P-1); if J = 0 then T := T+#39; K := 0; if pos(copy(S,P+1,1),'0123456789') = 0 then inc(K); if pos(copy(S,P+2,1),'0123456789') = 0 then inc(K); if pos(copy(S,P+3,1),'0123456789') = 0 then inc(K); if pos(copy(S,P+4,1),'0123456789') = 0 then inc(K); if K > 0 then begin S := copy(S,P+1,length(S)-P); Q := pos('#',S); if Q > 0 then begin S := copy(S,Q,length(S)-Q+1); T := T + '#'+copy(S,1,Q-1); end else begin S := '; T := T + S; end; continue; end; Inc(J); S := copy(S,P+1,length(S)-P); Q := pos('#',S); R := '; R := WideChar(StrToInt(copy(S,1,5))); T := T+R; if Q > 6 then T := T + copy(S,6,Q-6); S := copy(S,Q,length(S)-Q+1); if Q = 0 then S := copy(S,6,length(S)-6+1); end; if L = 0 then F := F+T+#39#13#10 else F := F+T+#13#10; end; CloseFile(H); AssignFile(H,DirectoryListBox2.Directory+'\'+FileListBox1.Items[I]); ReWrite(H); WriteLn(H,F); CloseFile(H); end; showmessage('Translater Ok'); end;
function TForm1.CopyFileTo(const Source, Destination: string): Boolean; var SourceStream: TFileStream; begin result := false; if not FileExists(Destination) then begin SourceStream := TFileStream.Create(Source, fmOpenRead); try with TFileStream.Create(Destination, fmCreate) do try CopyFrom(SourceStream, 0); finally free; end; finally SourceStream.free; end; result := true; end; end;
end. ================================== object Form1: TForm1 Left = 193 Top = 111 Width = 443 Height = 348 Caption = 'Form1' Color = clBtnFace Font.Charset = CHINESEBIG5_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = '細明體' Font.Style = [] OldCreateOrder = False OnCreate = FormCreate PixelsPerInch = 96 TextHeight = 11 object Label1: TLabel Left = 16 Top = 24 Width = 186 Height = 11 Caption = 'Source Directory( for Delphi 6)' end object Label2: TLabel Left = 224 Top = 24 Width = 186 Height = 11 Caption = 'Target Directory( for Delphi 5)' end object DirectoryListBox1: TDirectoryListBox Left = 16 Top = 64 Width = 185 Height = 193 ImeName = '蒙恬筆輸入法' ItemHeight = 16 TabOrder = 0 OnChange = DirectoryListBox1Change end object DirectoryListBox2: TDirectoryListBox Left = 224 Top = 64 Width = 185 Height = 193 ImeName = '蒙恬筆輸入法' ItemHeight = 16 TabOrder = 1 OnChange = DirectoryListBox1Change end object Edit1: TEdit Left = 16 Top = 40 Width = 185 Height = 19 ImeName = '蒙恬筆輸入法' TabOrder = 2 Text = 'Edit1' end object Edit2: TEdit Left = 224 Top = 40 Width = 185 Height = 19 ImeName = '蒙恬筆輸入法' TabOrder = 3 Text = 'Edit2' end object Button1: TButton Left = 216 Top = 288 Width = 75 Height = 25 Caption = 'Translater' TabOrder = 4 OnClick = Button1Click end object FileListBox1: TFileListBox Left = 38 Top = 142 Width = 145 Height = 97 ImeName = '蒙恬筆輸入法' ItemHeight = 11 TabOrder = 5 Visible = False end end