changeset 293:711aad3869d1

Error-parsing floats and bools
author Adam Chlipala <adamc@hcoop.net>
date Sun, 07 Sep 2008 12:19:15 -0400
parents 6e665c7c96f6
children df02b09ff1ed
files include/urweb.h src/c/urweb.c tests/fromStringErr.ur
diffstat 3 files changed, 23 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/include/urweb.h	Sun Sep 07 12:15:46 2008 -0400
+++ b/include/urweb.h	Sun Sep 07 12:19:15 2008 -0400
@@ -82,3 +82,5 @@
 lw_Basis_bool *lw_Basis_stringToBool(lw_context, lw_Basis_string);
 
 lw_Basis_int lw_Basis_stringToInt_error(lw_context, lw_Basis_string);
+lw_Basis_float lw_Basis_stringToFloat_error(lw_context, lw_Basis_string);
+lw_Basis_bool lw_Basis_stringToBool_error(lw_context, lw_Basis_string);
--- a/src/c/urweb.c	Sun Sep 07 12:15:46 2008 -0400
+++ b/src/c/urweb.c	Sun Sep 07 12:19:15 2008 -0400
@@ -799,3 +799,22 @@
   else
     lw_error(ctx, FATAL, "Can't parse int: %s", s);
 }
+
+lw_Basis_float lw_Basis_stringToFloat_error(lw_context ctx, lw_Basis_string s) {
+  char *endptr;
+  lw_Basis_float n = strtod(s, &endptr);
+
+  if (*s != '\0' && *endptr == '\0')
+    return n;
+  else
+    lw_error(ctx, FATAL, "Can't parse float: %s", s);
+}
+
+lw_Basis_bool lw_Basis_stringToBool_error(lw_context ctx, lw_Basis_string s) {
+  if (!strcasecmp (s, "True"))
+    return lw_Basis_True;
+  else if (!strcasecmp (s, "False"))
+    return lw_Basis_False;
+  else
+    lw_error(ctx, FATAL, "Can't parse bool: %s", s);
+}
--- a/tests/fromStringErr.ur	Sun Sep 07 12:15:46 2008 -0400
+++ b/tests/fromStringErr.ur	Sun Sep 07 12:19:15 2008 -0400
@@ -1,3 +1,5 @@
 fun main () : transaction page = return <html><body>
         3 = {cdata (show _ (readError _ "3" : int))}<br/>
+        12.12 = {cdata (show _ (readError _ "12.12" : float))}<br/>
+        True = {cdata (show _ (readError _ "True" : bool))}<br/>
 </body></html>