📄 goodies2.c
字号:
/*
* ApBUILDER CODE FILE - Intel Corporation
*
*
* Purpose: Contains source code for some memory management routines commonly found in 'C'
* run-time libraries. Provided here as extra code to modify as you see fit when
* standard 'C' run-time memory management routines are not sufficient.... or just
* to learn from.
*
* The Software is provided "AS IS."
*
* LIMITATION OF LIABILITY: NEITHER INTEL NOR ITS VENDORS OR AGENTS
* SHALL BE LIABLE FOR ANY LOSS OF PROFITS, LOSS OF USE, LOSS OF DATA,
* INTERRUPTION OF BUSINESS, NOR FOR INDIRECT, SPECIAL, INCIDENTAL OR
* CONSEQUENTIAL DAMAGES OF ANY KIND WHETHER UNDER THIS AGREEMENT OR
* OTHERWISE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
*
* While we have made every attempt to completely test this code, we highly recommend that
* you personally test it completely prior to actual inclusion in your project.
*
* Compiler: Hopefully, it's compiler independent.. may have problems with register vars in some though.
*
* Ext Packages: None
*
* Author: Brad B.
*
* Revisions:
*
*
*/
typedef unsigned int size_t;
int memcmp(const void *s1, const void *s2, size_t n)
{
const char* t1 = (const char*)s1;
const char* t2 = (const char*)s2;
if (n <= 0)
{
return(0);
}
n++;
while (1)
{
if (!--n || (*t1++ != *t2++))
{
return(n ? *--t1 - *--t2 : 0);
}
}
return(0);
}
void *memcpy(void *s1, const void *s2, size_t n)
{
char* t1 = (char*)s1;
const char* t2 = (const char*)s2;
if (n <= 0)
{
return((void *)s1);
}
if (n)
{
do
{
*t1++ = *t2++;
}while (--n);
}
return((void *)s1);
}
void *memmove(void *s, const void *ct, size_t n)
{
char *so = s;
if (n <= 0)
{
return((void *)s);
}
if (so > (char *)ct)
{
for(so += n, ct = (char *)ct + n; n--; ct = (char *)ct - 1, *--so = *(char *)ct);
}
else
{
for(; n--; *so++ = *(char *)ct, ct = (char *)ct + 1);
}
return((void *)s);
}
/* searches for a character in a string. */
void *memchr(const void *cs, int c, unsigned int n)
{
if (n <= 0)
{
return(NULL);
}
for(; *(unsigned char *)cs != (unsigned char)c && n; cs = (unsigned char *)cs + 1, n--);
return(n ? (void *)cs : NULL);
}
void *memset(void *s, int c, size_t n)
{
char* t = (char*)s;
if (n <= 0)
{
return(s);
}
if (n)
{
do
{
*t++ = (unsigned char)c;
}while(--n);
}
return(s);
}
char *strcat(char *s1, const char *s2)
{
char* s = s1;
s1--;
while (*++s1);
while (*s1++ = *s2++);
return s;
}
char *strchr(const char *s, int c)
{
char ch = (char) c;
while (*s)
{
if (*s++ == ch)
{
return((char *) --s);
}
}
if (ch == '\0')
{
return((char *)s);
}
return(0);
}
int strcmp(const char *s1, const char *s2)
{
while (1)
{
if (!*s1)
{
return(*s1 - *s2);
}
if (*s1++ != *s2++)
{
return(*(--s1) - *(--s2));
}
}
}
char *strcpy(char *s1, const char *s2)
{
char* s = s1;
while (*s1++ = *s2++);
return(s);
}
char *strncat(char *s1, const char *s2, size_t n)
{
char* s = s1;
if (n <= 0)
{
return(s);
}
n++;
while (*s1++);
s1--;
while (--n && *s2 && (*s1++ = *s2++));
*s1 = '\0';
return(s);
}
int strncmp(const char *s1, const char *s2, size_t n)
{
if (n <= 0)
{
return(0);
}
n++;
while (1)
{
n--;
if ((!*s1) || (n == 0))
{
return(n ? *s1 - *s2 : 0);
}
if (*s1++ != *s2++)
{
return(*--s1 - *--s2);
}
}
}
char *strncpy(char *s1, const char *s2, size_t n)
{
char* s = s1;
if (n <= 0)
{
return(s);
}
n++;
while (--n && (*s1++ = *s2++));
while (n--)
{
*s1++ = '\0';
}
return(s);
}
/*
searches the string for the occurrence of any
character out of the second string.
*/
char *strpbrk(const char *cs, const char *ct)
{
register char *search;
do
{
for(search = ct; *search && *search != *cs; search++);
if(*search)
{
return(cs);
}
} while(*cs++);
return(NULL); /* no matching character found */
}
/*
search the string for the last
occurrence of the given character.
*/
char *strrchr(const char *cs, int c)
{
register char *os = cs;
while(*cs++);
for(; --cs != os && *cs != (char)c;);
return(*cs == (char)c ? cs : NULL);
}
/* search for a string within a string, return index into first string of second string */
char *strstr(const char *cs, const char *ct)
{
register char *search;
register char *search2;
for(; *cs; cs++)
{
/* look if string ct is in cs */
for(search = ct, search2 = cs; *search && *search == *search2; ++search, ++search2);
/* if the string has been found in ct, return pointer */
if(!*search)
{
return(cs);
}
}
return(NULL);
/* no matching string found */
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -