2 * Software License Agreement (BSD License)
4 * Based on original stdio.c released by Atmel
5 * Copyright (c) 2008, Atmel Corporation
8 * Modified by Roel Verdult, Copyright (c) 2010
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the copyright holders nor the
18 * names of its contributors may be used to endorse or promote products
19 * derived from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
22 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 //------------------------------------------------------------------------------
36 //------------------------------------------------------------------------------
40 //------------------------------------------------------------------------------
42 //------------------------------------------------------------------------------
44 //------------------------------------------------------------------------------
45 /// Copies data from a source buffer into a destination buffer. The two buffers
46 /// must NOT overlap. Returns the destination buffer.
47 /// \param pDestination Destination buffer.
48 /// \param pSource Source buffer.
49 /// \param num Number of bytes to copy.
50 //------------------------------------------------------------------------------
51 void * memcpy(void *pDestination
, const void *pSource
, size_t num
)
53 unsigned char *pByteDestination
;
54 unsigned char *pByteSource
;
55 unsigned int *pAlignedSource
= (unsigned int *) pSource
;
56 unsigned int *pAlignedDestination
= (unsigned int *) pDestination
;
58 // If num is more than 4 bytes, and both dest. and source are aligned,
60 if ((((unsigned int) pAlignedDestination
& 0x3) == 0)
61 && (((unsigned int) pAlignedSource
& 0x3) == 0)
66 *pAlignedDestination
++ = *pAlignedSource
++;
71 // Copy remaining bytes
72 pByteDestination
= (unsigned char *) pAlignedDestination
;
73 pByteSource
= (unsigned char *) pAlignedSource
;
76 *pByteDestination
++ = *pByteSource
++;
82 //------------------------------------------------------------------------------
83 /// Fills a memory region with the given value. Returns a pointer to the
85 /// \param pBuffer Pointer to the start of the memory region to fill
86 /// \param value Value to fill the region with
87 /// \param num Size to fill in bytes
88 //------------------------------------------------------------------------------
89 void * memset(void *pBuffer
, int value
, size_t num
)
91 unsigned char *pByteDestination
;
92 unsigned int *pAlignedDestination
= (unsigned int *) pBuffer
;
93 unsigned int alignedValue
= (value
<< 24) | (value
<< 16) | (value
<< 8) | value
;
95 // Set words if possible
96 if ((((unsigned int) pAlignedDestination
& 0x3) == 0) && (num
>= 4)) {
98 *pAlignedDestination
++ = alignedValue
;
102 // Set remaining bytes
103 pByteDestination
= (unsigned char *) pAlignedDestination
;
105 *pByteDestination
++ = value
;
110 void* memmove(void *s1
, const void *s2
, size_t n
)
112 char *s
=(char*)s2
, *d
=(char*)s1
;
129 int memcmp(const void *av
, const void *bv
, size_t len
)
131 const unsigned char *a
= av
;
132 const unsigned char *b
= bv
;
135 for (i
=0; i
<len
; i
++)
139 return (int)(a
[i
] - b
[i
]);
147 //-----------------------------------------------------------------------------
148 /// Search a character in the given string.
149 /// Returns a pointer to the character location.
150 /// \param pString Pointer to the start of the string to search.
151 /// \param character The character to find.
152 //-----------------------------------------------------------------------------
153 char * strchr(const char *pString
, int character
)
155 char * p
= (char *)pString
;
156 char c
= character
& 0xFF;
167 //-----------------------------------------------------------------------------
168 /// Return the length of a given string
169 /// \param pString Pointer to the start of the string.
170 //-----------------------------------------------------------------------------
171 size_t strlen(const char *pString
)
173 unsigned int length
= 0;
175 while(*pString
++ != 0) {
182 //-----------------------------------------------------------------------------
183 /// Search a character backword from the end of given string.
184 /// Returns a pointer to the character location.
185 /// \param pString Pointer to the start of the string to search.
186 /// \param character The character to find.
187 //-----------------------------------------------------------------------------
188 char * strrchr(const char *pString
, int character
)
192 while(*pString
!= 0) {
193 if (*pString
++ == character
) {
200 //-----------------------------------------------------------------------------
201 /// Copy from source string to destination string
202 /// Return a pointer to the destination string
203 /// \param pDestination Pointer to the destination string.
204 /// \param pSource Pointer to the source string.
205 //-----------------------------------------------------------------------------
206 char * strcpy(char *pDestination
, const char *pSource
)
208 char *pSaveDest
= pDestination
;
210 for(; (*pDestination
= *pSource
) != 0; ++pSource
, ++pDestination
);
214 //-----------------------------------------------------------------------------
215 /// Compare the first specified bytes of 2 given strings
216 /// Return 0 if equals
217 /// Return >0 if 1st string > 2nd string
218 /// Return <0 if 1st string < 2nd string
219 /// \param pString1 Pointer to the start of the 1st string.
220 /// \param pString2 Pointer to the start of the 2nd string.
221 /// \param count Number of bytes that should be compared.
222 //-----------------------------------------------------------------------------
223 int strncmp(const char *pString1
, const char *pString2
, size_t count
)
228 r
= *pString1
- *pString2
;
230 if (*pString1
== 0) {
243 //-----------------------------------------------------------------------------
244 /// Copy the first number of bytes from source string to destination string
245 /// Return the pointer to the destination string.
246 /// \param pDestination Pointer to the start of destination string.
247 /// \param pSource Pointer to the start of the source string.
248 /// \param count Number of bytes that should be copied.
249 //-----------------------------------------------------------------------------
250 char * strncpy(char *pDestination
, const char *pSource
, size_t count
)
252 char *pSaveDest
= pDestination
;
255 *pDestination
= *pSource
;
266 // Following code is based on the BSD licensed code released by UoC
267 // Copyright (c) 1988 Regents of the University of California
269 int strcmp(const char *s1
, const char *s2
)
274 return (*(unsigned char *)s1
- *(unsigned char *)--s2
);
277 char *strtok_r(char *s
, const char *delim
, char **last
);
279 char *strtok(char *s
, const char *delim
)
282 return strtok_r(s
, delim
, &last
);
285 char *strtok_r(char *s
, const char *delim
, char **last
)
292 if (s
== NULL
&& (s
= *last
) == NULL
)
296 * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
300 for (spanp
= (char *)delim
; (sc
= *spanp
++) != 0;) {
305 if (c
== 0) { /* no non-delimiter characters */
312 * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
313 * Note that delim must have one NUL; we stop if we see that, too.
317 spanp
= (char *)delim
;
319 if ((sc
= *spanp
++) == c
) {
This page took 0.071451 seconds and 5 git commands to generate.