]> pd.if.org Git - pdclib/commitdiff
Added some of <string.h>.
authorsolar <unknown>
Sat, 8 Jan 2005 07:06:23 +0000 (07:06 +0000)
committersolar <unknown>
Sat, 8 Jan 2005 07:06:23 +0000 (07:06 +0000)
15 files changed:
_PDCLIB_aux.h
functions/string/memchr.c [new file with mode: 0644]
functions/string/memcmp.c [new file with mode: 0644]
functions/string/memcpy.c [new file with mode: 0644]
functions/string/memmove.c [new file with mode: 0644]
functions/string/strcat.c [new file with mode: 0644]
functions/string/strchr.c [new file with mode: 0644]
functions/string/strcmp.c [new file with mode: 0644]
functions/string/strcoll.c [new file with mode: 0644]
functions/string/strcpy.c [new file with mode: 0644]
functions/string/strcspn.c [new file with mode: 0644]
functions/string/strncat.c [new file with mode: 0644]
functions/string/strncmp.c [new file with mode: 0644]
functions/string/strncpy.c [new file with mode: 0644]
functions/string/strxfrm.c [new file with mode: 0644]

index 86712eefd4ef1550ce8958277aaa925836c242a1..4ef019e9c386b89d30dd672fdf4aee551cc83303 100755 (executable)
 
 #ifndef __STDC_VERSION__
 #define _PDCLIB_C_VERSION 90
 
 #ifndef __STDC_VERSION__
 #define _PDCLIB_C_VERSION 90
+#define _PDCLIB_restrict
 #elif __STDC_VERSION__ == 199409L
 #define _PDCLIB_C_VERSION 95
 #elif __STDC_VERSION__ == 199409L
 #define _PDCLIB_C_VERSION 95
+#define _PDCLIB_restrict
 #elif __STDC_VERSION__ == 199901L
 #define _PDCLIB_C_VERSION 99
 #elif __STDC_VERSION__ == 199901L
 #define _PDCLIB_C_VERSION 99
+#define _PDCLIB_restrict restrict
 #else
 #error Unsupported _ _STDC_VERSION_ _ (__STDC_VERSION__) (supported: ISO/IEC 9899:1990, 9899/AMD1:1995, and 9899:1999).
 #endif
 
 #ifndef __STDC_HOSTED__
 #else
 #error Unsupported _ _STDC_VERSION_ _ (__STDC_VERSION__) (supported: ISO/IEC 9899:1990, 9899/AMD1:1995, and 9899:1999).
 #endif
 
 #ifndef __STDC_HOSTED__
-#error Compiler does not define _ _STDC_HOSTED_ _ (not standard-compliant)!
+#warning Compiler does not define _ _STDC_HOSTED_ _ (not standard-compliant)!
 #elif __STDC_HOSTED__ == 0
 #define _PDCLIB_HOSTED 0
 #elif __STDC_HOSTED__ == 1
 #elif __STDC_HOSTED__ == 0
 #define _PDCLIB_HOSTED 0
 #elif __STDC_HOSTED__ == 1
 #warning PDCLib might not be fully conforming to either C89 or C95 prior to v2.x.
 #endif
 
 #warning PDCLib might not be fully conforming to either C89 or C95 prior to v2.x.
 #endif
 
-#if _PDCLIB_HOSTED != 0
-#warning Up to the 1.x release, PDCLib is only complete as a freestanding environment.
-#warning PDCLib might not be fully conforming as a hosted environment.
-#endif
-
 /* -------------------------------------------------------------------------- */
 /* Helper macros:                                                             */
 /* _PDCLIB_cc( x, y ) concatenates two preprocessor tokens without extending  */
 /* -------------------------------------------------------------------------- */
 /* Helper macros:                                                             */
 /* _PDCLIB_cc( x, y ) concatenates two preprocessor tokens without extending  */
