aboutsummaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/lib.h10
-rw-r--r--src/lib/memory.c26
-rw-r--r--src/lib/string.c19
3 files changed, 52 insertions, 3 deletions
diff --git a/src/lib/lib.h b/src/lib/lib.h
index aa5d3bc..f0d7d1b 100644
--- a/src/lib/lib.h
+++ b/src/lib/lib.h
@@ -1,8 +1,18 @@
#ifndef MELVIX_LIB_H
#define MELVIX_LIB_H
+#include <stddef.h>
+
size_t strlen(const char *str);
size_t strcmp(const char *s1, const char *s2);
+char *strcat(char *dst, const char *src);
+
+void *memory_copy(void *dest, const void *src, size_t count);
+
+void *memory_set(void *dest, char val, size_t count);
+
+int memory_compare(const void *a_ptr, const void *b_ptr, size_t size);
+
#endif
diff --git a/src/lib/memory.c b/src/lib/memory.c
new file mode 100644
index 0000000..e306ada
--- /dev/null
+++ b/src/lib/memory.c
@@ -0,0 +1,26 @@
+#include "../graphics/graphics.h"
+
+void *memory_copy(void *dest, const void *src, size_t count) {
+ const char *sp = (const char *) src;
+ char *dp = (char *) dest;
+ for (; count != 0; count--) *dp++ = *sp++;
+ return dest;
+}
+
+void *memory_set(void *dest, char val, size_t count) {
+ char *temp = (char *) dest;
+ for (; count != 0; count--) *temp++ = val;
+ return dest;
+}
+
+int memory_compare(const void *a_ptr, const void *b_ptr, size_t size) {
+ const unsigned char *a = (const unsigned char *) a_ptr;
+ const unsigned char *b = (const unsigned char *) b_ptr;
+ for (size_t i = 0; i < size; i++) {
+ if (a[i] < b[i])
+ return -1;
+ else if (b[i] < a[i])
+ return 1;
+ }
+ return 0;
+} \ No newline at end of file
diff --git a/src/lib/string.c b/src/lib/string.c
index a4be160..6ef0316 100644
--- a/src/lib/string.c
+++ b/src/lib/string.c
@@ -1,3 +1,5 @@
+#include <stddef.h>
+
size_t strlen(const char *str) {
size_t len = 0;
while (str[len])
@@ -7,8 +9,19 @@ size_t strlen(const char *str) {
size_t strcmp(const char *s1, const char *s2) {
while (*s1 && (*s1 == *s2)) {
- s1++;
- s2++;
+ s1++;
+ s2++;
+ }
+ return *(const unsigned char *) s1 - *(const unsigned char *) s2;
+}
+
+char *strcat(char *dst, const char *src) {
+ unsigned int i = 0;
+ unsigned int j = 0;
+ for (i = 0; dst[i] != 0; i++) {}
+ for (j = 0; src[j] != 0; j++) {
+ dst[i + j] = src[j];
}
- return *(const unsigned char*)s1 - *(const unsigned char*)s2;
+ dst[i + j] = 0;
+ return dst;
}