Jelajahi Sumber

Many misc changes

Vinicius Teshima 2 tahun lalu
induk
melakukan
8f5a776078

+ 26 - 0
include/toolbox/cstr_fixed.h

@@ -0,0 +1,26 @@
+#ifndef TOOLBOX_CSTR_FIXED_H
+#define TOOLBOX_CSTR_FIXED_H
+
+#ifndef TOOLBOX_CSTR_FIXED_MAX_SIZE
+#define TOOLBOX_CSTR_FIXED_MAX_SIZE ((size_t) 255L)
+#endif
+
+#include <stddef.h>
+
+struct cstr_fixed {
+	size_t size;
+	char data[TOOLBOX_CSTR_FIXED_MAX_SIZE];
+};
+
+struct cstr_fixed
+cstr_fixed_from_cstring(const char *cstring);
+
+_Bool
+cstr_fixed_equal(const struct cstr_fixed cstr_fixed1
+		 , const struct cstr_fixed cstr_fixed2);
+
+_Bool
+cstr_fixed_equal_cstring(const struct cstr_fixed cstr_fixed
+			 , const char *cstring);
+
+#endif

+ 1 - 2
include/toolbox/cstring.h

@@ -1,7 +1,6 @@
 #ifndef TOOLBOX_CSTRING_H
 #define TOOLBOX_CSTRING_H
 
-#include <stdbool.h>
 #include <stddef.h>
 
 __attribute__((__pure__))
@@ -9,7 +8,7 @@ size_t
 cstring_len(const char *cstring);
 
 __attribute__((__pure__))
-bool
+_Bool
 cstring_equal(const char *restrict cstring_1, const char *restrict cstring_2);
 
 #endif

+ 9 - 0
include/toolbox/hash.h

@@ -0,0 +1,9 @@
+#ifndef TOOLBOX_HASH_H
+#define TOOLBOX_HASH_H
+
+#include <stdint.h>
+
+uint64_t
+hash_cstr(const char *r_p_cstr);
+
+#endif

+ 13 - 0
include/toolbox/log.h

@@ -0,0 +1,13 @@
+#ifndef TOOLBOX_LOG_H
+#define TOOLBOX_LOG_H
+
+#include <stdio.h>
+
+#ifndef ERRFILE
+#define ERRFILE stdout
+#endif
+
+#define LOG_ERROR(TXT)						\
+	fprintf(ERRFILE, "[ERROR] %s: %s", __FILE__, TXT)	\
+
+#endif

+ 0 - 34
include/toolbox/tui.h

@@ -1,34 +0,0 @@
-#ifndef TOOLBOX_TUI_H
-#define TOOLBOX_TUI_H
-
-#include "toolbox/vec2/vec2_uint16.h"
-
-#include <stdint.h>
-#include <sys/cdefs.h>
-#include <sys/types.h>
-
-#ifdef TOOLBOX_TYPEDEF
-typedef struct window window_st;
-#endif
-
-struct window;
-
-__attribute__((__deprecated__("This is trash")))
-struct window* win_create(void);
-__attribute__((__deprecated__("This is trash")))
-void win_destroy(struct window **self);
-
-__attribute__((__deprecated__("This is trash")))
-void tui_open(const struct window *self);
-__attribute__((__deprecated__("This is trash")))
-void tui_close(const struct window *self);
-__attribute__((__deprecated__("This is trash")))
-void tui_clear(const struct window *self);
-__attribute__((__deprecated__("This is trash")))
-void tui_print_rect(const struct window *self
-					, const struct vec2_uint16 p_cord
-					, const struct vec2_uint16 p_size);
-__attribute__((__deprecated__("This is trash")))
-void tui_mov_cursor(const struct window *self, const struct vec2_uint16 p_cord);
-
-#endif

+ 1 - 1
src/main.c

@@ -4,7 +4,7 @@
 #include "toolbox/array.h"
 #include "toolbox/log.h"
 #include "toolbox/ansi_escape.h"
-#include "toolbox/return_codes.h"
+#include "toolbox/errno.h"
 #include "toolbox/vptr.h"
 
 #include <stdbool.h>

+ 21 - 6
src/toolbox/cstr_fixed.c

@@ -1,24 +1,36 @@
 #include "toolbox/cstr_fixed.h"
-#include "toolbox/cstring.h"
 
-#include <stdbool.h>
 #include <stddef.h>
+#include <string.h>
+
+#include "toolbox/cstring.h"
+#include "toolbox/errno.h"
+#include "toolbox/errcode.h"
+
+#define false 0
+#define true  1
 
 struct cstr_fixed
 cstr_fixed_from_cstring(const char *cstring) {
+	toolbox_errno = RET_OK;
 	struct cstr_fixed ret = {
 		.size = cstring_len(cstring),
 		.data = {0}
 	};
-	for(size_t i = 0; i < ret.size; ++i ) {
-		ret.data[i] = *(cstring + i);
+
+	if ( ret.size > TOOLBOX_CSTR_FIXED_MAX_SIZE ) {
+		toolbox_errno = RET_EVBTS;
+		return ret;
 	}
+
+	memcpy(ret.data, cstring, ret.size);
+
 	return ret;
 }
 
 _Bool
 cstr_fixed_equal(const struct cstr_fixed cstr_fixed1
-			  , const struct cstr_fixed cstr_fixed2) {
+		 , const struct cstr_fixed cstr_fixed2) {
 	if ( cstr_fixed1.size != cstr_fixed2.size ) {
 		return false;
 	}
@@ -32,6 +44,9 @@ cstr_fixed_equal(const struct cstr_fixed cstr_fixed1
 
 _Bool
 cstr_fixed_equal_cstring(const struct cstr_fixed cstr_fixed
-						 , const char *cstring) {
+			 , const char *cstring) {
 	return cstr_fixed_equal(cstr_fixed, cstr_fixed_from_cstring(cstring));
 }
+
+#undef false
+#undef true