Skip to content

Commit

Permalink
Fix windows run-time not supporting %z formatting.
Browse files Browse the repository at this point in the history
Following a stack overflow suggestion, add MACRO to substitute comparable formatting across platforms.
https://stackoverflow.com/questions/44382862/how-to-printf-a-size-t-without-warning-in-mingw-w64-gcc-7-1
Fixed a few additional additional %lu usage for size_t pick up by the windows build.
  • Loading branch information
pjbroad committed Sep 11, 2021
1 parent 6c117d2 commit c55904d
Show file tree
Hide file tree
Showing 16 changed files with 47 additions and 22 deletions.
3 changes: 2 additions & 1 deletion connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "main.h"
#include "multiplayer.h"
#include "pathfinder.h"
#include "platform.h"
#include "questlog.h"
#include "sound.h"
#include "text.h"
Expand Down Expand Up @@ -550,7 +551,7 @@ void Connection::process_incoming_data(queue_t *queue)
std::size_t size = _in_buffer[offset+1] + (_in_buffer[offset+2] << 8) + 2;
if (size > max_in_buffer_size)
{
LOG_ERROR("Packet overrun, protocol = %d, size = %zu\n", _in_buffer[offset], size);
LOG_ERROR("Packet overrun, protocol = %d, size = %" PRI_SIZET "\n", _in_buffer[offset], size);
_in_buffer_used = 0;
disconnect_from_server(packet_overrun);
break;
Expand Down
5 changes: 3 additions & 2 deletions ddsimage.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "misc.h"
#include "el_memory.h"
#include "io/elfilewrapper.h"
#include "platform.h"
#include <assert.h>

static Uint32 decompression_needed(const DdsHeader *header,
Expand Down Expand Up @@ -204,15 +205,15 @@ static Uint32 validate_header(DdsHeader *header, const char* file_name)
if (header->m_size != DDS_HEADER_SIZE)
{
LOG_ERROR("File '%s' is invalid. Size of header is"
" %u bytes, but must be %zu bytes for valid DDS files.",
" %u bytes, but must be %" PRI_SIZET " bytes for valid DDS files.",
file_name, header->m_size, DDS_HEADER_SIZE);
return 0;
}

if (header->m_pixel_format.m_size != DDS_PIXEL_FORMAT_SIZE)
{
LOG_ERROR("File '%s' is invalid. Size of pixe format header is"
" %u bytes, but must be %zu bytes for valid DDS files.",
" %u bytes, but must be %" PRI_SIZET " bytes for valid DDS files.",
file_name, header->m_pixel_format.m_size,
DDS_PIXEL_FORMAT_SIZE);
return 0;
Expand Down
3 changes: 2 additions & 1 deletion el_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#ifdef USE_SIMD
#include "mm_malloc.h"
#endif /* SIMD */
#include "platform.h"

void* malloc_aligned(const Uint64 size, const Uint64 alignment)
{
Expand All @@ -20,7 +21,7 @@ void* malloc_aligned(const Uint64 size, const Uint64 alignment)
#else /* USE_SIMD */
result = malloc(size);
#endif /* USE_SIMD */
LOG_DEBUG_VERBOSE("size: %lu, alignment: %lu, memory: %p", size, alignment, result);
LOG_DEBUG_VERBOSE("size: %" PRI_SIZET ", alignment: %" PRI_SIZET ", memory: %p", size, alignment, result);

return result;
}
Expand Down
3 changes: 2 additions & 1 deletion elconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
#include "openingwin.h"
#include "particles.h"
#include "password_manager.h"
#include "platform.h"
#include "pm_log.h"
#include "questlog.h"
#include "reflection.h"
Expand Down Expand Up @@ -1815,7 +1816,7 @@ static int set_var_OPT_MULTI(const char *str, size_t new_value)
size_t max_sel = option->args.multi.count;
if (new_value >= max_sel)
{
LOG_ERROR("Invalid value '%lu' for var '%s', type 'OPT_MULTI*' max '%lu'", new_value, str, max_sel);
LOG_ERROR("Invalid value '%'" PRI_SIZET " for var '%s', type 'OPT_MULTI*' max '%" PRI_SIZET "'", new_value, str, max_sel);
return 0;
}
option->func(option->var, new_value);
Expand Down
3 changes: 2 additions & 1 deletion elmemory.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "asc.h"
#include "init.h"
#include "io/elpathwrapper.h"
#include "platform.h"

/* The size of the array */
#define ELM_INITIAL_SIZE 1024
Expand Down Expand Up @@ -44,7 +45,7 @@ void elm_cleanup()
for(i = 0; i < ELM_INITIAL_SIZE*elm_allocs; i++) {
if(elm_memory[i].pointer != NULL && elm_memory[i].size != 0) {
malloc_calls_remaining++;
fprintf(fp, "%s:%i:%p (%zu bytes)\n", elm_memory[i].file, elm_memory[i].line, elm_memory[i].pointer, elm_memory[i].size);
fprintf(fp, "%s:%i:%p (%" PRI_SIZET " bytes)\n", elm_memory[i].file, elm_memory[i].line, elm_memory[i].pointer, elm_memory[i].size);
free(elm_memory[i].pointer);
}
}
Expand Down
3 changes: 2 additions & 1 deletion eye_candy_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "missiles.h"
#endif
#include "particles.h"
#include "platform.h"
#include "shadows.h"
#ifndef MAP_EDITOR
#include "skeletons.h"
Expand Down Expand Up @@ -649,7 +650,7 @@ extern "C" void ec_destroy_all_effects()
ec_idle();
}
if (!references.empty()) // unlikely to happen but just so we don't get stick on exit.
LOG_ERROR("%s: failed to clear up. references.size()=%lu", __PRETTY_FUNCTION__, references.size());
LOG_ERROR("%s: failed to clear up. references.size()=%" PRI_SIZET, __PRETTY_FUNCTION__, references.size());
#ifndef MAP_EDITOR
delete self_actor.obstruction;
#endif
Expand Down
3 changes: 2 additions & 1 deletion font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "io/elpathwrapper.h"
#include "init.h"
#include "gl_init.h"
#include "platform.h"
#include "textures.h"

namespace
Expand Down Expand Up @@ -133,7 +134,7 @@ FontOption::FontOption(size_t font_nr): _font_nr(font_nr), _file_name(), _file_b
if (font_nr > file_names.size())
{
_failed = true;
LOG_ERROR("Invalid font number %zu", font_nr);
LOG_ERROR("Invalid font number %" PRI_SIZET, font_nr);
}
else if (font_nr == 0)
{
Expand Down
9 changes: 5 additions & 4 deletions init.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
#include "new_actors.h"
#include "openingwin.h"
#include "particles.h"
#include "platform.h"
#include "questlog.h"
#include "reflection.h"
#include "rules.h"
Expand Down Expand Up @@ -230,7 +231,7 @@ static void load_cstate(void)
for(i = 0; i < ITEM_EDIT_QUANT; i++)
{
int curr_value;
safe_snprintf(str, sizeof(str), "%zu", i);
safe_snprintf(str, sizeof(str), "%" PRI_SIZET, i);
curr_value = json_cstate_get_int("quantities", str, -1);
if (curr_value != -1)
{
Expand All @@ -248,7 +249,7 @@ static void load_cstate(void)
int watch_this_stats[MAX_WATCH_STATS];
for(i = 0; i < MAX_WATCH_STATS; i++)
{
safe_snprintf(str, sizeof(str), "%zu", i);
safe_snprintf(str, sizeof(str), "%" PRI_SIZET, i);
watch_this_stats[i] = json_cstate_get_int("watched_stats", str, 0);
}
set_statsbar_watched_stats(watch_this_stats);
Expand Down Expand Up @@ -535,7 +536,7 @@ static void save_cstate(void)
char str[20];
for(i = 0; i < ITEM_EDIT_QUANT; i++)
{
safe_snprintf(str, sizeof(str), "%zu", i);
safe_snprintf(str, sizeof(str), "%" PRI_SIZET, i);
json_cstate_set_int("quantities", str, quantities.quantity[i].val);
}
json_cstate_set_int("quantities", "selected", (quantities.selected<ITEM_EDIT_QUANT) ?quantities.selected :0);
Expand All @@ -548,7 +549,7 @@ static void save_cstate(void)
get_statsbar_watched_stats(watch_this_stats);
for(i = 0; i < MAX_WATCH_STATS; i++)
{
safe_snprintf(str, sizeof(str), "%zu", i);
safe_snprintf(str, sizeof(str), "%" PRI_SIZET, i);
json_cstate_set_int("watched_stats", str, watch_this_stats[i]);
}
json_cstate_set_bool("watched_stats", "lock_selection", lock_skills_selection);
Expand Down
4 changes: 2 additions & 2 deletions io/e3d_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ static e3d_object* do_load_e3d_detail(e3d_object* cur_object)
{
if (index_size != sizeof(Uint16))
{
LOG_ERROR("File '%s' has wrong index size! Expected size %zu, found size %d.",
LOG_ERROR("File '%s' has wrong index size! Expected size %" PRI_SIZET ", found size %d.",
cur_object->file_name, sizeof(Uint16), index_size);
free_e3d_pointer(cur_object);
el_close(file);
Expand All @@ -487,7 +487,7 @@ static e3d_object* do_load_e3d_detail(e3d_object* cur_object)
{
if (index_size != sizeof(Uint32))
{
LOG_ERROR("File '%s' has wrong index size! Expected size %zu, found size %d.",
LOG_ERROR("File '%s' has wrong index size! Expected size %" PRI_SIZET ", found size %d.",
cur_object->file_name, sizeof(Uint32), index_size);
free_e3d_pointer(cur_object);
el_close(file);
Expand Down
3 changes: 2 additions & 1 deletion io/elc_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#endif // OSX
#include "elc_io.h"
#include "../errors.h"
#include "../platform.h"

#define BLOCK_SIZE 1024*1024

Expand All @@ -23,7 +24,7 @@ int read_and_check_elc_header(el_file_ptr file, const magic_number magic, versio
size = el_read(file, sizeof(elc_file_header), &header);
if (size != sizeof(elc_file_header))
{
LOG_ERROR("File '%s' too small! %d, %zu", filename, size, sizeof(elc_file_header));
LOG_ERROR("File '%s' too small! %d, %" PRI_SIZET, filename, size, sizeof(elc_file_header));
return -1;
}

Expand Down
3 changes: 2 additions & 1 deletion item_lists.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include "item_info.h"
#include "item_lists.h"
#include "loginwin.h"
#include "platform.h"
#include "questlog.h"
#include "sound.h"
#include "storage.h"
Expand Down Expand Up @@ -395,7 +396,7 @@ namespace ItemLists
// don't use a list with unequal or empty data sets
if ((quantities.size() != image_ids.size()) || (quantities.size() != item_ids.size()) || quantities.empty())
{
LOG_ERROR("%s: %s name=[%s] #id=%zu #cnts=%zu #uid=%zu\n", __FILE__,
LOG_ERROR("%s: %s name=[%s] #id=%" PRI_SIZET " #cnts=%" PRI_SIZET " #uid=%" PRI_SIZET "\n", __FILE__,
item_list_format_error, name_line.c_str(), size_t(image_ids.size()),
size_t(quantities.size()), size_t(item_ids.size()));
format_error = true;
Expand Down
5 changes: 3 additions & 2 deletions json_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "elloggingwrapper.h"
#include "counters.h"
#include "manufacture.h"
#include "platform.h"
#include "text.h"

// Helper functions
Expand All @@ -26,9 +27,9 @@ namespace JSON_IO
static size_t get_json_indent(void)
{ return 0; } // 0 is compact, non-zero give pretty output, 4 for example
static int exit_error(const char *function, size_t line, const std::string& message, int error_code)
{ LOG_ERROR("%s:%ld %s", function, line, message.c_str()); return error_code; }
{ LOG_ERROR("%s:%" PRI_SIZET " %s", function, line, message.c_str()); return error_code; }
static void info_message(const char *function, size_t line, std::string message)
{ LOG_INFO("%s:%ld %s", function, line, message.c_str()); }
{ LOG_INFO("%s:%" PRI_SIZET " %s", function, line, message.c_str()); }
static void console_message(const std::string& file_type, const std::string& message)
{ std::string full_message = "Problem with " + file_type + ": " + message; LOG_TO_CONSOLE(c_red3, full_message.c_str()); }
static void file_format_error(const std::string& file_type)
Expand Down
12 changes: 12 additions & 0 deletions platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@
#endif
#endif // FASTER_STARTUP

// work around %z not supported on windows due to Visucal C runtime
// https://stackoverflow.com/questions/44382862/how-to-printf-a-size-t-without-warning-in-mingw-w64-gcc-7-1
#ifdef _WIN32
# ifdef _WIN64
# define PRI_SIZET PRIu64
# else
# define PRI_SIZET PRIu32
# endif
#else
# define PRI_SIZET "zu"
#endif

// only ever use WINDOWS anywhere else, in case we need to add another 'catch' to
// enable WINDOWS
#if defined (_WIN32) || defined (_WIN64) || defined (WIN32)
Expand Down
5 changes: 3 additions & 2 deletions servers.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "gl_init.h"
#include "misc.h"
#include "multiplayer.h"
#include "platform.h"
#include "io/elpathwrapper.h"

#define DEFAULT_SERVERS_SIZE 4
Expand Down Expand Up @@ -185,11 +186,11 @@ void load_server_list(const char *filename)
fclose(f);

#ifdef USE_SSL
safe_snprintf(format, sizeof(format), "%%%zus %%%zus %%%zus %%u %%n%%%zus %%n",
safe_snprintf(format, sizeof(format), "%%%" PRI_SIZET "s %%%" PRI_SIZET "s %%%" PRI_SIZET "s %%u %%n%%%" PRI_SIZET "s %%n",
sizeof_field(server_def, id) - 1, sizeof_field(server_def, dir) - 1,
sizeof_field(server_def, address) - 1, sizeof(crypt) - 1);
#else // USE_SSL
safe_snprintf(format, sizeof(format), "%%%zus %%%zus %%%zus %%u %%%zu[^\r\n]",
safe_snprintf(format, sizeof(format), "%%%" PRI_SIZET "s %%%" PRI_SIZET "s %%%" PRI_SIZET "s %%u %%%" PRI_SIZET "[^\r\n]",
sizeof_field(server_def, id) - 1, sizeof_field(server_def, dir) - 1,
sizeof_field(server_def, address) - 1, sizeof_field(server_def, desc) - 1);
#endif // USE_SSL
Expand Down
3 changes: 2 additions & 1 deletion text_aliases.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "dbuffer.h"
#include "asc.h"
#include "chat.h"
#include "platform.h"

static char *numeric_aliases[100]; /* Stores the alias buffer */
static int numeric_alias_sizes[100]; /* Stores the alias buffer size */
Expand Down Expand Up @@ -313,7 +314,7 @@ static dbuffer_t *expand_alias_parameters( char *parameters, const char *aliaste

return_text = dbuffer_append_data(return_text, &nullchar, 1);

LOG_DEBUG("Finished, text is '%s', len %zu\n", return_text->data, return_text->current_size);
LOG_DEBUG("Finished, text is '%s', len %" PRI_SIZET "\n", return_text->data, return_text->current_size);

LEAVE_DEBUG_MARK("expand text aliases");

Expand Down
2 changes: 1 addition & 1 deletion url.c
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ static int only_call_from_open_web_link__go_to_url(void * url)
init_thread_log("web_link");

// build the command line and execute it
safe_snprintf (browser_command, sizeof (browser_command), "%s \"%s\"", browser_name, url),
safe_snprintf (browser_command, sizeof (browser_command), "%s \"%s\"", browser_name, (char *)url),
system(browser_command); // Do not use this command on UNIX.

// free the memory allocated in open_web_link()
Expand Down

0 comments on commit c55904d

Please sign in to comment.