CnPack Forum


 
Subject: 源码比较器打开dproj文件时出现乱码
FengJiwei
新警察
Rank: 1



UID 554
Digest Posts 0
Credits 35
Posts 14
点点分 35
Reading Access 10
Registered 2004-11-8
Status Offline
Post at 2008-10-7 11:32  Profile | Blog | P.M. 
源码比较器打开dproj文件时出现乱码

如图所示,dproj文件不是xml格式文件吗?用记事本打开显示正常,但为什么使用源代码比较器打开时前面的<?会显示乱码呢,另外如果此文件中包含中文字符(项目配置信息)时也是显示成乱码了,出现乱码后进行比较,明明两个相同的文件却显示有过修改,不明白了,请大家帮助解惑?

另外我尝试过,直接使用TMemo组件打开dproj文件(Memo1.Lines.LoadFromFile(*.dproj))时也出现乱码,但使用TRichEdit组件打开却不出现,奇怪了,dproj不是xml文件吗?xml文件不是文本文件吗?


Image Attachment: [SourceDiff乱码图示] SourceDiff.JPG (2008-10-7 11:32, 58.85 K)

Top
Passion (LiuXiao)
管理员
Rank: 9Rank: 9Rank: 9


UID 359
Digest Posts 19
Credits 6804
Posts 3577
点点分 6804
Reading Access 102
Registered 2004-3-28
Status Offline
Post at 2008-10-7 11:52  Profile | Blog | P.M. 
我猜可能是文本文件头的俩字节标识是UNICODE的FFFE还是啥,被读进来当作字符了。
回头我们查查。
Top
FengJiwei
新警察
Rank: 1



UID 554
Digest Posts 0
Credits 35
Posts 14
点点分 35
Reading Access 10
Registered 2004-11-8
Status Offline
Post at 2008-10-7 12:18  Profile | Blog | P.M. 
谢谢LiuXiao老大的及时回复,俺用TMemo组件为什么也不能正常显示呢,这个dproj不是文本格式文件吗?
Top
FengJiwei
新警察
Rank: 1



UID 554
Digest Posts 0
Credits 35
Posts 14
点点分 35
Reading Access 10
Registered 2004-11-8
Status Offline
Post at 2008-10-7 13:14  Profile | Blog | P.M. 
终于找到原因了,但还没有想好如何解决?

一切皆因dproj的编码是UTF-8格式,我用记事件把一个ANSI编码的文件另存为UTF-8格式后用源码比较器也是打开出现乱码,中文出现乱码,首字节出现乱码。

如何解决呢,还需要自己转换编码吗?能不能让源码比较器直接支持UTF-8格式呢?
Top
FengJiwei
新警察
Rank: 1



UID 554
Digest Posts 0
Credits 35
Posts 14
点点分 35
Reading Access 10
Registered 2004-11-8
Status Offline
Post at 2008-10-7 15:46  Profile | Blog | P.M. 
回复 #2 Passion 的帖子

报告LiuXiao老大:发现问题解决办法。

procedure TCnSourceDiffForm.DoOpenFile(OpenFile1: Boolean);
...
    if Kind = fkDiskFile then           // 存盘文件
      Lines1.LoadFromFile(FileName)  {此处不能直接LoadFromFile,应对非Ansi编码的文件进行转换处理}
...

以下是我的解决办法:
type
  TTextFormat=(tfAnsi,tfUnicode,tfUnicodeBigEndian,tfUtf8);
const
  TextFormatFlag:array[tfAnsi..tfUtf8] of word=($0000,$FFFE,$FEFF,$EFBB);

function WordLoHiExchange(w:Word):Word;register;
asm
  XCHG AL, AH
end;

{ TextFormat返回文本编码类型,sText未经处理的文本 }
procedure ReadTextFile(const FileName: string; var TextFormat: TTextFormat; var sText:string);
var
  w:Word;
  b:Byte;
begin
  with TFileStream.Create(FileName,fmOpenRead or fmShareDenyNone) do
  try
    Read(w,2);
    w:=WordLoHiExchange(w);//因为是以Word数据类型读取,故高低字节互换
    if w = TextFormatFlag[tfUnicode] then
      TextFormat:= tfUnicode
    else if w = TextFormatFlag[tfUnicodeBigEndian] then
      TextFormat:= tfUnicodeBigEndian
    else if w = TextFormatFlag[tfUtf8] then
    begin
      Read(b,1);//这里要注意一下,UFT-8必须要跳过三个字节。
      TextFormat:=tfUtf8;
    end else
    begin
      TextFormat:=tfANSI;
      Position:=0;
    end;
    SetLength(sText,Size-Position);
    ReadBuffer(sText[1],Size-Position);
  finally
    Free;
  end;
end;

procedure TCnSourceDiffForm.DoOpenFile(OpenFile1: Boolean);
var
  tff: TTextFormat;
  sMemo: string;
...
    if Kind = fkDiskFile then           // 存盘文件
    begin
      //Lines1.LoadFromFile(FileName)  
      ReadTextFile(Self.dlgOpen1.FileName, tff, sMemo);
      //
      case tff of
        tfAnsi: Lines1.Text:= sMemo;
        tfUtf8: Lines1.Text:= Utf8ToAnsi(sMemo);
        tfUnicode: Lines.Text:= WideCharToString(PWideChar(sMemo));
        //tfUnicodeBigEndian: //暂时没有找到这种编码的转换方法
      end;
    end;
Top
Passion (LiuXiao)
管理员
Rank: 9Rank: 9Rank: 9


UID 359
Digest Posts 19
Credits 6804
Posts 3577
点点分 6804
Reading Access 102
Registered 2004-3-28
Status Offline
Post at 2008-10-7 19:08  Profile | Blog | P.M. 
这样改确实是可行,但估计所有打开文件的地方都有这个问题。一个一个改起来还比较的麻烦。
Top
 




All times are GMT++8, the time now is 2024-9-17 03:42

    本论坛支付平台由支付宝提供
携手打造安全诚信的交易社区 Powered by Discuz! 5.0.0  © 2001-2006 Comsenz Inc.
Processed in 0.009085 second(s), 8 queries , Gzip enabled

Clear Cookies - Contact Us - CnPack Website - Archiver - WAP