bootstrappable.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /* Copyright (C) 2016 Jeremiah Orians
  2. * This file is part of M2-Planet.
  3. *
  4. * M2-Planet is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * M2-Planet is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with M2-Planet. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #define TRUE 1
  20. #define FALSE 0
  21. void require(int bool, char* error)
  22. {
  23. if(!bool)
  24. {
  25. fputs(error, stderr);
  26. exit(EXIT_FAILURE);
  27. }
  28. }
  29. int match(char* a, char* b)
  30. {
  31. if((NULL == a) && (NULL == b)) return TRUE;
  32. if(NULL == a) return FALSE;
  33. if(NULL == b) return FALSE;
  34. int i = -1;
  35. do
  36. {
  37. i = i + 1;
  38. if(a[i] != b[i])
  39. {
  40. return FALSE;
  41. }
  42. } while((0 != a[i]) && (0 !=b[i]));
  43. return TRUE;
  44. }
  45. int in_set(int c, char* s)
  46. {
  47. /* NULL set is always false */
  48. if(NULL == s) return FALSE;
  49. while(0 != s[0])
  50. {
  51. if(c == s[0]) return TRUE;
  52. s = s + 1;
  53. }
  54. return FALSE;
  55. }
  56. /* INTERNAL ONLY */
  57. int __index_number(char* s, char c)
  58. {
  59. int i = 0;
  60. while(s[i] != c)
  61. {
  62. i = i + 1;
  63. if(0 == s[i]) return -1;
  64. }
  65. return i;
  66. }
  67. /* INTERNAL ONLY */
  68. int __toupper(int c)
  69. {
  70. if(in_set(c, "abcdefghijklmnopqrstuvwxyz")) return (c & 0xDF);
  71. return c;
  72. }
  73. /* INTERNAL ONLY */
  74. int __set_reader(char* set, int mult, char* input)
  75. {
  76. int n = 0;
  77. int i = 0;
  78. int hold;
  79. int negative_p = FALSE;
  80. if(input[0] == '-')
  81. {
  82. negative_p = TRUE;
  83. i = i + 1;
  84. }
  85. while(in_set(input[i], set))
  86. {
  87. if('_' == input[i])
  88. {
  89. i = i + 1;
  90. continue;
  91. }
  92. n = n * mult;
  93. hold = __index_number(set, __toupper(input[i]));
  94. /* Input managed to change between in_set and index_number */
  95. if(-1 == hold) return 0;
  96. n = n + hold;
  97. i = i + 1;
  98. }
  99. /* loop exited before NULL and thus invalid input */
  100. if(0 != input[i]) return 0;
  101. if(negative_p)
  102. {
  103. n = 0 - n;
  104. }
  105. return n;
  106. }
  107. int strtoint(char *a)
  108. {
  109. int result = 0;
  110. /* If NULL string */
  111. if(0 == a[0])
  112. {
  113. result = 0;
  114. }
  115. /* Deal with binary */
  116. else if ('0' == a[0] && 'b' == a[1])
  117. {
  118. result = __set_reader("01_", 2, a+2);
  119. }
  120. /* Deal with hex */
  121. else if ('0' == a[0] && 'x' == a[1])
  122. {
  123. result = __set_reader("0123456789ABCDEFabcdef_", 16, a+2);
  124. }
  125. /* Deal with octal */
  126. else if('0' == a[0])
  127. {
  128. result = __set_reader("01234567_", 8, a+1);
  129. }
  130. /* Deal with decimal */
  131. else
  132. {
  133. result = __set_reader("0123456789_", 10, a);
  134. }
  135. /* Deal with sign extension for 64bit hosts */
  136. if(0 != (0x80000000 & result)) result = (0xFFFFFFFF << 31) | result;
  137. return result;
  138. }
  139. char* int2str(int x, int base, int signed_p)
  140. {
  141. require(1 < base, "int2str doesn't support a base less than 2\n");
  142. require(37 > base, "int2str doesn't support a base more than 36\n");
  143. /* Be overly conservative and save space for 32binary digits and padding null */
  144. char* p = calloc(34, sizeof(char));
  145. /* if calloc fails return null to let calling code deal with it */
  146. if(NULL == p) return p;
  147. p = p + 32;
  148. unsigned i;
  149. int sign_p = FALSE;
  150. char* table = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  151. if(signed_p && (10 == base) && (0 != (x & 0x80000000)))
  152. {
  153. /* Truncate to 31bits */
  154. i = -x & 0x7FFFFFFF;
  155. if(0 == i) return "-2147483648";
  156. sign_p = TRUE;
  157. } /* Truncate to 32bits */
  158. else i = x & (0x7FFFFFFF | (1 << 31));
  159. do
  160. {
  161. p[0] = table[i % base];
  162. p = p - 1;
  163. i = i / base;
  164. } while(0 < i);
  165. if(sign_p)
  166. {
  167. p[0] = '-';
  168. p = p - 1;
  169. }
  170. return p + 1;
  171. }