MCPcopy Create free account
hub / github.com/BYVoid/OpenCC / ConvertStream

Function ConvertStream

src/tools/CommandLineMain.cpp:394–439  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

392}
393
394void ConvertStream(FILE* fin, FILE* fout) {
395 const int BUFFER_SIZE = 1024 * 1024;
396 std::string buffer(BUFFER_SIZE, '\0');
397 ConverterStream stream(converter);
398
399 while (!feof(fin)) {
400 size_t length = fread(&buffer[0], sizeof(char), buffer.size(), fin);
401 if (length == 0) {
402 break;
403 }
404 measurement.inputBytes += length;
405 WarnIfStreamChunkContainsVariationSelector(buffer.data(), length);
406
407 const auto convertStart = std::chrono::steady_clock::now();
408 const bool isFinalChunk = length < buffer.size() && feof(fin);
409 const std::string converted =
410 isFinalChunk ? stream.Finish({buffer.data(), length})
411 : stream.ConvertChunk({buffer.data(), length});
412 measurement.convertMs += DurationToMilliseconds(
413 std::chrono::steady_clock::now() - convertStart);
414 measurement.outputBytes += converted.size();
415 const auto writeStart = std::chrono::steady_clock::now();
416 fputs(converted.c_str(), fout);
417 if (!noFlush) {
418 fflush(fout);
419 }
420 measurement.writeMs += DurationToMilliseconds(
421 std::chrono::steady_clock::now() - writeStart);
422 if (isFinalChunk) {
423 return;
424 }
425 }
426
427 const auto convertStart = std::chrono::steady_clock::now();
428 const std::string& converted = stream.Finish();
429 measurement.convertMs += DurationToMilliseconds(
430 std::chrono::steady_clock::now() - convertStart);
431 measurement.outputBytes += converted.size();
432 const auto writeStart = std::chrono::steady_clock::now();
433 fputs(converted.c_str(), fout);
434 if (!noFlush) {
435 fflush(fout);
436 }
437 measurement.writeMs += DurationToMilliseconds(
438 std::chrono::steady_clock::now() - writeStart);
439}
440
441void ConvertLineByLine() {
442 PrintInteractiveStdinHint();

Callers 2

ConvertFileStreamsFunction · 0.85
ConvertStdinFunction · 0.85

Calls 7

freadFunction · 0.85
DurationToMillisecondsFunction · 0.85
sizeMethod · 0.45
dataMethod · 0.45
FinishMethod · 0.45
ConvertChunkMethod · 0.45

Tested by

no test coverage detected