source: asp3_tinet_ecnl_rx/trunk/ntshell/ntshell/core/ntlibc.c@ 337

Last change on this file since 337 was 337, checked in by coas-nagasima, 6 years ago

ASP3版ECNLを追加

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 5.7 KB
Line 
1/**
2 * @file ntlibc.c
3 * @author CuBeatSystems
4 * @author Shinichiro Nakamura
5 * @copyright
6 * ===============================================================
7 * Natural Tiny Shell (NT-Shell) Version 0.3.1
8 * ===============================================================
9 * Copyright (c) 2010-2016 Shinichiro Nakamura
10 *
11 * Permission is hereby granted, free of charge, to any person
12 * obtaining a copy of this software and associated documentation
13 * files (the "Software"), to deal in the Software without
14 * restriction, including without limitation the rights to use,
15 * copy, modify, merge, publish, distribute, sublicense, and/or
16 * sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following
18 * conditions:
19 *
20 * The above copyright notice and this permission notice shall be
21 * included in all copies or substantial portions of the Software.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
25 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
27 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
28 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
30 * OTHER DEALINGS IN THE SOFTWARE.
31 */
32
33#include "ntlibc.h"
34
35int ntlibc_strlen(const char *s)
36{
37 const char *p = s;
38 int cnt = 0;
39 while (*p) {
40 cnt++;
41 p++;
42 }
43 return cnt;
44}
45
46char *ntlibc_strcpy(char *des, const char *src)
47{
48 char *d = des;
49 const char *s = src;
50 while (*s) {
51 *d = *s;
52 d++;
53 s++;
54 }
55 *d = '\0';
56 return des;
57}
58
59int ntlibc_strlcpy(char *des, const char *src, int n)
60{
61 char *d = des, *e = &des[n];
62 const char *s = src;
63 while (*s && d < e) {
64 *d = *s;
65 d++;
66 s++;
67 }
68 *d++ = '\0';
69 return (int)d - (int)des;
70}
71
72char *ntlibc_strcat(char *des, const char *src)
73{
74 char *d = des;
75 const char *s = src;
76 while (*d) {
77 d++;
78 }
79 while (*s) {
80 *d = *s;
81 d++;
82 s++;
83 }
84 *d = '\0';
85 return des;
86}
87
88int ntlibc_strlcat(char *des, const char *src, int n)
89{
90 char *d = des, *e = &des[n];
91 const char *s = src;
92 while (*d && d < e) {
93 d++;
94 }
95 while (*s && d < e) {
96 *d = *s;
97 d++;
98 s++;
99 }
100 *d++ = '\0';
101 return (int)d - (int)des;
102}
103#ifdef _MSC_VER
104int strlcat(char *des, const char *src, int n)
105{
106 return ntlibc_strlcat(des, src, n);
107}
108#endif
109int ntlibc_strcmp(const char *s1, const char *s2)
110{
111 char *p1 = (char *)s1;
112 char *p2 = (char *)s2;
113 while (*p1 || *p2) {
114 if (*p1 != *p2) {
115 return (*p1 < *p2) ? -1 : 1;
116 }
117 p1++;
118 p2++;
119 }
120 if (*p1 == *p2) {
121 return 0;
122 }
123 else {
124 return (*p1 < *p2) ? -1 : 1;
125 }
126}
127
128int ntlibc_stricmp(const char *s1, const char *s2)
129{
130 char *p1 = (char *)s1;
131 char *p2 = (char *)s2;
132 while (*p1 || *p2) {
133 if (ntlibc_toupper(*p1) != ntlibc_toupper(*p2)) {
134 return (*p1 < *p2) ? -1 : 1;
135 }
136 p1++;
137 p2++;
138 }
139 if (*p1 == *p2) {
140 return 0;
141 }
142 else {
143 return (*p1 < *p2) ? -1 : 1;
144 }
145}
146
147int ntlibc_strncmp(const char *s1, const char *s2, int n)
148{
149 char *p1 = (char *)s1;
150 char *p2 = (char *)s2;
151 int len = 0;
152 while (*p1 || *p2) {
153 if (n <= len) {
154 break;
155 }
156 if (*p1 != *p2) {
157 return (*p1 < *p2) ? -1 : 1;
158 }
159 p1++;
160 p2++;
161 len++;
162 }
163 return 0;
164}
165
166int ntlibc_isdigit(int c)
167{
168 if (('0' <= c) && (c <= '9')) {
169 return 1;
170 }
171 return 0;
172}
173
174int ntlibc_isalpha(int c)
175{
176 if (('A' <= c) && (c <= 'Z')) {
177 return 1;
178 }
179 if (('a' <= c) && (c <= 'z')) {
180 return 1;
181 }
182 return 0;
183}
184
185int ntlibc_iscntrl(int c)
186{
187 if (c == 0x07) { return 0; }
188 if (c == 0x08) { return 0; }
189 if (c == 0x09) { return 0; }
190 if (c == 0x0a) { return 0; }
191 if (c == 0x0b) { return 0; }
192 if (c == 0x0c) { return 0; }
193 if (c == 0x0d) { return 0; }
194 if ((0x00 <= c) && (c <= 0x1f)) {
195 return 1;
196 }
197 return 0;
198}
199
200int ntlibc_toupper(int c)
201{
202 if (('a' <= c) && (c <= 'z')) {
203 int diff = 'a' - 'A';
204 return c - diff;
205 }
206 return c;
207}
208
209int ntlibc_tolower(int c)
210{
211 if (('A' <= c) && (c <= 'Z')) {
212 int diff = 'a' - 'A';
213 return c + diff;
214 }
215 return c;
216}
217
218int ntlibc_atoi(const char *nptr)
219{
220 int cnt;
221 int num = 0;
222 int ofs = 0;
223 int sign = 0;
224 int scnt = 0;
225 char *p = (char *)nptr;
226 while (*p != '\0') {
227 if (!ntlibc_isdigit(*p)) {
228 if (*p == ' ') {
229 ofs++;
230 }
231 if (*p == '+') {
232 sign = 0;
233 ofs++;
234 if (scnt++ > 0) {
235 return 0;
236 }
237 }
238 if (*p == '-') {
239 sign = 1;
240 ofs++;
241 if (scnt++ > 0) {
242 return 0;
243 }
244 }
245 }
246 p++;
247 }
248 for (cnt = ofs; (nptr[cnt] >= '0') && (nptr[cnt] <= '9'); cnt++) {
249 num = 10 * num + (nptr[cnt] - '0');
250 }
251 if (sign) {
252 return -num;
253 }
254 else {
255 return num;
256 }
257}
258
259unsigned long ntlibc_strtoul(const char *__restrict nptr, char **__restrict endptr, int base)
260{
261 int cnt;
262 int num = 0;
263 int ofs = 0;
264 int sign = 0;
265 int scnt = 0;
266 char *p = (char *)nptr;
267 while (*p != '\0') {
268 if (!ntlibc_isdigit(*p)) {
269 if (*p == ' ') {
270 ofs++;
271 }
272 if (*p == '+') {
273 sign = 0;
274 ofs++;
275 if (scnt++ > 0) {
276 *endptr = p;
277 break;
278 }
279 }
280 if (*p == '-') {
281 sign = 1;
282 ofs++;
283 if (scnt++ > 0) {
284 *endptr = p;
285 break;
286 }
287 }
288 *endptr = p;
289 break;
290 }
291 p++;
292 }
293 for (cnt = ofs; (nptr[cnt] >= '0') && (nptr[cnt] <= '9'); cnt++) {
294 num = 10 * num + (nptr[cnt] - '0');
295 }
296 if (sign) {
297 return -num;
298 }
299 else {
300 return num;
301 }
302}
303
304char *ntlibc_strchr(const char *s, int c)
305{
306 char *p = (char *)s;
307 for (;;) {
308 if (*p == c) {
309 return p;
310 }
311 if (!*p)
312 break;
313 p++;
314 }
315 return 0;
316}
317
318char *ntlibc_utoa(unsigned int value, char *s, int radix)
319{
320 char *s1 = s;
321 char *s2 = s;
322
323 do {
324 *s2++ = "0123456789abcdefghijklmnopqrstuvwxyz"[value % radix];
325 value /= radix;
326 } while (value > 0);
327
328 *s2-- = '\0';
329
330 while (s1 < s2) {
331 char c = *s1;
332 *s1++ = *s2;
333 *s2-- = c;
334 }
335
336 return s;
337}
Note: See TracBrowser for help on using the repository browser.