From 4d6c6fd8dd6b552f650e1e5d58ff3cad4fc2b5ee Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Sat, 28 Aug 2021 14:19:21 +0200 Subject: [PATCH] str stuff --- kernel/kernel.c | 3 ++- lib/str.c | 21 +++++++++++++++++++++ lib/str.h | 2 ++ lib/strf.c | 16 ++++++++-------- lib/strf.h | 2 +- 5 files changed, 34 insertions(+), 10 deletions(-) create mode 100644 lib/str.c create mode 100644 lib/str.h diff --git a/kernel/kernel.c b/kernel/kernel.c index 7fbcba1..f7ea26c 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -6,7 +6,8 @@ void main() { // i.e. clear the screen et cetera. int test = 1234; - char* teststr = int_to_str(test); + char* teststr; + teststr = int_to_str(test, teststr); println(teststr, 0xf0); set_cursor_pos(28, 2); diff --git a/lib/str.c b/lib/str.c new file mode 100644 index 0000000..a0a7ef7 --- /dev/null +++ b/lib/str.c @@ -0,0 +1,21 @@ +#include "str.h" + +unsigned int strlen(char* str) { + char* c; + for( c = str; *c != '\0'; c++ ) // search for end-of-string + + return (unsigned int)(c - str); // get size by delta-address + +} + +char* strcat(char* buf, char* str) { + unsigned int bufferlen = strlen(buf); + + // remove the 0x0 char from the buffer + *(buf + bufferlen) = 0x3f; // placeholder + + // concat the str to buf + for( char* c = str; *c != '\0'; c++ ) { + + } +} diff --git a/lib/str.h b/lib/str.h new file mode 100644 index 0000000..f50e596 --- /dev/null +++ b/lib/str.h @@ -0,0 +1,2 @@ +unsigned int strlen(char* str); +char* strcat(char* buf, char* str); diff --git a/lib/strf.c b/lib/strf.c index 5259262..65dddc7 100644 --- a/lib/strf.c +++ b/lib/strf.c @@ -3,13 +3,13 @@ #define int_offset 48 // 0:48 - 9:57 -char* int_to_str(int i) { - char* strbuf = "XXXXXX"; - - while( i > 0 ) { - *(strbuf + 1) = (i % 10) + int_offset; - i /= 10; +char* int_to_str(int i, char* strbuf) { + if( i == 0 ) { + return (char*)(int_offset); + } else { + char cbuf; + cbuf = (char)((i % 10) + int_offset); + + return int_to_str(i / 10, strbuf + cbuf); } - - return strbuf; } diff --git a/lib/strf.h b/lib/strf.h index 2046d91..29649bc 100644 --- a/lib/strf.h +++ b/lib/strf.h @@ -1 +1 @@ -char* int_to_str(int i); +char* int_to_str(int i, char* strbuf);