From ea37dcbdede6f6bcc42692c0824ff207164bc6ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9gory=20Soutad=C3=A9?= Date: Mon, 14 Mar 2022 19:56:50 +0100 Subject: [PATCH] Copy file content into new file if they're different and writeUpdate() is called --- src/uPDFParser.cpp | 20 ++++++++++++++++++++ src/uPDFTypes.cpp | 2 +- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/uPDFParser.cpp b/src/uPDFParser.cpp index faa180a..225794e 100644 --- a/src/uPDFParser.cpp +++ b/src/uPDFParser.cpp @@ -792,11 +792,31 @@ namespace uPDFParser void Parser::writeUpdate(const std::string& filename) { + struct stat _stat; + + int statRet = stat(filename.c_str(), &_stat); + int newFd = open(filename.c_str(), O_WRONLY|O_APPEND|O_CREAT, S_IRUSR|S_IWUSR); if (newFd <= 0) EXCEPTION(UNABLE_TO_OPEN_FILE, "Unable to open " << filename << " (%m)"); + // Copy file if it doesn't exists + if (statRet == -1 && errno == ENOENT) + { + char buffer[4096]; + int ret; + lseek(fd, 0, SEEK_SET); + + while (true) + { + ret = ::read(fd, buffer, sizeof(buffer)); + if (ret <= 0) + break; + ::write(newFd, buffer, ret); + } + } + ::write(newFd, "\r", 1); std::stringstream xref; diff --git a/src/uPDFTypes.cpp b/src/uPDFTypes.cpp index 90fe49b..60b12c3 100644 --- a/src/uPDFTypes.cpp +++ b/src/uPDFTypes.cpp @@ -78,7 +78,7 @@ namespace uPDFParser res += (*it)->str(); } - return res + " ]"; + return res + "]"; } void Dictionary::addData(const std::string& key, DataType* value)