From 87c8120b9524762c45b3eccff4b09587db7a86fb Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Thu, 26 Jul 2012 14:17:56 +0200 Subject: [PATCH] Refactored libmaxsi/string.cpp into multiple files. However, parts libmaxsi/string.cpp remains as the kernel and parts of the standard library still rely on . --- libmaxsi/Makefile | 22 +++++++ libmaxsi/init.cpp | 6 +- libmaxsi/integer.cpp | 5 +- libmaxsi/signal.cpp | 1 - libmaxsi/stpcpy.cpp | 33 ++++++++++ libmaxsi/strcasecmp.cpp | 40 ++++++++++++ libmaxsi/strcat.cpp | 31 +++++++++ libmaxsi/strchr.cpp | 33 ++++++++++ libmaxsi/strchrnul.cpp | 31 +++++++++ libmaxsi/strcmp.cpp | 39 +++++++++++ libmaxsi/strcoll.cpp | 31 +++++++++ libmaxsi/strcpy.cpp | 34 ++++++++++ libmaxsi/strcspn.cpp | 44 +++++++++++++ libmaxsi/strdup.cpp | 36 ++++++++++ libmaxsi/string.cpp | 137 ++++----------------------------------- libmaxsi/strlen.cpp | 33 ++++++++++ libmaxsi/strncasecmp.cpp | 41 ++++++++++++ libmaxsi/strncat.cpp | 35 ++++++++++ libmaxsi/strncmp.cpp | 40 ++++++++++++ libmaxsi/strncpy.cpp | 35 ++++++++++ libmaxsi/strnlen.cpp | 33 ++++++++++ libmaxsi/strpbrk.cpp | 33 ++++++++++ libmaxsi/strrchr.cpp | 37 +++++++++++ libmaxsi/strspn.cpp | 44 +++++++++++++ libmaxsi/strstr.cpp | 45 +++++++++++++ libmaxsi/strtok.cpp | 31 +++++++++ libmaxsi/strtok_r.cpp | 43 ++++++++++++ libmaxsi/time.cpp | 1 - 28 files changed, 840 insertions(+), 134 deletions(-) create mode 100644 libmaxsi/stpcpy.cpp create mode 100644 libmaxsi/strcasecmp.cpp create mode 100644 libmaxsi/strcat.cpp create mode 100644 libmaxsi/strchr.cpp create mode 100644 libmaxsi/strchrnul.cpp create mode 100644 libmaxsi/strcmp.cpp create mode 100644 libmaxsi/strcoll.cpp create mode 100644 libmaxsi/strcpy.cpp create mode 100644 libmaxsi/strcspn.cpp create mode 100644 libmaxsi/strdup.cpp create mode 100644 libmaxsi/strlen.cpp create mode 100644 libmaxsi/strncasecmp.cpp create mode 100644 libmaxsi/strncat.cpp create mode 100644 libmaxsi/strncmp.cpp create mode 100644 libmaxsi/strncpy.cpp create mode 100644 libmaxsi/strnlen.cpp create mode 100644 libmaxsi/strpbrk.cpp create mode 100644 libmaxsi/strrchr.cpp create mode 100644 libmaxsi/strspn.cpp create mode 100644 libmaxsi/strstr.cpp create mode 100644 libmaxsi/strtok.cpp create mode 100644 libmaxsi/strtok_r.cpp diff --git a/libmaxsi/Makefile b/libmaxsi/Makefile index ea9a751a..4d1f404b 100644 --- a/libmaxsi/Makefile +++ b/libmaxsi/Makefile @@ -93,6 +93,28 @@ truncate.o \ umask.o \ unlink.o \ write.o \ +stpcpy.o \ +strcasecmp.o \ +strcat.o \ +strchr.o \ +strchrnul.o \ +strcmp.o \ +strcoll.o \ +strcpy.o \ +strcspn.o \ +strdup.o \ +strlen.o \ +strncasecmp.o \ +strncat.o \ +strncmp.o \ +strncpy.o \ +strnlen.o \ +strpbrk.o \ +strrchr.o \ +strspn.o \ +strstr.o \ +strtok.o \ +strtok_r.o \ UNPROCHEADERDIRS:=$(shell find include -type d) UNPROCHEADERS:=$(shell find include -type f) diff --git a/libmaxsi/init.cpp b/libmaxsi/init.cpp index 69d354b9..dd742aed 100644 --- a/libmaxsi/init.cpp +++ b/libmaxsi/init.cpp @@ -25,9 +25,9 @@ #include #include -#include #include #include +#include namespace Maxsi { @@ -40,9 +40,7 @@ namespace Maxsi extern "C" void initialize_standard_library(int argc, char* argv[]) { if ( argc ) - { - String::Copy(program_invocation_name, argv[0]); - } + strcpy(program_invocation_name, argv[0]); // Initialize stuff such as errno. init_error_functions(); diff --git a/libmaxsi/integer.cpp b/libmaxsi/integer.cpp index c1b3ad94..68682591 100644 --- a/libmaxsi/integer.cpp +++ b/libmaxsi/integer.cpp @@ -107,13 +107,10 @@ namespace Maxsi return ParseInteger(str, endptr, base); } -// TODO: This conflicts with libmaxsi/string.cpp:Maxsi::ToInt(). -#if 0 extern "C" int atoi(const char* str) { - return strtol(str, (char **) NULL, 10); + return (int) strtol(str, (char **) NULL, 10); } -#endif extern "C" long atol(const char* str) { diff --git a/libmaxsi/signal.cpp b/libmaxsi/signal.cpp index d86b37b9..5feb3841 100644 --- a/libmaxsi/signal.cpp +++ b/libmaxsi/signal.cpp @@ -23,7 +23,6 @@ *******************************************************************************/ #include -#include #include #include #include diff --git a/libmaxsi/stpcpy.cpp b/libmaxsi/stpcpy.cpp new file mode 100644 index 00000000..0c08ccc2 --- /dev/null +++ b/libmaxsi/stpcpy.cpp @@ -0,0 +1,33 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. + + This file is part of LibMaxsi. + + LibMaxsi is free software: you can redistribute it and/or modify it under + the terms of the GNU Lesser General Public License as published by the Free + Software Foundation, either version 3 of the License, or (at your option) + any later version. + + LibMaxsi is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + details. + + You should have received a copy of the GNU Lesser General Public License + along with LibMaxsi. If not, see . + + strcpy.cpp + Copy a string returning a pointer to its end. + +*******************************************************************************/ + +#include + +extern "C" char* stpcpy(char* dest, const char* src) +{ + while ( *src ) + *dest++ = *src++; + *dest = '\0'; + return dest; +} diff --git a/libmaxsi/strcasecmp.cpp b/libmaxsi/strcasecmp.cpp new file mode 100644 index 00000000..107584ed --- /dev/null +++ b/libmaxsi/strcasecmp.cpp @@ -0,0 +1,40 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. + + This file is part of LibMaxsi. + + LibMaxsi is free software: you can redistribute it and/or modify it under + the terms of the GNU Lesser General Public License as published by the Free + Software Foundation, either version 3 of the License, or (at your option) + any later version. + + LibMaxsi is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + details. + + You should have received a copy of the GNU Lesser General Public License + along with LibMaxsi. If not, see . + + strcasecmp.cpp + Compares two strings ignoring case. + +*******************************************************************************/ + +#include +#include + +extern "C" int strcasecmp(const char* a, const char* b) +{ + while ( true ) + { + char ac = tolower(*a++), bc = tolower(*b++); + if ( ac == '\0' && bc == '\0' ) + return 0; + if ( ac < bc ) + return -1; + if ( ac > bc ) + return 1; + } +} diff --git a/libmaxsi/strcat.cpp b/libmaxsi/strcat.cpp new file mode 100644 index 00000000..7ee906f9 --- /dev/null +++ b/libmaxsi/strcat.cpp @@ -0,0 +1,31 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. + + This file is part of LibMaxsi. + + LibMaxsi is free software: you can redistribute it and/or modify it under + the terms of the GNU Lesser General Public License as published by the Free + Software Foundation, either version 3 of the License, or (at your option) + any later version. + + LibMaxsi is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + details. + + You should have received a copy of the GNU Lesser General Public License + along with LibMaxsi. If not, see . + + strcat.cpp + Appends a string onto another string. + +*******************************************************************************/ + +#include + +extern "C" char* strcat(char* dest, const char* src) +{ + strcpy(dest + strlen(dest), src); + return dest; +} diff --git a/libmaxsi/strchr.cpp b/libmaxsi/strchr.cpp new file mode 100644 index 00000000..a82da570 --- /dev/null +++ b/libmaxsi/strchr.cpp @@ -0,0 +1,33 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. + + This file is part of LibMaxsi. + + LibMaxsi is free software: you can redistribute it and/or modify it under + the terms of the GNU Lesser General Public License as published by the Free + Software Foundation, either version 3 of the License, or (at your option) + any later version. + + LibMaxsi is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + details. + + You should have received a copy of the GNU Lesser General Public License + along with LibMaxsi. If not, see . + + strchr.cpp + Searches a string for a specific character. + +*******************************************************************************/ + +#include + +extern "C" char* strchr(const char* str, int c) +{ + char* ret = strchrnul(str, c); + if ( !*ret ) + return NULL; + return ret; +} diff --git a/libmaxsi/strchrnul.cpp b/libmaxsi/strchrnul.cpp new file mode 100644 index 00000000..b2120b9d --- /dev/null +++ b/libmaxsi/strchrnul.cpp @@ -0,0 +1,31 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. + + This file is part of LibMaxsi. + + LibMaxsi is free software: you can redistribute it and/or modify it under + the terms of the GNU Lesser General Public License as published by the Free + Software Foundation, either version 3 of the License, or (at your option) + any later version. + + LibMaxsi is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + details. + + You should have received a copy of the GNU Lesser General Public License + along with LibMaxsi. If not, see . + + strchrnul.cpp + Searches a string for a specific character. + +*******************************************************************************/ + +#include + +extern "C" char* strchrnul(const char* str, int c) +{ + for ( ; *str && *str != c; str++ ); + return (char*) str; +} diff --git a/libmaxsi/strcmp.cpp b/libmaxsi/strcmp.cpp new file mode 100644 index 00000000..6b3912a6 --- /dev/null +++ b/libmaxsi/strcmp.cpp @@ -0,0 +1,39 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. + + This file is part of LibMaxsi. + + LibMaxsi is free software: you can redistribute it and/or modify it under + the terms of the GNU Lesser General Public License as published by the Free + Software Foundation, either version 3 of the License, or (at your option) + any later version. + + LibMaxsi is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + details. + + You should have received a copy of the GNU Lesser General Public License + along with LibMaxsi. If not, see . + + strcmp.cpp + Compares two strings. + +*******************************************************************************/ + +#include + +extern "C" int strcmp(const char* a, const char* b) +{ + while ( true ) + { + char ac = *a++, bc = *b++; + if ( ac == '\0' && bc == '\0' ) + return 0; + if ( ac < bc ) + return -1; + if ( ac > bc ) + return 1; + } +} diff --git a/libmaxsi/strcoll.cpp b/libmaxsi/strcoll.cpp new file mode 100644 index 00000000..dc0f1d6f --- /dev/null +++ b/libmaxsi/strcoll.cpp @@ -0,0 +1,31 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. + + This file is part of LibMaxsi. + + LibMaxsi is free software: you can redistribute it and/or modify it under + the terms of the GNU Lesser General Public License as published by the Free + Software Foundation, either version 3 of the License, or (at your option) + any later version. + + LibMaxsi is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + details. + + You should have received a copy of the GNU Lesser General Public License + along with LibMaxsi. If not, see . + + strcoll.cpp + Compare two strings using the current locale. + +*******************************************************************************/ + +#include + +extern "C" int strcoll(const char* s1, const char* s2) +{ + // TODO: Pay attention to locales. + return strcmp(s1, s2); +} diff --git a/libmaxsi/strcpy.cpp b/libmaxsi/strcpy.cpp new file mode 100644 index 00000000..0e45d8a3 --- /dev/null +++ b/libmaxsi/strcpy.cpp @@ -0,0 +1,34 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. + + This file is part of LibMaxsi. + + LibMaxsi is free software: you can redistribute it and/or modify it under + the terms of the GNU Lesser General Public License as published by the Free + Software Foundation, either version 3 of the License, or (at your option) + any later version. + + LibMaxsi is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + details. + + You should have received a copy of the GNU Lesser General Public License + along with LibMaxsi. If not, see . + + strcpy.cpp + Copies a string and returns dest. + +*******************************************************************************/ + +#include + +extern "C" char* strcpy(char* dest, const char* src) +{ + char* origdest = dest; + while ( *src ) + *dest++ = *src++; + *dest = '\0'; + return origdest; +} diff --git a/libmaxsi/strcspn.cpp b/libmaxsi/strcspn.cpp new file mode 100644 index 00000000..75896fc6 --- /dev/null +++ b/libmaxsi/strcspn.cpp @@ -0,0 +1,44 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. + + This file is part of LibMaxsi. + + LibMaxsi is free software: you can redistribute it and/or modify it under + the terms of the GNU Lesser General Public License as published by the Free + Software Foundation, either version 3 of the License, or (at your option) + any later version. + + LibMaxsi is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + details. + + You should have received a copy of the GNU Lesser General Public License + along with LibMaxsi. If not, see . + + strcspn.cpp + Search a string for a set of characters. + +*******************************************************************************/ + +#include + +extern "C" size_t strcspn(const char* str, const char* reject) +{ + size_t rejectlen = 0; + while ( reject[rejectlen] ) { rejectlen++; } + for ( size_t result = 0; true; result++ ) + { + char c = str[result]; + if ( !c ) { return result; } + bool matches = false; + for ( size_t i = 0; i < rejectlen; i++ ) + { + if ( str[result] != reject[i] ) { continue; } + matches = true; + break; + } + if ( matches ) { return result; } + } +} diff --git a/libmaxsi/strdup.cpp b/libmaxsi/strdup.cpp new file mode 100644 index 00000000..c964e277 --- /dev/null +++ b/libmaxsi/strdup.cpp @@ -0,0 +1,36 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. + + This file is part of LibMaxsi. + + LibMaxsi is free software: you can redistribute it and/or modify it under + the terms of the GNU Lesser General Public License as published by the Free + Software Foundation, either version 3 of the License, or (at your option) + any later version. + + LibMaxsi is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + details. + + You should have received a copy of the GNU Lesser General Public License + along with LibMaxsi. If not, see . + + strdup.cpp + Creates a copy of a string. + +*******************************************************************************/ + +#include +#include + +extern "C" char* strdup(const char* input) +{ + size_t inputsize = strlen(input); + char* result = (char*) malloc(inputsize + 1); + if ( !result ) + return NULL; + memcpy(result, input, inputsize + 1); + return result; +} diff --git a/libmaxsi/string.cpp b/libmaxsi/string.cpp index 309c5129..04fbafa4 100644 --- a/libmaxsi/string.cpp +++ b/libmaxsi/string.cpp @@ -33,7 +33,7 @@ namespace Maxsi { namespace String { - DUAL_FUNCTION(size_t, strlen, Length, (const char* String)) + size_t Length(const char* String) { size_t Result = 0; @@ -45,14 +45,7 @@ namespace Maxsi return Result; } - extern "C" size_t strnlen(const char* str, size_t maxlen) - { - size_t result; - for ( result = 0; maxlen - result && str[result]; result++ ) { } - return result; - } - - DUAL_FUNCTION(char*, strcpy, Copy, (char* Dest, const char* Src)) + char* Copy(char* Dest, const char* Src) { char* OriginalDest = Dest; @@ -67,15 +60,7 @@ namespace Maxsi return OriginalDest; } - extern "C" char* strncpy(char* dest, const char* src, size_t len) - { - size_t i; - for ( i = 0; i < len && src[i]; i++ ) { dest[i] = src[i]; } - for ( ; i < len; i++ ) { dest[i] = '\0'; } - return dest; - } - - DUAL_FUNCTION(char*, strcat, Cat, (char* Dest, const char* Src)) + char* Cat(char* Dest, const char* Src) { char* OriginalDest = Dest; @@ -92,20 +77,7 @@ namespace Maxsi return OriginalDest; } - extern "C" char* strncat(char* dest, const char* src, size_t len) - { - size_t destlen = Length(dest); - size_t i; - - for ( i = 0; i < len && src[i]; i++ ) - { - dest[destlen + i] = src[i]; - } - dest[destlen+i] = 0; - return dest; - } - - DUAL_FUNCTION(int, strcmp, Compare, (const char* A, const char* B)) + int Compare(const char* A, const char* B) { while ( true ) { @@ -116,7 +88,7 @@ namespace Maxsi } } - DUAL_FUNCTION(int, strncmp, CompareN, (const char* A, const char* B, size_t MaxLength)) + int CompareN(const char* A, const char* B, size_t MaxLength) { while ( MaxLength-- ) { @@ -129,39 +101,7 @@ namespace Maxsi return 0; } -#ifndef SORTIX_KERNEL - inline int charcasecmp(char a, char b) - { - return tolower(a) == tolower(b); - } - - extern "C" int strcasecmp(const char* a, const char* b) - { - while ( true ) - { - char achar = *a++; - char bchar = *b++; - if ( !achar && !bchar ) { return 0; } - int cmp = charcasecmp(achar, bchar); - if ( cmp ) { return cmp; } - } - } - - extern "C" int strncasecmp(const char* a, const char* b, size_t n) - { - while ( n-- ) - { - char achar = *a++; - char bchar = *b++; - if ( !achar && !bchar ) { return 0; } - int cmp = charcasecmp(achar, bchar); - if ( cmp ) { return cmp; } - } - return 0; - } -#endif - - DUAL_FUNCTION(size_t, strspn, Accept, (const char* str, const char* accept)) + size_t Accept(const char* str, const char* accept) { size_t acceptlen = 0; while ( accept[acceptlen] ) { acceptlen++; } @@ -180,7 +120,7 @@ namespace Maxsi } } - DUAL_FUNCTION(size_t, strcspn, Reject, (const char* str, const char* reject)) + size_t Reject(const char* str, const char* reject) { size_t rejectlen = 0; while ( reject[rejectlen] ) { rejectlen++; } @@ -199,14 +139,7 @@ namespace Maxsi } } - extern "C" char* strpbrk(const char* str, const char* accept) - { - size_t rejectlen = Reject(str, accept); - if ( !str[rejectlen] ) { return NULL; } - return (char*) str + rejectlen; - } - - DUAL_FUNCTION(char*, strtok_r, TokenizeR, (char* str, const char* delim, char** saveptr)) + char* TokenizeR(char* str, const char* delim, char** saveptr) { if ( !str && !*saveptr ) { return NULL; } if ( !str ) { str = *saveptr; } @@ -219,13 +152,13 @@ namespace Maxsi return str; } - DUAL_FUNCTION(char*, strtok, TokenizeR, (char* str, const char* delim)) + char* Tokenize(char* str, const char* delim) { static char* lasttokensaveptr = NULL; return TokenizeR(str, delim, &lasttokensaveptr); } - DUAL_FUNCTION(char*, strchrnul, SeekNul, (const char* str, int c)) + char* SeekNul(const char* str, int c) { while ( *str ) { @@ -235,7 +168,7 @@ namespace Maxsi return (char*) str; } - DUAL_FUNCTION(char*, strrchr, SeekReverse, (const char* str, int c)) + char* SeekReverse(const char* str, int c) { const char* last = NULL; while ( *str ) @@ -246,7 +179,7 @@ namespace Maxsi return (char*) last; } - DUAL_FUNCTION(char*, strchr, Seek, (const char* str, int c)) + char* Seek(const char* str, int c) { char* result = SeekNul(str, c); if ( !*result ) { return NULL; } @@ -277,7 +210,7 @@ namespace Maxsi return dest; } - DUAL_FUNCTION(int, atoi, ToInt, (const char* str)) + int ToInt(const char* str) { bool negative = ( *str == '-' ); if ( negative ) { str++; } @@ -291,49 +224,6 @@ namespace Maxsi return (negative) ? -result : result; } - extern "C" int strcoll(const char* s1, const char* s2) - { - // TODO: Pay attention to locales. - return Compare(s1, s2); - } - - extern "C" char* stpcpy(char* s1, const char* s2) - { - char* result = Copy(s1, s2); - result += Length(s1); - return result; - } - - // TODO: This simple and hacky implementation runs in O(N^2) even though - // this problem can be solved in O(N). - extern "C" char* strstr(const char* haystack, const char* needle) - { - if ( !needle[0] ) { return (char*) haystack; } - for ( size_t i = 0; haystack[i]; i++ ) - { - bool diff = false; - for ( size_t j = 0; needle[j]; j++ ) - { - if ( haystack[i+j] != needle[j] ) { diff = true; break; } - } - if ( diff ) { continue; } - return (char*) haystack + i; - } - return NULL; - } - -#ifndef SORTIX_KERNEL - extern "C" char* strdup(const char* input) - { - size_t inputsize = strlen(input); - char* result = (char*) malloc(inputsize + 1); - if ( result == NULL ) { return NULL; } - memcpy(result, input, inputsize + 1); - return result; - } -#endif - -#if 1 char* Combine(size_t NumParameters, ...) { va_list param_pt; @@ -376,6 +266,5 @@ namespace Maxsi return Result; } -#endif } } diff --git a/libmaxsi/strlen.cpp b/libmaxsi/strlen.cpp new file mode 100644 index 00000000..42791050 --- /dev/null +++ b/libmaxsi/strlen.cpp @@ -0,0 +1,33 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. + + This file is part of LibMaxsi. + + LibMaxsi is free software: you can redistribute it and/or modify it under + the terms of the GNU Lesser General Public License as published by the Free + Software Foundation, either version 3 of the License, or (at your option) + any later version. + + LibMaxsi is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + details. + + You should have received a copy of the GNU Lesser General Public License + along with LibMaxsi. If not, see . + + strlen.cpp + Returns the length of a string. + +*******************************************************************************/ + +#include + +extern "C" size_t strlen(const char* str) +{ + size_t ret = 0; + while ( str[ret] ) + ret++; + return ret; +} diff --git a/libmaxsi/strncasecmp.cpp b/libmaxsi/strncasecmp.cpp new file mode 100644 index 00000000..3073f9f9 --- /dev/null +++ b/libmaxsi/strncasecmp.cpp @@ -0,0 +1,41 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. + + This file is part of LibMaxsi. + + LibMaxsi is free software: you can redistribute it and/or modify it under + the terms of the GNU Lesser General Public License as published by the Free + Software Foundation, either version 3 of the License, or (at your option) + any later version. + + LibMaxsi is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + details. + + You should have received a copy of the GNU Lesser General Public License + along with LibMaxsi. If not, see . + + strncasecmp.cpp + Compares a prefix of two strings ignoring case. + +*******************************************************************************/ + +#include +#include + +extern "C" int strncasecmp(const char* a, const char* b, size_t maxcount) +{ + while ( --maxcount ) + { + char ac = tolower(*a++), bc = tolower(*b++); + if ( ac == '\0' && bc == '\0' ) + return 0; + if ( ac < bc ) + return -1; + if ( ac > bc ) + return 1; + } + return 0; +} diff --git a/libmaxsi/strncat.cpp b/libmaxsi/strncat.cpp new file mode 100644 index 00000000..d649004e --- /dev/null +++ b/libmaxsi/strncat.cpp @@ -0,0 +1,35 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. + + This file is part of LibMaxsi. + + LibMaxsi is free software: you can redistribute it and/or modify it under + the terms of the GNU Lesser General Public License as published by the Free + Software Foundation, either version 3 of the License, or (at your option) + any later version. + + LibMaxsi is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + details. + + You should have received a copy of the GNU Lesser General Public License + along with LibMaxsi. If not, see . + + strncat.cpp + Appends parts of a string onto another string. + +*******************************************************************************/ + +#include + +extern "C" char* strncat(char* dest, const char* src, size_t n) +{ + size_t dest_len = strlen(dest); + size_t i; + for ( i = 0; i < n && src[i] != '\0'; i++ ) + dest[dest_len + i] = src[i]; + dest[dest_len + i] = '\0'; + return dest; +} diff --git a/libmaxsi/strncmp.cpp b/libmaxsi/strncmp.cpp new file mode 100644 index 00000000..120edcc6 --- /dev/null +++ b/libmaxsi/strncmp.cpp @@ -0,0 +1,40 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. + + This file is part of LibMaxsi. + + LibMaxsi is free software: you can redistribute it and/or modify it under + the terms of the GNU Lesser General Public License as published by the Free + Software Foundation, either version 3 of the License, or (at your option) + any later version. + + LibMaxsi is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + details. + + You should have received a copy of the GNU Lesser General Public License + along with LibMaxsi. If not, see . + + strncmp.cpp + Compares a prefix of two strings. + +*******************************************************************************/ + +#include + +extern "C" int strncmp(const char* a, const char* b, size_t maxcount) +{ + while ( --maxcount ) + { + char ac = *a++, bc = *b++; + if ( ac == '\0' && bc == '\0' ) + return 0; + if ( ac < bc ) + return -1; + if ( ac > bc ) + return 1; + } + return 0; +} diff --git a/libmaxsi/strncpy.cpp b/libmaxsi/strncpy.cpp new file mode 100644 index 00000000..4a0ad625 --- /dev/null +++ b/libmaxsi/strncpy.cpp @@ -0,0 +1,35 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. + + This file is part of LibMaxsi. + + LibMaxsi is free software: you can redistribute it and/or modify it under + the terms of the GNU Lesser General Public License as published by the Free + Software Foundation, either version 3 of the License, or (at your option) + any later version. + + LibMaxsi is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + details. + + You should have received a copy of the GNU Lesser General Public License + along with LibMaxsi. If not, see . + + strncpy.cpp + Copies a string into a fixed size buffer and returns dest. + +*******************************************************************************/ + +#include + +extern "C" char* strncpy(char* dest, const char* src, size_t n) +{ + size_t i; + for ( i = 0; i < n && src[i] != '\0'; i++ ) + dest[i] = src[i]; + for ( ; i < n; i++ ) + dest[i] = '\0'; + return dest; +} diff --git a/libmaxsi/strnlen.cpp b/libmaxsi/strnlen.cpp new file mode 100644 index 00000000..932ea6bf --- /dev/null +++ b/libmaxsi/strnlen.cpp @@ -0,0 +1,33 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. + + This file is part of LibMaxsi. + + LibMaxsi is free software: you can redistribute it and/or modify it under + the terms of the GNU Lesser General Public License as published by the Free + Software Foundation, either version 3 of the License, or (at your option) + any later version. + + LibMaxsi is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + details. + + You should have received a copy of the GNU Lesser General Public License + along with LibMaxsi. If not, see . + + strnlen.cpp + Returns the length of a fixed length string. + +*******************************************************************************/ + +#include + +extern "C" size_t strnlen(const char* str, size_t maxlen) +{ + size_t ret = 0; + while ( ret < maxlen && str[ret] ) + ret++; + return ret; +} diff --git a/libmaxsi/strpbrk.cpp b/libmaxsi/strpbrk.cpp new file mode 100644 index 00000000..746ee0ec --- /dev/null +++ b/libmaxsi/strpbrk.cpp @@ -0,0 +1,33 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. + + This file is part of LibMaxsi. + + LibMaxsi is free software: you can redistribute it and/or modify it under + the terms of the GNU Lesser General Public License as published by the Free + Software Foundation, either version 3 of the License, or (at your option) + any later version. + + LibMaxsi is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + details. + + You should have received a copy of the GNU Lesser General Public License + along with LibMaxsi. If not, see . + + strcspn.cpp + Search a string for any of a set of characters. + +*******************************************************************************/ + +#include + +extern "C" char* strpbrk(const char* str, const char* accept) +{ + size_t rejectlen = strcspn(str, accept); + if ( !str[rejectlen] ) + return NULL; + return (char*) str + rejectlen; +} diff --git a/libmaxsi/strrchr.cpp b/libmaxsi/strrchr.cpp new file mode 100644 index 00000000..62026918 --- /dev/null +++ b/libmaxsi/strrchr.cpp @@ -0,0 +1,37 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. + + This file is part of LibMaxsi. + + LibMaxsi is free software: you can redistribute it and/or modify it under + the terms of the GNU Lesser General Public License as published by the Free + Software Foundation, either version 3 of the License, or (at your option) + any later version. + + LibMaxsi is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + details. + + You should have received a copy of the GNU Lesser General Public License + along with LibMaxsi. If not, see . + + strrchr.cpp + Searches a string for a specific character. + +*******************************************************************************/ + +#include + +extern "C" char* strrchr(const char* str, int c) +{ + const char* last = NULL; + while ( *str ) + { + if ( *str == c ) + last = str; + str++; + } + return (char*) last; +} diff --git a/libmaxsi/strspn.cpp b/libmaxsi/strspn.cpp new file mode 100644 index 00000000..2f1a6228 --- /dev/null +++ b/libmaxsi/strspn.cpp @@ -0,0 +1,44 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. + + This file is part of LibMaxsi. + + LibMaxsi is free software: you can redistribute it and/or modify it under + the terms of the GNU Lesser General Public License as published by the Free + Software Foundation, either version 3 of the License, or (at your option) + any later version. + + LibMaxsi is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + details. + + You should have received a copy of the GNU Lesser General Public License + along with LibMaxsi. If not, see . + + strspn.cpp + Search a string for a set of characters. + +*******************************************************************************/ + +#include + +extern "C" size_t strspn(const char* str, const char* accept) +{ + size_t acceptlen = 0; + while ( accept[acceptlen] ) { acceptlen++; } + for ( size_t result = 0; true; result++ ) + { + char c = str[result]; + if ( !c ) { return result; } + bool matches = false; + for ( size_t i = 0; i < acceptlen; i++ ) + { + if ( str[result] != accept[i] ) { continue; } + matches = true; + break; + } + if ( !matches ) { return result; } + } +} diff --git a/libmaxsi/strstr.cpp b/libmaxsi/strstr.cpp new file mode 100644 index 00000000..9a43f5c3 --- /dev/null +++ b/libmaxsi/strstr.cpp @@ -0,0 +1,45 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. + + This file is part of LibMaxsi. + + LibMaxsi is free software: you can redistribute it and/or modify it under + the terms of the GNU Lesser General Public License as published by the Free + Software Foundation, either version 3 of the License, or (at your option) + any later version. + + LibMaxsi is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + details. + + You should have received a copy of the GNU Lesser General Public License + along with LibMaxsi. If not, see . + + strstr.cpp + Locate a substring. + +*******************************************************************************/ + +#include + +// TODO: This simple and hacky implementation runs in O(N^2) even though this +// problem can be solved in O(N). +extern "C" char* strstr(const char* haystack, const char* needle) +{ + if ( !needle[0] ) + return (char*) haystack; + for ( size_t i = 0; haystack[i]; i++ ) + { + bool diff = false; + for ( size_t j = 0; needle[j]; j++ ) + { + if ( haystack[i+j] != needle[j] ) { diff = true; break; } + } + if ( diff ) + continue; + return (char*) haystack + i; + } + return NULL; +} diff --git a/libmaxsi/strtok.cpp b/libmaxsi/strtok.cpp new file mode 100644 index 00000000..cda76547 --- /dev/null +++ b/libmaxsi/strtok.cpp @@ -0,0 +1,31 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. + + This file is part of LibMaxsi. + + LibMaxsi is free software: you can redistribute it and/or modify it under + the terms of the GNU Lesser General Public License as published by the Free + Software Foundation, either version 3 of the License, or (at your option) + any later version. + + LibMaxsi is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + details. + + You should have received a copy of the GNU Lesser General Public License + along with LibMaxsi. If not, see . + + strtok.cpp + Extract tokens from strings. + +*******************************************************************************/ + +#include + +extern "C" char* strtok(char* str, const char* delim) +{ + static char* lasttokensaveptr = NULL; + return strtok_r(str, delim, &lasttokensaveptr); +} diff --git a/libmaxsi/strtok_r.cpp b/libmaxsi/strtok_r.cpp new file mode 100644 index 00000000..c64e775d --- /dev/null +++ b/libmaxsi/strtok_r.cpp @@ -0,0 +1,43 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. + + This file is part of LibMaxsi. + + LibMaxsi is free software: you can redistribute it and/or modify it under + the terms of the GNU Lesser General Public License as published by the Free + Software Foundation, either version 3 of the License, or (at your option) + any later version. + + LibMaxsi is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + details. + + You should have received a copy of the GNU Lesser General Public License + along with LibMaxsi. If not, see . + + strtok_r.cpp + Extract tokens from strings. + +*******************************************************************************/ + +#include + +extern "C" char* strtok_r(char* str, const char* delim, char** saveptr) +{ + if ( !str && !*saveptr ) + return NULL; + if ( !str ) + str = *saveptr; + str += strspn(str, delim); // Skip leading + if ( !*str ) + return *saveptr = NULL; + size_t amount = strcspn(str, delim); + if ( str[amount] ) + *saveptr = str + amount + 1; + else + *saveptr = NULL; + str[amount] = '\0'; + return str; +} diff --git a/libmaxsi/time.cpp b/libmaxsi/time.cpp index dbc098ba..94e82b0f 100644 --- a/libmaxsi/time.cpp +++ b/libmaxsi/time.cpp @@ -23,7 +23,6 @@ *******************************************************************************/ #include -#include #include #include #include