aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFranklin Wei <me@fwei.tk>2017-08-17 15:27:52 -0400
committerFranklin Wei <franklin@rockbox.org>2020-12-07 19:27:19 -0500
commitb1f691f2deb0b59c6e237241e1995c5d95f37ffa (patch)
tree499df3c5baf7fbb9943909f0eb4d48db386b444c
parentb4b86bbdbeb99c9fd9cfa6b61454047eb5e8fe20 (diff)
downloadpuzzles-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.c2
-rw-r--r--misc.c6
-rw-r--r--net.c7
-rw-r--r--netslide.c7
-rw-r--r--puzzles.h4
-rw-r--r--rect.c7
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";