forked from embedded2013/freertos
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstring-util.c
More file actions
executable file
·130 lines (114 loc) · 2.52 KB
/
Copy pathstring-util.c
File metadata and controls
executable file
·130 lines (114 loc) · 2.52 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
#include <stddef.h>
#include <stdint.h>
#include <limits.h>
#define ALIGN (sizeof(size_t))
#define ONES ((size_t)-1/UCHAR_MAX)
#define HIGHS (ONES * (UCHAR_MAX/2+1))
#define HASZERO(x) ((x)-ONES & ~(x) & HIGHS)
#define SS (sizeof(size_t))
void *memset(void *dest, int c, size_t n)
{
unsigned char *s = dest;
c = (unsigned char)c;
for (; ((uintptr_t)s & ALIGN) && n; n--) *s++ = c;
if (n) {
size_t *w, k = ONES * c;
for (w = (void *)s; n>=SS; n-=SS, w++) *w = k;
for (s = (void *)w; n; n--, s++) *s = c;
}
return dest;
}
void *memcpy(void *dest, const void *src, size_t n)
{
void *ret = dest;
//Cut rear
uint8_t *dst8 = dest;
const uint8_t *src8 = src;
switch (n % 4) {
case 3 : *dst8++ = *src8++;
case 2 : *dst8++ = *src8++;
case 1 : *dst8++ = *src8++;
case 0 : ;
}
//stm32 data bus width
uint32_t *dst32 = (void *)dst8;
const uint32_t *src32 = (void *)src8;
n = n / 4;
while (n--) {
*dst32++ = *src32++;
}
return ret;
}
char *strchr(const char *s, int c)
{
for (; *s && *s != c; s++);
return (*s == c) ? (char *)s : NULL;
}
char *strcpy(char *dest, const char *src)
{
const unsigned char *s = src;
unsigned char *d = dest;
while ((*d++ = *s++));
return dest;
}
char *strncpy(char *dest, const char *src, size_t n)
{
const unsigned char *s = src;
unsigned char *d = dest;
while (n-- && (*d++ = *s++));
return dest;
}
size_t strlen ( const char * str )
{
int count;
for(count=0 ; str[count]!='\0';count++);
return count;
}
int strcmp ( const char * str1, const char * str2 )
{
int i=-1;
do
{
i++;
if(str1[i]!=str2[i])
{
return (str1[i]>str2[i])?1:-1;
}
}while(str1[i]!='\0' && str2[i]!='\0');
return 0;
}
char * strcat ( char * destination, const char * source )//suppose the destination has enough space for concatenating the source
{
int dLength = strlen(destination);
int sLength = strlen(source);
int i;
for (i = 0; i<sLength; i++)
{
destination[i+dLength]=source[i];
}
destination[dLength+sLength]='\0';
return destination;
}
char* itoa(int value, char* str)//only support base=10
{
int base = 10;
int divideNum = base;
int i=0;
while(value/divideNum > 0)
{
divideNum*=base;
}
if(value < 0)
{
str[0] = '-';
i++;
}
while(divideNum/base > 0)
{
divideNum/=base;
str[i++]=value/divideNum+48;
value%=divideNum;
}
str[i]='\0';
return str;
}