2042d2b4ee
- aggiustate maiuscole/minuscole per inclusione file.
244 lines
8.0 KiB
C++
244 lines
8.0 KiB
C++
//----------------------------------------------------------------------------
|
|
// EgalTech 2013-2014
|
|
//----------------------------------------------------------------------------
|
|
// File : FileCompare.cpp Data : 27.04.14 Versione : 1.5d5
|
|
// Contenuto : Funzioni per confronto tra due file (di testo).
|
|
//
|
|
//
|
|
//
|
|
// Modifiche : 11.12.13 DS Creazione modulo.
|
|
// 06.03.14 DS Possibilità di ri-sincronizzarsi dopo due linee.
|
|
// 27.04.14 DS Nel confronto binario ci si arresta alla 10^ differenza.
|
|
//
|
|
//----------------------------------------------------------------------------
|
|
|
|
//--------------------------- Include ----------------------------------------
|
|
#include "stdafx.h"
|
|
#include "/EgtDEv/Include/EGnFileCompare.h"
|
|
#include "/EgtDEv/Include/EGnScanner.h"
|
|
#include "/EgtDEv/Include/EgtStringConverter.h"
|
|
#include <fstream>
|
|
#include <iomanip>
|
|
|
|
using namespace std ;
|
|
|
|
//----------------------------------------------------------------------------
|
|
static string CharToHexString( char cVal) ;
|
|
|
|
//----------------------------------------------------------------------------
|
|
bool
|
|
TextFileCompare( const string& sFile1, const string& sFile2, const string& sRemIni, const string& sFileO, int& nDiff)
|
|
{
|
|
// apro il file per le differenze in append
|
|
ofstream ofOut ;
|
|
ofOut.open( stringtoW( sFileO), ios_base::out | ios_base::app) ;
|
|
if ( ! ofOut.good()) {
|
|
nDiff = -3 ;
|
|
return false ;
|
|
}
|
|
|
|
// emetto intestazione differenze
|
|
ofOut << "Compare (text) " << sFile1 << " to " << sFile2 << " :" << endl ;
|
|
|
|
// apro il primo file
|
|
Scanner scFile1 ;
|
|
if ( ! scFile1.Init( sFile1, sRemIni.c_str())) {
|
|
ofOut << " Missing first file !!!" << endl ;
|
|
ofOut.close() ;
|
|
nDiff = -1 ;
|
|
return false ;
|
|
}
|
|
|
|
// apro il secondo file
|
|
Scanner scFile2 ;
|
|
if ( ! scFile2.Init( sFile2, sRemIni.c_str())) {
|
|
ofOut << " Missing second file !!!" << endl ;
|
|
ofOut.close() ;
|
|
nDiff = -2 ;
|
|
return false ;
|
|
}
|
|
|
|
// ciclo sulle linee dei due file
|
|
nDiff = 0 ;
|
|
string sLine1, sLine2 ;
|
|
bool bOk1 = scFile1.GetLine( sLine1) ;
|
|
bool bOk2 = scFile2.GetLine( sLine2) ;
|
|
while ( bOk1 || bOk2) {
|
|
// se linee diverse
|
|
if ( sLine1.compare( sLine2) != 0) {
|
|
nDiff ++ ;
|
|
// emetto differenze
|
|
ofOut << " Diff :" << endl ;
|
|
ofOut << " F1(" << scFile1.GetCurrLineNbr() << "):" << sLine1 << endl ;
|
|
ofOut << " F2(" << scFile2.GetCurrLineNbr() << "):" << sLine2 << endl ;
|
|
// provo a caricare le linee successive
|
|
string sLine1S, sLine2S ;
|
|
bOk1 = scFile1.GetLine( sLine1S) ;
|
|
bOk2 = scFile2.GetLine( sLine2S) ;
|
|
// se le successive coincidono
|
|
if ( sLine1S.compare( sLine2S) == 0) {
|
|
ofOut << " resync F1(+1) with F2(+1)" << endl ;
|
|
sLine1 = sLine1S ;
|
|
sLine2 = sLine2S ;
|
|
}
|
|
// se la 1 corrente coincide con la 2 successiva
|
|
else if ( sLine1.compare( sLine2S) == 0) {
|
|
ofOut << " resync F1 with F2(+1)" << endl ;
|
|
// mi porto avanti con il file 2
|
|
sLine1 = sLine1S ;
|
|
bOk2 = scFile2.GetLine( sLine2) ;
|
|
}
|
|
// se la 2 corrente coincide con la 1 successiva
|
|
else if ( sLine2.compare( sLine1S) == 0) {
|
|
ofOut << " resync F1(+1) with F2" << endl ;
|
|
// mi porto avanti con il file 1
|
|
bOk1 = scFile1.GetLine( sLine1) ;
|
|
sLine2 = sLine2S ;
|
|
}
|
|
// altrimenti
|
|
else {
|
|
// carico le linee ancora successive
|
|
string sLine1T, sLine2T ;
|
|
bOk1 = scFile1.GetLine( sLine1T) ;
|
|
bOk2 = scFile2.GetLine( sLine2T) ;
|
|
// se la 1 corrente coincide con la 2 terza
|
|
if ( sLine1.compare( sLine2T) == 0) {
|
|
ofOut << " resync F1 with F2(+2)" << endl ;
|
|
// ritorno la terza del file 1 e mi porto avanti con il file 2
|
|
sLine1 = sLine1S ;
|
|
scFile1.UngetLine( sLine1T) ;
|
|
bOk2 = scFile2.GetLine( sLine2) ;
|
|
}
|
|
// se la 2 corrente coincide con la 1 terza
|
|
else if ( sLine2.compare( sLine1T) == 0) {
|
|
ofOut << " resync F1(+2) with F2" << endl ;
|
|
// mi porto avanti con il file 1 e ritorno la terza del file 2
|
|
bOk1 = scFile1.GetLine( sLine1) ;
|
|
sLine2 = sLine2S ;
|
|
scFile2.UngetLine( sLine2T) ;
|
|
}
|
|
// altrimenti, continuo in sincronismo
|
|
else {
|
|
sLine1 = sLine1S ;
|
|
scFile1.UngetLine( sLine1T) ;
|
|
sLine2 = sLine2S ;
|
|
scFile2.UngetLine( sLine2T) ;
|
|
}
|
|
}
|
|
}
|
|
// passo alle linee successive
|
|
else {
|
|
bOk1 = scFile1.GetLine( sLine1) ;
|
|
bOk2 = scFile2.GetLine( sLine2) ;
|
|
}
|
|
}
|
|
|
|
// emetto riassunto differenze
|
|
if ( nDiff == 0)
|
|
ofOut << " Same content" << endl ;
|
|
else
|
|
ofOut << " Found " << nDiff << " differences !!!" << endl ;
|
|
|
|
// chiudo il file di uscita
|
|
ofOut.close() ;
|
|
|
|
return true ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
bool
|
|
BinaryFileCompare( const string& sFile1, const string& sFile2, const string& sFileO, int& nDiff)
|
|
{
|
|
// apro il file per le differenze in append
|
|
ofstream ofOut ;
|
|
ofOut.open( stringtoW( sFileO), ios_base::out | ios_base::app) ;
|
|
if ( ! ofOut.good()) {
|
|
nDiff = -3 ;
|
|
return false ;
|
|
}
|
|
|
|
// emetto intestazione differenze
|
|
ofOut << "Compare (binary) " << sFile1 << " to " << sFile2 << " :" << endl ;
|
|
|
|
// apro il primo file
|
|
ifstream ifIn1 ;
|
|
ifIn1.open( stringtoW( sFile1), ios_base::in | ios_base::binary) ;
|
|
if ( ! ifIn1.good()) {
|
|
ofOut << " Missing first file !!!" << endl ;
|
|
ofOut.close() ;
|
|
nDiff = -1 ;
|
|
return false ;
|
|
}
|
|
|
|
// apro il secondo file
|
|
ifstream ifIn2 ;
|
|
ifIn2.open( stringtoW( sFile2), ios_base::in | ios_base::binary) ;
|
|
if ( ! ifIn2.good()) {
|
|
ofOut << " Missing second file !!!" << endl ;
|
|
ofOut.close() ;
|
|
ifIn1.close() ;
|
|
nDiff = -2 ;
|
|
return false ;
|
|
}
|
|
|
|
// ciclo sui buffer dei due file
|
|
nDiff = 0 ;
|
|
const int DIM_BUFF = 8192 ;
|
|
char cBuff1[DIM_BUFF] ;
|
|
char cBuff2[DIM_BUFF] ;
|
|
ifIn1.read( cBuff1, DIM_BUFF) ;
|
|
int nBuff1 = int( ifIn1.gcount()) ;
|
|
ifIn2.read( cBuff2, DIM_BUFF) ;
|
|
int nBuff2 = int( ifIn2.gcount()) ;
|
|
int nBase = 0 ;
|
|
while ( nBuff1 > 0 && nBuff2 > 0 && nDiff < 10) {
|
|
// confronto dei blocchi
|
|
for ( int i = 0 ; i < nBuff1 && i < nBuff2 ; ++i) {
|
|
if ( cBuff1[i] != cBuff2[i]) {
|
|
nDiff ++ ;
|
|
// emetto prime 10 differenze
|
|
if ( nDiff <= 10) {
|
|
ofOut << " F1(" << ( nBase + i) << "):" << CharToHexString( cBuff1[i]) << endl ;
|
|
ofOut << " F2(" << ( nBase + i) << "):" << CharToHexString( cBuff2[i]) << endl ;
|
|
}
|
|
}
|
|
}
|
|
// lettura prossimo blocco
|
|
nBase += min( nBuff1, nBuff2) ;
|
|
ifIn1.read( cBuff1, DIM_BUFF) ;
|
|
nBuff1 = int( ifIn1.gcount()) ;
|
|
ifIn2.read( cBuff2, DIM_BUFF) ;
|
|
nBuff2 = int( ifIn2.gcount()) ;
|
|
}
|
|
|
|
// emetto riassunto differenze
|
|
if ( nDiff == 0)
|
|
ofOut << " Same content" << endl ;
|
|
else if ( nDiff <= 10)
|
|
ofOut << " Found " << nDiff << " differences !!!" << endl ;
|
|
else
|
|
ofOut << " Found a lot of differences ( > 10) !!!" << endl ;
|
|
|
|
// chiudo i file
|
|
ofOut.close() ;
|
|
ifIn1.close() ;
|
|
ifIn2.close() ;
|
|
|
|
return true ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
string
|
|
CharToHexString( char cVal)
|
|
{
|
|
char szOut[5] ;
|
|
szOut[0] = '0' ;
|
|
szOut[1] = 'x' ;
|
|
for ( int i = 1 ; i >= 0 ; i--) {
|
|
szOut[2+i] = "0123456789ABCDEF"[cVal & 15] ;
|
|
cVal >>= 4 ;
|
|
}
|
|
szOut[4] = '\0' ;
|
|
|
|
return szOut ;
|
|
} |