From 0a1c5c69244b1199cb877aed029a3c9aca68f60a Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Fri, 12 May 2017 21:11:57 +0200 Subject: added a itoa --- src/kernel.c | 34 ++++++++++++++++------------------ src/stddef.h | 6 ++++++ src/stdlib.c | 43 ++++++++++++++++++++++++++++++++++++++++++- tests/Makefile | 16 ++++++++++++---- tests/test_itoa.c | 13 +++++++++++++ 5 files changed, 89 insertions(+), 23 deletions(-) create mode 100644 src/stddef.h create mode 100644 tests/test_itoa.c diff --git a/src/kernel.c b/src/kernel.c index 8232a12..91ed26b 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -12,8 +12,6 @@ void entry( void ) vga_put_string( &vga, "ABAOS 0.0.1 STARTING" ); - // clang 4.0.0 needs volatile otherwise it takes a random value - // from the register in the second vga_put_char_at below??! const char bar[] = "\\|/-"; int y_pos = vga_get_cursor_y( &vga ); int x_pos = vga_get_cursor_x( &vga ); @@ -33,21 +31,21 @@ void entry( void ) vga_put_hex( &vga, 4711 ); vga_put_newline( &vga ); - vga_set_color( &vga, VGA_COLOR_WHITE ); - vga_set_background_color( &vga, VGA_COLOR_RED ); - vga_clear_screen( &vga ); + //~ vga_set_color( &vga, VGA_COLOR_WHITE ); + //~ vga_set_background_color( &vga, VGA_COLOR_RED ); + //~ vga_clear_screen( &vga ); - for( int i = 0; i < 50; i++ ) { - for( int j = 0; j < i; j++ ) { - vga_put_char( &vga, '-' ); - } - vga_put_char( &vga, '>' ); - vga_put_string( &vga, (const char *)"TEST TEST TEST" ); - vga_put_newline( &vga ); - } - for( int j = 0; j < 50; j++ ) { - vga_put_char( &vga, '-' ); - } - vga_put_char( &vga, '>' ); - vga_put_string( &vga, (const char *)"TEST TEST TEST" ); + //~ for( int i = 0; i < 50; i++ ) { + //~ for( int j = 0; j < i; j++ ) { + //~ vga_put_char( &vga, '-' ); + //~ } + //~ vga_put_char( &vga, '>' ); + //~ vga_put_string( &vga, (const char *)"TEST TEST TEST" ); + //~ vga_put_newline( &vga ); + //~ } + //~ for( int j = 0; j < 50; j++ ) { + //~ vga_put_char( &vga, '-' ); + //~ } + //~ vga_put_char( &vga, '>' ); + //~ vga_put_string( &vga, (const char *)"TEST TEST TEST" ); } diff --git a/src/stddef.h b/src/stddef.h new file mode 100644 index 0000000..962e1aa --- /dev/null +++ b/src/stddef.h @@ -0,0 +1,6 @@ +#ifndef STDDEF_H +#define STDDEF_H + +#define NULL ( (void *)0 ) + +#endif /* STDDEF_H */ diff --git a/src/stdlib.c b/src/stdlib.c index 9a9425f..46bdce8 100644 --- a/src/stdlib.c +++ b/src/stdlib.c @@ -1,7 +1,48 @@ +#include +#include "string.h" #include "stdlib.h" +#include "stddef.h" +static void strreverse( char *s ) +{ + char *end = s + strlen( s ) - 1; + + while( s < end ) { + // XOR swap; + *s ^= *end; + *end ^= *s; + *s ^= *end; + s++; + end--; + } +} + char *itoa( int v, char *s, int base ) { - //~ *s = '\0'; + static char digit[] = "0123456789ABCDEF"; + bool sign = false; + char *p = s; + + if( base < 2 || base > 16 ) { + return NULL; + } + + if( v < 0 ) { + v = -v; + sign = true; + } + + do { + *p++ = digit[v % base]; + } while( ( v /= base ) > 0 ); + + if( sign ) { + *p++ = '-'; + } + *p = '\0'; + + strreverse( s ); + return s; } + diff --git a/tests/Makefile b/tests/Makefile index af20182..6b91c9b 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -5,19 +5,27 @@ LD := ld all: test -test_strlcpy: test_strlcpy.o - test_strlcpy: test_strlcpy.o ../src/string.o $(CC) -o test_strlcpy test_strlcpy.o ../src/string.o test_strlcpy.o: test_strlcpy.c $(CC) $(CFLAGS) $(INCLUDES) -c -o test_strlcpy.o test_strlcpy.c +test_itoa: test_itoa.o ../src/stdlib.o ../src/string.o + $(CC) -o test_itoa test_itoa.o ../src/stdlib.o ../src/string.o + +test_itoa.o: test_itoa.c ../src/stdlib.o + $(CC) $(CFLAGS) $(INCLUDES) -c -o test_itoa.o test_itoa.c + ../src/string.o: ../src/string.c $(CC) $(CFLAGS) -c -o ../src/string.o ../src/string.c -test: test_strlcpy +../src/stdlib.o: ../src/stdlib.c + $(CC) $(CFLAGS) -c -o ../src/stdlib.o ../src/stdlib.c + +test: test_strlcpy test_itoa ./test_strlcpy + ./test_itoa clean: - -rm -f test_strlcpy *.o + -rm -f test_strlcpy test_itoa *.o diff --git a/tests/test_itoa.c b/tests/test_itoa.c new file mode 100644 index 0000000..76df145 --- /dev/null +++ b/tests/test_itoa.c @@ -0,0 +1,13 @@ +#include "stdlib.h" +#include "string.h" + +int main( void ) +{ + char *res; + char buf[11]; + + res = itoa( 568876, buf, 10 ); + if( strcmp( res, buf ) ) return 1; + + return 0; +} -- cgit v1.2.3-54-g00ecf