Skip to content

Commit

Permalink
clang-tidy: use C++ casting
Browse files Browse the repository at this point in the history
Found with google-readability-casting

Signed-off-by: Rosen Penev <[email protected]>
  • Loading branch information
neheb committed May 19, 2021
1 parent b83b219 commit 28f6658
Show file tree
Hide file tree
Showing 41 changed files with 837 additions and 864 deletions.
26 changes: 13 additions & 13 deletions samples/exiv2json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,17 +255,17 @@ void fileSystemPush(const char* path,Jzon::Node& nfs)
memset(&buf,0,sizeof(buf));
stat(path,&buf);

fs.Add("st_dev" ,(int) buf.st_dev ); /* ID of device containing file */
fs.Add("st_ino" ,(int) buf.st_ino ); /* inode number */
fs.Add("st_mode" ,(int) buf.st_mode ); /* protection */
fs.Add("st_nlink" ,(int) buf.st_nlink ); /* number of hard links */
fs.Add("st_uid" ,(int) buf.st_uid ); /* user ID of owner */
fs.Add("st_gid" ,(int) buf.st_gid ); /* group ID of owner */
fs.Add("st_rdev" ,(int) buf.st_rdev ); /* device ID (if special file) */
fs.Add("st_size" ,(int) buf.st_size ); /* total size, in bytes */
fs.Add("st_atime" ,(int) buf.st_atime ); /* time of last access */
fs.Add("st_mtime" ,(int) buf.st_mtime ); /* time of last modification */
fs.Add("st_ctime" ,(int) buf.st_ctime ); /* time of last status change */
fs.Add("st_dev", static_cast<int>(buf.st_dev)); /* ID of device containing file */
fs.Add("st_ino", static_cast<int>(buf.st_ino)); /* inode number */
fs.Add("st_mode", static_cast<int>(buf.st_mode)); /* protection */
fs.Add("st_nlink", static_cast<int>(buf.st_nlink)); /* number of hard links */
fs.Add("st_uid", static_cast<int>(buf.st_uid)); /* user ID of owner */
fs.Add("st_gid", static_cast<int>(buf.st_gid)); /* group ID of owner */
fs.Add("st_rdev", static_cast<int>(buf.st_rdev)); /* device ID (if special file) */
fs.Add("st_size", static_cast<int>(buf.st_size)); /* total size, in bytes */
fs.Add("st_atime", static_cast<int>(buf.st_atime)); /* time of last access */
fs.Add("st_mtime", static_cast<int>(buf.st_mtime)); /* time of last modification */
fs.Add("st_ctime", static_cast<int>(buf.st_ctime)); /* time of last status change */

#if defined(_MSC_VER) || defined(__MINGW__)
size_t blksize = 1024;
Expand All @@ -274,8 +274,8 @@ void fileSystemPush(const char* path,Jzon::Node& nfs)
size_t blksize = buf.st_blksize;
size_t blocks = buf.st_blocks ;
#endif
fs.Add("st_blksize",(int) blksize ); /* blocksize for file system I/O */
fs.Add("st_blocks" ,(int) blocks ); /* number of 512B blocks allocated */
fs.Add("st_blksize", static_cast<int>(blksize)); /* blocksize for file system I/O */
fs.Add("st_blocks", static_cast<int>(blocks)); /* number of 512B blocks allocated */
}

int main(int argc, char* const argv[])
Expand Down
32 changes: 17 additions & 15 deletions samples/geotag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ std::string Position::toExifString(double d)
{
char result[200];
d *= 100;
sprintf(result,"%d/100",abs((int)d));
sprintf(result, "%d/100", abs(static_cast<int>(d)));
return std::string(result);
}

