You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
2.4 KiB

  1. /*
  2. ISC License
  3. Copyright (c) 2019, Antonio SJ Musumeci <trapexit@spawn.link>
  4. Permission to use, copy, modify, and/or distribute this software for any
  5. purpose with or without fee is hereby granted, provided that the above
  6. copyright notice and this permission notice appear in all copies.
  7. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. */
  15. #include "ef.hpp"
  16. #include "errno.hpp"
  17. #include <cstdint>
  18. #include <string>
  19. #include <stdlib.h>
  20. namespace str
  21. {
  22. int
  23. from(const std::string &value_,
  24. bool *bool_)
  25. {
  26. if((value_ == "true") ||
  27. (value_ == "1") ||
  28. (value_ == "on") ||
  29. (value_ == "yes"))
  30. *bool_ = true;
  31. ef((value_ == "false") ||
  32. (value_ == "0") ||
  33. (value_ == "off") ||
  34. (value_ == "no"))
  35. *bool_ = false;
  36. else
  37. return -EINVAL;
  38. return 0;
  39. }
  40. int
  41. from(const std::string &value_,
  42. int *int_)
  43. {
  44. int tmp;
  45. char *endptr;
  46. errno = 0;
  47. tmp = ::strtol(value_.c_str(),&endptr,10);
  48. if(errno != 0)
  49. return -EINVAL;
  50. if(endptr == value_.c_str())
  51. return -EINVAL;
  52. *int_ = tmp;
  53. return 0;
  54. }
  55. int
  56. from(const std::string &value_,
  57. uint64_t *uint64_)
  58. {
  59. char *endptr;
  60. uint64_t tmp;
  61. tmp = ::strtoll(value_.c_str(),&endptr,10);
  62. switch(*endptr)
  63. {
  64. case 'k':
  65. case 'K':
  66. tmp *= 1024ULL;
  67. break;
  68. case 'm':
  69. case 'M':
  70. tmp *= (1024ULL * 1024ULL);
  71. break;
  72. case 'g':
  73. case 'G':
  74. tmp *= (1024ULL * 1024ULL * 1024ULL);
  75. break;
  76. case 't':
  77. case 'T':
  78. tmp *= (1024ULL * 1024ULL * 1024ULL * 1024ULL);
  79. break;
  80. case '\0':
  81. break;
  82. default:
  83. return -EINVAL;
  84. }
  85. *uint64_ = tmp;
  86. return 0;
  87. }
  88. int
  89. from(const std::string &value_,
  90. std::string *str_)
  91. {
  92. *str_ = value_;
  93. return 0;
  94. }
  95. int
  96. from(const std::string &value_,
  97. const std::string *key_)
  98. {
  99. return -EINVAL;
  100. }
  101. }