From f7c6e54e2fdfab4ca2e7886a865060b2222833f8 Mon Sep 17 00:00:00 2001 From: Test_User Date: Wed, 4 Sep 2024 08:47:32 -0400 Subject: Separate haxstring/table from here --- Makefile | 40 +++++++-- general_network.c | 4 +- general_network.h | 4 +- haxstring.h | 45 ---------- haxstring_utils.c | 130 ----------------------------- haxstring_utils.h | 38 --------- networks/gnutls.h | 2 +- networks/gnutls_buffered.h | 2 +- networks/openssl.h | 2 +- networks/openssl_buffered.h | 2 +- networks/plaintext.c | 2 +- networks/plaintext.h | 2 +- networks/plaintext_buffered.c | 2 +- networks/plaintext_buffered.h | 2 +- protocols.h | 2 +- protocols/inspircd2.c | 5 +- protocols/inspircd2.h | 4 +- protocols/inspircd3.c | 5 +- protocols/inspircd3.h | 4 +- protocols/inspircd4.c | 5 +- protocols/inspircd4.h | 4 +- pseudoclients.c | 2 +- pseudoclients.h | 2 +- pseudoclients/haxserv.c | 5 +- pseudoclients/haxserv.h | 2 +- pseudoclients/services.c | 4 +- pseudoclients/services.h | 2 +- server_network.c | 4 +- server_network.h | 4 +- table.c | 190 ------------------------------------------ table.h | 51 ------------ 31 files changed, 76 insertions(+), 496 deletions(-) delete mode 100644 haxstring.h delete mode 100644 haxstring_utils.c delete mode 100644 haxstring_utils.h delete mode 100644 table.c delete mode 100644 table.h diff --git a/Makefile b/Makefile index 2d1bb07..ab8bf95 100644 --- a/Makefile +++ b/Makefile @@ -54,6 +54,8 @@ LDFLAGS = -lpthread printf '%s\n' 'LAST_ATOMICS = $(ATOMICS)' >> .makeopts printf '%s\n' 'LAST_IPv4 = $(IPv4)' >> .makeopts printf '%s\n' 'LAST_IPv6 = $(IPv6)' >> .makeopts + printf '%s\n' 'LAST_HAX_STRING_PATH = $(HAX_STRING_PATH)' >> .makeopts + printf '%s\n' 'LAST_HAX_TABLE_PATH = $(HAX_TABLE_PATH)' >> .makeopts printf '%s\n' 'LAST_CFLAGS = $(ORIGINAL_CFLAGS)' >> .makeopts printf '%s\n' 'LAST_LDFLAGS = $(ORIGINAL_LDFLAGS)' >> .makeopts printf '%s\n' 'LAST_CC = $(CC)' >> .makeopts @@ -186,6 +188,22 @@ ORIGINAL_CFLAGS := $(LAST_CFLAGS) CFLAGS += $(LAST_CFLAGS) endif +ifneq ($(HAX_STRING_PATH),) +ifneq ($(HAX_STRING_PATH),$(LAST_HAX_STRING_PATH)) +rebuild = 1 +endif +else +HAX_STRING_PATH += $(LAST_HAX_STRING_PATH) +endif + +ifneq ($(HAX_TABLE_PATH),) +ifneq ($(HAX_TABLE_PATH),$(LAST_HAX_TABLE_PATH)) +rebuild = 1 +endif +else +HAX_TABLE_PATH += $(LAST_HAX_TABLE_PATH) +endif + ifneq ($(ORIGINAL_LDFLAGS),) ifneq ($(ORIGINAL_LDFLAGS),$(LAST_LDFLAGS)) rebuild = 1 @@ -249,12 +267,28 @@ endif CFLAGS += $(INCLUDEFLAGS) -fPIC -fno-plt -D_REENTRANT -ggdb3 -Wall -Wextra -Wsign-conversion -Wno-unknown-warning-option -Wno-unused-parameter -Wno-implicit-fallthrough -Wno-alloc-size -std=gnu99 +ifneq ($(HAX_STRING_PATH),) +CFLAGS += -I$(HAX_STRING_PATH) +endif +ifneq ($(HAX_TABLE_PATH),) +CFLAGS += -I$(HAX_TABLE_PATH) +endif + +LDFLAGS += -lhax_string_utils -lhax_table + +ifneq ($(HAX_STRING_PATH),) +LDFLAGS += -L$(HAX_STRING_PATH) +endif +ifneq ($(HAX_TABLE_PATH),) +LDFLAGS += -L$(HAX_TABLE_PATH) +endif + USE_PLAINTEXT = 0 USE_CLIENT = 0 USE_GNUTLS = 0 USE_SERVER = 0 -OFILES = config.o general_network.o haxstring_utils.o real_main.o table.o mutex.o +OFILES = config.o general_network.o real_main.o mutex.o SOFILES = HaxIRCd.so USE_IRCD := 0 @@ -466,8 +500,6 @@ $(call DEPS,config,o) $(call DEPS,general_network,o) -$(call DEPS,haxstring_utils,o) - $(call DEPS,real_main,o) $(call DEPS,main,o) @@ -476,8 +508,6 @@ $(call DEPS,mutex,o) $(call DEPS,protocols,o) -$(call DEPS,table,o) - ifeq ($(USE_PLAINTEXT),1) $(call DEPS,networks/plaintext,o) endif diff --git a/general_network.c b/general_network.c index 13ac26c..f7c088f 100644 --- a/general_network.c +++ b/general_network.c @@ -38,8 +38,8 @@ #include "config.h" #include "general_network.h" -#include "haxstring.h" -#include "haxstring_utils.h" +#include "hax_string.h" +#include "hax_string_utils.h" #ifdef USE_PLAINTEXT_NETWORK #include "networks/plaintext.h" diff --git a/general_network.h b/general_network.h index 9f35f64..2bb3771 100644 --- a/general_network.h +++ b/general_network.h @@ -33,9 +33,9 @@ #include #include -#include "haxstring.h" +#include "hax_string.h" #include "protocol_numbers.h" -#include "table.h" +#include "hax_table.h" #ifdef ENONET #define RETRY_ACCEPT (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK || errno == ENETDOWN || errno == EPROTO || errno == ENOPROTOOPT || errno == EHOSTDOWN || errno == ENONET || errno == EHOSTUNREACH || errno == EOPNOTSUPP || errno == ENETUNREACH) diff --git a/haxstring.h b/haxstring.h deleted file mode 100644 index f0c3947..0000000 --- a/haxstring.h +++ /dev/null @@ -1,45 +0,0 @@ -// Hax's strings -// -// Written by: Test_User -// -// This is free and unencumbered software released into the public -// domain. -// -// Anyone is free to copy, modify, publish, use, compile, sell, or -// distribute this software, either in source code form or as a compiled -// binary, for any purpose, commercial or non-commercial, and by any -// means. -// -// In jurisdictions that recognize copyright laws, the author or authors -// of this software dedicate any and all copyright interest in the -// software to the public domain. We make this dedication for the benefit -// of the public at large and to the detriment of our heirs and -// successors. We intend this dedication to be an overt act of -// relinquishment in perpetuity of all present and future rights to this -// software under copyright law. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR -// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -// OTHER DEALINGS IN THE SOFTWARE. - -#pragma once - -#include -#include -#include - -struct string { - char *data; - size_t len; -}; - -#define STRING(x) (struct string){x, sizeof(x)-1} -#define NULSTR(x) (struct string){x, strlen(x)} - -#define STRING_EQ(x, y) ((x).len == (y).len && memcmp((x).data, (y).data, (x).len) == 0) - -#define WRITES(x, y) write((x), (y).data, (y).len) diff --git a/haxstring_utils.c b/haxstring_utils.c deleted file mode 100644 index 82be667..0000000 --- a/haxstring_utils.c +++ /dev/null @@ -1,130 +0,0 @@ -// Hax's string utils -// -// Written by: Test_User -// -// This is free and unencumbered software released into the public -// domain. -// -// Anyone is free to copy, modify, publish, use, compile, sell, or -// distribute this software, either in source code form or as a compiled -// binary, for any purpose, commercial or non-commercial, and by any -// means. -// -// In jurisdictions that recognize copyright laws, the author or authors -// of this software dedicate any and all copyright interest in the -// software to the public domain. We make this dedication for the benefit -// of the public at large and to the detriment of our heirs and -// successors. We intend this dedication to be an overt act of -// relinquishment in perpetuity of all present and future rights to this -// software under copyright law. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR -// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -// OTHER DEALINGS IN THE SOFTWARE. - -#include -#include -#include - -#include "haxstring.h" -#include "haxstring_utils.h" - -size_t str_to_unsigned(struct string str, char *err) { - if (str.len == 0) { - *err = 1; - return 0; - } - - size_t val = 0; - while (str.len > 0) { - switch(str.data[0]) { - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - if (val > ((size_t)-1)/10) { - *err = 1; - return 0; - } - val *= 10; - if (val > (-((size_t)((unsigned char)str.data[0] - 0x30) + 1))) { - *err = 1; - return 0; - } - val += (unsigned char)str.data[0] - 0x30; - break; - default: - *err = 1; - return 0; - } - - str.data++; - str.len--; - } - - *err = 0; - return val; -} - -int unsigned_to_str(size_t number, struct string *str) { - size_t len = 0; - { - size_t tmp = number; - do { - len++; - } while ((tmp = tmp / 10) != 0); - } - - void *tmp = malloc(len); - if (!tmp) - return 1; - - str->data = tmp; - str->len = len; - - for (size_t i = len; i > 0; i--) { - str->data[i - 1] = (char)((number % 10) + 0x30); - number = number / 10; - } - - return 0; -} - -int str_clone(struct string *dest, struct string source) { - dest->data = malloc(source.len); - if (!dest->data) - return 1; - memcpy(dest->data, source.data, source.len); - dest->len = source.len; - - return 0; -} - -int str_combine(struct string *dest, size_t count, struct string *sources) { - size_t len = 0; - for (size_t i = 0; i < count; i++) - len += sources[i].len; - - dest->data = malloc(len); - if (!dest->data && len != 0) - return 1; - dest->len = len; - - size_t offset = 0; - for (size_t i = 0; i < count; i++) { - memcpy(&(dest->data[offset]), sources[i].data, sources[i].len); - offset += sources[i].len; - } - - return 0; -} diff --git a/haxstring_utils.h b/haxstring_utils.h deleted file mode 100644 index f0e2826..0000000 --- a/haxstring_utils.h +++ /dev/null @@ -1,38 +0,0 @@ -// Header for hax's string utils -// -// Written by: Test_User -// -// This is free and unencumbered software released into the public -// domain. -// -// Anyone is free to copy, modify, publish, use, compile, sell, or -// distribute this software, either in source code form or as a compiled -// binary, for any purpose, commercial or non-commercial, and by any -// means. -// -// In jurisdictions that recognize copyright laws, the author or authors -// of this software dedicate any and all copyright interest in the -// software to the public domain. We make this dedication for the benefit -// of the public at large and to the detriment of our heirs and -// successors. We intend this dedication to be an overt act of -// relinquishment in perpetuity of all present and future rights to this -// software under copyright law. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR -// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -// OTHER DEALINGS IN THE SOFTWARE. - -#pragma once - -#include -#include "haxstring.h" - -size_t str_to_unsigned(struct string str, char *err); -int unsigned_to_str(size_t number, struct string *str); - -int str_clone(struct string *dest, struct string source); -int str_combine(struct string *dest, size_t count, struct string *sources); diff --git a/networks/gnutls.h b/networks/gnutls.h index 585bd03..192f75b 100644 --- a/networks/gnutls.h +++ b/networks/gnutls.h @@ -30,7 +30,7 @@ #include -#include "../haxstring.h" +#include "hax_string.h" #include "../general_network.h" int init_gnutls_network(void); diff --git a/networks/gnutls_buffered.h b/networks/gnutls_buffered.h index aa82457..a60cc17 100644 --- a/networks/gnutls_buffered.h +++ b/networks/gnutls_buffered.h @@ -30,7 +30,7 @@ #include -#include "../haxstring.h" +#include "hax_string.h" #include "../general_network.h" int init_gnutls_buffered_network(void); diff --git a/networks/openssl.h b/networks/openssl.h index 5039ca5..bcfc42c 100644 --- a/networks/openssl.h +++ b/networks/openssl.h @@ -30,7 +30,7 @@ #include -#include "../haxstring.h" +#include "hax_string.h" #include "../general_network.h" int init_openssl_network(void); diff --git a/networks/openssl_buffered.h b/networks/openssl_buffered.h index e09c800..d71d723 100644 --- a/networks/openssl_buffered.h +++ b/networks/openssl_buffered.h @@ -30,7 +30,7 @@ #include -#include "../haxstring.h" +#include "hax_string.h" #include "../general_network.h" int init_openssl_buffered_network(void); diff --git a/networks/plaintext.c b/networks/plaintext.c index 8d071f1..38f8cdc 100644 --- a/networks/plaintext.c +++ b/networks/plaintext.c @@ -38,7 +38,7 @@ #include "../config.h" #include "../general_network.h" -#include "../haxstring.h" +#include "hax_string.h" #include "plaintext.h" int init_plaintext_network(void) { diff --git a/networks/plaintext.h b/networks/plaintext.h index 0827f3b..a499825 100644 --- a/networks/plaintext.h +++ b/networks/plaintext.h @@ -30,7 +30,7 @@ #include -#include "../haxstring.h" +#include "hax_string.h" #include "../general_network.h" int init_plaintext_network(void); diff --git a/networks/plaintext_buffered.c b/networks/plaintext_buffered.c index 59e3a2f..975e022 100644 --- a/networks/plaintext_buffered.c +++ b/networks/plaintext_buffered.c @@ -39,7 +39,7 @@ #include "../config.h" #include "../general_network.h" -#include "../haxstring.h" +#include "hax_string.h" #include "../main.h" #include "../mutex.h" #include "plaintext_buffered.h" diff --git a/networks/plaintext_buffered.h b/networks/plaintext_buffered.h index 4670d14..025b442 100644 --- a/networks/plaintext_buffered.h +++ b/networks/plaintext_buffered.h @@ -30,7 +30,7 @@ #include -#include "../haxstring.h" +#include "hax_string.h" #include "../general_network.h" int init_plaintext_buffered_network(void); diff --git a/protocols.h b/protocols.h index 0b40752..5017271 100644 --- a/protocols.h +++ b/protocols.h @@ -30,7 +30,7 @@ #include -#include "haxstring.h" +#include "hax_string.h" #include "general_network.h" #include "protocol_numbers.h" #include "server_network.h" diff --git a/protocols/inspircd2.c b/protocols/inspircd2.c index 2915451..46b663b 100644 --- a/protocols/inspircd2.c +++ b/protocols/inspircd2.c @@ -35,8 +35,9 @@ #include "../config.h" #include "../general_network.h" -#include "../haxstring.h" -#include "../haxstring_utils.h" +#include "hax_string.h" +#include "hax_string_utils.h" +#include "hax_table.h" #include "../main.h" #include "../mutex.h" #include "../server_network.h" diff --git a/protocols/inspircd2.h b/protocols/inspircd2.h index 6caaff3..dbd06a0 100644 --- a/protocols/inspircd2.h +++ b/protocols/inspircd2.h @@ -31,9 +31,9 @@ #include #include "../config.h" -#include "../haxstring.h" +#include "hax_string.h" #include "../general_network.h" -#include "../table.h" +#include "hax_table.h" extern struct table inspircd2_protocol_init_commands; extern struct table inspircd2_protocol_commands; diff --git a/protocols/inspircd3.c b/protocols/inspircd3.c index c94ebe5..93eca9b 100644 --- a/protocols/inspircd3.c +++ b/protocols/inspircd3.c @@ -35,8 +35,9 @@ #include "../config.h" #include "../general_network.h" -#include "../haxstring.h" -#include "../haxstring_utils.h" +#include "hax_string.h" +#include "hax_string_utils.h" +#include "hax_table.h" #include "../main.h" #include "../mutex.h" #include "../server_network.h" diff --git a/protocols/inspircd3.h b/protocols/inspircd3.h index 5631321..31c7802 100644 --- a/protocols/inspircd3.h +++ b/protocols/inspircd3.h @@ -31,9 +31,9 @@ #include #include "../config.h" -#include "../haxstring.h" +#include "hax_string.h" #include "../general_network.h" -#include "../table.h" +#include "hax_table.h" struct inspircd3_protocol_specific_user { struct table memberships; diff --git a/protocols/inspircd4.c b/protocols/inspircd4.c index a66e33c..b376bb2 100644 --- a/protocols/inspircd4.c +++ b/protocols/inspircd4.c @@ -35,8 +35,9 @@ #include "../config.h" #include "../general_network.h" -#include "../haxstring.h" -#include "../haxstring_utils.h" +#include "hax_string.h" +#include "hax_string_utils.h" +#include "hax_table.h" #include "../main.h" #include "../mutex.h" #include "../server_network.h" diff --git a/protocols/inspircd4.h b/protocols/inspircd4.h index 7ac2cd7..14755fd 100644 --- a/protocols/inspircd4.h +++ b/protocols/inspircd4.h @@ -31,9 +31,9 @@ #include #include "../config.h" -#include "../haxstring.h" +#include "hax_string.h" #include "../general_network.h" -#include "../table.h" +#include "hax_table.h" struct inspircd4_protocol_specific_user { struct table memberships; diff --git a/pseudoclients.c b/pseudoclients.c index 7a59ae4..3ace6db 100644 --- a/pseudoclients.c +++ b/pseudoclients.c @@ -29,7 +29,7 @@ #include #include -#include "haxstring.h" +#include "hax_string.h" #include "pseudoclients.h" struct pseudoclient pseudoclients[NUM_PSEUDOCLIENTS] = {0}; diff --git a/pseudoclients.h b/pseudoclients.h index 24a38fe..b3c9a21 100644 --- a/pseudoclients.h +++ b/pseudoclients.h @@ -28,7 +28,7 @@ #pragma once -#include "haxstring.h" +#include "hax_string.h" #include "general_network.h" struct pseudoclient { diff --git a/pseudoclients/haxserv.c b/pseudoclients/haxserv.c index e93abf4..59f9d33 100644 --- a/pseudoclients/haxserv.c +++ b/pseudoclients/haxserv.c @@ -32,8 +32,9 @@ #include #include "../config.h" -#include "../haxstring.h" -#include "../haxstring_utils.h" +#include "hax_string.h" +#include "hax_string_utils.h" +#include "hax_table.h" #include "../general_network.h" #include "../pseudoclients.h" diff --git a/pseudoclients/haxserv.h b/pseudoclients/haxserv.h index 5143bd4..d249169 100644 --- a/pseudoclients/haxserv.h +++ b/pseudoclients/haxserv.h @@ -28,7 +28,7 @@ #pragma once -#include "../haxstring.h" +#include "hax_string.h" #include "../general_network.h" struct command_def { diff --git a/pseudoclients/services.c b/pseudoclients/services.c index 2a79906..5d4c249 100644 --- a/pseudoclients/services.c +++ b/pseudoclients/services.c @@ -30,8 +30,8 @@ #include #include "../config.h" -#include "../haxstring.h" -#include "../haxstring_utils.h" +#include "hax_string.h" +#include "hax_string_utils.h" #include "../general_network.h" #include "../pseudoclients.h" #include "services.h" diff --git a/pseudoclients/services.h b/pseudoclients/services.h index 84e62cf..fd1610e 100644 --- a/pseudoclients/services.h +++ b/pseudoclients/services.h @@ -28,7 +28,7 @@ #pragma once -#include "../haxstring.h" +#include "hax_string.h" #include "../general_network.h" struct command_def { diff --git a/server_network.c b/server_network.c index 8d5897b..ee82df1 100644 --- a/server_network.c +++ b/server_network.c @@ -35,8 +35,8 @@ #include #include "config.h" -#include "haxstring.h" -#include "haxstring_utils.h" +#include "hax_string.h" +#include "hax_string_utils.h" #include "main.h" #include "protocols.h" #include "server_network.h" diff --git a/server_network.h b/server_network.h index a4ca2df..3906ff3 100644 --- a/server_network.h +++ b/server_network.h @@ -31,8 +31,8 @@ #include #include "general_network.h" -#include "haxstring.h" -#include "table.h" +#include "hax_string.h" +#include "hax_table.h" struct server_network_info { size_t net_type; diff --git a/table.c b/table.c deleted file mode 100644 index 8dddf95..0000000 --- a/table.c +++ /dev/null @@ -1,190 +0,0 @@ -// My table library thing -// -// Written by: Test_User -// -// This is free and unencumbered software released into the public -// domain. -// -// Anyone is free to copy, modify, publish, use, compile, sell, or -// distribute this software, either in source code form or as a compiled -// binary, for any purpose, commercial or non-commercial, and by any -// means. -// -// In jurisdictions that recognize copyright laws, the author or authors -// of this software dedicate any and all copyright interest in the -// software to the public domain. We make this dedication for the benefit -// of the public at large and to the detriment of our heirs and -// successors. We intend this dedication to be an overt act of -// relinquishment in perpetuity of all present and future rights to this -// software under copyright law. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR -// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -// OTHER DEALINGS IN THE SOFTWARE. - -#include -#include -#include -#include -#include - -#include "haxstring.h" -#include "table.h" - -// currently going with a binary lookup... - -static inline int compare(struct string a, struct string b) { - size_t len; - if (a.len > b.len) - len = b.len; - else - len = a.len; - - int val = memcmp(a.data, b.data, len); - - if (val == 0) { - if (a.len < b.len) - return -1; - else if (a.len > b.len) - return 1; - } - - return val; -} - -static inline size_t search(struct table tbl, struct string name, char *exists) { - if (tbl.len == 0) { - *exists = 0; - return 0; - } - - size_t low = 0, high = tbl.len - 1; - - size_t mid = high/2; - - while (low != high) { - int val = compare(tbl.array[mid].name, name); - - if (val == 0) { - *exists = 1; - return mid; - } else if (val > 0) { - low = mid + 1; - if (mid > low) - break; - if (low > high) - low = high; - } else { - high = mid - 1; - if (mid < high) - break; - if (high < low) - high = low; - } - - mid = low + ((high-low)/2); - } - - int val = compare(tbl.array[mid].name, name); - if (val > 0) { - *exists = 0; - return mid+1; - } else if (val == 0) { - *exists = 1; - return mid; - } else { - *exists = 0; - return mid; - } -} - -int set_table_index(struct table *tbl, struct string name, void *ptr) { - char exists; - size_t index = search(*tbl, name, &exists); - - if (!exists) { - void *tmp = realloc(tbl->array, sizeof(*(tbl->array)) * (tbl->len+1)); - if (tmp == 0) - return 1; - - tbl->array = tmp; - - memmove(&(tbl->array[index+1]), &(tbl->array[index]), (tbl->len - index) * sizeof(*(tbl->array))); - tbl->len++; - } else { - tbl->array[index].ptr = ptr; - - return 0; // don't overwrite old allocated name - } - - char *data = malloc(name.len); - if (data == 0) - return 1; - - memcpy(data, name.data, name.len); - - tbl->array[index] = (struct table_index){{data, name.len}, ptr}; - - return 0; -} - -void * get_table_index(struct table tbl, struct string name) { - char exists; - size_t index = search(tbl, name, &exists); - if (!exists) - return 0; - - return tbl.array[index].ptr; -} - -char has_table_index(struct table tbl, struct string name) { - char exists; - search(tbl, name, &exists); - return exists; -} - -void * remove_table_index(struct table *tbl, struct string name) { - char exists; - size_t index = search(*tbl, name, &exists); - - if (!exists) - return 0; - - void *ptr = tbl->array[index].ptr; - free(tbl->array[index].name.data); - - memmove(&(tbl->array[index]), &(tbl->array[index+1]), (tbl->len - index - 1) * sizeof(*(tbl->array))); - tbl->len--; - - void *tmp = realloc(tbl->array, sizeof(*(tbl->array)) * tbl->len); - if (tmp || (tbl->len == 0)) - tbl->array = tmp; - // else: realloc failed on shrinking... so now we have a table that's allocated a bit too big, not much of an issue - - return ptr; -} - -void clear_table(struct table *tbl) { - for (size_t i = 0; i < tbl->len; i++) - free(tbl->array[i].name.data); - - tbl->array = realloc(tbl->array, 0); - tbl->len = 0; -} - -size_t get_table_offset(struct table tbl, struct string name, char *exists) { - return search(tbl, name, exists); -} - -// TODO: Proper lookup -void * get_table_prefix(struct table tbl, struct string name) { - for (size_t i = 0; i < tbl.len; i++) - if (tbl.array[i].name.len <= name.len && memcmp(tbl.array[i].name.data, name.data, tbl.array[i].name.len) == 0) - return tbl.array[i].ptr; - - return 0; -} diff --git a/table.h b/table.h deleted file mode 100644 index 61cc1d4..0000000 --- a/table.h +++ /dev/null @@ -1,51 +0,0 @@ -// Header for my table library thing -// -// Written by: Test_User -// -// This is free and unencumbered software released into the public -// domain. -// -// Anyone is free to copy, modify, publish, use, compile, sell, or -// distribute this software, either in source code form or as a compiled -// binary, for any purpose, commercial or non-commercial, and by any -// means. -// -// In jurisdictions that recognize copyright laws, the author or authors -// of this software dedicate any and all copyright interest in the -// software to the public domain. We make this dedication for the benefit -// of the public at large and to the detriment of our heirs and -// successors. We intend this dedication to be an overt act of -// relinquishment in perpetuity of all present and future rights to this -// software under copyright law. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR -// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, -// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -// OTHER DEALINGS IN THE SOFTWARE. - -#pragma once - -#include "haxstring.h" - -struct table_index { - struct string name; - void *ptr; -}; - -struct table { - struct table_index *array; - size_t len; -}; - -extern int set_table_index(struct table *tbl, struct string name, void *ptr); -extern void * get_table_index(struct table tbl, struct string name); -extern char has_table_index(struct table tbl, struct string name); -extern void * remove_table_index(struct table *tbl, struct string name); // returns same as get_table_index -extern void clear_table(struct table *tbl); -extern size_t get_table_offset(struct table tbl, struct string name, char *exists); - -// Longest index that starts with -extern void * get_table_prefix(struct table tbl, struct string name); -- cgit v1.2.3