diff options
author | Marvin Borner | 2020-08-09 16:51:01 +0200 |
---|---|---|
committer | Marvin Borner | 2020-08-09 16:51:01 +0200 |
commit | 162d024a53e1e31e00ff0b6f47dd4590edebc551 (patch) | |
tree | 711d3886c300dfaddffdafaa89b690b45eb2101d /src/lib | |
parent | 79f2fa136f26a0b87917336e089485712ee49bd6 (diff) |
Heavy restructuring of libc, kernel and apps
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/conv.c | 144 | ||||
-rw-r--r-- | src/lib/inc/arg.h | 11 | ||||
-rw-r--r-- | src/lib/inc/assert.h | 11 | ||||
-rw-r--r-- | src/lib/inc/conv.h | 15 | ||||
-rw-r--r-- | src/lib/inc/def.h | 28 | ||||
-rw-r--r-- | src/lib/inc/math.h | 10 | ||||
-rw-r--r-- | src/lib/inc/mem.h | 18 | ||||
-rw-r--r-- | src/lib/inc/print.h | 13 | ||||
-rw-r--r-- | src/lib/inc/str.h | 17 | ||||
-rw-r--r-- | src/lib/math.c | 15 | ||||
-rw-r--r-- | src/lib/mem.c | 33 | ||||
-rw-r--r-- | src/lib/print.c | 96 | ||||
-rw-r--r-- | src/lib/str.c | 98 |
13 files changed, 0 insertions, 509 deletions
diff --git a/src/lib/conv.c b/src/lib/conv.c deleted file mode 100644 index 3bde4ec..0000000 --- a/src/lib/conv.c +++ /dev/null @@ -1,144 +0,0 @@ -// MIT License, Copyright (c) 2020 Marvin Borner - -#include <def.h> -#include <math.h> -#include <mem.h> -#include <str.h> - -static const char HTOA_TABLE[] = "0123456789ABCDEF"; -static const char ITOA_TABLE[] = "0123456789"; - -int atoi(char *str) -{ - u32 s_str = strlen(str); - if (!s_str) - return 0; - - u8 negative = 0; - if (str[0] == '-') - negative = 1; - - u32 i = 0; - if (negative) - i++; - - int ret = 0; - for (; i < s_str; i++) { - ret += (str[i] - '0') * pow(10, (int)((s_str - i) - 1)); - } - - if (negative) - ret *= -1; - return ret; -} - -char *htoa(u32 n) -{ - char *ret = (char *)malloc(10); - - int i = 0; - while (n) { - ret[i++] = HTOA_TABLE[n & 0xF]; - n >>= 4; - } - - if (!i) { - ret[0] = '0'; - i++; - } - - for (; i <= 9; i++) - ret[i] = 0; - - char *aux = strdup(ret); - free(ret); - ret = aux; - - strinv(ret); - return ret; -} - -int htoi(char *str) -{ - u32 s_str = strlen(str); - - u32 i = 0; - int ret = 0; - for (; i < s_str; i++) { - char c = str[i]; - int aux = 0; - if (c >= '0' && c <= '9') - aux = c - '0'; - else if (c >= 'A' && c <= 'F') - aux = (c - 'A') + 10; - - ret += aux * pow(16, (int)((s_str - i) - 1)); - } - - return ret; -} - -char *itoa(int n) -{ - if (!n) { - char *ret = (char *)malloc(2); - ret[0] = '0'; - ret[1] = 0; - return ret; - } - u8 negative = (u8)(n < 0); - if (negative) - n *= -1; - - int sz; - for (sz = 0; n % pow(10, sz) != n; sz++) { - } - - char *ret = (char *)malloc((u32)(sz + 1)); - - for (int i = 0; i < sz; i++) { - int digit = (n % pow(10, i + 1)) / pow(10, i); - ret[i] = ITOA_TABLE[digit]; - } - ret[sz] = 0; - - if (negative) { - char *aux = (char *)malloc((u32)(sz + 2)); - strcpy(aux, ret); - aux[sz] = '-'; - aux[sz + 1] = 0; - free(ret); - ret = aux; - } - - strinv(ret); - return ret; -} - -char *conv_base(int value, char *result, int base, int is_signed) -{ - if (base < 2 || base > 36) { - *result = '\0'; - return result; - } - - char *ptr = result, *ptr1 = result, tmp_char; - int tmp_value; - - do { - tmp_value = value; - value /= base; - *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" - [35 + (tmp_value - value * base)]; - } while (value); - - if (is_signed && tmp_value < 0) - *ptr++ = '-'; - *ptr-- = '\0'; - while (ptr1 < ptr) { - tmp_char = *ptr; - *ptr-- = *ptr1; - *ptr1++ = tmp_char; - } - return result; -} diff --git a/src/lib/inc/arg.h b/src/lib/inc/arg.h deleted file mode 100644 index 73e592d..0000000 --- a/src/lib/inc/arg.h +++ /dev/null @@ -1,11 +0,0 @@ -// MIT License, Copyright (c) 2020 Marvin Borner - -#ifndef ARG_H -#define ARG_H - -typedef __builtin_va_list va_list; -#define va_start __builtin_va_start -#define va_end __builtin_va_end -#define va_arg __builtin_va_arg - -#endif diff --git a/src/lib/inc/assert.h b/src/lib/inc/assert.h deleted file mode 100644 index 2cb095b..0000000 --- a/src/lib/inc/assert.h +++ /dev/null @@ -1,11 +0,0 @@ -// MIT License, Copyright (c) 2020 Marvin Borner - -#ifndef ASSERT_H -#define ASSERT_H - -#include <print.h> - -#define assert(exp) \ - (exp) ? 0 : printf("%s:%d: %s: Assertion '%s' failed\n", __FILE__, __LINE__, __func__, #exp) - -#endif diff --git a/src/lib/inc/conv.h b/src/lib/inc/conv.h deleted file mode 100644 index d878deb..0000000 --- a/src/lib/inc/conv.h +++ /dev/null @@ -1,15 +0,0 @@ -// MIT License, Copyright (c) 2020 Marvin Borner - -#ifndef CONV_H -#define CONV_H - -#include <def.h> - -int atoi(char *str); -char *htoa(u32 n); -int htoi(char *str); -char *itoa(int n); - -char *conv_base(int value, char *result, int base, int is_signed); - -#endif diff --git a/src/lib/inc/def.h b/src/lib/inc/def.h deleted file mode 100644 index 42939a0..0000000 --- a/src/lib/inc/def.h +++ /dev/null @@ -1,28 +0,0 @@ -// MIT License, Copyright (c) 2020 Marvin Borner - -#ifndef DEF_H -#define DEF_H - -/** - * Types - */ - -typedef signed char s8; -typedef unsigned char u8; - -typedef signed short s16; -typedef unsigned short u16; - -typedef signed long s32; -typedef unsigned long u32; - -typedef signed long long s64; -typedef unsigned long long u64; - -/** - * Macros - */ - -#define NULL ((void *)0) - -#endif diff --git a/src/lib/inc/math.h b/src/lib/inc/math.h deleted file mode 100644 index 268e3e7..0000000 --- a/src/lib/inc/math.h +++ /dev/null @@ -1,10 +0,0 @@ -// MIT License, Copyright (c) 2020 Marvin Borner - -#ifndef MATH_H -#define MATH_H - -#include <def.h> - -int pow(int base, int exp); - -#endif diff --git a/src/lib/inc/mem.h b/src/lib/inc/mem.h deleted file mode 100644 index 2aaf4d8..0000000 --- a/src/lib/inc/mem.h +++ /dev/null @@ -1,18 +0,0 @@ -// MIT License, Copyright (c) 2020 Marvin Borner - -#ifndef MEM_H -#define MEM_H - -#include <def.h> - -#define malloc(n) ((void *)((HEAP += n) - n)) // TODO: Implement real/better malloc/free -#define free(x) - -extern u32 HEAP; -extern u32 HEAP_START; - -void *memcpy(void *dst, const void *src, u32 n); -void *memset(void *dst, int c, u32 n); -int memcmp(const void *s1, const void *s2, u32 n); - -#endif diff --git a/src/lib/inc/print.h b/src/lib/inc/print.h deleted file mode 100644 index 1a2419b..0000000 --- a/src/lib/inc/print.h +++ /dev/null @@ -1,13 +0,0 @@ -// MIT License, Copyright (c) 2020 Marvin Borner -// I may (re)move this in the future // TODO - -#ifndef PRINT_H -#define PRINT_H - -#include <arg.h> - -int printf(const char *format, ...); -int vprintf(const char *format, va_list ap); -int vsprintf(char *str, const char *format, va_list ap); - -#endif diff --git a/src/lib/inc/str.h b/src/lib/inc/str.h deleted file mode 100644 index f4c63b1..0000000 --- a/src/lib/inc/str.h +++ /dev/null @@ -1,17 +0,0 @@ -// MIT License, Copyright (c) 2020 Marvin Borner - -#ifndef STRING_H -#define STRING_H - -#include <def.h> - -u32 strlen(const char *s); -char *strcpy(char *dst, const char *src); -char *strchr(const char *s, int c); -char *strcat(char *dst, const char *src); -int strcmp(const char *s1, const char *s2); -int strncmp(const char *s1, const char *s2, u32 n); -char *strinv(char *s); -char *strdup(const char *s); - -#endif diff --git a/src/lib/math.c b/src/lib/math.c deleted file mode 100644 index 9cd9cea..0000000 --- a/src/lib/math.c +++ /dev/null @@ -1,15 +0,0 @@ -// MIT License, Copyright (c) 2020 Marvin Borner - -int pow(int base, int exp) -{ - if (exp < 0) - return 0; - - if (!exp) - return 1; - - int ret = base; - for (int i = 1; i < exp; i++) - ret *= base; - return ret; -} diff --git a/src/lib/mem.c b/src/lib/mem.c deleted file mode 100644 index 00b9735..0000000 --- a/src/lib/mem.c +++ /dev/null @@ -1,33 +0,0 @@ -// MIT License, Copyright (c) 2020 Marvin Borner - -#include <def.h> - -void *memcpy(void *dst, const void *src, u32 n) -{ - const char *sp = (const char *)src; - char *dp = (char *)dst; - for (; n != 0; n--) - *dp++ = *sp++; - return dst; -} - -void *memset(void *dst, char val, u32 n) -{ - char *temp = (char *)dst; - for (; n != 0; n--) - *temp++ = val; - return dst; -} - -int memcmp(const void *s1, const void *s2, u32 n) -{ - const u8 *a = (const u8 *)s1; - const u8 *b = (const u8 *)s2; - for (u32 i = 0; i < n; i++) { - if (a[i] < b[i]) - return -1; - else if (b[i] < a[i]) - return 1; - } - return 0; -} diff --git a/src/lib/print.c b/src/lib/print.c deleted file mode 100644 index b9cddfa..0000000 --- a/src/lib/print.c +++ /dev/null @@ -1,96 +0,0 @@ -#include <arg.h> -#include <conv.h> -#include <def.h> -#include <mem.h> -#include <serial.h> -#include <str.h> - -static void append(char *dest, char *src, int index) -{ - for (u32 i = index; i < strlen(src) + index; i++) - dest[i] = src[i - index]; - dest[index + strlen(src)] = 0; -} - -int vsprintf(char *str, const char *format, va_list ap) -{ - u8 ready_to_format = 0; - - int i = 0; - char buf = 0; - char format_buffer[20] = "\0"; - - for (; *format; format++) { - if (ready_to_format) { - ready_to_format = 0; - - if (*format == '%') { - str[i] = '%'; - continue; - } - - buf = *format; - - // TODO: Improve this repetitive code - if (buf == 's') { - char *string = va_arg(ap, char *); - append(str, string, i); - i = strlen(str); - } else if (buf == 'x') { - conv_base(va_arg(ap, u32), format_buffer, 16, 0); - append(str, format_buffer, i); - i = strlen(str); - } else if (buf == 'd' || buf == 'i') { - conv_base(va_arg(ap, s32), format_buffer, 10, 1); - append(str, format_buffer, i); - i = strlen(str); - } else if (buf == 'u') { - conv_base(va_arg(ap, u32), format_buffer, 10, 0); - append(str, format_buffer, i); - i = strlen(str); - } else if (buf == 'o') { - conv_base(va_arg(ap, u32), format_buffer, 8, 0); - append(str, format_buffer, i); - i = strlen(str); - } else if (buf == 'b') { - conv_base(va_arg(ap, u32), format_buffer, 2, 0); - append(str, format_buffer, i); - i = strlen(str); - } else if (buf == 'c') { - str[i] = (char)va_arg(ap, int); - i++; - } - } else { - if (*format == '%') - ready_to_format = 1; - else { - str[i] = *format; - i++; - } - } - - format_buffer[0] = '\0'; - } - - return strlen(str); -} - -int vprintf(const char *format, va_list ap) -{ - char buf[1024]; - memset(buf, 0, 1024); - int len = vsprintf(buf, format, ap); - serial_print(buf); // TODO: Remove temporary serial print - return len; -} - -// TODO: Fix printf for *very* large strings (serial works) -int printf(const char *format, ...) -{ - va_list ap; - va_start(ap, format); - int len = vprintf(format, ap); - va_end(ap); - - return len; -} diff --git a/src/lib/str.c b/src/lib/str.c deleted file mode 100644 index d1ee325..0000000 --- a/src/lib/str.c +++ /dev/null @@ -1,98 +0,0 @@ -// MIT License, Copyright (c) 2020 Marvin Borner - -#include <def.h> -#include <mem.h> - -u32 strlen(const char *s) -{ - const char *ss = s; - while (*ss) - ss++; - return ss - s; -} - -char *strcpy(char *dst, const char *src) -{ - char *q = dst; - const char *p = src; - char ch; - - do { - *q++ = ch = *p++; - } while (ch); - - return dst; -} - -int strcmp(const char *s1, const char *s2) -{ - const u8 *c1 = (const u8 *)s1; - const u8 *c2 = (const u8 *)s2; - u8 ch; - int d = 0; - - while (1) { - d = (int)(ch = *c1++) - (int)*c2++; - if (d || !ch) - break; - } - - return d; -} - -int strncmp(const char *s1, const char *s2, u32 n) -{ - const u8 *c1 = (const u8 *)s1; - const u8 *c2 = (const u8 *)s2; - u8 ch; - int d = 0; - - while (n--) { - d = (int)(ch = *c1++) - (int)*c2++; - if (d || !ch) - break; - } - - return d; -} - -char *strchr(const char *s, int c) -{ - while (*s != (char)c) { - if (!*s) - return NULL; - s++; - } - - return (char *)s; -} - -char *strcat(char *dst, const char *src) -{ - strcpy(strchr(dst, '\0'), src); - return dst; -} - -char *strinv(char *s) -{ - u32 s_str = strlen(s); - - int iterations = (int)s_str / 2; - for (int i = 0; i < iterations; i++) { - char aux = s[i]; - s[i] = s[(s_str - i) - 1]; - s[(s_str - i) - 1] = aux; - } - return s; -} - -char *strdup(const char *s) -{ - int l = strlen(s) + 1; - char *d = malloc(l); - - if (d) - memcpy(d, s, l); - - return d; -} |