📄 mdiff.cpp
字号:
// $mdiff\mdiff.cpp 1.5 milbo$ print difference between two files,// ignoring text in either file between []//// This prints the FIRST difference of multiple consecutive different lines.// It then resynchs and looks for further differences.// Prints up to 5 differences.//// I wrote this to do diffs between test results that have different printed// time results but should be otherwise the same.// Bracketed time results look like this [Time 2.34]//// Warning: this is raw research code -- expect it to be quite messy.// milbo durban dec05#include <stdio.h>#include <stdlib.h>#include <string.h>#include <stdarg.h>#define MAX_LEN 10000#define MAX_DIFFS 10//-----------------------------------------------------------------------------static void __cdecl Err (const char *pArgs, ...) // args like printf{static char s[MAX_LEN];va_list pArg;va_start(pArg, pArgs);vsprintf(s, pArgs, pArg);va_end(pArg);printf("%s", s);exit(-1);}//-----------------------------------------------------------------------------static void PrintErr (int iLine, char sFile1[], char sLine1[], char sFile2[], char sLine2[]){int iLen = strlen(sLine1)-1;int nLen = strlen(sFile1);if (strlen(sFile2) > nLen) nLen = strlen(sFile2);printf("%*s %5d: %s%s", nLen, sFile1, iLine, sLine1, ((iLen < 0 || sLine1[iLen] != '\n')? "\n": ""));iLen = strlen(sLine2)-1;printf("%*s %5d: %s%s\n", nLen, sFile2, iLine, sLine2, ((iLen < 0 || sLine2[iLen] != '\n')? "\n": ""));}//-----------------------------------------------------------------------------// Like strcmp but skips (i.e. ignores) text between [square brackets]static int iStrCmpSkipBrackets (char s1[], char s2[]){int i, j;for (i = 0, j = 0; s1[i] && s2[j]; i++, j++) { if (s1[i] == '[') // skip [] while (s1[i] && s1[i] != ']') i++; if (s2[j] == '[') while (s2[j] && s2[j] != ']') // skip [] j++; if (s1[i] != s2[j]) return s1[i] - s2[j]; }return 0;}//-----------------------------------------------------------------------------int __cdecl main (int argc, char *argv[]){if (argc != 3) Err("Usage: mdiff file1 file2\n");FILE *pFile1 = fopen(argv[1], "r");if (NULL == pFile1) Err("mdiff: can't open %s\n", argv[1]);FILE *pFile2 = fopen(argv[2], "r");if (NULL == pFile2) Err("mdiff: can't open %s\n", argv[2]);// following are static simply to keep these big variables off the stackstatic char sLine1[MAX_LEN+1], sLine2[MAX_LEN+1];static char LastLine1[MAX_LEN+1], LastLine2[MAX_LEN+1];int iLine = 0, iErr = 0;bool fLastLineHasErr = false;while (fgets(sLine1, MAX_LEN, pFile1)) { // fLineHasErr prevents us printing the same line twice, if it has multiple errors bool fLineHasErr = false; iLine++; if (!fgets(sLine2, MAX_LEN, pFile2)) { if (iErr++ < MAX_DIFFS); // can't get line from file2 PrintErr(iLine, argv[1], sLine1, argv[2], "SHORT FILE"); iErr = MAX_DIFFS; // prevent further messages fLineHasErr = true; break; } if (fLastLineHasErr) { // basic resync (allows resynch after extra lines in one of the input files) if (0 == iStrCmpSkipBrackets(sLine1, LastLine2)) fgets(sLine1, MAX_LEN, pFile1); if (0 == iStrCmpSkipBrackets(sLine2, LastLine1)) fgets(sLine2, MAX_LEN, pFile2); } int i, j; for (i = 0, j = 0; sLine1[i] && sLine2[j]; i++, j++) { if (sLine1[i] == '[') // skip [] while (sLine1[i] && sLine1[i] != ']') i++; if (sLine2[j] == '[') while (sLine2[j] && sLine2[j] != ']') // skip [] j++; if (sLine1[i] != sLine2[j]) { if (!fLastLineHasErr // don't print consecutive differences && iErr++ < MAX_DIFFS) PrintErr(iLine, argv[1], sLine1, argv[2], sLine2); fLineHasErr = true; fLastLineHasErr = true; break; } } if (!fLineHasErr) fLastLineHasErr = false; if (!fLineHasErr && (sLine1[i] != 0 || sLine2[j] != 0) // different line lengths? && iErr++ < MAX_DIFFS) { PrintErr(iLine, argv[1], sLine1, argv[2], sLine2); fLastLineHasErr = true; } if (iErr >= MAX_DIFFS) { printf("Reached MAX_DIFFS %d\n", MAX_DIFFS); break; } strcpy(LastLine1, sLine1); strcpy(LastLine2, sLine2); }if (fgets(sLine2, MAX_LEN, pFile2)) // extra line(s) in File2? { iLine++; if (iErr++ < MAX_DIFFS) PrintErr(iLine, argv[1], "SHORT FILE", argv[2], sLine2); }return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -