numerate_number.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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<stdlib.h>
  18. #include<string.h>
  19. #include<stdio.h>
  20. // void* calloc(int count, int size);
  21. #define TRUE 1
  22. //CONSTANT TRUE 1
  23. #define FALSE 0
  24. //CONSTANT FALSE 0
  25. int in_set(int c, char* s);
  26. void file_print(char* s, FILE* f);
  27. char* numerate_number(int a)
  28. {
  29. char* result = calloc(16, sizeof(char));
  30. if(NULL == result)
  31. {
  32. file_print("calloc failed in numerate_number\n", stderr);
  33. exit(EXIT_FAILURE);
  34. }
  35. int i = 0;
  36. /* Deal with Zero case */
  37. if(0 == a)
  38. {
  39. result[0] = '0';
  40. return result;
  41. }
  42. /* Deal with negatives */
  43. if(0 > a)
  44. {
  45. result[0] = '-';
  46. i = 1;
  47. a = a * -1;
  48. }
  49. /* Using the largest 10^n number possible in 32bits */
  50. int divisor = 0x3B9ACA00;
  51. /* Skip leading Zeros */
  52. while(0 == (a / divisor)) divisor = divisor / 10;
  53. /* Now simply collect numbers until divisor is gone */
  54. while(0 < divisor)
  55. {
  56. result[i] = ((a / divisor) + 48);
  57. a = a % divisor;
  58. divisor = divisor / 10;
  59. i = i + 1;
  60. }
  61. return result;
  62. }
  63. int char2hex(int c)
  64. {
  65. if (c >= '0' && c <= '9') return (c - 48);
  66. else if (c >= 'a' && c <= 'f') return (c - 87);
  67. else if (c >= 'A' && c <= 'F') return (c - 55);
  68. else return -1;
  69. }
  70. int hex2char(int c)
  71. {
  72. if((c >= 0) && (c <= 9)) return (c + 48);
  73. else if((c >= 10) && (c <= 15)) return (c + 55);
  74. else return -1;
  75. }
  76. int char2dec(int c)
  77. {
  78. if (c >= '0' && c <= '9') return (c - 48);
  79. else return -1;
  80. }
  81. int dec2char(int c)
  82. {
  83. if((c >= 0) && (c <= 9)) return (c + 48);
  84. else return -1;
  85. }
  86. int index_number(char* s, char c)
  87. {
  88. int i = 0;
  89. while(s[i] != c)
  90. {
  91. i = i + 1;
  92. if(0 == s[i]) return -1;
  93. }
  94. return i;
  95. }
  96. int toupper(int c)
  97. {
  98. if(in_set(c, "abcdefghijklmnopqrstuvwxyz")) return (c & 0xDF);
  99. return c;
  100. }
  101. int set_reader(char* set, int mult, char* input)
  102. {
  103. int n = 0;
  104. int i = 0;
  105. int hold;
  106. int negative_p = FALSE;
  107. if(input[0] == '-')
  108. {
  109. negative_p = TRUE;
  110. i = i + 1;
  111. }
  112. while(in_set(input[i], set))
  113. {
  114. n = n * mult;
  115. hold = index_number(set, toupper(input[i]));
  116. /* Input managed to change between in_set and index_number */
  117. if(-1 == hold) return 0;
  118. n = n + hold;
  119. i = i + 1;
  120. }
  121. /* loop exited before NULL and thus invalid input */
  122. if(0 != input[i]) return 0;
  123. if(negative_p)
  124. {
  125. n = 0 - n;
  126. }
  127. return n;
  128. }
  129. int numerate_string(char *a)
  130. {
  131. /* If NULL string */
  132. if(0 == a[0])
  133. {
  134. return 0;
  135. }
  136. /* Deal with binary*/
  137. else if ('0' == a[0] && 'b' == a[1])
  138. {
  139. return set_reader("01", 2, a+2);
  140. }
  141. /* Deal with hex */
  142. else if ('0' == a[0] && 'x' == a[1])
  143. {
  144. return set_reader("0123456789ABCDEFabcdef", 16, a+2);
  145. }
  146. /* Deal with octal */
  147. else if('0' == a[0])
  148. {
  149. return set_reader("01234567", 8, a+1);
  150. }
  151. /* Deal with decimal */
  152. else
  153. {
  154. return set_reader("0123456789", 10, a);
  155. }
  156. }