Skip to content

Commit

Permalink
remove various temporaries
Browse files Browse the repository at this point in the history
No need for them.

Also rewrite some functions to use ifstream instead of fopen.

Signed-off-by: Rosen Penev <[email protected]>
  • Loading branch information
neheb committed Dec 9, 2024
1 parent 2b6eac5 commit 72f23ef
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 57 deletions.
104 changes: 55 additions & 49 deletions samples/geotag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <sys/types.h>

#include <algorithm>
#include <fstream>
#include <iostream>

#if __has_include(<filesystem>)
Expand Down Expand Up @@ -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 = "<?xml";
bResult = strncmp(lead, buffer, strlen(lead)) == 0;

// swallow it
if (bResult) {
len = sip(f, buffer, sizeof buffer, len);
bResult = XML_Parse(parser, buffer, static_cast<int>(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<int>(len), len == 0) == XML_STATUS_OK;
};
bool bResult = true;
std::vector<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
file.read(buffer.data(), buffer.size() - 100);
std::streamsize len = file.gcount();
const char* lead = "<?xml";
bResult = len > 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<int>(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<int>(len), len == 0) == XML_STATUS_OK;
}

XML_ParserFree(parser);
return bResult;
}

Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions samples/path-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
4 changes: 2 additions & 2 deletions src/basicio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/version.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ static std::vector<std::string> 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(' ');
Expand Down
6 changes: 3 additions & 3 deletions unitTests/test_futils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)"));
}

Expand Down

0 comments on commit 72f23ef

Please sign in to comment.