Expand All @@ -266,13 +266,13 @@ std::string Position::toExifString(double d,bool bRational,bool bLat)
const char* EW = d>=0.0?"E":"W";
const char* NSEW = bLat ? NS: EW;
if ( d < 0 ) d = -d;
int deg = (int) d;
d -= deg;
d *= 60;
int min = (int) d ;
d -= min;
d *= 60;
int sec = (int)d;
int deg = static_cast<int>(d);
d -= deg;
d *= 60;
int min = static_cast<int>(d);
d -= min;
d *= 60;
int sec = static_cast<int>(d);
char result[200];
if ( bRational )
sprintf(result,"%d/1 %d/1 %d/1" ,deg,min,sec);
Expand Down Expand Up @@ -335,7 +335,7 @@ class UserData
// XML Parser Callbacks
static void startElement(void* userData, const char* name, const char** atts )
{
auto me = (UserData*)userData;
auto me = static_cast<UserData*>(userData);
//for ( int i = 0 ; i < me->indent ; i++ ) printf(" ");
//printf("begin %s\n",name);
me->bTime = strcmp(name,"time")==0;
Expand All @@ -357,7 +357,7 @@ static void startElement(void* userData, const char* name, const char** atts )

static void endElement(void* userData, const char* name)
{
auto me = (UserData*)userData;
auto me = static_cast<UserData*>(userData);
me->indent-- ;
if ( strcmp(name,"trkpt")==0 ) {

Expand All @@ -379,7 +379,7 @@ static void endElement(void* userData, const char* name)

void charHandler(void* userData,const char* s,int len)
{
auto me = (UserData*)userData;
auto me = static_cast<UserData*>(userData);

if ( me->nTrkpt == 1 ) {
char buffer[100];
Expand Down Expand Up @@ -593,14 +593,14 @@ bool readXML(const char* path,Options& options)
// swallow it
if ( bResult ) {
len = sip(f,buffer,sizeof buffer,len);
bResult = XML_Parse(parser, buffer,(int)len, len == 0 ) == XML_STATUS_OK;
bResult = XML_Parse(parser, buffer, static_cast<int>(len), len == 0) == XML_STATUS_OK;
}

// 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,(int)len, len == 0 ) == XML_STATUS_OK;
bResult = XML_Parse(parser, buffer, static_cast<int>(len), len == 0) == XML_STATUS_OK;
};
}

Expand Down Expand Up @@ -870,10 +870,12 @@ int main(int argc,const char* argv[])
#endif
char* path = realpath(arg,buffer);
if ( t && path ) {
if ( options.verbose) printf("%s %ld %s",path,(long int)t,asctime(localtime(&t)));
if (options.verbose)
printf("%s %ld %s", path, static_cast<long int>(t), asctime(localtime(&t)));
gFiles.push_back(path);
}
if ( path && path != buffer ) ::free((void*) path);
if (path && path != buffer)
::free(path);
}
if ( type == typeUnknown ) {
fprintf(stderr,"error: illegal syntax %s\n",arg);
Expand Down
24 changes: 12 additions & 12 deletions samples/getopt-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,12 @@ int main(int argc, char** const argv)
do {
n = ::getopt(argc,argv,::optstring);
if ( n >= 0 ) {
char N = (char) n;
std::cout << n << " = " << N ;
} else {
std::cout << n ;
}
std::cout << " optind = " << ::optind
char N = static_cast<char>(n);
std::cout << n << " = " << N;
} else {
std::cout << n ;
}
std::cout << " optind = " << ::optind
<< " opterr = " << ::opterr
<< " optopt = " << ::optopt
<< " optarg = " << Safe(::optarg)
Expand All @@ -132,12 +132,12 @@ int main(int argc, char** const argv)
do {
n = Util::getopt(argc,argv,::optstring);
if ( n >= 0 ) {
char N = (char) n;
std::cout << n << " = " << N ;
} else {
std::cout << n ;
}
std::cout << " optind = " << Util::optind
char N = static_cast<char>(n);
std::cout << n << " = " << N;
} else {
std::cout << n ;
}
std::cout << " optind = " << Util::optind
<< " opterr = " << Util::opterr
<< " optopt = " << Util::optopt
<< " optarg = " << Safe(Util::optarg)
Expand Down
14 changes: 7 additions & 7 deletions samples/iotest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ int WriteReadSeek(BasicIo &io)
throw Error(Exiv2::kerDataSourceOpenFailed, io.path(), strError());
}
IoCloser closer(io);
if ((size_t) io.write((byte*)tester1, (long)size1) != size1) {
if (static_cast<size_t>(io.write(reinterpret_cast<const byte*>(tester1), static_cast<long>(size1))) != size1) {
std::cerr << ": WRS initial write failed\n";
return 2;
}
Expand All @@ -188,13 +188,13 @@ int WriteReadSeek(BasicIo &io)
std::cerr << ": WRS size is not " << size1 << "\n";
return 2;
}
long backup = (long)size1;
long backup = static_cast<long>(size1);
io.seek(-backup, BasicIo::cur);

int c = EOF;
std::memset(buf, -1, sizeof(buf));
for (int i = 0; (c=io.getb()) != EOF; ++i) {
buf[i] = (byte)c;
buf[i] = static_cast<byte>(c);
}

// Make sure we got the null back
Expand All @@ -203,7 +203,7 @@ int WriteReadSeek(BasicIo &io)
return 3;
}

if (strcmp(tester1, (char*)buf) != 0 ) {
if (strcmp(tester1, reinterpret_cast<char*>(buf)) != 0) {
std::cerr << ": WRS strings don't match 1\n";
return 4;
}
Expand Down Expand Up @@ -232,7 +232,7 @@ int WriteReadSeek(BasicIo &io)
}

io.seek(insert, BasicIo::beg);
if((size_t)io.write((byte*)tester2, (long)size2) != size2) {
if (static_cast<size_t>(io.write(reinterpret_cast<const byte*>(tester2), static_cast<long>(size2))) != size2) {
std::cerr << ": WRS bad write 1\n";
return 9;
}
Expand All @@ -242,7 +242,7 @@ int WriteReadSeek(BasicIo &io)
throw Error(Exiv2::kerDataSourceOpenFailed, io.path(), strError());
}
std::memset(buf, -1, sizeof(buf));
if ((size_t) io.read(buf, sizeof(buf)) != insert + size2) {
if (static_cast<size_t>(io.read(buf, sizeof(buf))) != insert + size2) {
std::cerr << ": WRS something went wrong\n";
return 10;
}
Expand All @@ -253,7 +253,7 @@ int WriteReadSeek(BasicIo &io)
return 11;
}

if (std::strcmp(expect, (char*)buf) != 0 ) {
if (std::strcmp(expect, reinterpret_cast<char*>(buf)) != 0) {
std::cerr << ": WRS strings don't match 2\n";
return 12;
}
Expand Down
2 changes: 1 addition & 1 deletion samples/mmap-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ try {
}
// Map it to memory
const Exiv2::byte* pData = file.mmap();
long size = (long)file.size();
long size = static_cast<long>(file.size());
DataBuf buf(size);
// Read from the memory mapped region
memcpy(buf.pData_, pData, buf.size_);
Expand Down
2 changes: 1 addition & 1 deletion samples/tiff-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ void mini1(const char* path)
enforce(wm == wmIntrusive, Exiv2::kerErrorMessage, "encode returned an unexpected value");
std::cout << "Test 3: Wrote non-empty Exif data without original binary data:\n";
exifData.clear();
ByteOrder bo = ExifParser::decode(exifData, &blob[0], (uint32_t) blob.size());
ByteOrder bo = ExifParser::decode(exifData, &blob[0], static_cast<uint32_t>(blob.size()));
enforce(bo == bigEndian, Exiv2::kerErrorMessage, "decode returned an unexpected value");
print(exifData);
}
Expand Down
22 changes: 12 additions & 10 deletions src/actions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,15 +225,15 @@ namespace Action {
std::stringstream output(std::stringstream::out|std::stringstream::binary);
result = printStructure(output, option, path);
if ( result == 0 ) {
size_t size = (long) output.str().size();
Exiv2::DataBuf iccProfile((long)size);
Exiv2::DataBuf ascii((long)(size * 3 + 1));
size_t size = static_cast<long>(output.str().size());
Exiv2::DataBuf iccProfile(static_cast<long>(size));
Exiv2::DataBuf ascii(static_cast<long>(size * 3 + 1));
ascii.pData_[size * 3] = 0;
::memcpy(iccProfile.pData_,output.str().c_str(),size);
if ( Exiv2::base64encode(iccProfile.pData_,size,(char*)ascii.pData_,size*3) ) {
if (Exiv2::base64encode(iccProfile.pData_, size, reinterpret_cast<char*>(ascii.pData_), size * 3)) {
long chunk = 60 ;
std::string code = std::string("data:") + std::string((char*)ascii.pData_);
long length = (long) code.size() ;
std::string code = std::string("data:") + std::string(reinterpret_cast<char*>(ascii.pData_));
long length = static_cast<long>(code.size());
for ( long start = 0 ; start < length ; start += chunk ) {
long count = (start+chunk) < length ? chunk : length - start ;
std::cout << code.substr(start,count) << std::endl;
Expand Down Expand Up @@ -1039,7 +1039,8 @@ namespace Action {
} else {

if ( bStdout ) { // -eC-
std::cout.write((const char*)image->iccProfile()->pData_,image->iccProfile()->size_);
std::cout.write(reinterpret_cast<const char*>(image->iccProfile()->pData_),
image->iccProfile()->size_);
} else {
if (Params::instance().verbose_) {
std::cout << _("Writing iccProfile: ") << target << std::endl;
Expand Down Expand Up @@ -1172,7 +1173,7 @@ namespace Action {
{
std::string xmpPacket;
for ( long i = 0 ; i < xmpBlob.size_ ; i++ ) {
xmpPacket += (char) xmpBlob.pData_[i];
xmpPacket += static_cast<char>(xmpBlob.pData_[i]);
}
Exiv2::Image::UniquePtr image = Exiv2::ImageFactory::open(path);
assert(image.get() != 0);
Expand Down Expand Up @@ -1784,7 +1785,7 @@ namespace {
{
int rc = 1;
time_t t = mktime(tm); // interpret tm according to current timezone settings
if (t != (time_t)-1) {
if (t != static_cast<time_t>(-1)) {
rc = 0;
actime_ = t;
modtime_ = t;
Expand Down Expand Up @@ -1827,7 +1828,8 @@ namespace {
tm->tm_sec = tmp;

// Conversions to set remaining fields of the tm structure
if (mktime(tm) == (time_t)-1) return 11;
if (mktime(tm) == static_cast<time_t>(-1))
return 11;

return 0;
} // str2Tm
Expand Down
Loading

0 comments on commit 28f6658

Please sign in to comment.