forked from embedded2013/rtenv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstr_util.c
214 lines (183 loc) · 5.07 KB
/
str_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
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
#include <stdarg.h> /* For variable list */
#include "str_util.h"
#include "syscall.h"
/* TODO: Modulize kernel.c */
extern int mq_open(const char *name, int oflag);
/* Hand made tools */
int strncmp(const char *str_a, const char *str_b, size_t n)
{
int i = 0;
for(i = 0; i < n; i++) {
if (str_a[i] != str_b[i]) {
return str_a[i] - str_b[i];
}
}
return 0;
}
/* Warning!! Last atoi buf element needs to be set to \0 */
/* for itoa(), htoa() and addrtoa() */
/* Example: itoa_buf[MAX_ITOA_CHARS - 1] = 0; */
/* itoa_buf = itoa(100, itoa_buf) */
enum int_type_t {
SIGNED_INT,
UNSIGNED_INT
};
#define MAX_ITOA_CHARS (32)
#define itoa(val, str) num_to_string(val, 10, str, SIGNED_INT)
#define htoa(val, str) num_to_string(val, 16, str, SIGNED_INT)
static char* num_to_string(unsigned int val, int base, char *buf, enum int_type_t int_type)
{
char has_minus = 0;
int i = 30;
/* Sepecial case: 0 */
if (val == 0) {
buf[1] = '0';
return &buf[1];
}
if (int_type == SIGNED_INT && (int)val < 0) {
val = (int)-val;
has_minus = 1;
}
for (; val && (i - 1) ; --i, val /= base)
buf[i] = "0123456789abcdef"[val % base];
if (has_minus) {
buf[i] = '-';
return &buf[i];
}
return &buf[i + 1];
}
void my_puts(char *msg)
{
int fdout = mq_open("/tmp/mqueue/out", 0);
if (!msg) {
return;
}
write(fdout, msg, strlen(msg) + 1);
}
void my_printf(const char *fmt_str, ...)
{
va_list param = {0};
char param_chr[] = {0, 0};
int param_int = 0;
char itoa_buf[MAX_ITOA_CHARS] = {0};
char *str_to_output = 0;
int curr_char = 0;
va_start(param, fmt_str);
/* Let's parse */
while (fmt_str[curr_char]) {
/* Deal with normal string
* increase index by 1 here */
if (fmt_str[curr_char++] != '%') {
param_chr[0] = fmt_str[curr_char - 1];
str_to_output = param_chr;
}
/* % case-> retrive latter params */
else {
switch (fmt_str[curr_char]) {
case 'S':
case 's':
{
str_to_output = va_arg(param, char *);
}
break;
case 'd':
case 'D':
{
param_int = va_arg(param, int);
str_to_output = itoa(param_int, itoa_buf);
}
break;
case 'X':
case 'x':
{
param_int = va_arg(param, int);
str_to_output = htoa(param_int, itoa_buf);
}
break;
case 'c':
case 'C':
{
param_chr[0] = (char) va_arg(param, int);
str_to_output = param_chr;
break;
}
default:
{
param_chr[0] = fmt_str[curr_char];
str_to_output = param_chr;
}
} /* switch (fmt_str[curr_char]) */
curr_char++;
} /* if (fmt_str[curr_char++] == '%') */
my_puts(str_to_output);
} /* while (fmt_str[curr_char]) */
va_end(param);
}
#ifndef USE_ASM_OPTI_FUNC
int strcmp(const char *str_a, const char *str_b)
{
int i = 0;
while(str_a[i]) {
if (str_a[i] != str_b[i]) {
return str_a[i] - str_b[i];
}
i++;
}
return 0;
}
size_t strlen(const char *string)
{
int chars = 0;
while(*string++) {
chars++;
}
return chars;
}
void *memcpy(void *dest, const void *src, size_t n)
{
int i;
for (i = 0; i < n; i++) {
*((char *)dest + i) = *((char *)src + i);
}
return dest;
}
void *memset(void *dest, int c, size_t n)
{
int i;
for (i = 0; i < n; i++) {
*((char *)dest + i) = c;
}
return dest;
}
#else /* ASM optimized version */
int strcmp(const char *a, const char *b) __attribute__ ((naked));
int strcmp(const char *a, const char *b)
{
__asm__(
"strcmp_lop: \n"
" ldrb r2, [r0],#1 \n"
" ldrb r3, [r1],#1 \n"
" cmp r2, #1 \n"
" it hi \n"
" cmphi r2, r3 \n"
" beq strcmp_lop \n"
" sub r0, r2, r3 \n"
" bx lr \n"
:::
);
}
size_t strlen(const char *s) __attribute__ ((naked));
size_t strlen(const char *s)
{
__asm__(
" sub r3, r0, #1 \n"
"strlen_loop: \n"
" ldrb r2, [r3, #1]! \n"
" cmp r2, #0 \n"
" bne strlen_loop \n"
" sub r0, r3, r0 \n"
" bx lr \n"
:::
);
}
#endif /* USE_ASM_OPTI_FUNC */