📄 cpd.cpp
字号:
#include "cpd.h"
using namespace std;
// Debug functions
/*
void OutputHashTable(HawkEye he, long Key)
{
cout << "==Outputting Hash Table==" << endl;
FileBlock *ndPtr= he.HashTable[Key];
while( ndPtr != NULL ){
cout << ndPtr->HashKey << endl;
ndPtr= ndPtr->Next;
}
cout << "==Output Completed==" << endl;
}
*/
void HawkEye::OutputFileBlock(FileBlock *ndPtr)
{
#ifdef DEBUGFUNCTION
if(ndPtr == NULL)
cout << "Current FileBlock is NULL!!!" << endl;
else{
cout << "==Outputting File Block==" << endl;
cout << "StartPoint: " << ndPtr->StartPoint << endl;
cout << "Length: " << ndPtr->Length << endl;
cout << "FileName: " << ndPtr->FileName << endl;
cout << "HashKey: " << ndPtr->HashKey << endl;
cout << "==Output Completed==" << endl;
}
#endif
/*
cout << "==========Hash table statistic===========" << endl;
for(i= 0; i< MAX; i++){
FileBlock *fbp= HashTable[i];
int counter= 0;
while( fbp != NULL ){
counter++;
fbp=fbp->Next;
}
cout << i << ':' << counter << endl;
}
*/
}
/*
void Scan(long key)
{
FileBlock *ndPtr= HashTable[key];
int Counter= 0;
while( ndPtr != NULL ){
Counter++;
ndPtr= ndPtr->Next;
}
cout << "Scanning completed, 1003 has " << Counter << " elements." << endl;
}
*/
// Independent function
// Repo
string HawkEye::Suck(FileBlock *fbp)
{
#ifdef DEBUG_Suck
cout << "To Be Sucked: " << endl;
OutputFileBlock(fbp);
#endif
string ToBeReturned= "";
fstream fin;
fin.open( (fbp->FileName).c_str(), fstream::in );
if( !fin.good() )
cout << "Open file error in Suck()" << endl;
fin.seekg(fbp->StartPoint);
char ch;
for(i= 0; i< fbp->Length; i++){
fin.read( &ch, sizeof(char) );
ToBeReturned+= ch;
}
fin.close();
#ifdef DEBUG_Suck
cout << "Sucked Out: " << ToBeReturned << endl;
#endif
return ToBeReturned;
}
string HawkEye::GetFileName(string AbsPath)
{
int dashpos, dotpos;
dashpos= AbsPath.rfind('/');
if(dashpos == (int)string::npos)
//! If it is already the relative path
dashpos= -1;
dotpos= AbsPath.rfind('.');
return AbsPath.substr(dashpos+1, dotpos-dashpos-1);
}
string HawkEye::GetSuffix(string AbsPath)
{
unsigned int dashpos, dotpos;
dotpos= AbsPath.rfind('.');
if(dotpos == string::npos)
return "";
dashpos= AbsPath.rfind('/');
if(dashpos == string::npos)
//! If it is already the relative path
dashpos= 0;
return '.' + AbsPath.substr(dotpos+1, AbsPath.size()-dotpos-1);
}
// Independent function
int HawkEye::LCS(string astr, string bstr)
{
/*
unsigned int asize= astr.size(),
bsize= bstr.size();
int counter[asize+1][bsize+1];
for(i= 1; i<= asize; i++)
counter[i][0]= 0;
for(i= 0; i<= bsize; i++)
counter[0][i]= 0;
for(i= 1; i<= asize; i++)
for(j= 1; j<= bsize; j++)
if( astr[i-1] == bstr[j-1] )
counter[i][j]= counter[i-1][j-1] + 1;
else{
if( counter[i-1][j] > counter[i][j-1] )
counter[i][j]= counter[i-1][j];
else
counter[i][j]= counter[i][j-1];
}
return counter[asize][bsize];
*/
return 0;
}
// Independent function
// inFileName may use abs path or rel path
// outFileName may use abs path or rel path too,
// auto saved into repository
bool HawkEye::ReturnKiller(string inFileName, string outFileName)
{
fstream fin, fout;
cout << "$$fin=" << inFileName << endl;
fin.open( inFileName.c_str(), fstream::in);
assert( fin.good() );
outFileName= GetFileName(outFileName) + GetSuffix(outFileName);
cout << "$$fout=" << REPOSITORYPATH + outFileName << endl;
fout.open( (REPOSITORYPATH + outFileName).c_str(), fstream::out);
assert( fout.good() );
char ch;
while( fin.read( &ch, sizeof(char) ) )
if( ch != '\n' )
fout << ch;
fin.close();
fout.close();
return true;
}
// repo
void HawkEye::SaveHashTable()
{
fstream fout;
fout.open( (REPOSITORYPATH + m_HashTableName).c_str(), fstream::out );
fout << " " << endl;
if(fout.good())
for(i= 0; i< MAX; i++)
if( NULL != HashTable[i] ){
FileBlock *ndPtr= HashTable[i];
while( NULL != ndPtr ){
FileBlock *successor= ndPtr->Next;
fout << ndPtr->HashKey << ' ' << ndPtr->StartPoint << ' ' << ndPtr->Length << ' ' << ndPtr->FileName << ' ';
ndPtr= successor;
}
}
fout.close();
}
// repo
void HawkEye::LoadHashTable()
{
long HashKey;
fstream fin;
fin.open( (REPOSITORYPATH + m_HashTableName).c_str(), fstream::in );
if( !fin.good() ){
cout << "No document in repository yet!" << endl;
cout << "Please add document into repository first" << endl;
}else{
while( fin >> HashKey ){
FileBlock *ndPtr= new FileBlock;
ndPtr->HashKey= HashKey;
fin >> ndPtr->StartPoint >> ndPtr->Length >> ndPtr->FileName;
ndPtr->Next= NULL;
Insert(ndPtr);
}
}
fin.close();
}
void HawkEye::CleanUpHashTalbe()
{
for(i= 0; i< MAX; i++)
if( NULL != HashTable[i] ){
FileBlock *ndPtr= HashTable[i];
FileBlock *ndPtrToNext;
while( NULL != ndPtr->Next)
{
ndPtrToNext= ndPtr->Next;
delete ndPtr;
ndPtr= ndPtrToNext;
}
delete ndPtr;
}
}
//! Rebuild hash table
//!
//! Generate new hash table from current repository
//! repo
void HawkEye::RebuildHashTable()
{
fstream fin;
fin.open( (REPOSITORYPATH + "filelist.lst").c_str(), fstream::in);
string filename;
while( fin >> filename )
{
cout<<filename<<" before ins_Chunk"<<endl;
Ins_Chunk(REPOSITORYPATH + filename);
}
fin.close();
SaveHashTable();
}
//! Choose a table
//!
//! Set another hash table for current use
//! \param filename Then file name of new hash table
void HawkEye::SetHashTable(string filename)
{
m_HashTableName= filename;
}
//! Change the threshold of least string length
//!
//! Set the least string length in bytes, one can
//! adjust it dynamically by invoking this function
//! \param threshold The least string length
void HawkEye::SetStringSizeTreshold(int threshold)
{
String_Size_Treshold= threshold;
}
//! Constructor
//!
//! initialize HashTable
HawkEye::HawkEye()
{
SetHashTable("hstb.txt");
for(i= 0; i< MAX; i++)
HashTable[i]= NULL;
SetStringSizeTreshold(2);
}
//! Destructor
//!
//! Maybe Clean up HashTable
//! But nothing to do now
HawkEye::~HawkEye()
{
unsigned int size= OutputMessage.size();
if( (size != 0) && (size > String_Size_Treshold) ){
OutputMessage[size-2]='i';
OutputMessage[size-1]='s';
OutputMessage+= " Violated.";
cout << OutputMessage << endl;
}
CleanUpHashTalbe();
}
//! Check Document into Repository
//! \param PathAndName a file name string
void HawkEye::CheckIn(string PathAndName)
{
fstream FileListFin, FileListFout;
FileListFin.open( (REPOSITORYPATH + "filelist.lst").c_str(), fstream::in);
string Name= GetFileName(PathAndName);
string Suffix= GetSuffix(PathAndName);
if( FileListFin.good() ){
//! Check for if it already exsits
char EntryInList[300];
while( !FileListFin.eof() ){
FileListFin.getline(EntryInList, 300);
if( Name == EntryInList ){
cout << "File already exsit! Adding failed" << endl;
exit(1);
}
}
}
//! Open or create(if not exist) file list
FileListFout.open( (REPOSITORYPATH + "filelist.lst").c_str(), fstream::out|fstream::app);
//! Add it into file list
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -