Skip to content

Commit cb5b60e

Browse files
committed
Set buffer size optional
1 parent 20ba566 commit cb5b60e

3 files changed

Lines changed: 32 additions & 11 deletions

File tree

PystykorvaLib/Pystykorva.cpp

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,33 @@ namespace Pystykorva
105105
_callbacks(callbacks),
106106
_rdi(options.Directory, std::filesystem::directory_options::skip_permission_denied)
107107
{
108+
if (_options.SearchExpression.empty())
109+
{
110+
throw std::invalid_argument("Search expression cannot be empty");
111+
}
112+
else if (_options.SearchExpression.size() > 0x400)
113+
{
114+
throw std::invalid_argument("Search expression is too long");
115+
}
116+
117+
if (_options.ReplacementText.size() > 0x400)
118+
{
119+
throw std::invalid_argument("Replacement text is too long");
120+
}
121+
122+
if (_options.BufferSize == 0)
123+
{
124+
throw std::invalid_argument("Buffer size cannot be zero");
125+
}
126+
else if (_options.BufferSize < _options.SearchExpression.size() * 4)
127+
{
128+
throw std::invalid_argument("Buffer size is too small for the search expression");
129+
}
130+
131+
if (_options.MaximumThreads == 0)
132+
{
133+
throw std::invalid_argument("Maximum threads cannot be zero");
134+
}
108135
}
109136

110137
Runner::~Runner()
@@ -114,11 +141,6 @@ namespace Pystykorva
114141

115142
void Runner::Start()
116143
{
117-
if (!_options.MaximumThreads)
118-
{
119-
_options.MaximumThreads = 1u;
120-
}
121-
122144
_start = std::chrono::high_resolution_clock::now();
123145

124146
if (_callbacks.Started)

PystykorvaLib/Pystykorva.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ namespace Pystykorva
2929
std::filesystem::file_time_type MaximumTime = std::filesystem::file_time_type() + std::chrono::years(2000);
3030

3131
uint32_t MaximumThreads = std::thread::hardware_concurrency();
32+
uint64_t BufferSize = 0x400 * 0x400; // 1 MiB
3233
};
3334

3435
enum Status : uint32_t
@@ -194,8 +195,8 @@ namespace Pystykorva
194195
std::filesystem::path Next();
195196
void BackgroundThread(std::stop_token token);
196197

197-
Options _options;
198-
Callbacks _callbacks;
198+
const Options _options;
199+
const Callbacks _callbacks;
199200

200201
std::chrono::high_resolution_clock::time_point _start;
201202
std::stop_source _stopSource;

PystykorvaLib/TextProcessor.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -352,9 +352,7 @@ namespace Pystykorva
352352

353353
void TextProcessor::ProcessFile(std::fstream& file, size_t size, Result& result) const
354354
{
355-
constexpr size_t ChunkSize = 0x400 * 0x400; // 1MB Chunk Read
356-
357-
size_t initialAllocation = std::min(ChunkSize, size);
355+
size_t initialAllocation = std::min(_options.BufferSize, size);
358356
std::string rawBuffer(initialAllocation, '\0');
359357

360358
std::string activeWindow;
@@ -372,7 +370,7 @@ namespace Pystykorva
372370
}
373371

374372
size_t remainingBytes = size - totalBytesProcessed;
375-
size_t bytesToRead = std::min(ChunkSize, remainingBytes);
373+
size_t bytesToRead = std::min(_options.BufferSize, remainingBytes);
376374

377375
file.read(rawBuffer.data(), bytesToRead);
378376
size_t bytesRead = file.gcount();

0 commit comments

Comments
 (0)