Move remainder of Maxsi::String into kernel tree.

This commit is contained in:
Jonas 'Sortie' Termansen 2012-09-23 13:27:42 +02:00
parent f3988b92f3
commit c0fabc2e8d
24 changed files with 150 additions and 362 deletions

View File

@ -88,7 +88,6 @@ strcoll.o \
strcpy.o \
strcspn.o \
strdup.o \
string.o \
strlen.o \
strncasecmp.o \
strncat.o \

View File

@ -23,7 +23,6 @@
******************************************************************************/
#include <libmaxsi/platform.h>
#include <libmaxsi/string.h>
#include <stdio.h>
#include <string.h>

View File

@ -1,40 +0,0 @@
/******************************************************************************
COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011.
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 <http://www.gnu.org/licenses/>.
string.h
Useful functions for manipulating strings.
******************************************************************************/
#ifndef LIBMAXSI_STRING_H
#define LIBMAXSI_STRING_H
namespace Maxsi
{
namespace String
{
char* Clone(const char* Source);
char* Combine(size_t NumParameters, ...);
char* Substring(const char* src, size_t offset, size_t length);
bool StartsWith(const char* Haystack, const char* Needle);
}
}
#endif

View File

@ -1,269 +0,0 @@
/******************************************************************************
COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011.
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 <http://www.gnu.org/licenses/>.
string.cpp
Useful functions for manipulating strings.
******************************************************************************/
#include <libmaxsi/platform.h>
#include <libmaxsi/string.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
namespace Maxsi
{
namespace String
{
size_t Length(const char* String)
{
size_t Result = 0;
while ( String[Result] != '\0' )
{
Result++;
}
return Result;
}
char* Copy(char* Dest, const char* Src)
{
char* OriginalDest = Dest;
while ( *Src != '\0' )
{
*Dest = *Src;
Dest++; Src++;
}
*Dest = '\0';
return OriginalDest;
}
char* Cat(char* Dest, const char* Src)
{
char* OriginalDest = Dest;
while ( *Dest != '\0' ) { Dest++; }
while ( *Src != '\0' )
{
*Dest = *Src;
Dest++; Src++;
}
*Dest = '\0';
return OriginalDest;
}
int Compare(const char* A, const char* B)
{
while ( true )
{
if ( *A == '\0' && *B == '\0' ) { return 0; }
if ( *A < *B ) { return -1; }
if ( *A > *B ) { return 1; }
A++; B++;
}
}
int CompareN(const char* A, const char* B, size_t MaxLength)
{
while ( MaxLength-- )
{
if ( *A == '\0' && *B == '\0' ) { return 0; }
if ( *A < *B ) { return -1; }
if ( *A > *B ) { return 1; }
A++; B++;
}
return 0;
}
size_t Accept(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; }
}
}
size_t Reject(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; }
}
}
char* TokenizeR(char* str, const char* delim, char** saveptr)
{
if ( !str && !*saveptr ) { return NULL; }
if ( !str ) { str = *saveptr; }
str += Accept(str, delim); // Skip leading
if ( !*str ) { return NULL; }
size_t amount = Reject(str, delim);
if ( str[amount] ) { *saveptr = str + amount + 1; }
else { *saveptr = NULL; }
str[amount] = '\0';
return str;
}
char* Tokenize(char* str, const char* delim)
{
static char* lasttokensaveptr = NULL;
return TokenizeR(str, delim, &lasttokensaveptr);
}
char* SeekNul(const char* str, int c)
{
while ( *str )
{
if ( *str == c ) { return (char*) str; }
str++;
}
return (char*) str;
}
char* SeekReverse(const char* str, int c)
{
const char* last = NULL;
while ( *str )
{
if ( *str == c ) { last = str; }
str++;
}
return (char*) last;
}
char* Seek(const char* str, int c)
{
char* result = SeekNul(str, c);
if ( !*result ) { return NULL; }
return result;
}
bool StartsWith(const char* haystack, const char* needle)
{
return CompareN(haystack, needle, Length(needle)) == 0;
}
char* Clone(const char* Input)
{
size_t InputSize = Length(Input);
char* Result = new char[InputSize + 1];
if ( Result == NULL ) { return NULL; }
memcpy(Result, Input, InputSize + 1);
return Result;
}
char* Substring(const char* src, size_t offset, size_t length)
{
size_t srclen = Length(src);
char* dest = new char[length + 1];
if ( !dest ) { return NULL; }
memcpy(dest, src + offset, length * sizeof(char));
dest[length] = 0;
return dest;
}
int ToInt(const char* str)
{
bool negative = ( *str == '-' );
if ( negative ) { str++; }
int result = 0;
int c;
while ( (c = *str++) != 0 )
{
if ( c < '0' || '9' < c ) { return result; }
result = result * 10 + c - '0';
}
return (negative) ? -result : result;
}
char* Combine(size_t NumParameters, ...)
{
va_list param_pt;
va_start(param_pt, NumParameters);
// First calculate the string length.
size_t ResultLength = 0;
const char* TMP = 0;
for ( size_t I = 0; I < NumParameters; I++ )
{
TMP = va_arg(param_pt, const char*);
if ( TMP != NULL ) { ResultLength += Length(TMP); }
}
// Allocate a string with the desired length.
char* Result = new char[ResultLength+1];
if ( !Result ) { return NULL; }
Result[0] = 0;
va_end(param_pt);
va_start(param_pt, NumParameters);
size_t ResultOffset = 0;
for ( size_t I = 0; I < NumParameters; I++ )
{
TMP = va_arg(param_pt, const char*);
if ( TMP )
{
size_t TMPLength = Length(TMP);
Copy(Result + ResultOffset, TMP);
ResultOffset += TMPLength;
}
}
return Result;
}
}
}

