-
Notifications
You must be signed in to change notification settings - Fork 0
/
cThreadFileReader.cpp
86 lines (78 loc) · 1.76 KB
/
cThreadFileReader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "pch.h"
#include "cThreadFileReader.h"
#include "cApp.h"
#include "Util.h"
namespace SSFI
{
void cThreadFileReader::FlushWord()
{
if (_word.length() > 0) // last word to process.
{
App.Dict.AddWord(_word);
_word.clear();
}
}
void cThreadFileReader::ReadFile(const fsx::path& filePath)
{
std::ifstream file(filePath.string()); // is RAII.
_word.clear();
int nChars = 0;
const int blockSizeMax = 16 * 1024;
std::vector<char> block(blockSizeMax);
while (this->App.PleaseExit != ExitNow)
{
if (file.eof())
{
break;
}
if (file.fail())
{
// Bad !
std::cout << "Failed to read file " << Util::CvtA(filePath) << std::endl;
return;
}
// read a block.
if (App.Params.SerialFileReads)
{
App.Params.SerialFileReadMutex.lock();
}
file.read(&block[0], blockSizeMax);
if (App.Params.SerialFileReads)
{
App.Params.SerialFileReadMutex.unlock();
}
std::streamsize sizeBlock = file.gcount();
for (int i = 0; i < sizeBlock; i++)
{
char ch = block[i];
nChars++;
// Words are delimited by any character other than A- Z, a-z, or 0-9. Words should be matched case insensitive.
if (std::isalnum(ch))
{
_word += ch; // append word.
}
else
{
FlushWord(); // end of a word?
}
}
}
FlushWord(); // partial word at end?
App.FilesRead++;
}
void cThreadFileReader::Run() // virtual
{
// Wait for PleaseExit or a file to process . If i find it, read it
while (this->App.PleaseExit != ExitNow)
{
cAppFileToRead file = App.FilesToRead.WaitForFile(this->App.PleaseExit);
if (file.IsEmpty())
{
if (this->App.PleaseExit != WaitForMore) // nothing left to do i guess. exit.
break;
continue;
}
ReadFile(file.Path);
}
}
}