[译]RGDiff-视觉差异工具
By robot-v1.0
本文链接 https://www.kyfws.com/applications/rgdiff-visual-diff-tool-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 8 分钟阅读 - 3521 个词 阅读量 0[译]RGDiff-视觉差异工具
原文地址:https://www.codeproject.com/Articles/3730/RGDiff-Visual-Diff-Tool
原文作者:Rob Groves
译文由本站 robot-v1.0 翻译
前言
Capture results from GNU Diff and display visually.
从GNU Diff捕获结果并可视显示.
介绍(Introduction)
RGDiff项目解析标准命令行Diff程序的输出,并以图形方式显示结果.它通过使用WIN32"脱壳"来重用GNU Diff程序(The RGDiff project parses the output from a standard command line Diff program and presents the results in a graphical manner. It re-uses the GNU Diff program by “shelling out” using the WIN32) CreateProcess()
功能.(function.)
所使用的技术可以轻松地合并到其他需要Diff功能的程序中.(The techniques used could easily be incorporated into other programs requiring Diff functionality.)
本文将详细说明上述操作.(This article explains how the above is done in more detail.)
运行RGDiff(Running RGDiff)
RGDiff需要GNU WIN32(RGDiff needs the GNU WIN32)差异文件(diff.exe)程序与以下目录位于同一目录中(program to be in the same directory as)RGDiff.exe(RGDiff.exe).该目录应该是可写的,因为RGDiff需要创建一些临时文件.(. The directory should be writeable as RGDiff needs to create some temporary files.)
RGDiff与MFC静态链接,因为它是一个独立的工具,并且(RGDiff is statically linked with MFC because it is a standalone tool and)MFC70.dll(MFC70.dll)分布不如(is probably not as widely distributed as)MFC42.dll(MFC42.dll).我知道在CodeProject成员中可能并非如此,但是为了安全起见…(. I know this is probably not true among CodeProject members, but just to be on the safe side…)
背景(Background)
每当使用Microsoft的WinDiff时,我都会对它的"斑马条纹"感到沮丧.我使用了其他GUI(Whenever I have used Microsoft’s WinDiff, I could not help but be frustrated by its “Zebra Stripes”. I have used other GUI)*差异(Diff)*工具,并发现并排方法可以帮助我更好地了解文件之间的差异.我环顾了WWW,找不到任何免费的工具,没有做我想做的事,因此,我决定去写我自己的书.(tools, and found that a side by side approach helps me understand the differences between the files much better. I had a look around the WWW and couldn’t find anything that was free and did what I wanted, I therefore decided to have a go at writing my own.)
我的第一个决定是是否写自己的(My first decision was whether to write my own)*差异(Diff)*算法或窃取别人的.我看着(algorithm or rip off somebody else’s. I looked at)*差异(Diff)*算法,WinDiff代码和GNU(algorithms, the WinDiff code, and the GNU)*差异(Diff)*代码,并在我有空的时候发现它们无懈可击:-((code, and found them all impenetrable in the time I had available :-()
因此,我认为对我来说,唯一现实的方法是使用现有命令行(I therefore decided that the only realistic approach for me was to use an existing command line)*差异(Diff)实用程序作为衍生过程,这是 nix上的常见设计模式. GNU(*utility as a spawned process, which is a common design pattern on nix. The GNU)*差异(Diff)*实用程序似乎是一个好的开始,而在像Richard Stallman这样的程序员的支持下,我觉得可能需要花一些时间来改进他们的算法! (一个人必须知道他的局限性).(utility seemed like a good start, and with programmers like Richard Stallman behind it, I felt that it might take me a while to improve on their algorithms! (A man’s gotta know his limitations).)
的(The) GNU WIN32项目(GNU WIN32 Project) 提供编译(provides a compiled)差异文件(diff.exe)(以及许多其他有用的东西),并且我包括了((and a lot of other useful stuff), and I have included)*差异文件(diff.exe)*作为两个下载的一部分.上页提供了有关在读者需要时如何获取源代码的详细信息.(as part of both downloads. The above page provides details of how to get the source code if the reader should require that.)
使用的技巧(Techniques Used)
该项目使用标准的WIN32/MFC技术,如下所示:(This project uses standard WIN32/MFC techniques as follows:)
- 产生一个过程并捕获结果(Spawn a process and capture the results)
- 具有多个窗口的SDI应用程序(SDI application with multiple windows)
- 分离窗(Splitter windows)
- 所有者绘制列表框(Owner draw list boxes)
- 专用列表框(Specialized list boxes)
- 注册表访问(Registry access)
比较文件(Comparing Files)
这是Diff用于比较2个文件的地方.命令和输出将类似于以下内容:(This is where Diff is used to compare 2 files. The command and output will be something like the following:)
C:\Code\DIFF>diff f1.cpp f2.cpp
1,2d0
< // program to add 2 numbers
< //
8c6,7
< return x + y;
---
> int res(x+y)
> return res;
15a15,20
>
> //int main(int argc, char** argv)
> //{
> // cout << "Result: " << Addit(1,2) << endl;
> // return 1;
> //}
我们感兴趣的线是没有(The lines we are interested in are the ones without)**<(<)要么(or)>(>)**开头的符号,因为这些符号只是为了帮助人们查看哪些行来自哪个文件.其他行告诉我们文件之间的区别是什么,或者换句话说,我们需要对两个文件进行处理以使其与另一个相同.我们解析并使用这些行来构建视觉显示.(signs at the start, as these are simply to help a human see which lines come from which file. The other lines tell us what the differences between the files are, or put another way, what we need to do to either file to make it the same as the other. It is these lines that we parse out and use to build the visual display.)
上面的结果解释如下:(The results above are interpreted as follows:)
**1,2d0**
表示删除文件1的第1-2行;或者,如果将文件2更改为文件1,则在文件2的第0行之后附加文件1的第1-2行.(means delete lines 1-2 of file 1; or, if changing file 2 into file 1, append lines 1-2 of file 1 after line 0 of file 2.)**8c6,7**
表示将文件1的第8行更改为文件2的6-7行;或者,如果将文件2更改为文件1,则将文件2的第6-7行更改为文件1的第8行.(means change line 8 of file 1 to read as lines 6-7 of file 2; or, if changing file 2 into file 1, change lines 6-7 of file 2 to read as line 8 of file 1.)**15a15,20**
表示在文件1的第15行之后附加文件2的第15-20行;或者,如果将文件2更改为文件1,则删除文件2的第15-20行.(means append lines 15-20 of file 2 after line 15 of file 1; or, if changing file 2 into file 1, delete lines 15-20 of file 2.) 的(The) GNU Diff文档(GNU Diff documentation) 全面描述了结果的解释方式.(fully describes the way the results should be interpreted.)
比较目录(Comparing Directories)
比较2个文件是好的,但是能够递归WinDiff之类的子目录会更好.幸运的是(Comparing 2 files is good, but being able to recurse sub-directories like WinDiff is better. Luckily)*差异文件(diff.exe)*提供命令行选项以允许此操作.命令和输出将类似于以下内容:(provides command line options to allow this. The command and output will be something like the following:)
C:\Code\DIFF>diff --recursive --report-identical-files --brief dir1 dir2
Files dir1\file1.cpp and dir2\file1.cpp are identical
Files dir1\file2.cpp and dir2\file2.cpp differ
Only in dir1: file3.cpp
Only in dir2: file4.cpp
命令行选项主要是自我解释,但是(The command line options are mostly self explanatory, but) --brief
告诉Diff仅汇总每个文件.如果省略此选项,则将为每个文件生成完整的Diff报告,耗时更长.上面的命令执行得非常快,尽管我只在200个文件左右,有20个左右子目录的中等大小的项目上尝试过.(tells Diff to only summarize each file. If this was omitted, a full Diff report for each file would be produced, taking longer. The above command executes very quickly, although I have only tried it on medium size projects of 200 or so files, with 20 or so sub-directories.)
与WinDiff一样,对结果进行解析以生成摘要报告.双击一个文件,然后再次调用Diff来比较这2个文件,并显示相同的详细报告,就像用户刚刚选择比较这2个文件一样.(The results are parsed to produce a summary report just like with WinDiff. Double clicking a file then invokes Diff again to compare those 2 files, and a detailed report is displayed the same, as if the user had just chosen to compare those 2 files.)
请注意,因为文件名可以包含序列(Note that because file names can contain the sequence) " and "
,在解析结果时需要格外小心.(, some care is required when parsing the results.)
滚动视图(The Scrolling Views)
我选择将每个视图实现为所有者绘图列表框.这使我可以在很大程度上自定义显示,并且我认为它们看起来还可以.一个问题是,当在详细信息模式下并排显示2个文件时,我希望它们在滚动其中一个时保持同步.我通过捕获所有消息来实现此目的,并且在收到滚动消息时,首先将其发送到另一个列表框.这可以正常工作,但有时当一个视图在另一个视图之后稍稍移动时,有时看起来会有些奇怪.(I have chosen to implement each view as an owner-draw list box. This allows me to customize the display to a great extent and I think they look OK. One problem is that when 2 files are displayed side by side in detail mode, I want them to keep in step when either one is scrolled. I achieve this by capturing all messages and when a scroll message is received, I first send it to the other list box. This works fine but can look a little odd sometimes as one view moves slightly after the other.)
未来方向(Future Direction)
我很高兴RGDiff提供了一个有用但最小的实现,而无需花费太多的精力.但是,以下是我认为可以改善RGDiff的一些事项:(I am fairly pleased that RGDiff provides a useful, yet minimal implementation without too much effort expended. However the following are a few things that I think could improve RGDiff:)
- 鸟瞰图在左边距,像WinDiff(Birds eye view in the left margin, like WinDiff)
- 合并功能(一次或合并)(Merge functionality (one at a time, or en-masse))
- 详细视图之间的视图之间更好的同步滚动(Better synchronized scrolling between views, in detail view)
- 更好地记住最近使用的文件,例如WinDiff的CodeProject前端(Better remembering of recently used files, like CodeProject front end to WinDiff)
历史(History)
1.0.0.3-2003年3月3日-初始版本(1.0.0.3 - 3rd March 2003 - Initial Version)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
VC7.0 C++ VC6 Windows Win2K MFC Visual-Studio Dev 新闻 翻译