Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions app/actions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1795,14 +1795,12 @@ int metacopy(const std::string& source, const std::string& tgt, Exiv2::ImageType
// if we used a temporary target, copy it to stdout
if (rc == 0 && bStdout) {
_setmode(fileno(stdout), O_BINARY);
if (auto f = std::fopen(target.c_str(), "rb")) {
char buffer[8 * 1024];
size_t n = 1;
while (!feof(f) && n > 0) {
n = fread(buffer, 1, sizeof buffer, f);
fwrite(buffer, 1, n, stdout);
if (auto f = std::ifstream(target, std::ios::binary)) {
std::vector<char> buffer(8 * 1024);

while (f.read(buffer.data(), buffer.size()) || f.gcount() > 0) {
std::fwrite(buffer.data(), 1, f.gcount(), stdout);
}
fclose(f);
}
}

Expand Down
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
12 changes: 6 additions & 6 deletions src/jp2image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

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

namespace Exiv2 {
Expand Down Expand Up @@ -218,13 +219,12 @@ void Jp2Image::readMetadata() {
std::copy_n(data.c_data(pad), icc.size(), icc.begin());
#ifdef EXIV2_DEBUG_MESSAGES
const char* iccPath = "/tmp/libexiv2_jp2.icc";
FILE* f = fopen(iccPath, "wb");
if (f) {
fwrite(icc.c_data(), icc.size(), 1, f);
fclose(f);
if (auto f = std::ofstream(iccPath, std::ios::binary)) {
f.write(reinterpret_cast<const char*>(icc.c_data()), static_cast<std::streamsize>(icc.size()));
f.close();
std::cout << "Exiv2::Jp2Image::readMetadata: wrote iccProfile " << icc.size() << " bytes to " << iccPath
<< '\n';
}
std::cout << "Exiv2::Jp2Image::readMetadata: wrote iccProfile " << icc.size() << " bytes to " << iccPath
<< '\n';
#endif
setIccProfile(std::move(icc));
}
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