util.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * =====================================================================================
  3. *
  4. * ________ .__ __ ________ ____ ________
  5. * \_____ \ __ __|__| ____ | | __\______ \ _______ _/_ |/ _____/
  6. * / / \ \| | \ |/ ___\| |/ / | | \_/ __ \ \/ /| / __ \
  7. * / \_/. \ | / \ \___| < | ` \ ___/\ / | \ |__\ \
  8. * \_____\ \_/____/|__|\___ >__|_ \/_______ /\___ >\_/ |___|\_____ /
  9. * \__> \/ \/ \/ \/ \/
  10. *
  11. * www.optixx.org
  12. *
  13. *
  14. * Version: 1.0
  15. * Created: 07/21/2009 03:32:16 PM
  16. * Author: david@optixx.org
  17. *
  18. * =====================================================================================
  19. */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. uint8_t *util_strupper(uint8_t * s)
  24. {
  25. uint8_t *p;
  26. for (p = s; *p != '\0'; p++)
  27. if (*p >= 'a' && *p <= 'z')
  28. *p += 'A' - 'a';
  29. return s;
  30. }
  31. uint8_t *util_strlower(uint8_t * s)
  32. {
  33. uint8_t *p;
  34. for (p = s; *p != '\0'; p++)
  35. if (*p >= 'A' && *p <= 'Z')
  36. *p += 'a' - 'A';
  37. return s;
  38. }
  39. void util_chomp(uint8_t * s)
  40. {
  41. uint16_t len;
  42. len = strlen((char *) s);
  43. if (len >= 2 && s[len - 1] == '\n' && s[len - 2] == '\r')
  44. s[len - 2] = '\0';
  45. else if (len >= 1 && (s[len - 1] == '\n' || s[len - 1] == '\r'))
  46. s[len - 1] = '\0';
  47. }
  48. void util_trim(uint8_t * s)
  49. {
  50. uint8_t *p = s;
  51. uint8_t *q;
  52. /*
  53. * skip leading whitespace
  54. */
  55. while (*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n')
  56. p++;
  57. /*
  58. * now p points at the first non-whitespace uint8_tacter
  59. */
  60. if (*p == '\0') {
  61. /*
  62. * only whitespace
  63. */
  64. *s = '\0';
  65. return;
  66. }
  67. q = s + strlen((char *) s);
  68. /*
  69. * skip trailing whitespace
  70. */
  71. /*
  72. * we have found p < q such that *p is non-whitespace, so this loop terminates with q >= p
  73. */
  74. do
  75. q--;
  76. while (*q == ' ' || *q == '\t' || *q == '\r' || *q == '\n');
  77. /*
  78. * now q points at the last non-whitespace uint8_tacter
  79. */
  80. /*
  81. * cut off trailing whitespace
  82. */
  83. *++q = '\0';
  84. /*
  85. * move to string
  86. */
  87. memmove(s, p, q + 1 - p);
  88. }
  89. uint32_t util_sscandec(const uint8_t * s)
  90. {
  91. uint32_t result;
  92. if (*s == '\0')
  93. return -1;
  94. result = 0;
  95. for (;;) {
  96. if (*s >= '0' && *s <= '9')
  97. result = 10 * result + *s - '0';
  98. else if (*s == '\0')
  99. return result;
  100. else
  101. return -1;
  102. s++;
  103. }
  104. }
  105. uint32_t util_sscanhex(const uint8_t * s)
  106. {
  107. int32_t result;
  108. if (*s == '\0')
  109. return -1;
  110. result = 0;
  111. for (;;) {
  112. if (*s >= '0' && *s <= '9')
  113. result = 16 * result + *s - '0';
  114. else if (*s >= 'A' && *s <= 'F')
  115. result = 16 * result + *s - 'A' + 10;
  116. else if (*s >= 'a' && *s <= 'f')
  117. result = 16 * result + *s - 'a' + 10;
  118. else if (*s == '\0')
  119. return result;
  120. else
  121. return -1;
  122. s++;
  123. }
  124. }
  125. uint8_t util_sscanbool(const uint8_t * s)
  126. {
  127. if (*s == '0' && s[1] == '\0')
  128. return 0;
  129. if (*s == '1' && s[1] == '\0')
  130. return 1;
  131. return -1;
  132. }