CCompare secondary development interface description

ccompare.dll supports the secondary development function of comparing files, comparing and synchronizing text format files.

Environmental description

Development library environment description: QT 

The development environment of the test library version is: QT5.12.10, compiler VS (2017 v141 community version). In theory, you only need the version of your development QT environment to be consistent with the library version of CCompare.dll. The VS version is the vs2017 (v141) toolchain used during testing.

When testing and developing, pay attention to the qt version you are currently using, which is consistent with the qt version compiled by the current binary ccompare.dl; otherwise, there may be problems with compiling and linking. If your qt version is not qt5.12.10, you can install qt5.12.10 first and pass the test.

Test code example path: https://gitee.com/cxasm/cc-compare.git

vs environment configuration:

Under the ccompare directory of the test project, there are four files ccmp.h blockuserdata.h ccompare.dll ccompare.lib, which are the secondary development library files and header files of the release version. Set the include directory and library directory in vs, and set them to the ccompare directory in the sample code. The vs configuration is shown in the figure below.

Features

1 Compare and sync function

See the compareSyncFile function in the code, you only need to call this interface, input the two file path parameters to be processed, and the comparison will be automatically completed and displayed. Synchronization can be performed in the pop-up window. This kind of scene is relatively foolish, and the comparison and synchronization interface is directly completed, which is used in scenes that need to be compared and synchronized. The test run in the example code is as follows:

After comparing the synchronous operation interface, it is as follows:

 If you have customized requirements for the synchronization interface, please find secondary development.

2 Comparison function

See the compareFile function in the code , you only need to call this interface, input the two files to be processed, it will be compared automatically, and the comparison result will be given. After users get the comparison results, they can do their own custom rendering and display. ccompare.dll is used to complete the complex comparison process and algorithm, and it can flexibly display some user-defined parts. This method is more flexible, and it is more common to use scenarios in secondary development.

In the test example, after getting the comparison result,  a simple custom display is made in the callback of on_cmpSuccess . Custom display rules are: 1) The same parts are displayed in black. 2) Different parts are highlighted in red 3) Different lines are indicated by [x] in front of them. 4) Use [pad] in front of the alignment line. Based on this simple example, users can completely customize the rules to meet their own display effects. The test run in the example code is as follows:

 After comparison, the custom display effect of the test example is as follows:

Develop detailed interface description

1 Contrasting synchronous interfaces

void compareSyncFile(QString leftPath, QString rightPath);

Parameter description: leftPath is the path of the text file on the left, and rightPath is the path of the text file on the right.

Instructions for use:

CCmp* pcmp = new CCmp(this);

//The calling method of file synchronization, a comparison interface will pop up, which can be synchronized

pcmp->compareSyncFile(QString(".//testfile/1.txt"), QString(".//testfile/2.txt"));

After running, the comparison and synchronization interface in the above figure will pop up.

Note that the pcmp object is the parent object of the new pop-up window, if delete pcmp will cause the synchronization window to also close.

Preferably where needed CCmp * pcmp = new  CCmp ( this );

Then destruct pcmp when the main window is closed. This part is the knowledge of qt. We assume that you are familiar with QT/C++ knowledge and will not explain it.

2 Comparison interface

QObject* compareFile(QString leftPath, QString rightPath);

Parameter description: leftPath is the path of the text file on the left, and rightPath is the path of the text file on the right.

Return value: QObject* is an internal comparison object. Its QT parent object is the pcmp newly created earlier. It can be manually released, and it will be automatically recycled when pcmp is destructed. If you use a pcmp object and call the compareFile function many times, each call will occupy a comparison object. In order to reduce memory overhead, you can manually delete and release the return value after the comparison process is completed.

Instructions for use:

CCmp* pcmp = new CCmp(this);

//The way of file comparison, after obtaining the differences by yourself, perform custom rendering and display. When the comparison is complete, the cmpResult signal is emitted

connect(pcmp, &CCmp::cmpFinished, this, &QtWidgetsApplication1::on_cmpSuccess);

pcmp->compareFile(QString(".//testfile/1.txt"), QString(".//testfile/2.txt"));

The user needs to handle the cmpFinished signal by himself, and the result is in the signal function.

2.1 Signals cmpFinished  signal function description 

void cmpFinished(int resultType, QStringList* leftContents, QStringList* rightContents, QVector<UnequalCharsPosInfo>* leftUnequalInfo, QVector<UnequalCharsPosInfo>* rightUnequalInfo, const QList<BlockUserData*>* leftBlockData, const QList<BlockUserData*>* rightBlockData);

