-
Notifications
You must be signed in to change notification settings - Fork 4
/
util.c
72 lines (60 loc) · 1.05 KB
/
util.c
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
//
// $Id$
// $Revision$
// $Date$
// $Author$
// $HeadURL$
//
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include "m8cdis.h"
#include "util.h"
//
//
//
int utilHtoi (char *s)
{
char *t = s;
int x = 0;
int y = 1;
if (*s == '0' && (s [1] == 'x' || s [1] == 'X'))
t += 2;
s += strlen (s);
while (t <= --s)
{
if ((tolower ((int) *s) == 'h') && !*(s + 1))
continue;
else if ('0' <= *s && *s <= '9')
x += y * (*s - '0');
else if ('a' <= *s && *s <= 'f')
x += y * (*s - 'a' + 10);
else if ('A' <= *s && *s <= 'F')
x += y * (10 + *s - 'A');
else
return -1;
y <<= 4;
}
return x;
}
//
//
//
void utilDeleteLeadingWhiteSpace (char *s)
{
char *p = s;
while (*p && isspace ((int) *p))
p++;
if (*p)
strcpy (s, p);
}
void utilDeleteTrailingWhiteSpace (char *s)
{
char *p = s + (strlen (s) - 1);
while ((p > s) && (isspace ((int) *p) || (*p == '\n') || (*p == '\r')))
*p-- = '\0';
}