conv.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /** @file conv.c
  2. * @brief Functions to convert from string to integer.
  3. */
  4. #include <stdlib.h>
  5. #include <errno.h>
  6. #include <limits.h>
  7. /** @brief Converts string to long int
  8. *
  9. * Converts string to long int and check for overflow and valid
  10. * input values. Function indicates error by return value. It
  11. * returns the convertion result by second argument.
  12. *
  13. * @param [in] str Input string to convert.
  14. * @param [out] val Pointer to result value.
  15. * @param [in] base Base to convert.
  16. * @return 0 - success, < 0 - error
  17. */
  18. int faux_conv_atol(const char *str, long int *val, int base) {
  19. char *endptr;
  20. long int res;
  21. res = strtol(str, &endptr, base);
  22. // Check fof overflow
  23. if (((LONG_MIN == res) || (LONG_MAX == res)) && (ERANGE == errno))
  24. return -1;
  25. // No valid digits at all
  26. if ((0 == res) && ((endptr == str) || (errno != 0)))
  27. return -1;
  28. *val = res;
  29. return 0;
  30. }
  31. /** @brief Converts string to unsigned long int
  32. *
  33. * Converts string to unsigned long int and check for overflow and valid
  34. * input values. Function indicates error by return value. It
  35. * returns the convertion result by second argument.
  36. *
  37. * @param [in] str Input string to convert.
  38. * @param [out] val Pointer to result value.
  39. * @param [in] base Base to convert.
  40. * @return 0 - success, < 0 - error
  41. */
  42. int faux_conv_atoul(const char *str, unsigned long int *val, int base) {
  43. char *endptr;
  44. unsigned long int res;
  45. res = strtoul(str, &endptr, base);
  46. // Check fof overflow
  47. if ((ULONG_MAX == res) && (ERANGE == errno))
  48. return -1;
  49. // No valid digits at all
  50. if ((0 == res) && ((endptr == str) || (errno != 0)))
  51. return -1;
  52. *val = res;
  53. return 0;
  54. }
  55. /*--------------------------------------------------------- */
  56. int faux_conv_atos(const char *str, short *val, int base)
  57. {
  58. long int tmp;
  59. if (faux_conv_atol(str, &tmp, base) < 0)
  60. return -1;
  61. if ((tmp < SHRT_MIN) || (tmp > SHRT_MAX)) /* Overflow */
  62. return -1;
  63. *val = tmp;
  64. return 0;
  65. }
  66. /*--------------------------------------------------------- */
  67. int faux_conv_atous(const char *str, unsigned short *val, int base)
  68. {
  69. unsigned long int tmp;
  70. if (faux_conv_atoul(str, &tmp, base) < 0)
  71. return -1;
  72. if (tmp > USHRT_MAX) /* Overflow */
  73. return -1;
  74. *val = tmp;
  75. return 0;
  76. }
  77. /*--------------------------------------------------------- */
  78. int faux_conv_atoi(const char *str, int *val, int base)
  79. {
  80. long int tmp;
  81. if (faux_conv_atol(str, &tmp, base) < 0)
  82. return -1;
  83. if ((tmp < INT_MIN) || (tmp > INT_MAX)) /* Overflow */
  84. return -1;
  85. *val = tmp;
  86. return 0;
  87. }
  88. /*--------------------------------------------------------- */
  89. int faux_conv_atoui(const char *str, unsigned int *val, int base)
  90. {
  91. unsigned long int tmp;
  92. if (faux_conv_atoul(str, &tmp, base) < 0)
  93. return -1;
  94. if (tmp > UINT_MAX) /* Overflow */
  95. return -1;
  96. *val = tmp;
  97. return 0;
  98. }