Parameter description: resultType return type. 0 means that the comparison is complete; 1 means that no comparison is needed, and the actual content of a file is empty. 

QStringList* leftContents: the content of each line on the left. 

QStringList* rightContents: the content of each line on the right.

The above two parameters return the content of each row in the comparison result, and the user can get each row by himself and then display it.

Note that QString uses utf16 encoding, and the toutf8() interface must be used to output the QString content into a utf8-encoded string for display. The following intervals of different blocks are measured according to the utf8 encoding length. Be sure to use utf8 encoding for all.

QVector<UnequalCharsPosInfo>* leftUnequalInfo: Different intervals of the left content

QVector<UnequalCharsPosInfo>* rightUnequalInfo: Different intervals of the content on the right

As mentioned earlier, the content of each row is in leftContents, but there are different parts in it, and users of these parts must be able to know their corresponding positions. leftUnequalInfo represents the interval of each distinct block. Note that the interval inside is from the overall content, not just a certain line.

We need to get the content of all utf8 content first, as follows:

QString leftText = leftContents->join("");

QByteArray leftUtf8Chars = leftText.toUtf8();

Then the interval value of leftUnequalInfo is the range represented by leftUtf8Chars. Pay attention to the conversion here.

If you want to get information about different intervals in a row, relative to the row itself, you can refer to the getUnEqualBlock function in the example . This function will perform a relative conversion on the range, and return the range of different blocks in the row.

const QList<BlockUserData*>* leftBlockData: the state of the comparison results of each row on the left

const QList<BlockUserData*>* rightBlockData: the status of the comparison results of each row on the right

The status description of each row, note that there is a BlockUserData for each row, indicating the result status of the comparison of the row - whether it is a relative row, an unequal row, or an aligned row . BlockUserData is defined in the blockuserdata.h function. Just pay attention to the int m_blockType variable. The status of the row is indicated by the following enumeration value BLOCKSTATUS . The last TEMP_INSERT_BLOCK does not need to be concerned externally and is used internally.

enum BLOCKSTATUS {

UNKNOWN_BLOCK = 0, // unknown

EQUAL_BLOCK = 1, //equal

UNEQUAL_BLOCK, // not equal

PAD_BLOCK, //alignment

LAST_PAD_EMPTY_BLOCK, // The last empty line for alignment, only possible in the last line

TEMP_INSERT_BLOCK //temporary insert block

};

For detailed usage, please refer to the on_cmpSuccess function in the sample code.

Parameter release instructions: All parameters do not need to be released by themselves; the framework will handle the release by itself; and do not modify the parameter values ​​​​of the function.

3 Interface class description

typedef struct UnequalCharsPosInfo_ {

int  start;  //start offset of different blocks

int  length;  //length of different blocks

}UnequalCharsPosInfo;

class CC_EXPORT CCmp : public QObject

{

Q_OBJECT

public:

CCmp(QObject* parent=nullptr);

virtual ~CCmp();

void  setCmpMode( int  mode ); //0 default value, ignore the blank characters before and at the end of the line 1: only ignore the blank characters at the end of the line 2: ignore all the blank characters before , during and at the end of the line .

void  setCmpParameter( bool  isBlankLineDoMatch , int  lineMatchEqualRata ); //Compare parameters. // isBlankLineDoMatch Whether blank lines participate in the comparison , the default value is true ; 

// lineMatchEqualRata The similarity rate of lines identified as equal is 50 by default , the higher the value, the stricter the matching, 50-90 is recommended.

// Compare and synchronize files .

void compareSyncFile(QString leftPath, QString rightPath);

// Only compare files, and the comparison result will be output as cmpResult signal . Comparing results is done asynchronously.

QObject* compareFile(QString leftPath, QString rightPath);

signals:

void  cmpFinished( int  resultType , QStringList * leftContents , QStringList * rightContents , QVector < UnequalCharsPosInfo >* leftUnequalInfo , QVector < UnequalCharsPosInfo >* rightUnequalInfo , const  QList < BlockUserData *>* leftBlockData , const  QList < BlockUserData *>* rightBlockData );  // comparison The completed signal needs to be connected to the signal, and the user renders the comparison result in his own slot function. Only with compareFileInterface usage; compareSyncFile does not trigger the signal.

};

Again: This example is tested in the qt5.12.10 environment, and the dynamic library and Lib are both developed based on qt5.12.10.

The compiler is vs2017. Can support linux system. Test code example path: https://gitee.com/cxasm/cc-compare.git

Guess you like

Origin blog.csdn.net/peterbig/article/details/127370575