flipbip_string.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (c) 1988 Regents of the University of California.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #include "flipbip_string.h"
  30. // #include <string.h>
  31. #include <ctype.h>
  32. char *
  33. flipbip_strtok(char *s, const char *delim)
  34. {
  35. static char *last;
  36. return flipbip_strtok_r(s, delim, &last);
  37. }
  38. char *
  39. flipbip_strtok_r(char *s, const char *delim, char **last)
  40. {
  41. char *spanp;
  42. int c, sc;
  43. char *tok;
  44. if (s == NULL && (s = *last) == NULL)
  45. return (NULL);
  46. /*
  47. * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
  48. */
  49. cont:
  50. c = *s++;
  51. for (spanp = (char *)delim; (sc = *spanp++) != 0;) {
  52. if (c == sc)
  53. goto cont;
  54. }
  55. if (c == 0) { /* no non-delimiter characters */
  56. *last = NULL;
  57. return (NULL);
  58. }
  59. tok = s - 1;
  60. /*
  61. * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
  62. * Note that delim must have one NUL; we stop if we see that, too.
  63. */
  64. for (;;) {
  65. c = *s++;
  66. spanp = (char *)delim;
  67. do {
  68. if ((sc = *spanp++) == c) {
  69. if (c == 0)
  70. s = NULL;
  71. else
  72. s[-1] = 0;
  73. *last = s;
  74. return (tok);
  75. }
  76. } while (sc != 0);
  77. }
  78. /* NOTREACHED */
  79. }
  80. /* Copyright (c) 2012, The Linux Foundation. All rights reserved.
  81. *
  82. * Redistribution and use in source and binary forms, with or without
  83. * modification, are permitted provided that the following conditions are
  84. * met:
  85. * * Redistributions of source code must retain the above copyright
  86. * notice, this list of conditions and the following disclaimer.
  87. * * Redistributions in binary form must reproduce the above
  88. * copyright notice, this list of conditions and the following
  89. * disclaimer in the documentation and/or other materials provided
  90. * with the distribution.
  91. * * Neither the name of The Linux Foundation nor the names of its
  92. * contributors may be used to endorse or promote products derived
  93. * from this software without specific prior written permission.
  94. *
  95. * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
  96. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  97. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
  98. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
  99. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  100. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  101. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  102. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  103. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  104. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  105. * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  106. */
  107. void
  108. flipbip_strrev(unsigned char *str)
  109. {
  110. int i;
  111. int j;
  112. unsigned char a;
  113. unsigned len = strlen((const char *)str);
  114. for (i = 0, j = len - 1; i < j; i++, j--)
  115. {
  116. a = str[i];
  117. str[i] = str[j];
  118. str[j] = a;
  119. }
  120. }
  121. int
  122. flipbip_itoa(int num, unsigned char* str, int len, int base)
  123. {
  124. int sum = num;
  125. int i = 0;
  126. int digit;
  127. if (len == 0)
  128. return -1;
  129. do
  130. {
  131. digit = sum % base;
  132. if (digit < 0xA)
  133. str[i++] = '0' + digit;
  134. else
  135. str[i++] = 'A' + digit - 0xA;
  136. sum /= base;
  137. }while (sum && (i < (len - 1)));
  138. if (i == (len - 1) && sum)
  139. return -1;
  140. str[i] = '\0';
  141. flipbip_strrev(str);
  142. return 0;
  143. }