From b1f691f2deb0b59c6e237241e1995c5d95f37ffa Mon Sep 17 00:00:00 2001 From: Franklin Wei Date: Thu, 17 Aug 2017 15:27:52 -0400 Subject: Introduce ftoa() as a replacement for the %g format specifier Not all platforms support printing floats, this is a more portable (if uglier) way. --- midend.c | 2 +- misc.c | 6 ++++++ net.c | 7 +++++-- netslide.c | 7 +++++-- puzzles.h | 4 ++++ rect.c | 7 +++++-- 6 files changed, 26 insertions(+), 7 deletions(-) diff --git a/midend.c b/midend.c index 15636d4..f2d27d3 100644 --- a/midend.c +++ b/midend.c @@ -2049,7 +2049,7 @@ void midend_serialise(midend *me, */ if (me->ourgame->is_timed) { char buf[80]; - sprintf(buf, "%g", me->elapsed); + ftoa(buf, me->elapsed); wr("TIME", buf); } diff --git a/misc.c b/misc.c index bb9019e..ed31acb 100644 --- a/misc.c +++ b/misc.c @@ -402,6 +402,12 @@ void copy_left_justified(char *buf, size_t sz, const char *str) buf[sz - 1] = 0; } +/* another kludge for platforms without %g support in *printf() */ +int ftoa(char *buf, float f) +{ + return sprintf(buf, "%d.%06d", (int)f, abs((int)((f - (int)f)*1e6))); +} + /* Returns a dynamically allocated label for a generic button. * Game-specific buttons should go into the `label' field of key_label * instead. */ diff --git a/net.c b/net.c index 5796e95..d3032b6 100644 --- a/net.c +++ b/net.c @@ -249,7 +249,10 @@ static char *encode_params(const game_params *params, bool full) if (params->wrapping) ret[len++] = 'w'; if (full && params->barrier_probability) - len += sprintf(ret+len, "b%g", params->barrier_probability); + { + len += sprintf(ret+len, "b"); + len += ftoa(ret + len, params->barrier_probability); + } if (full && !params->unique) ret[len++] = 'a'; assert(len < lenof(ret)); @@ -281,7 +284,7 @@ static config_item *game_configure(const game_params *params) ret[3].name = "Barrier probability"; ret[3].type = C_STRING; - sprintf(buf, "%g", params->barrier_probability); + ftoa(buf, params->barrier_probability); ret[3].u.string.sval = dupstr(buf); ret[4].name = "Ensure unique solution"; diff --git a/netslide.c b/netslide.c index a2e3281..14af2a6 100644 --- a/netslide.c +++ b/netslide.c @@ -242,7 +242,10 @@ static char *encode_params(const game_params *params, bool full) if (params->wrapping) ret[len++] = 'w'; if (full && params->barrier_probability) - len += sprintf(ret+len, "b%g", params->barrier_probability); + { + len += sprintf(ret+len, "b"); + len += ftoa(ret + len, params->barrier_probability); + } /* Shuffle limit is part of the limited parameters, because we have to * provide the target move count. */ if (params->movetarget) @@ -276,7 +279,7 @@ static config_item *game_configure(const game_params *params) ret[3].name = "Barrier probability"; ret[3].type = C_STRING; - sprintf(buf, "%g", params->barrier_probability); + ftoa(buf, params->barrier_probability); ret[3].u.string.sval = dupstr(buf); ret[4].name = "Number of shuffling moves"; diff --git a/puzzles.h b/puzzles.h index d46a70e..29b00dd 100644 --- a/puzzles.h +++ b/puzzles.h @@ -416,6 +416,10 @@ void draw_text_outline(drawing *dr, int x, int y, int fonttype, * less than buffer size. */ void copy_left_justified(char *buf, size_t sz, const char *str); +/* An ugly, but working float-to-string implementation for platforms + * that don't have one */ +int ftoa(char *buf, float f); + /* Returns a generic label based on the value of `button.' To be used whenever a `label' field returned by the request_keys() game function is NULL. Dynamically allocated, to be freed by caller. */ diff --git a/rect.c b/rect.c index 3cb67bd..b13de75 100644 --- a/rect.c +++ b/rect.c @@ -163,7 +163,10 @@ static char *encode_params(const game_params *params, bool full) sprintf(data, "%dx%d", params->w, params->h); if (full && params->expandfactor) - sprintf(data + strlen(data), "e%g", params->expandfactor); + { + sprintf(data + strlen(data), "e"); + ftoa(data + strlen(data), params->expandfactor); + } if (full && !params->unique) strcat(data, "a"); @@ -189,7 +192,7 @@ static config_item *game_configure(const game_params *params) ret[2].name = "Expansion factor"; ret[2].type = C_STRING; - sprintf(buf, "%g", params->expandfactor); + ftoa(buf, params->expandfactor); ret[2].u.string.sval = dupstr(buf); ret[3].name = "Ensure unique solution"; -- cgit v1.1