summaryrefslogtreecommitdiff
path: root/apps/plugins/puzzles
diff options
context:
space:
mode:
authorFranklin Wei <git@fwei.tk>2017-08-16 11:35:32 -0400
committerFranklin Wei <git@fwei.tk>2017-08-16 11:40:42 -0400
commitc78ff7f6153d2a787bfd8cb3410dff8db74b2789 (patch)
treee3a507a1a25640d4c8e2cbedc464d38e6af5575c /apps/plugins/puzzles
parentbf25f3e6e7b1518b0dd499676ac298172dc25fa1 (diff)
puzzles: fix floating-point formatting
This is pretty ad-hoc, but the only other ways are to rewrite sprintf (which would use too much memory on the c200v2), or implement support for floats in rockbox's formatter, neither of which are acceptable. Change-Id: I70d59fd3e90a16e2db9ae0a84cd8c14807f50b46
Diffstat (limited to 'apps/plugins/puzzles')
-rw-r--r--apps/plugins/puzzles/rbcompat.h1
-rw-r--r--apps/plugins/puzzles/rbwrappers.c6
-rw-r--r--apps/plugins/puzzles/src/midend.c2
-rw-r--r--apps/plugins/puzzles/src/net.c7
-rw-r--r--apps/plugins/puzzles/src/netslide.c7
-rw-r--r--apps/plugins/puzzles/src/rect.c7
6 files changed, 23 insertions, 7 deletions
diff --git a/apps/plugins/puzzles/rbcompat.h b/apps/plugins/puzzles/rbcompat.h
index 4bb9f39ca7..66c86f1cf4 100644
--- a/apps/plugins/puzzles/rbcompat.h
+++ b/apps/plugins/puzzles/rbcompat.h
@@ -15,6 +15,7 @@ double cos_wrapper(double rads);
int vsprintf_wrapper(char *s, const char *fmt, va_list ap);
float fabs_wrapper(float n);
float floor_wrapper(float n);
+int ftoa(char *buf, int len, float f);
float atan_wrapper(float x);
float atan2_wrapper(float y, float x);
diff --git a/apps/plugins/puzzles/rbwrappers.c b/apps/plugins/puzzles/rbwrappers.c
index 95bc2fc5b1..2d857c1cc9 100644
--- a/apps/plugins/puzzles/rbwrappers.c
+++ b/apps/plugins/puzzles/rbwrappers.c
@@ -23,6 +23,12 @@ int puts_wrapper(const char *s)
return 0;
}
+int ftoa(char *buf, int len, float f)
+{
+ /* biggest hack ever */
+ return rb->snprintf(buf, len, "%d.%06d", (int)f, (int)((f - (int)f)*1e6));
+}
+
/* fixed-point wrappers */
static long lastphase = 0, lastsin = 0, lastcos = 0x7fffffff;
diff --git a/apps/plugins/puzzles/src/midend.c b/apps/plugins/puzzles/src/midend.c
index 2eb5ee93e8..6dbdd33905 100644
--- a/apps/plugins/puzzles/src/midend.c
+++ b/apps/plugins/puzzles/src/midend.c
@@ -1701,7 +1701,7 @@ void midend_serialise(midend *me,
*/
if (me->ourgame->is_timed) {
char buf[80];
- sprintf(buf, "%g", me->elapsed);
+ ftoa(buf, 80, me->elapsed);
wr("TIME", buf);
}
diff --git a/apps/plugins/puzzles/src/net.c b/apps/plugins/puzzles/src/net.c
index de51f82fd7..9289afb682 100644
--- a/apps/plugins/puzzles/src/net.c
+++ b/apps/plugins/puzzles/src/net.c
@@ -257,7 +257,10 @@ static char *encode_params(const game_params *params, int 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, 400, params->barrier_probability);
+ }
if (full && !params->unique)
ret[len++] = 'a';
assert(len < lenof(ret));
@@ -292,7 +295,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, 80, params->barrier_probability);
ret[3].sval = dupstr(buf);
ret[3].ival = 0;
diff --git a/apps/plugins/puzzles/src/netslide.c b/apps/plugins/puzzles/src/netslide.c
index c56e1abd6a..663febc61a 100644
--- a/apps/plugins/puzzles/src/netslide.c
+++ b/apps/plugins/puzzles/src/netslide.c
@@ -241,7 +241,10 @@ static char *encode_params(const game_params *params, int 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, 400, params->barrier_probability);
+ }
/* Shuffle limit is part of the limited parameters, because we have to
* provide the target move count. */
if (params->movetarget)
@@ -278,7 +281,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, 80, params->barrier_probability);
ret[3].sval = dupstr(buf);
ret[3].ival = 0;
diff --git a/apps/plugins/puzzles/src/rect.c b/apps/plugins/puzzles/src/rect.c
index 465e1436fa..0c06c74945 100644
--- a/apps/plugins/puzzles/src/rect.c
+++ b/apps/plugins/puzzles/src/rect.c
@@ -163,7 +163,10 @@ static char *encode_params(const game_params *params, int 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), 256, params->expandfactor);
+ }
if (full && !params->unique)
strcat(data, "a");
@@ -191,7 +194,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, 80, params->expandfactor);
ret[2].sval = dupstr(buf);
ret[2].ival = 0;