-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathjamutil.c
More file actions
219 lines (182 loc) · 3.76 KB
/
jamutil.c
File metadata and controls
219 lines (182 loc) · 3.76 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/****************************************************************************/
/* */
/* Module: jamutil.c */
/* */
/* Copyright (C) Altera Corporation 1997 */
/* */
/* Description: Utility functions. Most of these are private copies */
/* of standard 'C' library functions. Having them here */
/* is intended to reduce porting hassles by eliminating */
/* the need for local run-time library functions */
/* */
/****************************************************************************/
/****************************************************************************/
/* */
/* Actel version 1.1 May 2003 */
/* */
/****************************************************************************/
#include "jamutil.h"
char jam_toupper(char ch)
{
return ((char) (((ch >= 'a') && (ch <= 'z')) ? (ch + 'A' - 'a') : ch));
}
int jam_iscntrl(char ch)
{
return (((ch >= 0) && (ch <= 0x1f)) || (ch == 0x7f));
}
int jam_isalpha(char ch)
{
return (((ch >= 'A') && (ch <= 'Z')) || ((ch >= 'a') && (ch <= 'z')));
}
int jam_isdigit(char ch)
{
return ((ch >= '0') && (ch <= '9'));
}
int jam_isalnum(char ch)
{
return (((ch >= 'A') && (ch <= 'Z')) || ((ch >= 'a') && (ch <= 'z')) ||
((ch >= '0') && (ch <= '9')));
}
int jam_isspace(char ch)
{
return (((ch >= 0x09) && (ch <= 0x0d)) || (ch == 0x20));
}
int jam_is_name_char(char ch)
{
return (((ch >= 'A') && (ch <= 'Z')) || ((ch >= 'a') && (ch <= 'z')) ||
((ch >= '0') && (ch <= '9')) || (ch == '_'));
}
int jam_is_hex_char(char ch)
{
return (((ch >= 'A') && (ch <= 'F')) || ((ch >= 'a') && (ch <= 'f')) ||
((ch >= '0') && (ch <= '9')));
}
int jam_strlen(char *string)
{
int len = 0;
while (string[len] != '\0') ++len;
return (len);
}
long jam_atol(char *string)
{
long result = 0L;
int index = 0;
while ((string[index] >= '0') && (string[index] <= '9'))
{
result = (result * 10) + (string[index] - '0');
++index;
}
return (result);
}
void jam_ltoa(char *buffer, long number)
{
int index = 0;
int rev_index = 0;
char reverse[32];
if (number < 0L)
{
buffer[index++] = '-';
number = 0 - number;
}
else if (number == 0)
{
buffer[index++] = '0';
}
while (number != 0)
{
reverse[rev_index++] = (char) ((number % 10) + '0');
number /= 10;
}
while (rev_index > 0)
{
buffer[index++] = reverse[--rev_index];
}
buffer[index] = '\0';
}
int jam_strcmp(char *left, char *right)
{
int result = 0;
char l, r;
do
{
l = *left;
r = *right;
result = l - r;
++left;
++right;
}
while ((result == 0) && (l != '\0') && (r != '\0'));
return (result);
}
int jam_stricmp(char *left, char *right)
{
int result = 0;
char l, r;
do
{
l = jam_toupper(*left);
r = jam_toupper(*right);
result = l - r;
++left;
++right;
}
while ((result == 0) && (l != '\0') && (r != '\0'));
return (result);
}
int jam_strncmp(char *left, char *right, int count)
{
int result = 0;
char l, r;
do
{
l = *left;
r = *right;
result = l - r;
++left;
++right;
--count;
}
while ((result == 0) && (count > 0) && (l != '\0') && (r != '\0'));
return (result);
}
int jam_strnicmp(char *left, char *right, int count)
{
int result = 0;
char l, r;
do
{
l = jam_toupper(*left);
r = jam_toupper(*right);
result = l - r;
++left;
++right;
--count;
}
while ((result == 0) && (count > 0) && (l != '\0') && (r != '\0'));
return (result);
}
void jam_strcpy(char *left, char *right)
{
char ch;
do
{
*left = *right;
ch = *right;
++left;
++right;
}
while (ch != '\0');
}
void jam_strncpy(char *left, char *right, int count)
{
char ch;
do
{
*left = *right;
ch = *right;
++left;
++right;
--count;
}
while ((ch != '\0') && (count != 0));
}