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