Skip to content

Commit

Permalink
Replaced async with thread for mingw support
Browse files Browse the repository at this point in the history
  • Loading branch information
echo-devim committed Feb 18, 2021
1 parent 486ff1b commit 4bd58f9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
10 changes: 9 additions & 1 deletion src/core/hexeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,14 @@ vector<Match *> HexEditor::findPatterns() {
}
}

void HexEditor::initCompare() {
this->fileCompared = false;
}

bool HexEditor::hasCompared() {
return this->fileCompared;
}

vector<pair<unsigned long, uint8_t>> HexEditor::compareTo(HexEditor &hexEditor) {
vector<pair<unsigned long, uint8_t>> diff_bytes;

Expand All @@ -254,8 +262,8 @@ vector<pair<unsigned long, uint8_t>> HexEditor::compareTo(HexEditor &hexEditor)
if (this->getCurrentData()[i] != byte_new) {
diff_bytes.push_back(pair<unsigned long, uint8_t>(i, byte_new));
}
this->bytesRead = i;
}
this->fileCompared = true;

return diff_bytes;
}
3 changes: 3 additions & 0 deletions src/core/hexeditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class HexEditor
atomic<unsigned long> bytesRead;
atomic<unsigned long> bytesSaved;
atomic<bool> fileSaved;
atomic<bool> fileCompared;
HexEditor();
HexEditor(string path);
~HexEditor();
Expand All @@ -44,6 +45,8 @@ class HexEditor
string getCurrentPath();
vector<Match *> findPatterns();
vector<pair<unsigned long, uint8_t>> compareTo(HexEditor &hexEditor);
void initCompare();
bool hasCompared();

private:
PatternMatching *patternMatching;
Expand Down
10 changes: 6 additions & 4 deletions src/fhex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -835,26 +835,28 @@ void Fhex::compare(QString filename) {
this->progressBar->setValue(0);
this->statusBar.setText("Comparing file.. please wait");

future<vector<pair<unsigned long, uint8_t>>> fut_res = async([this, filename]()
vector<pair<unsigned long, uint8_t>> res;
this->hexEditor->initCompare();
std::thread th([this, filename, &res]()
{
HexEditor newHexEditor;
newHexEditor.loadFileAsync(filename.toStdString());
while(!newHexEditor.isFileLoaded()) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
return this->hexEditor->compareTo(newHexEditor);
res = this->hexEditor->compareTo(newHexEditor);
});

while (fut_res.wait_for(std::chrono::milliseconds(100)) != std::future_status::ready) {
while (!this->hexEditor->hasCompared()) {
int val = (this->hexEditor->bytesRead * 100) / this->hexEditor->loadedFileSize;
this->progressBar->setValue(val);
this->repaint();
this->app->processEvents();
}
th.join();

this->progressBar->setVisible(false);

vector<pair<unsigned long, uint8_t>> res = fut_res.get();
unsigned long changes = res.size();
unsigned long start_offset = 0;
unsigned long offset = 0;
Expand Down

0 comments on commit 4bd58f9

Please sign in to comment.