2009-06-23 135 views
0

我想寫我的第一個迭代器類/容器類型。基本上,我希望能夠遍歷一個文件,將文件即時轉換爲HEX,並將結果傳遞到boost :: xpressive庫中。我不想對RAM中的字符串進行一次性轉換,因爲我需要能夠處理的某些文件比預期的系統RAM(多GB)大。迭代器的概念問題

這是我的代碼的樣子。我使用的是MSVC++ 2008

頁眉的迭代器:

class hexFile; 

class hexIterator : public std::iterator<std::bidirectional_iterator_tag, wchar_t> 
{ 
    hexFile *parent; 
    __int64 cacheCurrentFor; 
    wchar_t cacheCharacters[2]; 
    void updateCache(); 
public: 
    bool highCharacter; 
    __int64 filePosition; 
    hexIterator(); 
    hexIterator(hexFile *file, bool begin); 
    hexIterator operator++(); 
    hexIterator operator++(int) 
    { 
     return ++(*this); 
    } 
    hexIterator operator--(); 
    hexIterator operator--(int) 
    { 
     return --(*this); 
    } 
    bool operator==(const hexIterator& toCompare) const; 
    bool operator!=(const hexIterator& toCompare) const; 
    wchar_t& operator*(); 
    wchar_t* operator->(); 
}; 

執行情況的迭代器:

#include <stdafx.h> 
class hexFile; 
hexIterator::hexIterator() 
{ 
    parent = NULL; 
    highCharacter = false; 
    filePosition = 0; 
} 
hexIterator::hexIterator(hexFile *file, bool begin) : parent(file) 
{ 
    if(begin) 
    { 
     filePosition = 0; 
     highCharacter = false; 
    } else 
    { 
     filePosition = parent->fileLength; 
     highCharacter = true; 
    } 
} 

hexIterator hexIterator::operator++() 
{ 
    if (highCharacter) 
    { 
     highCharacter = false; 
     filePosition++; 
    } else 
    { 
     highCharacter = true; 
    } 
    return (*this); 
} 

hexIterator hexIterator::operator--() 
{ 
    if (highCharacter) 
    { 
     highCharacter = false; 
    } else 
    { 
     filePosition--; 
     highCharacter = true; 
    } 
    return (*this); 
} 

bool hexIterator::operator==(const hexIterator& toCompare) const 
{ 
    if (toCompare.filePosition == filePosition && 
     toCompare.highCharacter == highCharacter) 
     return true; 
    else return false; 
} 

bool hexIterator::operator!=(const hexIterator& toCompare) const 
{ 
    if (toCompare.filePosition == filePosition && 
     toCompare.highCharacter == highCharacter) 
     return false; 
    else return true; 
} 
wchar_t& hexIterator::operator*() 
{ 
    updateCache(); 
    if (highCharacter) 
     return cacheCharacters[1]; 
    return cacheCharacters[0]; 
} 

wchar_t* hexIterator::operator->() 
{ 
    updateCache(); 
    if (highCharacter) 
     return cacheCharacters + 1; 
    return cacheCharacters; 
} 

void hexIterator::updateCache() 
{ 
    if (filePosition == cacheCurrentFor) 
     return; 
    BYTE rawData; 
    DWORD numberRead; 
    LONG lowValue = static_cast<LONG>(filePosition); 
    LONG highValue = static_cast<LONG>(filePosition >> 32); 
    SetFilePointer(parent->theFile, lowValue, &highValue, FILE_BEGIN); 
    if (!ReadFile(parent->theFile, &rawData, 1, &numberRead, 0)) 
     throw std::runtime_error(eAsciiMsg("Error reading from file.")); 
    static const wchar_t hexCharacters[] = L"ABCDEF"; 
    cacheCharacters[0] = hexCharacters[rawData & 0x0F]; 
    cacheCharacters[1] = hexCharacters[rawData >> 4]; 
    cacheCurrentFor = filePosition; 
} 

頁眉集裝箱

class hexFile { 
public: 
    HANDLE theFile; 
    unsigned __int64 fileLength; 
    hexFile(const std::wstring& fileName) 
    { 
     theFile = CreateFile(fileName.c_str(),GENERIC_READ,FILE_SHARE_DELETE|FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,NULL,NULL); 
     if (theFile == INVALID_HANDLE_VALUE) 
     { 
      throw std::runtime_error(eAsciiMsg("Could not open file!")); 
     } 
     BY_HANDLE_FILE_INFORMATION sizeFinder; 
     GetFileInformationByHandle(theFile, &sizeFinder); 
     fileLength = sizeFinder.nFileSizeHigh; 
     fileLength <<= 32; 
     fileLength += sizeFinder.nFileSizeLow; 
    }; 
    ~hexFile() 
    { 
     CloseHandle(theFile); 
    }; 
    hexIterator begin() 
    { 
     hexIterator theIterator(this, true); 
     return theIterator; 
    }; 
    hexIterator end() 
    { 
     hexIterator theIterator(this, false); 
     return theIterator; 
    }; 
}; 

但是,不管是什麼測試用例我嘗試,正則表達式永遠不會匹配。我假設這是我的迭代器的一些概念問題..它應該是一個常數雙向迭代器在所有情況下。

我錯過了什麼,還是有更簡單的方法來做到這一點?

Billy3

回答

1

這是相當多的代碼讀取;乍一看,我什麼也沒看到。

對於實現筆記,這聽起來像是一個iostream,而不是迭代器,它可以顯着加快緩衝。 Boost有一個圖書館來幫助實施those

如果你想使用迭代器,他們也有一個library來幫助解決這個問題,它確保你獲得所需的所有方法,同時簡化實現。

對於調試,我會嘗試進行單元測試;應該希望指出問題;從一些小的東西開始,比如一個有兩個字符的文件,在調試器中遍歷,看看發生了什麼,然後迭代直到你通過所有的東西。

+0

問題是boost :: xpressive不知道如何使用iostream。 – 2009-06-23 03:24:50

1

operator++(int)應在operator++(),而不是其他的方式來定義。嘗試切換它們。相同operator--

+0

沒有喜悅:(對不起。 – 2009-06-23 02:50:44

+0

編輯上述內容以反映差異。 – 2009-06-23 02:52:30

1

呃...事實證明我是一個完全白癡。

boost :: xpressive :: regex_match(fileInstance.begin(),fileInstance.end(),results,internalRegex);

嗯...我的意思做的是

的boost :: xpressive中的regex :: _search(fileInstance.begin(),fileInstance.end(),結果,internalRegex);

迭代器從來沒有錯的東西。

對不起,浪費別人的時間, Billy3