From 27e25cfb081417f311437de03e77e502f034f3c7 Mon Sep 17 00:00:00 2001 From: dekadenZ Date: Fri, 25 Apr 2014 03:20:56 +0200 Subject: [PATCH 1/3] Macros for format references to size_t --- neo/portability/inttypes.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 neo/portability/inttypes.h diff --git a/neo/portability/inttypes.h b/neo/portability/inttypes.h new file mode 100644 index 00000000..e1778af7 --- /dev/null +++ b/neo/portability/inttypes.h @@ -0,0 +1,13 @@ +#pragma once + +#ifdef _MSV_VER + +#define PRIuSIZE "Iu" +#define PRIxSIZE "Ix" + +#else // ifdef _MSV_VER + +#define PRIuSIZE "zu" +#define PRIxSIZE "zx" + +#endif // ifdef _MSV_VER From 69f26fbe11cf7a8596b5aa1451584282c0792e07 Mon Sep 17 00:00:00 2001 From: dekadenZ Date: Fri, 25 Apr 2014 03:30:03 +0200 Subject: [PATCH 2/3] Replaced sizeof format references with macro --- neo/framework/Common.cpp | 11 ++++------- neo/framework/File.cpp | 25 ++++--------------------- neo/framework/Zip.cpp | 17 +++-------------- neo/libs/zlib/trees.c | 13 +++---------- 4 files changed, 14 insertions(+), 52 deletions(-) diff --git a/neo/framework/Common.cpp b/neo/framework/Common.cpp index dbe8af58..828489a5 100644 --- a/neo/framework/Common.cpp +++ b/neo/framework/Common.cpp @@ -48,6 +48,8 @@ If you have questions concerning this license or the applicable additional terms #include "../sys/sys_savegame.h" +#include "../portability/inttypes.h" + #if defined( _DEBUG ) #define BUILD_DEBUG "-debug" #else @@ -1933,12 +1935,7 @@ CONSOLE_COMMAND( testSIMD, "test SIMD code", NULL ) // RB begin CONSOLE_COMMAND( testFormattingSizes, "test printf format security", 0 ) { -#ifdef _MSC_VER - common->Printf( " sizeof( int32 ): %Iu bytes\n", sizeof( int32 ) ); - common->Printf( " sizeof( int64 ): %Iu bytes\n", sizeof( int64 ) ); -#else - common->Printf( " sizeof( int32 ): %zu bytes\n", sizeof( int32 ) ); - common->Printf( " sizeof( int64 ): %zu bytes\n", sizeof( int64 ) ); -#endif + common->Printf( " sizeof( int32 ): %" PRIuSIZE " bytes\n", sizeof( int32 ) ); + common->Printf( " sizeof( int64 ): %" PRIuSIZE " bytes\n", sizeof( int64 ) ); } // RB end \ No newline at end of file diff --git a/neo/framework/File.cpp b/neo/framework/File.cpp index 7bb61ce8..46a3f2d6 100644 --- a/neo/framework/File.cpp +++ b/neo/framework/File.cpp @@ -31,6 +31,7 @@ If you have questions concerning this license or the applicable additional terms #pragma hdrstop #include "Unzip.h" +#include "../portability/inttypes.h" /* ================= @@ -844,11 +845,7 @@ int idFile_Memory::Write( const void* buffer, int len ) { if( maxSize != 0 ) { -#ifdef _MSC_VER - common->Error( "idFile_Memory::Write: exceeded maximum size %Iu", maxSize ); -#else - common->Error( "idFile_Memory::Write: exceeded maximum size %zu", maxSize ); -#endif + common->Error( "idFile_Memory::Write: exceeded maximum size %" PRIuSIZE "", maxSize ); return 0; } int extra = granularity * ( 1 + alloc / granularity ); @@ -934,13 +931,7 @@ void idFile_Memory::PreAllocate( size_t len ) { if( maxSize != 0 ) { -// RB begin -#ifdef _MSC_VER - idLib::Error( "idFile_Memory::SetLength: exceeded maximum size %Iu", maxSize ); -#else - idLib::Error( "idFile_Memory::SetLength: exceeded maximum size %zu", maxSize ); -#endif -// RB end + idLib::Error( "idFile_Memory::SetLength: exceeded maximum size %" PRIuSIZE "", maxSize ); } char* newPtr = ( char* )Mem_Alloc( len, TAG_IDFILE ); if( allocated > 0 ) @@ -1126,13 +1117,7 @@ void idFile_Memory::TruncateData( size_t len ) { if( len > allocated ) { -// RB begin -#ifdef _MSV_VER - idLib::Error( "idFile_Memory::TruncateData: len (%Iu) exceeded allocated size (%Iu)", len, allocated ); -#else - idLib::Error( "idFile_Memory::TruncateData: len (%zu) exceeded allocated size (%zu)", len, allocated ); -#endif -// RB end + idLib::Error( "idFile_Memory::TruncateData: len (%" PRIuSIZE ") exceeded allocated size (%" PRIuSIZE ")", len, allocated ); } else { @@ -2126,5 +2111,3 @@ CONSOLE_COMMAND( testEndianNessReset, "Tests the read/write compatibility betwee { fileSystem->RemoveFile( testEndianNessFilename ); } - - diff --git a/neo/framework/Zip.cpp b/neo/framework/Zip.cpp index 4246d520..63a5fd74 100644 --- a/neo/framework/Zip.cpp +++ b/neo/framework/Zip.cpp @@ -36,6 +36,7 @@ Contains external code for building ZipFiles. */ #include "Zip.h" +#include "../portability/inttypes.h" // #undef STDC @@ -504,13 +505,7 @@ bool idZipBuilder::CreateZipFileFromFiles( const idList< idFile_Memory* >& srcFi errcode = zipWriteInFileInZip( zf, buffer.Ptr(), ( unsigned int )bytesRead ); if( errcode != ZIP_OK ) { -// RB begin -#ifdef _MSC_VER - idLib::Warning( "Error writing to zipfile (%Iu bytes)!", bytesRead ); -#else - idLib::Warning( "Error writing to zipfile (%zu bytes)!", bytesRead ); -#endif -// RB end + idLib::Warning( "Error writing to zipfile (%" PRIuSIZE " bytes)!", bytesRead ); continue; } } @@ -618,13 +613,7 @@ bool idZipBuilder::AddFile( zipFile zf, idFile_Memory* src, bool deleteFile ) errcode = zipWriteInFileInZip( zf, buffer.Ptr(), ( unsigned int )bytesRead ); if( errcode != ZIP_OK ) { -// RB begin -#ifdef _MSC_VER - idLib::Warning( "Error writing to zipfile (%Iu bytes)!", bytesRead ); -#else - idLib::Warning( "Error writing to zipfile (%zu bytes)!", bytesRead ); -#endif -// RB end + idLib::Warning( "Error writing to zipfile (%" PRIuSIZE " bytes)!", bytesRead ); continue; } } diff --git a/neo/libs/zlib/trees.c b/neo/libs/zlib/trees.c index b9c72b4c..f45067ab 100644 --- a/neo/libs/zlib/trees.c +++ b/neo/libs/zlib/trees.c @@ -35,6 +35,7 @@ /* #define GEN_TREES_H */ #include "deflate.h" +#include "../portability/inttypes.h" #ifdef DEBUG # include @@ -940,17 +941,9 @@ void ZLIB_INTERNAL _tr_flush_block(s, buf, stored_len, last) /* Determine the best encoding. Compute the block lengths in bytes. */ opt_lenb = (s->opt_len+3+7)>>3; static_lenb = (s->static_len+3+7)>>3; -// RB begin -#ifdef _MSC_VER - Tracev((stderr, "\nopt %Iu(%Iu) stat %Iu(%Iu) stored %Iu lit %u ", + Tracev((stderr, "\nopt %" PRIuSIZE "(%" PRIuSIZE ") stat %" PRIuSIZE "(%" PRIuSIZE ") stored %" PRIuSIZE " lit %u ", opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len, s->last_lit)); -#else - Tracev((stderr, "\nopt %zu(%zu) stat %zu(%zu) stored %zu lit %u ", - opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len, - s->last_lit)); -#endif -// RB end if (static_lenb <= opt_lenb) opt_lenb = static_lenb; } else { @@ -1009,7 +1002,7 @@ void ZLIB_INTERNAL _tr_flush_block(s, buf, stored_len, last) Tracev((stderr,"\ncomprlen %Iu(%Iu) ", s->compressed_len>>3, s->compressed_len-7*last)); #else - Tracev((stderr,"\ncomprlen %zu(%zu) ", s->compressed_len>>3, + Tracev((stderr,"\ncomprlen %" PRIuSIZE "(%" PRIuSIZE ") ", s->compressed_len>>3, s->compressed_len-7*last)); #endif // RB end From e4605633f04d6a39e5bf1e2a6f589385a143940a Mon Sep 17 00:00:00 2001 From: dekadenZ Date: Fri, 25 Apr 2014 15:22:15 +0200 Subject: [PATCH 3/3] Move format specifier macros to idlib/sys/sys_defines.h --- neo/framework/Common.cpp | 2 +- neo/framework/File.cpp | 2 +- neo/framework/Zip.cpp | 2 +- neo/idlib/sys/sys_defines.h | 19 +++++++++++++++++++ neo/libs/zlib/trees.c | 2 +- neo/portability/inttypes.h | 13 ------------- 6 files changed, 23 insertions(+), 17 deletions(-) delete mode 100644 neo/portability/inttypes.h diff --git a/neo/framework/Common.cpp b/neo/framework/Common.cpp index 828489a5..d5224281 100644 --- a/neo/framework/Common.cpp +++ b/neo/framework/Common.cpp @@ -48,7 +48,7 @@ If you have questions concerning this license or the applicable additional terms #include "../sys/sys_savegame.h" -#include "../portability/inttypes.h" +#include "idlib/sys/sys_defines.h" #if defined( _DEBUG ) #define BUILD_DEBUG "-debug" diff --git a/neo/framework/File.cpp b/neo/framework/File.cpp index 46a3f2d6..0684de3e 100644 --- a/neo/framework/File.cpp +++ b/neo/framework/File.cpp @@ -31,7 +31,7 @@ If you have questions concerning this license or the applicable additional terms #pragma hdrstop #include "Unzip.h" -#include "../portability/inttypes.h" +#include "idlib/sys/sys_defines.h" /* ================= diff --git a/neo/framework/Zip.cpp b/neo/framework/Zip.cpp index 63a5fd74..fb1eca30 100644 --- a/neo/framework/Zip.cpp +++ b/neo/framework/Zip.cpp @@ -36,7 +36,7 @@ Contains external code for building ZipFiles. */ #include "Zip.h" -#include "../portability/inttypes.h" +#include "idlib/sys/sys_defines.h" // #undef STDC diff --git a/neo/idlib/sys/sys_defines.h b/neo/idlib/sys/sys_defines.h index 11d3fee8..82940727 100644 --- a/neo/idlib/sys/sys_defines.h +++ b/neo/idlib/sys/sys_defines.h @@ -257,3 +257,22 @@ extern volatile int ignoredReturnValue; #define MIN_UNSIGNED_TYPE( x ) 0 #endif + + +/* + * Macros for format conversion specifications for integer arguments of type + * size_t or ssize_t. + */ +#ifdef _MSV_VER + +#define PRIiSIZE "Ii" +#define PRIuSIZE "Iu" +#define PRIxSIZE "Ix" + +#else // ifdef _MSV_VER + +#define PRIiSIZE "zi" +#define PRIuSIZE "zu" +#define PRIxSIZE "zx" + +#endif // ifdef _MSV_VER diff --git a/neo/libs/zlib/trees.c b/neo/libs/zlib/trees.c index f45067ab..1cbe076c 100644 --- a/neo/libs/zlib/trees.c +++ b/neo/libs/zlib/trees.c @@ -35,7 +35,7 @@ /* #define GEN_TREES_H */ #include "deflate.h" -#include "../portability/inttypes.h" +#include "idlib/sys/sys_defines.h" #ifdef DEBUG # include diff --git a/neo/portability/inttypes.h b/neo/portability/inttypes.h deleted file mode 100644 index e1778af7..00000000 --- a/neo/portability/inttypes.h +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once - -#ifdef _MSV_VER - -#define PRIuSIZE "Iu" -#define PRIxSIZE "Ix" - -#else // ifdef _MSV_VER - -#define PRIuSIZE "zu" -#define PRIxSIZE "zx" - -#endif // ifdef _MSV_VER