// // // // AnsiToUtf8 // // 2010.7.3 // // QQ:52688226 // // // // program ansi2utf8; {$APPTYPE CONSOLE} {$R *.res} uses windows, SysUtils, classes; label start, restart; var fi, fo, fin, fout: textfile; // s, T, k, Si: string; hOutput: THandle; TS, Sr: TStringList; begin //---------------------------- try begin if ParamCount = 2 then begin assignfile(fi, ParamStr(1)); reset(fi); assignfile(fo, ParamStr(2)); rewrite(fo); s := #239#187#191; write(fo, s); while not eof(fi) do begin readln(fi, s); s := AnsitoUtf8(s); writeln(fo, s); end; begin closefile(fi); closefile(fo); if FileExists(ParamStr(2)) = true then begin writeln('Operation Successful!'); //readln; end else begin SetConsoleTextAttribute(hOutput, 12); writeln('Operation Failed!'); //readln; end end end //------------------------ else if ParamCount = 3 then begin if (UpperCase(Paramstr(3)) = '-K') then begin Ts := TStringList.Create; assignfile(Fi, ParamStr(1)); reset(fi); assignfile(Fo, 'Buffer'); rewrite(Fo); //s := #239#187#191; //write(fo, s); //readln(fi, S); //if s <> #239#187#191 then begin while not eof(Fi) do begin readln(Fi, S); //writeln(S); //调试 s := utf8toansi(S); //writeln(S); //调试 writeln(Fo, s); end; //--------------------------- closefile(fi); closefile(fo); assignfile(Fi, ParamStr(1)); reset(fi); readln(Fi, S); S := utf8toansi(S); T := StringReplace(s, '?', '', [rfIgnoreCase]); try Ts.LoadFromFile('Buffer'); Ts.Text := StringReplace(Ts.Text, s, t, [rfIgnoreCase]); TS.SaveToFile(ParamStr(2)); finally Ts.Free; closefile(fi); deleteFile('Buffer'); end; end; begin if FileExists(ParamStr(2)) = true then begin writeln('Operation Successful!'); end else begin writeln('Operation Failed!'); end end end else begin writeln('"' + paramstr(3) + '"为无效参数!'); readln; end end //----------------------------------------- else begin //1 //-------------------------------------------------------------------- 内部 SetConsoleTitle('AnsiToUtf8 计算机DIY群_2010.7.3'); hOutput := GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(hOutput, 13); //SetConsoleTextAttribute(hOutput, FOREGROUND_INTENSITY); writeln('使用方法:'); SetConsoleTextAttribute(hOutput, 12); Writeln('参数(绝对路径)'); SetConsoleTextAttribute(hOutput, 14); writeln('<输入Ansi格式文件> <输出Utf8格式文件>'); SetConsoleTextAttribute(hOutput, 15); writeln('<CMD>例:AnsiToUtf8 Ansi.txt Utf8.txt 输出为Utf8格式文件'); writeln(' AnsiToUtf8 Utf8.txt Ansi.txt -k 输出为Ansi格式文件'); writeln('或: Ansi.txt Utf8.txt 输出为Utf8格式文件'); writeln(' Utf8.txt Ansi.txt -k 输出为Ansi格式文件'); writeln(' '); writeln(' Haiou327 V2010.7.3'); //readln; restart: begin SetConsoleTextAttribute(hOutput, 15); T:=''; //Si:=''; s:=''; k:=''; readln(T); Sr := TStringlist.Create; Sr.DelimitedText := T; Sr.Delimiter := ' '; Si := Sr[0]; k := sr[1]; if Sr.Count = 2 then begin //2 assignfile(fi, Si); reset(fi); assignfile(fo, k); rewrite(fo); s := #239#187#191; write(fo, s); while not eof(fi) do begin readln(fi, s); s := AnsitoUtf8(s); writeln(fo, s); end; begin closefile(fi); closefile(fo); Sr.Free; writeln('Operation Successful!'); //readln; goto restart; end end else begin if Sr.Count = 3 then begin if (UpperCase(Sr[2]) = '-K') then begin Ts := TStringList.Create; Si := Sr[0]; k:=sr[1]; assignfile(Fi, Si); reset(fi); assignfile(Fo, 'Buffer'); rewrite(Fo); Si := Sr[0]; //k := sr[1]; begin while not eof(Fi) do begin readln(Fi, S); s := utf8toansi(S); writeln(Fo, s); end; closefile(fi); closefile(fo); assignfile(Fi, Sr[0]); reset(fi); readln(Fi, S); S := utf8toansi(S); T := StringReplace(s, '?', '', [rfIgnoreCase]); Ts.LoadFromFile('Buffer'); Ts.Text := StringReplace(Ts.Text, s, t, [rfIgnoreCase]); TS.SaveToFile(sr[1]); Sr.Free; Ts.Free; closefile(fi); deleteFile('Buffer'); end; writeln('Operation Successful!'); //T:=''; //readln; goto restart; end //ppp else begin SetConsoleTextAttribute(hOutput, 12); writeln('"'+(Sr[2]) + '" 为无效参数!'); //readln; goto restart; end end //0000 else begin SetConsoleTextAttribute(hOutput, 12); writeln('"'+T+'"参数过长不被接收,请重新输入!'); SetConsoleTextAttribute(hOutput, 15); writeln('例:Ansi.txt Utf8.txt 输出为Utf8格式文件'); writeln(' Utf8.txt Ansi.txt -k 输出为Ansi格式文件'); goto restart; end end; end //restart end //1 end //-----------外部 //--------------------------------------------------------------------------参数 except SetConsoleTextAttribute(hOutput, 12); writeln('"' + Paramstr(1) + Si + '" 目标文件不存在或程序异常终止!'); SetConsoleTextAttribute(hOutput, 15); readln; exit; end; end.