Browse Source

Added Jupiter_strtod type functions

release/0.19
JustinAJ 11 years ago
parent
commit
dc5a60e752
  1. 91
      Jupiter/Functions.c
  2. 46
      Jupiter/Functions.h

91
Jupiter/Functions.c

@ -12,6 +12,7 @@
#include <time.h> // Used by getTime() #include <time.h> // Used by getTime()
#include <ctype.h> // toupper #include <ctype.h> // toupper
#include <wctype.h> // towupper #include <wctype.h> // towupper
#include <math.h> // pow
#include "Functions.h" #include "Functions.h"
// Little Endian // Little Endian
@ -605,6 +606,12 @@ unsigned int strtoui(const char *str, int base)
return strtoi_nospace(str, 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) int strtoi_nospace(const char *str, int base)
{ {
if (*str != '-') return strtoui_nospace(str, base); if (*str != '-') return strtoui_nospace(str, base);
@ -651,6 +658,39 @@ unsigned int strtoui_nospace(const char *str, int base)
return total; 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 */ /** Safe string converters */
int strtoi_s(const char *str, size_t length, int base) 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); 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) signed int strtoi_nospace_s(const char *str, size_t length, int base)
{ {
if (length == 0) return 0; if (length == 0) return 0;
@ -732,3 +782,44 @@ unsigned int strtoui_nospace_s(const char *str, size_t length, int base)
return total; 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;
}

46
Jupiter/Functions.h

@ -351,46 +351,84 @@ JUPITER_API int getBase(unsigned char c, int base);
* @brief Interprets a string into an integer. * @brief Interprets a string into an integer.
* *
* @param str String to interpret. * @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. * @return Interpretation of the string as an integer on success, 0 otherwise.
*/ */
JUPITER_API int strtoi(const char *str, int base); JUPITER_API int strtoi(const char *str, int base);
JUPITER_API unsigned int strtoui(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. * @brief Interprets a string into an integer.
* Note: This function assumes no preceeding space characters exist. * Note: This function assumes no preceeding space characters exist.
* *
* @param str String to interpret. * @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. * @return Interpretation of the string as an integer on success, 0 otherwise.
*/ */
JUPITER_API int strtoi_nospace(const char *str, int base); JUPITER_API int strtoi_nospace(const char *str, int base);
JUPITER_API unsigned int strtoui_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. * @brief Interprets a string into an integer.
* *
* @param str String to interpret. * @param str String to interpret.
* @param length Length of str. * @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. * @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 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); 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. * @brief Interprets a string into an integer.
* Note: This function assumes no preceeding space characters exist. * Note: This function assumes no preceeding space characters exist.
* *
* @param str String to interpret. * @param str String to interpret.
* @param length Length of str. * @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. * @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 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); 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 #if defined __cplusplus
} }

Loading…
Cancel
Save