diff options
| author | Franklin Wei <me@fwei.tk> | 2017-08-17 15:27:52 -0400 |
|---|---|---|
| committer | Franklin Wei <franklin@rockbox.org> | 2020-12-07 19:27:19 -0500 |
| commit | b1f691f2deb0b59c6e237241e1995c5d95f37ffa (patch) | |
| tree | 499df3c5baf7fbb9943909f0eb4d48db386b444c | |
| parent | b4b86bbdbeb99c9fd9cfa6b61454047eb5e8fe20 (diff) | |
| download | puzzles-b1f691f2deb0b59c6e237241e1995c5d95f37ffa.zip puzzles-b1f691f2deb0b59c6e237241e1995c5d95f37ffa.tar.gz puzzles-b1f691f2deb0b59c6e237241e1995c5d95f37ffa.tar.bz2 puzzles-b1f691f2deb0b59c6e237241e1995c5d95f37ffa.tar.xz | |
Introduce ftoa() as a replacement for the %g format specifier
Not all platforms support printing floats, this is a more
portable (if uglier) way.
| -rw-r--r-- | midend.c | 2 | ||||
| -rw-r--r-- | misc.c | 6 | ||||
| -rw-r--r-- | net.c | 7 | ||||
| -rw-r--r-- | netslide.c | 7 | ||||
| -rw-r--r-- | puzzles.h | 4 | ||||
| -rw-r--r-- | rect.c | 7 |
6 files changed, 26 insertions, 7 deletions
@@ -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); } @@ -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. */ @@ -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"; @@ -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"; @@ -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. */ @@ -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"; |