Skip to content

Commit d7d0d3d

Browse files
committed
remove various temporaries
No need for them. Also rewrite some functions to use ifstream instead of fopen. Signed-off-by: Rosen Penev <rosenp@gmail.com>
1 parent 5131014 commit d7d0d3d

File tree

5 files changed

+68
-65
lines changed

5 files changed

+68
-65
lines changed

samples/geotag.cpp

Lines changed: 60 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <sys/types.h>
99

1010
#include <algorithm>
11+
#include <fstream>
1112
#include <iostream>
1213

1314
#if __has_include(<filesystem>)
@@ -475,48 +476,55 @@ bool readDir(const char* path, Options& options) {
475476
return bResult;
476477
}
477478

478-
inline size_t sip(FILE* f, char* buffer, size_t max_len, size_t len) {
479-
while (!feof(f) && len < max_len && buffer[len - 1] != '>')
480-
buffer[len++] = fgetc(f);
479+
inline size_t sip(std::ifstream& f, char* buffer, size_t max_len, size_t len) {
480+
while (f && len < max_len && buffer[len - 1] != '>') {
481+
char c;
482+
f.get(c);
483+
buffer[len++] = c;
484+
}
481485
return len;
482486
}
483487

484488
bool readXML(const char* path, Options& options) {
485-
FILE* f = fopen(path, "r");
489+
std::ifstream file(path);
490+
if (!file) {
491+
return false;
492+
}
493+
486494
XML_Parser parser = XML_ParserCreate(nullptr);
487-
bool bResult = f && parser;
488-
if (bResult) {
489-
char buffer[8 * 1024];
490-
UserData me(options);
491-
492-
XML_SetUserData(parser, &me);
493-
XML_SetElementHandler(parser, startElement, endElement);
494-
XML_SetCharacterDataHandler(parser, charHandler);
495-
496-
// a little sip at the data
497-
size_t len = fread(buffer, 1, sizeof(buffer) - 100, f);
498-
const char* lead = "<?xml";
499-
bResult = strncmp(lead, buffer, strlen(lead)) == 0;
500-
501-
// swallow it
502-
if (bResult) {
503-
len = sip(f, buffer, sizeof buffer, len);
504-
bResult = XML_Parse(parser, buffer, static_cast<int>(len), len == 0) == XML_STATUS_OK;
505-
}
495+
if (!parser) {
496+
return false;
497+
}
506498

507-
// drink the rest of the file
508-
while (bResult && len != 0) {
509-
len = fread(buffer, 1, sizeof(buffer) - 100, f);
510-
len = sip(f, buffer, sizeof buffer, len);
511-
bResult = XML_Parse(parser, buffer, static_cast<int>(len), len == 0) == XML_STATUS_OK;
512-
};
499+
bool bResult = true;
500+
std::vector<char> buffer(8 * 1024);
501+
UserData me(options);
502+
503+
XML_SetUserData(parser, &me);
504+
XML_SetElementHandler(parser, startElement, endElement);
505+
XML_SetCharacterDataHandler(parser, charHandler);
506+
507+
// A little sip at the data
508+
file.read(buffer.data(), buffer.size() - 100);
509+
std::streamsize len = file.gcount();
510+
const char* lead = "<?xml";
511+
bResult = len > 0 && strncmp(lead, buffer.data(), strlen(lead)) == 0;
512+
513+
// Swallow it
514+
if (bResult) {
515+
len = sip(file, buffer.data(), buffer.size(), len);
516+
bResult = XML_Parse(parser, buffer.data(), static_cast<int>(len), len == 0) == XML_STATUS_OK;
513517
}
514518

515-
if (f)
516-
fclose(f);
517-
if (parser)
518-
XML_ParserFree(parser);
519+
// Drink the rest of the file
520+
while (bResult && len > 0) {
521+
file.read(buffer.data(), buffer.size() - 100);
522+
len = file.gcount();
523+
len = sip(file, buffer.data(), buffer.size(), len);
524+
bResult = XML_Parse(parser, buffer.data(), static_cast<int>(len), len == 0) == XML_STATUS_OK;
525+
}
519526

527+
XML_ParserFree(parser);
520528
return bResult;
521529
}
522530

@@ -525,8 +533,7 @@ bool readImage(const char* path, Options& /* options */) {
525533
bool bResult = false;
526534

527535
try {
528-
Image::UniquePtr image = ImageFactory::open(path);
529-
if (image.get()) {
536+
if (auto image = ImageFactory::open(path)) {
530537
image->readMetadata();
531538
ExifData& exifData = image->exifData();
532539
bResult = !exifData.empty();
@@ -547,8 +554,7 @@ time_t readImageTime(const std::string& path, std::string* pS = nullptr) {
547554
for (size_t i = 0; !result && dateStrings[i]; i++) {
548555
const char* dateString = dateStrings[i];
549556
try {
550-
Image::UniquePtr image = ImageFactory::open(path);
551-
if (image.get()) {
557+
if (auto image = ImageFactory::open(path)) {
552558
image->readMetadata();
553559
ExifData& exifData = image->exifData();
554560
// printf("%s => %s\n",dateString, exifData[dateString].toString().c_str());
@@ -579,22 +585,21 @@ bool sina(const char* s, const char** a) {
579585
}
580586

581587
int readFile(const char* path, const Options& /* options */) {
582-
FILE* f = fopen(path, "r");
583-
int nResult = f ? typeFile : typeUnknown;
584-
if (f) {
585-
const char* ext = strstr(path, ".");
586-
if (ext) {
587-
const char* docs[] = {".doc", ".txt", nullptr};
588-
const char* code[] = {".cpp", ".h", ".pl", ".py", ".pyc", nullptr};
589-
if (sina(ext, docs))
590-
nResult = typeDoc;
591-
if (sina(ext, code))
592-
nResult = typeCode;
593-
}
594-
fclose(f);
588+
if (!fs::exists(path)) {
589+
return typeUnknown;
590+
}
591+
592+
const char* ext = strstr(path, ".");
593+
if (ext) {
594+
const char* docs[] = {".doc", ".txt", nullptr};
595+
const char* code[] = {".cpp", ".h", ".pl", ".py", ".pyc", nullptr};
596+
if (sina(ext, docs))
597+
return typeDoc;
598+
if (sina(ext, code))
599+
return typeCode;
595600
}
596601

597-
return nResult;
602+
return typeFile;
598603
}
599604

600605
Position* searchTimeDict(TimeDict_t& td, const time_t& time, long long delta) {
@@ -843,15 +848,13 @@ int main(int argc, const char* argv[]) {
843848
}
844849
*/
845850
for (auto&& path : gFiles) {
846-
std::string stamp;
847851
try {
848-
time_t t = readImageTime(path, &stamp);
849-
Position* pPos = searchTimeDict(gTimeDict, t, Position::deltaMax_);
850-
Exiv2::Image::UniquePtr image = Exiv2::ImageFactory::open(path);
851-
if (image.get()) {
852+
if (auto image = Exiv2::ImageFactory::open(path)) {
852853
image->readMetadata();
853854
Exiv2::ExifData& exifData = image->exifData();
854-
if (pPos) {
855+
std::string stamp;
856+
time_t t = readImageTime(path, &stamp);
857+
if (auto pPos = searchTimeDict(gTimeDict, t, Position::deltaMax_)) {
855858
exifData["Exif.GPSInfo.GPSProcessingMethod"] = "charset=Ascii HYBRID-FIX";
856859
exifData["Exif.GPSInfo.GPSVersionID"] = "2 2 0 0";
857860
exifData["Exif.GPSInfo.GPSMapDatum"] = "WGS-84";

samples/path-test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ int main(int argc, char* const argv[]) {
3232
std::istringstream is(line);
3333
is >> path >> dir >> base;
3434
auto p = fs::path(path);
35-
std::string d = p.parent_path().string();
36-
std::string b = p.filename().string();
35+
auto d = p.parent_path();
36+
auto b = p.filename();
3737

3838
if (d != dir || b != base) {
3939
std::cout << path << "\t'" << d << "'\t '" << b << "'\t ==> Testcase failed\n";

src/basicio.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -942,7 +942,7 @@ std::string XPathIo::writeDataToFile(const std::string& orgPath) {
942942
if (_setmode(_fileno(stdin), _O_BINARY) == -1)
943943
throw Error(ErrorCode::kerInputDataReadFailed);
944944
#endif
945-
std::ofstream fs(path.c_str(), std::ios::out | std::ios::binary | std::ios::trunc);
945+
std::ofstream fs(path, std::ios::out | std::ios::binary | std::ios::trunc);
946946
// read stdin and write to the temp file.
947947
char readBuf[100 * 1024];
948948
std::streamsize readBufSize = 0;
@@ -955,7 +955,7 @@ std::string XPathIo::writeDataToFile(const std::string& orgPath) {
955955
} while (readBufSize);
956956
fs.close();
957957
} else if (prot == pDataUri) {
958-
std::ofstream fs(path.c_str(), std::ios::out | std::ios::binary | std::ios::trunc);
958+
std::ofstream fs(path, std::ios::out | std::ios::binary | std::ios::trunc);
959959
// read data uri and write to the temp file.
960960
size_t base64Pos = orgPath.find("base64,");
961961
if (base64Pos == std::string::npos) {

src/version.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ static std::vector<std::string> getLoadedLibraries() {
179179

180180
// read file /proc/self/maps which has a list of files in memory
181181
// (this doesn't yield anything on __sun__)
182-
std::ifstream maps("/proc/self/maps", std::ifstream::in);
182+
std::ifstream maps("/proc/self/maps");
183183
std::string string;
184184
while (std::getline(maps, string)) {
185185
std::size_t pos = string.find_last_of(' ');

unitTests/test_futils.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ TEST(strError, returnSuccessAfterClosingFile) {
3030
// -> reset errno so that a real failure is only detected here
3131
errno = 0;
3232

33-
std::string tmpFile("tmp.dat");
34-
std::ofstream auxFile(tmpFile.c_str());
33+
fs::path tmpFile("tmp.dat");
34+
std::ofstream auxFile(tmpFile);
3535
auxFile.close();
36-
fs::remove(tmpFile.c_str());
36+
fs::remove(tmpFile);
3737
ASSERT_TRUE(Internal::contains(strError(), "(errno = 0)"));
3838
}
3939

0 commit comments

Comments
 (0)