123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376 |
- /** @file conv.c
- * @brief Functions to convert from string to integer.
- */
- #include <stdlib.h>
- #include <errno.h>
- #include <limits.h>
- #include "faux/conv.h"
- #include "faux/str.h"
- /** @brief Converts string to long int
- *
- * Converts string to long int and check for overflow and valid
- * input values. Function indicates error by return value. It
- * returns the convertion result by second argument.
- *
- * @param [in] str Input string to convert.
- * @param [out] val Pointer to result value.
- * @param [in] base Base to convert.
- * @return BOOL_TRUE - success, BOOL_FALSE - error
- */
- bool_t faux_conv_atol(const char *str, long int *val, int base)
- {
- char *endptr = NULL;
- long int res = 0;
- errno = 0; // man recommends to do so
- res = strtol(str, &endptr, base);
- // Check for overflow
- if (((LONG_MIN == res) || (LONG_MAX == res)) && (ERANGE == errno))
- return BOOL_FALSE;
- // No valid digits at all
- if ((0 == res) && ((endptr == str) || (errno != 0)))
- return BOOL_FALSE;
- *val = res;
- return BOOL_TRUE;
- }
- /** @brief Converts string to unsigned long int
- *
- * Converts string to unsigned long int and check for overflow and valid
- * input values. Function indicates error by return value. It
- * returns the convertion result by second argument.
- *
- * @param [in] str Input string to convert.
- * @param [out] val Pointer to result value.
- * @param [in] base Base to convert.
- * @return BOOL_TRUE - success, BOOL_FALSE - error
- */
- bool_t faux_conv_atoul(const char *str, unsigned long int *val, int base)
- {
- char *endptr = NULL;
- unsigned long int res = 0;
- errno = 0; // man recommends to do so
- res = strtoul(str, &endptr, base);
- // Check for overflow
- if ((ULONG_MAX == res) && (ERANGE == errno))
- return BOOL_FALSE;
- // No valid digits at all
- if ((0 == res) && ((endptr == str) || (errno != 0)))
- return BOOL_FALSE;
- *val = res;
- return BOOL_TRUE;
- }
- /** @brief Converts string to long long int
- *
- * Converts string to long long int and check for overflow and valid
- * input values. Function indicates error by return value. It
- * returns the convertion result by second argument.
- *
- * @param [in] str Input string to convert.
- * @param [out] val Pointer to result value.
- * @param [in] base Base to convert.
- * @return BOOL_TRUE - success, BOOL_FALSE - error
- */
- bool_t faux_conv_atoll(const char *str, long long int *val, int base)
- {
- char *endptr = NULL;
- long long int res = 0;
- errno = 0; // man recommends to do so
- res = strtoll(str, &endptr, base);
- // Check for overflow
- if (((LLONG_MIN == res) || (LLONG_MAX == res)) && (ERANGE == errno))
- return BOOL_FALSE;
- // No valid digits at all
- if ((0 == res) && ((endptr == str) || (errno != 0)))
- return BOOL_FALSE;
- *val = res;
- return BOOL_TRUE;
- }
- /** @brief Converts string to unsigned long long int
- *
- * Converts string to unsigned long long int and check for overflow and valid
- * input values. Function indicates error by return value. It
- * returns the convertion result by second argument.
- *
- * @param [in] str Input string to convert.
- * @param [out] val Pointer to result value.
- * @param [in] base Base to convert.
- * @return BOOL_TRUE - success, BOOL_FALSE - error
- */
- bool_t faux_conv_atoull(const char *str, unsigned long long int *val, int base)
- {
- char *endptr = NULL;
- unsigned long long int res = 0;
- errno = 0; // man recommends to do so
- res = strtoull(str, &endptr, base);
- // Check for overflow
- if ((ULLONG_MAX == res) && (ERANGE == errno))
- return BOOL_FALSE;
- // No valid digits at all
- if ((0 == res) && ((endptr == str) || (errno != 0)))
- return BOOL_FALSE;
- *val = res;
- return BOOL_TRUE;
- }
- /** @brief Converts string to int
- *
- * Converts string to int and check for overflow and valid
- * input values. Function indicates error by return value. It
- * returns the convertion result by second argument.
- *
- * @param [in] str Input string to convert.
- * @param [out] val Pointer to result value.
- * @param [in] base Base to convert.
- * @return BOOL_TRUE - success, BOOL_FALSE - error
- */
- bool_t faux_conv_atoi(const char *str, int *val, int base)
- {
- long int tmp = 0;
- // Use existent func. The long int is longer or equal to int.
- if (faux_conv_atol(str, &tmp, base) < 0)
- return BOOL_FALSE;
- if ((tmp < INT_MIN) || (tmp > INT_MAX)) // Overflow
- return BOOL_FALSE;
- *val = tmp;
- return BOOL_TRUE;
- }
- /** @brief Converts string to unsigned int
- *
- * Converts string to unsigned int and check for overflow and valid
- * input values. Function indicates error by return value. It
- * returns the convertion result by second argument.
- *
- * @param [in] str Input string to convert.
- * @param [out] val Pointer to result value.
- * @param [in] base Base to convert.
- * @return BOOL_TRUE - success, BOOL_FALSE - error
- */
- bool_t faux_conv_atoui(const char *str, unsigned int *val, int base)
- {
- unsigned long int tmp = 0;
- // Use existent func. The long int is longer or equal to int.
- if (faux_conv_atoul(str, &tmp, base) < 0)
- return BOOL_FALSE;
- if (tmp > UINT_MAX) // Overflow
- return BOOL_FALSE;
- *val = tmp;
- return BOOL_TRUE;
- }
- /** @brief Converts string to short
- *
- * Converts string to short and check for overflow and valid
- * input values. Function indicates error by return value. It
- * returns the convertion result by second argument.
- *
- * @param [in] str Input string to convert.
- * @param [out] val Pointer to result value.
- * @param [in] base Base to convert.
- * @return BOOL_TRUE - success, BOOL_FALSE - error
- */
- bool_t faux_conv_atos(const char *str, short *val, int base)
- {
- long int tmp = 0;
- if (faux_conv_atol(str, &tmp, base) < 0)
- return BOOL_FALSE;
- if ((tmp < SHRT_MIN) || (tmp > SHRT_MAX)) // Overflow
- return BOOL_FALSE;
- *val = tmp;
- return BOOL_TRUE;
- }
- /** @brief Converts string to unsigned short
- *
- * Converts string to unsigned short and check for overflow and valid
- * input values. Function indicates error by return value. It
- * returns the convertion result by second argument.
- *
- * @param [in] str Input string to convert.
- * @param [out] val Pointer to result value.
- * @param [in] base Base to convert.
- * @return BOOL_TRUE - success, BOOL_FALSE - error
- */
- bool_t faux_conv_atous(const char *str, unsigned short *val, int base)
- {
- unsigned long int tmp = 0;
- if (faux_conv_atoul(str, &tmp, base) < 0)
- return BOOL_FALSE;
- if (tmp > USHRT_MAX) // Overflow
- return BOOL_FALSE;
- *val = tmp;
- return BOOL_TRUE;
- }
- /** @brief Converts string to char
- *
- * Converts string to char and check for overflow and valid
- * input values. Function indicates error by return value. It
- * returns the convertion result by second argument.
- *
- * @param [in] str Input string to convert.
- * @param [out] val Pointer to result value.
- * @param [in] base Base to convert.
- * @return BOOL_TRUE - success, BOOL_FALSE - error
- */
- bool_t faux_conv_atoc(const char *str, char *val, int base)
- {
- long int tmp = 0;
- if (faux_conv_atol(str, &tmp, base) < 0)
- return BOOL_FALSE;
- if ((tmp < CHAR_MIN) || (tmp > CHAR_MAX)) // Overflow
- return BOOL_FALSE;
- *val = tmp;
- return BOOL_TRUE;
- }
- /** @brief Converts string to unsigned char
- *
- * Converts string to unsigned char and check for overflow and valid
- * input values. Function indicates error by return value. It
- * returns the convertion result by second argument.
- *
- * @param [in] str Input string to convert.
- * @param [out] val Pointer to result value.
- * @param [in] base Base to convert.
- * @return BOOL_TRUE - success, BOOL_FALSE - error
- */
- bool_t faux_conv_atouc(const char *str, unsigned char *val, int base)
- {
- unsigned long int tmp = 0;
- if (faux_conv_atoul(str, &tmp, base) < 0)
- return BOOL_FALSE;
- if (tmp > UCHAR_MAX) // Overflow
- return BOOL_FALSE;
- *val = tmp;
- return BOOL_TRUE;
- }
- /** @brief Converts string to bool_t
- *
- * Case insensitive.
- *
- * @param [in] str Input string to convert.
- * @param [out] val Pointer to result value.
- * @return BOOL_TRUE - success, BOOL_FALSE - error
- */
- bool_t faux_conv_str2bool(const char *str, bool_t *val)
- {
- if (!str)
- return BOOL_FALSE;
- if (faux_str_casecmp(str, "true") == 0) {
- if (val)
- *val = BOOL_TRUE;
- return BOOL_TRUE;
- }
- if (faux_str_casecmp(str, "false") == 0) {
- if (val)
- *val = BOOL_FALSE;
- return BOOL_TRUE;
- }
- return BOOL_FALSE;
- }
- /** @brief Converts bool_t to string
- *
- * @param [in] val Boolean value.
- * @return "true" or "false" strings
- */
- const char *faux_conv_bool2str(bool_t val)
- {
- if (val)
- return "true";
- return "false";
- }
- /** @brief Converts string to tri_t
- *
- * Case insensitive.
- *
- * @param [in] str Input string to convert.
- * @param [out] val Pointer to result value.
- * @return BOOL_TRUE - success, BOOL_FALSE - error
- */
- bool_t faux_conv_str2tri(const char *str, tri_t *val)
- {
- if (!str)
- return BOOL_FALSE;
- if (faux_str_casecmp(str, "true") == 0) {
- if (val)
- *val = TRI_TRUE;
- return BOOL_TRUE;
- }
- if (faux_str_casecmp(str, "false") == 0) {
- if (val)
- *val = TRI_FALSE;
- return BOOL_TRUE;
- }
- if (faux_str_casecmp(str, "undefined") == 0) {
- if (val)
- *val = TRI_UNDEFINED;
- return BOOL_TRUE;
- }
- return BOOL_FALSE;
- }
- /** @brief Converts tri_t to string
- *
- * @param [in] val tri_t value.
- * @return "true"/"false"/"undefined" strings
- */
- const char *faux_conv_tri2str(tri_t val)
- {
- if (TRI_TRUE == val)
- return "true";
- if (TRI_FALSE == val)
- return "false";
- return "undefined";
- }
|