@@ -330,7 +330,7 @@ extern "C"
330330 } else if (v->type == ValueType::Number) {
331331 return v->numberValue != 0 ;
332332 } else if (v->type == ValueType::String) {
333- return strlen (v->stringValue ) != 0 && ! value_stringsEqual (v-> stringValue , " false " ) && strcmp (v-> stringValue , " 0 " ) != 0 ;
333+ return value_stringToBool (v->stringValue );
334334 } else if (v->type == ValueType::Infinity || v->type == ValueType::NegativeInfinity) {
335335 return true ;
336336 } else if (v->type == ValueType::NaN) {
@@ -346,7 +346,7 @@ extern "C"
346346 if (v->type == ValueType::String)
347347 dst->assign (v->stringValue );
348348 else if (v->type == ValueType::Number)
349- dst-> assign ( value_doubleToString (v->numberValue ) );
349+ value_doubleToString (v->numberValue , dst );
350350 else if (v->type == ValueType::Bool)
351351 dst->assign (v->boolValue ? " true" : " false" );
352352 else if (v->type == ValueType::Infinity)
@@ -359,6 +359,19 @@ extern "C"
359359 dst->clear ();
360360 }
361361
362+ /* !
363+ * Returns the string representation of the given value.
364+ * \note It is the caller's responsibility to free allocated memory.
365+ */
366+ char *value_toCString (const ValueData *v)
367+ {
368+ std::string out;
369+ value_toString (v, &out);
370+ char *ret = (char *)malloc ((out.size () + 1 ) * sizeof (char ));
371+ strncpy (ret, out.c_str (), out.size () + 1 );
372+ return ret;
373+ }
374+
362375 /* ! Writes the UTF-16 representation of the given value to dst. */
363376 void value_toUtf16 (const libscratchcpp::ValueData *v, std::u16string *dst)
364377 {
@@ -367,6 +380,51 @@ extern "C"
367380 dst->assign (utf8::utf8to16 (s));
368381 }
369382
383+ /* !
384+ * Converts the given number to string.
385+ * \note It is the caller's responsibility to free allocated memory.
386+ */
387+ char *value_doubleToCString (double v)
388+ {
389+ std::string out;
390+ value_doubleToString (v, &out);
391+ char *ret = (char *)malloc ((out.size () + 1 ) * sizeof (char ));
392+ strncpy (ret, out.c_str (), out.size () + 1 );
393+ return ret;
394+ }
395+
396+ /* !
397+ * Converts the given boolean to string.
398+ * \note Do not free allocated memory!
399+ */
400+ const char *value_boolToCString (bool v)
401+ {
402+ if (v) {
403+ static const char *ret = " true" ;
404+ return ret;
405+ } else {
406+ static const char *ret = " false" ;
407+ return ret;
408+ }
409+ }
410+
411+ /* ! Converts the given string to double. */
412+ double value_stringToDouble (const char *s)
413+ {
414+ if (strcmp (s, " Infinity" ) == 0 )
415+ return std::numeric_limits<double >::infinity ();
416+ else if (strcmp (s, " -Infinity" ) == 0 )
417+ return -std::numeric_limits<double >::infinity ();
418+
419+ return value_stringToDoubleImpl (s);
420+ }
421+
422+ /* ! Converts the given string to boolean. */
423+ bool value_stringToBool (const char *s)
424+ {
425+ return strlen (s) != 0 && !value_stringsEqual (s, " false" ) && strcmp (s, " 0" ) != 0 ;
426+ }
427+
370428 /* operations */
371429
372430 /* ! Adds the given values and writes the result to dst. */
0 commit comments