View File

@ -86,6 +86,7 @@ HEADERS:=$(shell find include -type f)
OBJS=$(CPUOBJS) \
kernel.o \
crc32.o \
string.o \
interrupt.o \
worker.o \
time.o \

View File

@ -28,8 +28,8 @@
#include <sortix/kernel/video.h>
#include <sortix/kernel/memorymanagement.h>
#include <sortix/kernel/pci.h>
#include <sortix/kernel/string.h>
#include <sortix/mman.h>
#include <libmaxsi/string.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
@ -39,8 +39,6 @@
#include "cpu.h"
#include "bga.h"
using namespace Maxsi;
namespace Sortix {
namespace BGA {

View File

@ -23,7 +23,7 @@
******************************************************************************/
#include <sortix/kernel/platform.h>
#include <libmaxsi/string.h>
#include <sortix/kernel/string.h>
#include <assert.h>
#include <errno.h>
#include <string.h>
@ -34,8 +34,6 @@
#include "filesystem.h"
#include "mount.h"
using namespace Maxsi;
namespace Sortix
{
namespace Directory

View File

@ -23,7 +23,6 @@
******************************************************************************/
#include <sortix/kernel/platform.h>
#include <libmaxsi/string.h>
#include <errno.h>
#include <string.h>
#include "syscall.h"
@ -35,8 +34,6 @@
#include <sortix/fcntl.h>
#include <sortix/unistd.h>
using namespace Maxsi;
namespace Sortix
{
namespace FileSystem

View File

@ -23,7 +23,7 @@
******************************************************************************/
#include <sortix/kernel/platform.h>
#include <libmaxsi/string.h>
#include <sortix/kernel/string.h>
#include <assert.h>
#include <errno.h>
#include <string.h>
@ -36,8 +36,6 @@
#include "devfs.h"
#include "videofs.h"
using namespace Maxsi;
namespace Sortix
{
class DevATA : public DevBuffer

View File

@ -24,7 +24,7 @@
#include <sortix/kernel/platform.h>
#include <sortix/kernel/kthread.h>
#include <libmaxsi/string.h>
#include <sortix/kernel/string.h>
#include <errno.h>
#include <string.h>
#include "../filesystem.h"
@ -33,8 +33,6 @@
#include "initfs.h"
#include "../initrd.h"
using namespace Maxsi;
namespace Sortix
{
class DevInitFSFile : public DevBuffer

View File

@ -23,7 +23,7 @@
******************************************************************************/
#include <sortix/kernel/platform.h>
#include <libmaxsi/string.h>
#include <sortix/kernel/string.h>
#include <assert.h>
#include <errno.h>
#include <string.h>
@ -32,8 +32,6 @@
#include "../stream.h"
#include "ramfs.h"
using namespace Maxsi;
namespace Sortix
{
class DevRAMFSFile : public DevBuffer

View File

@ -23,13 +23,10 @@
*******************************************************************************/
#include <sortix/kernel/platform.h>
#include <libmaxsi/string.h>
#include <errno.h>
#include <string.h>
#include "util.h"
using namespace Maxsi;
namespace Sortix {
DevStringBuffer::DevStringBuffer(char* str)

View File

@ -24,15 +24,13 @@
#include <sortix/kernel/platform.h>
#include <sortix/kernel/video.h>
#include <libmaxsi/string.h>
#include <sortix/kernel/string.h>
#include <errno.h>
#include <string.h>
#include "../directory.h"
#include "util.h"
#include "videofs.h"
using namespace Maxsi;
namespace Sortix {
class DevFrameBuffer : public DevBuffer

View File

@ -0,0 +1,39 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012.
This file is part of Sortix.
Sortix is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
Sortix 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 General Public License for more
details.
You should have received a copy of the GNU General Public License along with
Sortix. If not, see <http://www.gnu.org/licenses/>.
string.h
Useful functions for manipulating strings that don't belong in libc.
*******************************************************************************/
#ifndef SORTIX_STRING_H
#define SORTIX_STRING_H
namespace Sortix {
namespace String {
char* Clone(const char* Source);
char* Combine(size_t NumParameters, ...);
char* Substring(const char* src, size_t offset, size_t length);
bool StartsWith(const char* Haystack, const char* Needle);
} // namespace String
} // namespace Sortix
#endif

View File

@ -25,17 +25,15 @@
#include <sortix/kernel/platform.h>
#include <sortix/kernel/memorymanagement.h>
#include <sortix/kernel/crc32.h>
#include <sortix/kernel/string.h>
#include <sortix/stat.h>
#include <sortix/mman.h>
#include <sortix/initrd.h>
#include <libmaxsi/string.h>
#include <errno.h>
#include <string.h>
#include "initrd.h"
#include "syscall.h"
using namespace Maxsi;
namespace Sortix {
namespace InitRD {

View File

@ -23,7 +23,6 @@
*******************************************************************************/
#include <sortix/kernel/platform.h>
#include <libmaxsi/string.h>
#include <errno.h>
#include "syscall.h"
#include "kernelinfo.h"
@ -32,8 +31,6 @@
#define VERSIONSTR "unknown"
#endif
using namespace Maxsi;
namespace Sortix {
namespace Info {

View File

@ -23,13 +23,10 @@
******************************************************************************/
#include <sortix/kernel/platform.h>
#include <libmaxsi/string.h>
#include <string.h>
#include <sortix/kernel/log.h>
#include "syscall.h"
using namespace Maxsi;
namespace Sortix
{
namespace Log

View File

@ -24,15 +24,13 @@
#include <sortix/kernel/platform.h>
#include <sortix/kernel/panic.h>
#include <libmaxsi/string.h>
#include <sortix/kernel/string.h>
#include <string.h>
#include "mount.h"
#include "fs/ramfs.h"
#include "fs/initfs.h"
#include "fs/devfs.h"
using namespace Maxsi;
namespace Sortix
{
class MountPoint

View File

@ -23,15 +23,12 @@
******************************************************************************/
#include <sortix/kernel/platform.h>
#include <libmaxsi/string.h>
#include <string.h>
#include "interrupt.h"
#include <sortix/kernel/log.h>
#include "calltrace.h"
#include <sortix/kernel/panic.h>
using namespace Maxsi;
namespace Sortix
{
#if defined(DEBUG) || defined(PANIC_SHORT)

View File

@ -26,12 +26,12 @@
#include <sortix/kernel/kthread.h>
#include <sortix/kernel/worker.h>
#include <sortix/kernel/memorymanagement.h>
#include <sortix/kernel/string.h>
#include <sortix/signal.h>
#include <sortix/unistd.h>
#include <sortix/fork.h>
#include <sortix/mman.h>
#include <sortix/wait.h>
#include <libmaxsi/string.h>
#include <sortix/kernel/sortedlist.h>
#include <assert.h>
#include <errno.h>
@ -47,8 +47,6 @@
#include "elf.h"
#include "syscall.h"
using namespace Maxsi;
namespace Sortix
{
bool ProcessSegment::Intersects(ProcessSegment* segments)

View File

@ -23,7 +23,6 @@
******************************************************************************/
#include <sortix/kernel/platform.h>
#include <libmaxsi/string.h>
#include <sortix/kernel/log.h>
#include "vga.h"
#include "keyboard.h"
@ -31,8 +30,6 @@
#include "serialterminal.h"
#include "scheduler.h"
using namespace Maxsi;
namespace Sortix
{
namespace SerialTerminal

100
sortix/string.cpp Normal file
View File

@ -0,0 +1,100 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012.
This file is part of Sortix.
Sortix is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
Sortix 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 General Public License for more
details.
You should have received a copy of the GNU General Public License along with
Sortix. If not, see <http://www.gnu.org/licenses/>.
string.cpp
Useful functions for manipulating strings that don't belong in libc.
*******************************************************************************/
#include <stdarg.h>
#include <string.h>
#include <sortix/kernel/string.h>
namespace Sortix {
namespace String {
bool StartsWith(const char* haystack, const char* needle)
{
return strncmp(haystack, needle, strlen(needle)) == 0;
}
char* Clone(const char* Input)
{
size_t InputSize = strlen(Input);
char* Result = new char[InputSize + 1];
if ( Result == NULL ) { return NULL; }
memcpy(Result, Input, InputSize + 1);
return Result;
}
char* Substring(const char* src, size_t offset, size_t length)
{
size_t srclen = strlen(src);
char* dest = new char[length + 1];
if ( !dest ) { return NULL; }
memcpy(dest, src + offset, length * sizeof(char));
dest[length] = 0;
return dest;
}
char* Combine(size_t NumParameters, ...)
{
va_list param_pt;
va_start(param_pt, NumParameters);
// First calculate the string length.
size_t ResultLength = 0;
const char* TMP = 0;
for ( size_t I = 0; I < NumParameters; I++ )
{
TMP = va_arg(param_pt, const char*);
if ( TMP != NULL ) { ResultLength += strlen(TMP); }
}
// Allocate a string with the desired length.
char* Result = new char[ResultLength+1];
if ( !Result ) { return NULL; }
Result[0] = 0;
va_end(param_pt);
va_start(param_pt, NumParameters);
size_t ResultOffset = 0;
for ( size_t I = 0; I < NumParameters; I++ )
{
TMP = va_arg(param_pt, const char*);
if ( TMP )
{
size_t TMPLength = strlen(TMP);
strcpy(Result + ResultOffset, TMP);
ResultOffset += TMPLength;
}
}
return Result;
}
} // namespace String
} // namespace Sortix

View File

@ -24,13 +24,10 @@
#include <sortix/kernel/platform.h>
#include "cpu.h"
#include <libmaxsi/string.h>
#include <string.h>
#include "vga.h"
#include "uart.h"
using namespace Maxsi;
namespace Sortix
{
namespace UART

View File

@ -26,13 +26,11 @@
#include <sortix/kernel/kthread.h>
#include <sortix/kernel/textbuffer.h>
#include <sortix/kernel/video.h>
#include <sortix/kernel/string.h>
#include <stdarg.h>
#include <errno.h>
#include <libmaxsi/string.h>
#include <string.h>
using namespace Maxsi;
namespace Sortix {
bool ReadParamString(const char* str, ...)