bootstrappable.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. int i = -1;
  32. do
  33. {
  34. i = i + 1;
  35. if(a[i] != b[i])
  36. {
  37. return FALSE;
  38. }
  39. } while((0 != a[i]) && (0 !=b[i]));
  40. return TRUE;
  41. }
  42. int in_set(int c, char* s)
  43. {
  44. while(0 != s[0])
  45. {
  46. if(c == s[0]) return TRUE;
  47. s = s + 1;
  48. }
  49. return FALSE;
  50. }
  51. int index_number(char* s, char c)
  52. {
  53. int i = 0;
  54. while(s[i] != c)
  55. {
  56. i = i + 1;
  57. if(0 == s[i]) return -1;
  58. }
  59. return i;
  60. }
  61. int toupper(int c)
  62. {
  63. if(in_set(c, "abcdefghijklmnopqrstuvwxyz")) return (c & 0xDF);
  64. return c;
  65. }
  66. int __set_reader(char* set, int mult, char* input)
  67. {
  68. int n = 0;
  69. int i = 0;
  70. int hold;
  71. int negative_p = FALSE;
  72. if(input[0] == '-')
  73. {
  74. negative_p = TRUE;
  75. i = i + 1;
  76. }
  77. while(in_set(input[i], set))
  78. {
  79. n = n * mult;
  80. hold = index_number(set, toupper(input[i]));
  81. /* Input managed to change between in_set and index_number */
  82. if(-1 == hold) return 0;
  83. n = n + hold;
  84. i = i + 1;
  85. }
  86. /* loop exited before NULL and thus invalid input */
  87. if(0 != input[i]) return 0;
  88. if(negative_p)
  89. {
  90. n = 0 - n;
  91. }
  92. return n;
  93. }
  94. int strtoint(char *a)
  95. {
  96. int result = 0;
  97. /* If NULL string */
  98. if(0 == a[0])
  99. {
  100. result = 0;
  101. }
  102. /* Deal with binary*/
  103. else if ('0' == a[0] && 'b' == a[1])
  104. {
  105. result = __set_reader("01", 2, a+2);
  106. }
  107. /* Deal with hex */
  108. else if ('0' == a[0] && 'x' == a[1])
  109. {
  110. result = __set_reader("0123456789ABCDEFabcdef", 16, a+2);
  111. }
  112. /* Deal with octal */
  113. else if('0' == a[0])
  114. {
  115. result = __set_reader("01234567", 8, a+1);
  116. }
  117. /* Deal with decimal */
  118. else
  119. {
  120. result = __set_reader("0123456789", 10, a);
  121. }
  122. /* Deal with sign extension for 64bit hosts */
  123. if(0 != (0x80000000 & result)) result = (0xFFFFFFFF << 31) | result;
  124. return result;
  125. }