XXI. Le code source complet de l'article▲
Vous pouvez télécharger l'archive du code source complet ici
XXI-A. Fichier: c_stddef.h▲
Sélectionnez
#ifndef _H_CSTDDEF
#define _H_CSTDDEF
#undef NULL
#define NULL ((void *) 0)
typedef
unsigned
int
c_size_t;
#endif /* _H_CSTDDEF */
XXI-B. Fichier: c_string.h▲
Sélectionnez
#ifndef _H_CSTRING
#define _H_CSTRING
#include "c_stddef.h"
void
*
c_memset (
void
*
s, int
c, c_size_t n);
void
*
c_memcpy (
void
*
dest, const
void
*
src, c_size_t n);
void
*
c_memmove (
void
*
dest, const
void
*
src, c_size_t n);
int
c_memcmp (
const
void
*
s1, const
void
*
s2, c_size_t n);
void
*
c_memchr (
const
void
*
s, int
c, c_size_t n);
c_size_t c_strlen (
const
char
*
s);
char
*
c_strcpy (
char
*
dest, const
char
*
src);
char
*
c_strncpy (
char
*
dest, const
char
*
src, c_size_t n);
char
*
c_strcat (
char
*
dest, const
char
*
src);
char
*
c_strncat (
char
*
dest, const
char
*
src, c_size_t n);
char
*
c_strchr (
const
char
*
s, int
c);
char
*
c_strrchr (
const
char
*
s, int
c);
int
c_strcmp (
const
char
*
s1, const
char
*
s2);
int
c_strncmp (
const
char
*
s1, const
char
*
s2, c_size_t n);
char
*
c_strpbrk (
const
char
*
s1, const
char
*
s2);
c_size_t c_strspn (
const
char
*
s1, const
char
*
s2);
c_size_t c_strcspn (
const
char
*
s1, const
char
*
s2);
char
*
c_strstr (
const
char
*
s1, const
char
*
s2);
char
*
c_strtok (
char
*
s1, const
char
*
s2);
#endif /* _H_CSTRING */
XXI-C. Fichier: c_string.c▲
Sélectionnez
#include "c_string.h"
void
*
c_memset (
void
*
s, int
c, c_size_t n)
{
unsigned
char
*
p_s =
s;
while
(
n--
)
{
*
p_s++
=
c;
}
return
s;
}
void
*
c_memcpy (
void
*
dest, const
void
*
src, c_size_t n)
{
char
*
p_dest =
dest;
const
char
*
p_src =
src;
while
(
n--
)
{
*
p_dest++
=
*
p_src++
;
}
return
dest;
}
void
*
c_memmove (
void
*
dest, const
void
*
src, c_size_t n)
{
char
*
p_dest =
dest;
const
char
*
p_src =
src;
if
(
p_src <=
p_dest)
{
p_dest +=
n -
1
;
p_src +=
n -
1
;
while
(
n--
)
{
*
p_dest--
=
*
p_src--
;
}
}
else
{
c_memcpy (
dest, src, n);
}
return
dest;
}
int
c_memcmp (
const
void
*
s1, const
void
*
s2, c_size_t n)
{
const
unsigned
char
*
p_s1 =
s1;
const
unsigned
char
*
p_s2 =
s2;
while
(
n--
)
{
if
(*
p_s1 !=
*
p_s2)
{
return
*
p_s1 <
*
p_s2 ? -
1
: 1
;
}
p_s1++
;
p_s2++
;
}
return
0
;
}
void
*
c_memchr (
const
void
*
s, int
c, c_size_t n)
{
const
unsigned
char
*
p_s =
s;
while
(
n--
)
{
if
(*
p_s ==
c)
{
return
(
void
*
) p_s;
}
p_s++
;
}
return
NULL
;
}
c_size_t c_strlen (
const
char
*
s)
{
c_size_t size =
0
;
while
(*
s++
)
{
size++
;
}
return
size;
}
char
*
c_strcpy (
char
*
dest, const
char
*
src)
{
while
((*
dest++
=
*
src++
));
return
dest;
}
char
*
c_strncpy (
char
*
dest, const
char
*
src, c_size_t n)
{
c_size_t i =
0
;
if
(
n >
0
)
{
for
(
i =
0
; i <
n; i++
)
{
if
(
src[i] !=
'
\0
'
)
{
dest[i] =
src[i];
}
else
{
break
;
}
}
if
(
i <
n)
{
do
{
dest[i] =
'
\0
'
;
i++
;
}
while
(
i <
n);
}
}
return
dest;
}
char
*
c_strcat (
char
*
dest, const
char
*
src)
{
const
char
*
p1 =
src;
char
*
p2 =
dest +
c_strlen (
dest);
do
{
*
p2++
=
*
p1;
}
while
(*
p1++
);
return
dest;
}
char
*
c_strncat (
char
*
dest, const
char
*
src, c_size_t n)
{
const
char
*
p1 =
src;
char
*
p2 =
dest +
c_strlen (
dest);
do
{
*
p2++
=
*
p1++
;
}
while
(
n--
&&
*
p1);
return
dest;
}
char
*
c_strchr (
const
char
*
s, int
c)
{
const
char
*
p =
s;
while
(*
p !=
(
char
) c)
{
if
(*
p ==
0
)
{
return
NULL
;
}
p++
;
}
return
(
char
*
) p;
}
char
*
c_strrchr (
const
char
*
s, int
c)
{
const
char
*
p =
s +
c_strlen (
s);
do
{
if
(*
p ==
(
char
) c)
{
return
(
char
*
) p;
}
}
while
(--
p >=
s);
return
NULL
;
}
int
c_strcmp (
const
char
*
s1, const
char
*
s2)
{
const
unsigned
char
*
p_s1 =
s1;
const
unsigned
char
*
p_s2 =
s2;
while
(*
p_s1 ==
*
p_s2)
{
if
(*
p_s1 ==
0
)
{
return
0
;
}
p_s1++
;
p_s2++
;
}
return
*
p_s1 <
*
p_s2 ? -
1
: 1
;
}
int
c_strncmp (
const
char
*
s1, const
char
*
s2, c_size_t n)
{
const
unsigned
char
*
p_s1 =
s1;
const
unsigned
char
*
p_s2 =
s2;
while
(
n--
)
{
if
(*
p_s1 ==
0
||
*
p_s1 !=
*
p_s2)
{
return
*
p_s1 <
*
p_s2 ? -
1
: 1
;
}
p_s1++
;
p_s2++
;
}
return
0
;
}
char
*
c_strpbrk (
const
char
*
s1, const
char
*
s2)
{
const
char
*
p_s1 =
s1;
int
i =
0
;
int
len =
c_strlen (
s2);
while
(*
p_s1++
)
{
for
(
i =
0
; i <
len; i++
)
{
if
(
s2[i] ==
*
p_s1)
{
return
(
char
*
)p_s1;
}
}
}
return
NULL
;
}
c_size_t c_strspn (
const
char
*
s1, const
char
*
s2)
{
c_size_t j =
0
;
c_size_t i =
0
;
c_size_t len_s1 =
c_strlen (
s1);
c_size_t len_s2 =
c_strlen (
s2);
c_size_t size =
0
;
int
quit =
0
;
for
(
i =
0
; !
quit &&
i <
len_s1; i++
)
{
quit =
1
;
for
(
j =
0
; quit &&
j <
len_s2; j++
)
{
if
(
s2[j] ==
s1[i])
{
quit =
0
;
size++
;
}
}
}
return
size;
}
c_size_t c_strcspn (
const
char
*
s1, const
char
*
s2)
{
c_size_t j =
0
;
c_size_t i =
0
;
c_size_t len_s1 =
c_strlen (
s1);
c_size_t len_s2 =
c_strlen (
s2);
c_size_t size =
0
;
int
quit =
0
;
for
(
i =
0
; !
quit &&
i <
len_s1; i++
)
{
for
(
j =
0
; !
quit &&
j <
len_s2; j++
)
{
if
(
s2[j] ==
s1[i])
{
quit =
1
;
}
}
if
(!
quit)
{
size++
;
}
}
return
size;
}
char
*
c_strstr (
const
char
*
s1, const
char
*
s2)
{
const
char
*
p_s1 =
s1;
c_size_t len =
c_strlen (
s2);
while
(*
p_s1)
{
if
(
c_memcmp (
p_s1, s2, len) ==
0
)
{
return
(
char
*
)p_s1;
}
p_s1++
;
}
return
NULL
;
}
char
*
c_strtok (
char
*
s1, const
char
*
s2)
{
static
char
*
ptr =
NULL
;
char
*
ret =
NULL
;
if
(
s1 ==
NULL
)
{
s1 =
ptr;
}
while
(*
s1 &&
c_strchr (
s2, *
s1))
{
++
s1;
}
if
(*
s1 ==
'
\0
'
)
{
ret =
NULL
;
}
else
{
ret =
s1;
while
(*
s1 &&
!
c_strchr (
s2, *
s1))
{
++
s1;
}
if
(*
s1)
{
*
s1++
=
'
\0
'
;
}
ptr =
s1;
}
return
ret;
}