Skip to content

Commit 92ce2c8

Browse files
authoredMar 11, 2025··
Merge pull request Exiv2#3203 from neheb/3
vector to make_unique
2 parents 6b1ed06 + 328009d commit 92ce2c8

File tree

4 files changed

+13
-23
lines changed

4 files changed

+13
-23
lines changed
 

‎samples/geotag.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -452,10 +452,9 @@ bool readDir(const char* path, Options& options) {
452452
DIR* dir = opendir(path);
453453
if (dir != nullptr) {
454454
bResult = true;
455-
struct dirent* ent;
456455

457456
// print all the files and directories within directory
458-
while ((ent = readdir(dir)) != nullptr) {
457+
while (auto ent = readdir(dir)) {
459458
std::string pathName = makePath(path, ent->d_name);
460459
if (ent->d_name[0] != '.') {
461460
// printf("reading %s => %s\n",ent->d_name,pathName.c_str());

‎samples/iotest.cpp

+8-16
Original file line numberDiff line numberDiff line change
@@ -37,30 +37,23 @@ int main(int argc, char* const argv[]) {
3737
const char* ba = argv[5]; // block argument
3838

3939
if (argc >= 5) {
40-
int blocksize = argc == 6 ? atoi(ba) : 10000;
41-
// ensure blocksize is sane
42-
if (blocksize > 1024 * 1024)
43-
blocksize = 10000;
44-
45-
std::vector<Exiv2::byte> bytes(blocksize);
46-
4740
// copy fileIn from a remote location.
4841
auto io = Exiv2::ImageFactory::createIo(fr);
4942
if (io->open() != 0) {
50-
Error(Exiv2::ErrorCode::kerFileOpenFailed, io->path(), "rb", strError());
43+
throw Error(Exiv2::ErrorCode::kerFileOpenFailed, io->path(), "rb", strError());
5144
}
5245
FileIo output(f0);
5346
if (!output.open("wb")) {
5447
Error(Exiv2::ErrorCode::kerFileOpenFailed, output.path(), "w+b", strError());
5548
}
56-
size_t l = 0;
57-
if (!bytes.empty()) {
58-
size_t r;
59-
while ((r = io->read(bytes.data(), blocksize)) > 0) {
60-
l += r;
61-
output.write(bytes.data(), r);
49+
int blocksize = std::min(argc == 6 ? atoi(ba) : 10000, 1024 * 1024);
50+
if (blocksize > 0) {
51+
auto bytes = std::make_unique<Exiv2::byte[]>(blocksize);
52+
while (auto r = io->read(bytes.get(), blocksize)) {
53+
output.write(bytes.get(), r);
6254
}
6355
} else {
56+
size_t l = 0;
6457
// read/write byte-wise (#1029)
6558
while (l++ < io->size()) {
6659
output.putb(io->getb());
@@ -118,9 +111,8 @@ int main(int argc, char* const argv[]) {
118111
throw Error(Exiv2::ErrorCode::kerFileOpenFailed, f2, "w+b", strError());
119112
}
120113

121-
size_t readCount = 0;
122114
byte buf[32];
123-
while ((readCount = fileOut1.read(buf, sizeof(buf)))) {
115+
while (auto readCount = fileOut1.read(buf, sizeof(buf))) {
124116
if (memIo2.write(buf, readCount) != readCount) {
125117
std::cerr << argv[0] << ": MemIo bad write 2\n";
126118
return 13;

‎src/image.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -451,10 +451,10 @@ void Image::printIFDStructure(BasicIo& io, std::ostream& out, Exiv2::PrintStruct
451451

452452
const size_t restore = io.tell();
453453
io.seekOrThrow(offset, BasicIo::beg, ErrorCode::kerCorruptedMetadata); // position
454-
std::vector<byte> bytes(count); // allocate memory
455-
io.readOrThrow(bytes.data(), count, ErrorCode::kerCorruptedMetadata);
454+
auto bytes = std::make_unique<byte[]>(count); // allocate memory
455+
io.readOrThrow(bytes.get(), count, ErrorCode::kerCorruptedMetadata);
456456
io.seekOrThrow(restore, BasicIo::beg, ErrorCode::kerCorruptedMetadata);
457-
IptcData::printStructure(out, makeSliceUntil(bytes.data(), count), depth);
457+
IptcData::printStructure(out, makeSliceUntil(bytes.get(), count), depth);
458458
}
459459
} else if (option == kpsRecursive && tag == 0x927c /* MakerNote */ && count > 10) {
460460
const size_t restore = io.tell(); // save

‎src/xmpsidecar.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ void XmpSidecar::readMetadata() {
5656
std::string xmpPacket;
5757
const long len = 64 * 1024;
5858
byte buf[len];
59-
size_t l;
60-
while ((l = io_->read(buf, len)) > 0) {
59+
while (auto l = io_->read(buf, len)) {
6160
xmpPacket.append(reinterpret_cast<char*>(buf), l);
6261
}
6362
if (io_->error())

0 commit comments

Comments
 (0)
Please sign in to comment.