diff --git a/samples/geotag.cpp b/samples/geotag.cpp index 1b5b222421..dad727e7c5 100644 --- a/samples/geotag.cpp +++ b/samples/geotag.cpp @@ -8,6 +8,7 @@ #include #include +#include #include #if __has_include() @@ -475,48 +476,55 @@ bool readDir(const char* path, Options& options) { return bResult; } -inline size_t sip(FILE* f, char* buffer, size_t max_len, size_t len) { - while (!feof(f) && len < max_len && buffer[len - 1] != '>') - buffer[len++] = fgetc(f); +inline size_t sip(std::ifstream& f, char* buffer, size_t max_len, size_t len) { + while (f && len < max_len && buffer[len - 1] != '>') { + char c; + f.get(c); + buffer[len++] = c; + } return len; } bool readXML(const char* path, Options& options) { - FILE* f = fopen(path, "r"); + std::ifstream file(path); + if (!file) { + return false; + } + XML_Parser parser = XML_ParserCreate(nullptr); - bool bResult = f && parser; - if (bResult) { - char buffer[8 * 1024]; - UserData me(options); - - XML_SetUserData(parser, &me); - XML_SetElementHandler(parser, startElement, endElement); - XML_SetCharacterDataHandler(parser, charHandler); - - // a little sip at the data - size_t len = fread(buffer, 1, sizeof(buffer) - 100, f); - const char* lead = "(len), len == 0) == XML_STATUS_OK; - } + if (!parser) { + return false; + } - // drink the rest of the file - while (bResult && len != 0) { - len = fread(buffer, 1, sizeof(buffer) - 100, f); - len = sip(f, buffer, sizeof buffer, len); - bResult = XML_Parse(parser, buffer, static_cast(len), len == 0) == XML_STATUS_OK; - }; + bool bResult = true; + std::vector buffer(8 * 1024); + UserData me(options); + + XML_SetUserData(parser, &me); + XML_SetElementHandler(parser, startElement, endElement); + XML_SetCharacterDataHandler(parser, charHandler); + + // A little sip at the data + file.read(buffer.data(), buffer.size() - 100); + std::streamsize len = file.gcount(); + const char* lead = " 0 && strncmp(lead, buffer.data(), strlen(lead)) == 0; + + // Swallow it + if (bResult) { + len = sip(file, buffer.data(), buffer.size(), len); + bResult = XML_Parse(parser, buffer.data(), static_cast(len), len == 0) == XML_STATUS_OK; } - if (f) - fclose(f); - if (parser) - XML_ParserFree(parser); + // Drink the rest of the file + while (bResult && len > 0) { + file.read(buffer.data(), buffer.size() - 100); + len = file.gcount(); + len = sip(file, buffer.data(), buffer.size(), len); + bResult = XML_Parse(parser, buffer.data(), static_cast(len), len == 0) == XML_STATUS_OK; + } + XML_ParserFree(parser); return bResult; } @@ -547,8 +555,7 @@ time_t readImageTime(const std::string& path, std::string* pS = nullptr) { for (size_t i = 0; !result && dateStrings[i]; i++) { const char* dateString = dateStrings[i]; try { - Image::UniquePtr image = ImageFactory::open(path); - if (image.get()) { + if (auto image = ImageFactory::open(path)) { image->readMetadata(); ExifData& exifData = image->exifData(); // printf("%s => %s\n",dateString, exifData[dateString].toString().c_str()); @@ -579,22 +586,21 @@ bool sina(const char* s, const char** a) { } int readFile(const char* path, const Options& /* options */) { - FILE* f = fopen(path, "r"); - int nResult = f ? typeFile : typeUnknown; - if (f) { - const char* ext = strstr(path, "."); - if (ext) { - const char* docs[] = {".doc", ".txt", nullptr}; - const char* code[] = {".cpp", ".h", ".pl", ".py", ".pyc", nullptr}; - if (sina(ext, docs)) - nResult = typeDoc; - if (sina(ext, code)) - nResult = typeCode; - } - fclose(f); + if (!fs::exists(path)) { + return typeUnknown; + } + + const char* ext = strstr(path, "."); + if (ext) { + const char* docs[] = {".doc", ".txt", nullptr}; + const char* code[] = {".cpp", ".h", ".pl", ".py", ".pyc", nullptr}; + if (sina(ext, docs)) + return typeDoc; + if (sina(ext, code)) + return typeCode; } - return nResult; + return typeFile; } Position* searchTimeDict(TimeDict_t& td, const time_t& time, long long delta) { diff --git a/samples/path-test.cpp b/samples/path-test.cpp index 5046a7c49b..56c25d55f6 100644 --- a/samples/path-test.cpp +++ b/samples/path-test.cpp @@ -32,8 +32,8 @@ int main(int argc, char* const argv[]) { std::istringstream is(line); is >> path >> dir >> base; auto p = fs::path(path); - std::string d = p.parent_path().string(); - std::string b = p.filename().string(); + auto d = p.parent_path(); + auto b = p.filename(); if (d != dir || b != base) { std::cout << path << "\t'" << d << "'\t '" << b << "'\t ==> Testcase failed\n"; diff --git a/src/basicio.cpp b/src/basicio.cpp index 4c3b5c428f..0a905078af 100644 --- a/src/basicio.cpp +++ b/src/basicio.cpp @@ -942,7 +942,7 @@ std::string XPathIo::writeDataToFile(const std::string& orgPath) { if (_setmode(_fileno(stdin), _O_BINARY) == -1) throw Error(ErrorCode::kerInputDataReadFailed); #endif - std::ofstream fs(path.c_str(), std::ios::out | std::ios::binary | std::ios::trunc); + std::ofstream fs(path, std::ios::out | std::ios::binary | std::ios::trunc); // read stdin and write to the temp file. char readBuf[100 * 1024]; std::streamsize readBufSize = 0; @@ -955,7 +955,7 @@ std::string XPathIo::writeDataToFile(const std::string& orgPath) { } while (readBufSize); fs.close(); } else if (prot == pDataUri) { - std::ofstream fs(path.c_str(), std::ios::out | std::ios::binary | std::ios::trunc); + std::ofstream fs(path, std::ios::out | std::ios::binary | std::ios::trunc); // read data uri and write to the temp file. size_t base64Pos = orgPath.find("base64,"); if (base64Pos == std::string::npos) { diff --git a/src/version.cpp b/src/version.cpp index 09ea5b7875..dbf65ed7e1 100644 --- a/src/version.cpp +++ b/src/version.cpp @@ -179,7 +179,7 @@ static std::vector getLoadedLibraries() { // read file /proc/self/maps which has a list of files in memory // (this doesn't yield anything on __sun__) - std::ifstream maps("/proc/self/maps", std::ifstream::in); + std::ifstream maps("/proc/self/maps"); std::string string; while (std::getline(maps, string)) { std::size_t pos = string.find_last_of(' '); diff --git a/unitTests/test_futils.cpp b/unitTests/test_futils.cpp index 755b06a023..f13055f42b 100644 --- a/unitTests/test_futils.cpp +++ b/unitTests/test_futils.cpp @@ -30,10 +30,10 @@ TEST(strError, returnSuccessAfterClosingFile) { // -> reset errno so that a real failure is only detected here errno = 0; - std::string tmpFile("tmp.dat"); - std::ofstream auxFile(tmpFile.c_str()); + fs::path tmpFile("tmp.dat"); + std::ofstream auxFile(tmpFile); auxFile.close(); - fs::remove(tmpFile.c_str()); + fs::remove(tmpFile); ASSERT_TRUE(Internal::contains(strError(), "(errno = 0)")); }