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

Function ConvertFile

src/tools/CommandLineMain.cpp:509–560  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

507}
508
509void ConvertFile(std::string fileName) {
510 FILE* fin = OpenFileUtf8(fileName, "rb");
511 if (!fin) {
512 throw FileNotFound(fileName);
513 }
514
515 if (!outputFileName.IsNull() &&
516 IsSameFileUtf8(fileName, outputFileName.Get())) {
517 if (!inPlace) {
518 fclose(fin);
519 throw Exception("Input and output refer to the same file; use "
520 "--in-place to overwrite it.");
521 }
522
523 std::string targetFileName = outputFileName.Get();
524 GetRealPathUtf8(outputFileName.Get(), &targetFileName);
525 if (HasMultipleLinksUtf8(targetFileName)) {
526 fclose(fin);
527 throw Exception("--in-place cannot safely update files with multiple "
528 "hard links.");
529 }
530
531 std::string tempFileName;
532 FILE* fout = CreateTempFileNearUtf8(targetFileName, &tempFileName);
533 if (!fout) {
534 fclose(fin);
535 if (tempFileName.empty()) {
536 tempFileName = "temporary file";
537 }
538 throw FileNotWritable(tempFileName);
539 }
540
541 try {
542 ConvertFileStreams(fin, fout);
543 if (PreserveMetadataUtf8(targetFileName, tempFileName) != 0) {
544 RemoveFileUtf8(tempFileName);
545 throw FileNotWritable(tempFileName);
546 }
547 if (ReplaceFileUtf8(tempFileName, targetFileName) != 0) {
548 RemoveFileUtf8(tempFileName);
549 throw FileNotWritable(targetFileName);
550 }
551 } catch (...) {
552 RemoveFileUtf8(tempFileName);
553 throw;
554 }
555 return;
556 }
557
558 FILE* fout = GetOutputStream();
559 ConvertFileStreams(fin, fout);
560}
561
562void ConvertStdin() {
563 PrintInteractiveStdinHint();

Callers 1

CommandLineMainFunction · 0.85

Calls 14

IsSameFileUtf8Function · 0.85
GetRealPathUtf8Function · 0.85
HasMultipleLinksUtf8Function · 0.85
CreateTempFileNearUtf8Function · 0.85
ConvertFileStreamsFunction · 0.85
PreserveMetadataUtf8Function · 0.85
RemoveFileUtf8Function · 0.85
ReplaceFileUtf8Function · 0.85
GetOutputStreamFunction · 0.85
OpenFileUtf8Function · 0.70
ExceptionFunction · 0.50
IsNullMethod · 0.45

Tested by

no test coverage detected