diff --git a/Jupiter/Functions.c b/Jupiter/Functions.c index 11d881f..f8b4588 100644 --- a/Jupiter/Functions.c +++ b/Jupiter/Functions.c @@ -12,6 +12,7 @@ #include // Used by getTime() #include // toupper #include // towupper +#include // pow #include "Functions.h" // Little Endian @@ -605,6 +606,12 @@ unsigned int strtoui(const char *str, int base) return strtoi_nospace(str, base); } +double Jupiter_strtod(const char *str) +{ + while (isspace(*str)) str++; + return Jupiter_strtod_nospace(str); +} + int strtoi_nospace(const char *str, int base) { if (*str != '-') return strtoui_nospace(str, base); @@ -651,6 +658,39 @@ unsigned int strtoui_nospace(const char *str, int base) return total; } +double Jupiter_strtod_nospace(const char *str) +{ + const int base = 10; + double total = 0.0; + double decimal = 0.0; + int tval; + + if (*str == '-') return -1 * Jupiter_strtod_nospace(++str); + if (*str == '+') str++; + + while ((tval = getBase(*str, base)) != -1) + { + total = base * total + tval; + str++; + } + + if (*str == '.') + { + str++; + tval = base; + while ((decimal = getBase(*str, base)) != -1) + { + total = total + (decimal / tval); + str++; + tval = tval * base; + } + if (*str == 'e' || *str == 'E') + total = total * pow(base, strtoi_nospace(++str, 10)); + } + + return total; +} + /** Safe string converters */ int strtoi_s(const char *str, size_t length, int base) @@ -673,6 +713,16 @@ unsigned int strtoui_s(const char *str, size_t length, int base) return strtoui_nospace_s(str, length, base); } +double Jupiter_strtod_s(const char *str, size_t length) +{ + while (isspace(*str)) + { + str++; + length--; + } + return Jupiter_strtod_nospace_s(str, length); +} + signed int strtoi_nospace_s(const char *str, size_t length, int base) { if (length == 0) return 0; @@ -732,3 +782,44 @@ unsigned int strtoui_nospace_s(const char *str, size_t length, int base) return total; } + +double Jupiter_strtod_nospace_s(const char *str, size_t length) +{ + const int base = 10; + double total = 0.0; + double decimal = 0.0; + int tval; + + if (length == 0) return 0; + if (*str == '-') return -1 * Jupiter_strtod_nospace_s(++str, --length); + if (*str == '+') + { + str++; + length--; + } + + while ((tval = getBase(*str, base)) != -1) + { + total = base * total + tval; + if (--length == 0) return total; + str++; + } + + if (*str == '.') + { + str++; + length--; + tval = base; + while (length != 0 && (decimal = getBase(*str, base)) != -1) + { + total = total + (decimal / tval); + if (--length == 0) return total; + str++; + tval = tval * base; + } + if (*str == 'e' || *str == 'E') + total = total * pow(base, strtoi_nospace_s(++str, --length, 10)); + } + + return total; +} \ No newline at end of file diff --git a/Jupiter/Functions.h b/Jupiter/Functions.h index a7d0778..9a6a521 100644 --- a/Jupiter/Functions.h +++ b/Jupiter/Functions.h @@ -351,46 +351,84 @@ JUPITER_API int getBase(unsigned char c, int base); * @brief Interprets a string into an integer. * * @param str String to interpret. -* @param base Base of the string to interpret. 0 is acceptable. +* @param base Base of the string to interpret. * @return Interpretation of the string as an integer on success, 0 otherwise. */ JUPITER_API int strtoi(const char *str, int base); JUPITER_API unsigned int strtoui(const char *str, int base); +/** +* @brief Interpets a string into a floating point decimal number. +* +* @param str String to interpret. +* @param length Length of the string to interpret. +* @return Interpretation of the string as a floating point decimal on success, 0.0 otherwise. +*/ +JUPITER_API double Jupiter_strtod(const char *str); + /** * @brief Interprets a string into an integer. * Note: This function assumes no preceeding space characters exist. * * @param str String to interpret. -* @param base Base of the string to interpret. 0 is acceptable. +* @param base Base of the string to interpret. * @return Interpretation of the string as an integer on success, 0 otherwise. */ JUPITER_API int strtoi_nospace(const char *str, int base); JUPITER_API unsigned int strtoui_nospace(const char *str, int base); +/** +* @brief Interpets a string into a floating point decimal number. +* Note: This function assumes no preceeding space characters exist. +* +* @param str String to interpret. +* @param length Length of the string to interpret. +* @return Interpretation of the string as a floating point decimal on success, 0.0 otherwise. +*/ +JUPITER_API double Jupiter_strtod_nospace(const char *str); + /** * @brief Interprets a string into an integer. * * @param str String to interpret. * @param length Length of str. -* @param base Base of the string to interpret. 0 is acceptable. +* @param base Base of the string to interpret. * @return Interpretation of the string as an integer on success, 0 otherwise. */ JUPITER_API int strtoi_s(const char *str, size_t length, int base); JUPITER_API unsigned int strtoui_s(const char *str, size_t length, int base); +/** +* @brief Interpets a string into a floating point decimal number. +* +* @param str String to interpret. +* @param length Length of the string to interpret. +* @return Interpretation of the string as a floating point decimal on success, 0.0 otherwise. +*/ +JUPITER_API double Jupiter_strtod_s(const char *str, size_t length); + /** * @brief Interprets a string into an integer. * Note: This function assumes no preceeding space characters exist. * * @param str String to interpret. * @param length Length of str. -* @param base Base of the string to interpret. 0 is acceptable. +* @param base Base of the string to interpret. * @return Interpretation of the string as an integer on success, 0 otherwise. */ JUPITER_API int strtoi_nospace_s(const char *str, size_t length, int base); JUPITER_API unsigned int strtoui_nospace_s(const char *str, size_t length, int base); +/** +* @brief Interpets a string into a floating point decimal number. +* Note: This function assumes no preceeding space characters exist. +* +* @param str String to interpret. +* @param length Length of the string to interpret. +* @return Interpretation of the string as a floating point decimal on success, 0.0 otherwise. +*/ +JUPITER_API double Jupiter_strtod_nospace_s(const char *str, size_t length); + #if defined __cplusplus }