diff --git a/functions/string/memchr.c b/functions/string/memchr.c
new file mode 100644 (file)
index 0000000..4194a85
--- /dev/null
@@ -0,0 +1,25 @@
+/* $Id$ */
+
+/* Release $Name$ */
+
+/* memchr( const void *, int, size_t )
+
+   This file is part of the Public Domain C Library (PDCLib).
+   Permission is granted to use, modify, and / or redistribute at will.
+*/
+
+#include <string.h>
+
+void * memchr( const void * s, int c, size_t n )
+{
+    const unsigned char * p = (const unsigned char *) s;
+    while ( n-- )
+    {
+        if ( *p == (unsigned char) c )
+        {
+            return (void *) p;
+        }
+        ++p;
+    }
+    return NULL;
+}
diff --git a/functions/string/memcmp.c b/functions/string/memcmp.c
new file mode 100644 (file)
index 0000000..d873c07
--- /dev/null
@@ -0,0 +1,27 @@
+/* $Id$ */
+
+/* Release $Name$ */
+
+/* memcmp( const void *, const void *, size_t )
+
+   This file is part of the Public Domain C Library (PDCLib).
+   Permission is granted to use, modify, and / or redistribute at will.
+*/
+
+#include <string.h>
+
+int memcmp( const void * s1, const void * s2, size_t n )
+{
+    const unsigned char * p1 = (const unsigned char *) s1;
+    const unsigned char * p2 = (const unsigned char *) s2;
+    while ( n-- )
+    {
+        if ( *p1 != *p2 )
+        {
+            return *p2 - *p1;
+        }
+        ++p1;
+        ++p2;
+    }
+    return 0;
+}
diff --git a/functions/string/memcpy.c b/functions/string/memcpy.c
new file mode 100644 (file)
index 0000000..b483be5
--- /dev/null
@@ -0,0 +1,22 @@
+/* $Id$ */
+
+/* Release $Name$ */
+
+/* memcpy( void *, const void *, size_t )
+
+   This file is part of the Public Domain C Library (PDCLib).
+   Permission is granted to use, modify, and / or redistribute at will.
+*/
+
+#include <string.h>
+
+void * memcpy( void * _PDCLIB_restrict s1, const void * _PDCLIB_restrict s2, size_t n )
+{
+    char * dest = (char *) s1;
+    const char * src = (const char *) s2;
+    while ( n-- )
+    {
+        *dest++ = *src++;
+    }
+    return s1;
+}
diff --git a/functions/string/memmove.c b/functions/string/memmove.c
new file mode 100644 (file)
index 0000000..b46a15a
--- /dev/null
@@ -0,0 +1,34 @@
+/* $Id$ */
+
+/* Release $Name$ */
+
+/* memmove( void *, const void *, size_t )
+
+   This file is part of the Public Domain C Library (PDCLib).
+   Permission is granted to use, modify, and / or redistribute at will.
+*/
+
+#include <string.h>
+
+void * memmove( void * s1, const void * s2, size_t n )
+{
+    char * dest = (char *) s1;
+    const char * src = (const char *) s2;
+    if ( dest < src )
+    {
+        while ( n-- )
+        {
+            *dest++ = *src++;
+        }
+    }
+    else
+    {
+        src += n;
+        dest += n;
+        while ( n-- )
+        {
+            *dest-- = *src--;
+        }
+    }
+    return s1;
+}
diff --git a/functions/string/strcat.c b/functions/string/strcat.c
new file mode 100644 (file)
index 0000000..422565d
--- /dev/null
@@ -0,0 +1,22 @@
+/* $Id$ */
+
+/* Release $Name$ */
+
+/* strcat( char *, const char * )
+
+   This file is part of the Public Domain C Library (PDCLib).
+   Permission is granted to use, modify, and / or redistribute at will.
+*/
+
+#include <_PDCLIB_aux.h>
+
+char * strcat( char * _PDCLIB_restrict s1, const char * _PDCLIB_restrict s2 )
+{
+    char * rc = s1;
+    if ( *s1 )
+    {
+        while ( *++s1 );
+    }
+    while ( (*s1++ = *s2++) );
+    return rc;
+}
diff --git a/functions/string/strchr.c b/functions/string/strchr.c
new file mode 100644 (file)
index 0000000..ced1d55
--- /dev/null
@@ -0,0 +1,23 @@
+/* $Id$ */
+
+/* Release $Name$ */
+
+/* strchr( const char *, int )
+
+   This file is part of the Public Domain C Library (PDCLib).
+   Permission is granted to use, modify, and / or redistribute at will.
+*/
+
+#include <string.h>
+
+char * strchr( const char * s, int c )
+{
+    do
+    {
+        if ( *s == (char) c )
+        {
+            return (char *) s;
+        }
+    } while ( *s++ );
+    return NULL;
+}
diff --git a/functions/string/strcmp.c b/functions/string/strcmp.c
new file mode 100644 (file)
index 0000000..59cc1d8
--- /dev/null
@@ -0,0 +1,19 @@
+/* $Id$ */
+
+/* Release $Name$ */
+
+/* strcmp( const char *, const char * )
+
+   This file is part of the Public Domain C Library (PDCLib).
+   Permission is granted to use, modify, and / or redistribute at will.
+*/
+
+int strcmp( const char * s1, const char * s2 )
+{
+    while ( ( *s1 ) && ( *s1 == *s2 ) )
+    {
+        ++s1;
+        ++s2;
+    }
+    return ( *s1 - *s2 );
+}
diff --git a/functions/string/strcoll.c b/functions/string/strcoll.c
new file mode 100644 (file)
index 0000000..51fc59f
--- /dev/null
@@ -0,0 +1,18 @@
+/* $Id$ */
+
+/* Release $Name$ */
+
+/* strcoll( const char *, const char * )
+
+   This file is part of the Public Domain C Library (PDCLib).
+   Permission is granted to use, modify, and / or redistribute at will.
+*/
+
+/* TODO: Dummy function, PDCLib does not support locales yet. */
+
+int strcmp( const char * s1, const char * s2 );
+
+int strcoll( const char * s1, const char * s2 )
+{
+    return strcmp( s1, s2 );
+}
diff --git a/functions/string/strcpy.c b/functions/string/strcpy.c
new file mode 100644 (file)
index 0000000..18fc91e
--- /dev/null
@@ -0,0 +1,18 @@
+/* $Id$ */
+
+/* Release $Name$ */
+
+/* strcpy( char *, const char * )
+
+   This file is part of the Public Domain C Library (PDCLib).
+   Permission is granted to use, modify, and / or redistribute at will.
+*/
+
+#include <_PDCLIB_aux.h>
+
+char * strcpy( char * _PDCLIB_restrict s1, const char * _PDCLIB_restrict s2 )
+{
+    char * rc = s1;
+    while ( ( *s1++ = *s2++ ) );
+    return rc;
+}
diff --git a/functions/string/strcspn.c b/functions/string/strcspn.c
new file mode 100644 (file)
index 0000000..0417d51
--- /dev/null
@@ -0,0 +1,30 @@
+/* $Id$ */
+
+/* Release $Name$ */
+
+/* strcspn( const char *, const char * )
+
+   This file is part of the Public Domain C Library (PDCLib).
+   Permission is granted to use, modify, and / or redistribute at will.
+*/
+
+#include <string.h>
+
+size_t strcspn( const char * src_1, const char * src_2 )
+{
+    size_t len = 0;
+    const char * src_p;
+    while ( src_1[len] != '\0' )
+    {
+        src_p = src_2;
+        while ( *src_p != '\0' )
+        {
+            if ( src_1[len] == *src_p++ )
+            {
+                return len;
+            }
+        }
+        ++len;
+    }
+    return len;
+}
diff --git a/functions/string/strncat.c b/functions/string/strncat.c
new file mode 100644 (file)
index 0000000..ff9e389
--- /dev/null
@@ -0,0 +1,29 @@
+/* $Id$ */
+
+/* Release $Name$ */
+
+/* strncat( char *, const char *, size_t )
+
+   This file is part of the Public Domain C Library (PDCLib).
+   Permission is granted to use, modify, and / or redistribute at will.
+*/
+
+#include <string.h>
+
+char * strncat( char * _PDCLIB_restrict s1, const char * _PDCLIB_restrict s2, size_t n )
+{
+    char * rc = s1;
+    while ( *s1 )
+    {
+        ++s1;
+    }
+    while ( n && ( *s1++ = *s2++ ) )
+    {
+        --n;
+    }
+    if ( n == 0 )
+    {
+        *s1 = '\0';
+    }
+    return rc;
+}
diff --git a/functions/string/strncmp.c b/functions/string/strncmp.c
new file mode 100644 (file)
index 0000000..f64984e
--- /dev/null
@@ -0,0 +1,29 @@
+/* $Id$ */
+
+/* Release $Name$ */
+
+/* strncmp( const char *, const char *, size_t )
+
+   This file is part of the Public Domain C Library (PDCLib).
+   Permission is granted to use, modify, and / or redistribute at will.
+*/
+
+#include <string.h>
+
+int strncmp( const char * s1, const char * s2, size_t n )
+{
+    while ( n && ( *s1 == *s2 ) )
+    {
+        ++s1;
+        ++s2;
+        --n;
+    }
+    if ( ( n == 0 ) )
+    {
+        return 0;
+    }
+    else
+    {
+        return ( *s1 - *s2 );
+    }
+}
diff --git a/functions/string/strncpy.c b/functions/string/strncpy.c
new file mode 100644 (file)
index 0000000..614ca13
--- /dev/null
@@ -0,0 +1,28 @@
+/* $Id$ */
+
+/* Release $Name$ */
+
+/* strncpy( char *, const char *, size_t )
+
+   This file is part of the Public Domain C Library (PDCLib).
+   Permission is granted to use, modify, and / or redistribute at will.
+*/
+
+#include <string.h>
+
+char * strncpy( char * _PDCLIB_restrict s1, const char * _PDCLIB_restrict s2, size_t n )
+{
+    char * rc = s1;
+    while ( ( n > 0 ) && ( *s1++ = *s2++ ) )
+    {
+        /* Cannot do "n--" in the conditional as size_t is unsigned and we have
+           to check it again for >0 in the next loop.
+        */
+        --n;
+    }
+    while ( n-- )
+    {
+        *s1++ = '\0';
+    }
+    return rc;
+}
diff --git a/functions/string/strxfrm.c b/functions/string/strxfrm.c
new file mode 100644 (file)
index 0000000..60575cb
--- /dev/null
@@ -0,0 +1,26 @@
+/* $Id$ */
+
+/* Release $Name$ */
+
+/* strxfrm( char *, const char *, size_t )
+
+   This file is part of the Public Domain C Library (PDCLib).
+   Permission is granted to use, modify, and / or redistribute at will.
+*/
+
+#include <string.h>
+
+/* TODO: Dummy function, no locale support yet. */
+
+size_t strxfrm( char * _PDCLIB_restrict s1, const char * _PDCLIB_restrict s2, size_t n )
+{
+    size_t len = strlen( s2 );
+    if ( len < n )
+    {
+        /* Cannot use strncpy() here as the filling of s1 with '\0' is not part
+           of the spec.
+        */
+        while ( n-- && ( *s1++ = *s2++ ) );
+    }
+    return len;
+}