summaryrefslogtreecommitdiff
path: root/bittorrent/mbtstr
diff options
context:
space:
mode:
authorMartial Simon <msimon_fr@hotmail.com>2025-09-15 01:07:58 +0200
committerMartial Simon <msimon_fr@hotmail.com>2025-09-15 01:07:58 +0200
commit967be9e750221ab2ab783f95df79bb26d290a45e (patch)
tree6802900a5e975f9f68b169f0f503f040056d6952 /bittorrent/mbtstr
add: added projectsHEADmain
Diffstat (limited to 'bittorrent/mbtstr')
-rw-r--r--bittorrent/mbtstr/include/mbtstr/str.h30
-rw-r--r--bittorrent/mbtstr/include/mbtstr/utils.h17
-rw-r--r--bittorrent/mbtstr/include/mbtstr/view.h88
-rw-r--r--bittorrent/mbtstr/meson.build45
-rw-r--r--bittorrent/mbtstr/src/cmp.c18
-rw-r--r--bittorrent/mbtstr/src/contains.c16
-rw-r--r--bittorrent/mbtstr/src/ctor.c19
-rw-r--r--bittorrent/mbtstr/src/dtor.c15
-rw-r--r--bittorrent/mbtstr/src/fprint.c17
-rw-r--r--bittorrent/mbtstr/src/free.c11
-rw-r--r--bittorrent/mbtstr/src/init.c12
-rw-r--r--bittorrent/mbtstr/src/pushc.c18
-rw-r--r--bittorrent/mbtstr/src/pushcstr.c11
-rw-r--r--bittorrent/mbtstr/src/pushcv.c10
14 files changed, 327 insertions, 0 deletions
diff --git a/bittorrent/mbtstr/include/mbtstr/str.h b/bittorrent/mbtstr/include/mbtstr/str.h
new file mode 100644
index 0000000..aaea5d6
--- /dev/null
+++ b/bittorrent/mbtstr/include/mbtstr/str.h
@@ -0,0 +1,30 @@
+#ifndef MBTSTR_STR_H
+#define MBTSTR_STR_H
+
+////////////////////////////////////////////////////////////////////////////////
+/// THIS FILE WILL BE OVERWRITTEN ///
+////////////////////////////////////////////////////////////////////////////////
+
+// mbtutils
+#include <mbtstr/view.h>
+// libc
+#include <stdbool.h>
+
+struct mbt_str
+{
+ char *data;
+ size_t size;
+ size_t capacity;
+};
+
+bool mbt_str_ctor(struct mbt_str *str, size_t capacity) MBT_NONNULL(1);
+void mbt_str_dtor(struct mbt_str *str);
+
+struct mbt_str *mbt_str_init(size_t capacity);
+void mbt_str_free(struct mbt_str *str);
+
+bool mbt_str_pushc(struct mbt_str *str, char c) MBT_NONNULL(1);
+bool mbt_str_pushcstr(struct mbt_str *str, const char *cstr) MBT_NONNULL(1);
+bool mbt_str_pushcv(struct mbt_str *str, struct mbt_cview view) MBT_NONNULL(1);
+
+#endif /* !MBTSTR_STR_H */
diff --git a/bittorrent/mbtstr/include/mbtstr/utils.h b/bittorrent/mbtstr/include/mbtstr/utils.h
new file mode 100644
index 0000000..0bcbc79
--- /dev/null
+++ b/bittorrent/mbtstr/include/mbtstr/utils.h
@@ -0,0 +1,17 @@
+#ifndef MBTSTR_UTILS_H
+#define MBTSTR_UTILS_H
+
+////////////////////////////////////////////////////////////////////////////////
+/// THIS FILE WILL BE OVERWRITTEN ///
+////////////////////////////////////////////////////////////////////////////////
+
+// see the Attributes section in the subject
+#define MBT_ATTR(...) __attribute__((__VA_ARGS__))
+#define MBT_UNUSED MBT_ATTR(unused)
+#define MBT_PACKED MBT_ATTR(packed)
+#define MBT_NONNULL(...) MBT_ATTR(nonnull(__VA_ARGS__))
+#define MBT_RET_NONNULL MBT_ATTR(returns_nonnull)
+
+#define MBT_UNREACHABLE() __builtin_unreachable()
+
+#endif /* !MBTSTR_UTILS_H */
diff --git a/bittorrent/mbtstr/include/mbtstr/view.h b/bittorrent/mbtstr/include/mbtstr/view.h
new file mode 100644
index 0000000..dd6e196
--- /dev/null
+++ b/bittorrent/mbtstr/include/mbtstr/view.h
@@ -0,0 +1,88 @@
+#ifndef MBTSTR_VIEW_H
+#define MBTSTR_VIEW_H
+
+////////////////////////////////////////////////////////////////////////////////
+/// THIS FILE WILL BE OVERWRITTEN ///
+////////////////////////////////////////////////////////////////////////////////
+
+// mbtutils
+#include <mbtstr/utils.h>
+// libc
+#include <stdbool.h>
+#include <stdio.h>
+
+struct mbt_cview
+{
+ const char *data;
+ size_t size;
+};
+
+struct mbt_view
+{
+ char *data;
+ size_t size;
+};
+
+#define MBT_VIEW(Data, Size) \
+ ((struct mbt_view){ \
+ .data = (Data), \
+ .size = (Size), \
+ })
+
+// a "View" here is just any struct with a .data and a .size
+#define MBT_VIEW_OF(View) \
+ ((struct mbt_view){ \
+ .data = (View).data, \
+ .size = (View).size, \
+ })
+
+#define MBT_VIEW_ARR(Array) \
+ ((struct mbt_view){ \
+ .data = (Array), \
+ .size = sizeof((Array)), \
+ })
+
+// a "View" here is just any struct with a .data and a .size
+#define MBT_VIEW_SUB(View, Offset) \
+ ((struct mbt_view){ \
+ .data = (View).data + (Offset), \
+ .size = (View).size - (Offset), \
+ })
+
+#define MBT_CVIEW(Data, Size) \
+ ((struct mbt_cview){ \
+ .data = (Data), \
+ .size = (Size), \
+ })
+
+// a "View" here is just any struct with a .data and a .size
+#define MBT_CVIEW_OF(View) \
+ ((struct mbt_cview){ \
+ .data = (View).data, \
+ .size = (View).size, \
+ })
+
+#define MBT_CVIEW_LIT(Lit) \
+ ((struct mbt_cview){ \
+ .data = (Lit), \
+ .size = sizeof((Lit)) - 1, \
+ })
+
+#define MBT_CVIEW_ARR(Array) \
+ ((struct mbt_cview){ \
+ .data = (Array), \
+ .size = sizeof((Array)), \
+ })
+
+// a "View" here is just any struct with a .data and a .size
+#define MBT_CVIEW_SUB(View, Offset) \
+ ((struct mbt_cview){ \
+ .data = (View).data + (Offset), \
+ .size = (View).size - (Offset), \
+ })
+
+int mbt_cview_cmp(struct mbt_cview lhs, struct mbt_cview rhs);
+bool mbt_cview_contains(struct mbt_cview view, char c);
+void mbt_cview_fprint(struct mbt_cview view, FILE *stream) MBT_NONNULL(2);
+
+#endif /* !MBTSTR_VIEW_H */
diff --git a/bittorrent/mbtstr/meson.build b/bittorrent/mbtstr/meson.build
new file mode 100644
index 0000000..4565c94
--- /dev/null
+++ b/bittorrent/mbtstr/meson.build
@@ -0,0 +1,45 @@
+project(
+ 'mbtstr', # project name
+ 'c', # project language
+ meson_version: '>= 1.1.0', # meson minimum version
+ version: '1.0.0', # project version
+ default_options: [
+ 'c_args=-D_GNU_SOURCE',
+ 'c_std=c99', # -std=c99
+ 'debug=true', # -g
+ 'optimization=0', # -O0
+ 'warning_level=3', # -Wall -Wextra -Wpedantic
+ 'werror=true', # -Werror
+ 'b_sanitize=address,undefined', # -fsanitize=address,undefined
+ ],
+)
+
+SRCFILES = run_command('find','src -name *.c').stdout().split()
+
+mbtstrlib_inc = [include_directories('include')]
+
+mbtstrlib = shared_library(
+ 'mbtstr',
+ # python c horrible
+ sources: SRCFILES,
+ include_directories: mbtstrlib_inc + [include_directories('src')],
+)
+
+mbtstrlib_dep = declare_dependency(
+ include_directories: mbtstrlib_inc,
+ link_with: mbtstrlib,
+)
+
+mbtstrexe = executable(
+ 'mbtstrexe',
+ sources: SRCFILES,
+ include_directories: [include_directories('src')],
+ dependencies: [mbtstrlib_dep],
+)
+
+
+
+
+
+
+
diff --git a/bittorrent/mbtstr/src/cmp.c b/bittorrent/mbtstr/src/cmp.c
new file mode 100644
index 0000000..58c12d8
--- /dev/null
+++ b/bittorrent/mbtstr/src/cmp.c
@@ -0,0 +1,18 @@
+#include <complex.h>
+#include <stdbool.h>
+#include <string.h>
+
+#include "mbtstr/view.h"
+
+int mbt_cview_cmp(struct mbt_cview lhs, struct mbt_cview rhs)
+{
+ size_t min = lhs.size > rhs.size ? rhs.size : lhs.size;
+ for (size_t i = 0; i < min; i++)
+ {
+ if (lhs.data[i] != rhs.data[i])
+ return lhs.data[i] - rhs.data[i];
+ }
+ if (rhs.size == lhs.size)
+ return 0;
+ return lhs.size - rhs.size;
+}
diff --git a/bittorrent/mbtstr/src/contains.c b/bittorrent/mbtstr/src/contains.c
new file mode 100644
index 0000000..b1bcafc
--- /dev/null
+++ b/bittorrent/mbtstr/src/contains.c
@@ -0,0 +1,16 @@
+#include <complex.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "mbtstr/view.h"
+
+bool mbt_cview_contains(struct mbt_cview view, char c)
+{
+ for (size_t i = 0; i < view.size; i++)
+ {
+ if (view.data[i] == c)
+ return true;
+ }
+ return false;
+}
diff --git a/bittorrent/mbtstr/src/ctor.c b/bittorrent/mbtstr/src/ctor.c
new file mode 100644
index 0000000..5354f88
--- /dev/null
+++ b/bittorrent/mbtstr/src/ctor.c
@@ -0,0 +1,19 @@
+#include <stdbool.h>
+#include <stdlib.h>
+
+#include "mbtstr/str.h"
+
+bool mbt_str_ctor(struct mbt_str *str, size_t capacity)
+{
+ str->size = 0;
+ str->capacity = capacity;
+ if (str->capacity == 0)
+ {
+ str->data = NULL;
+ return true;
+ }
+ str->data = calloc(capacity + 1, 1);
+ if (str->data == NULL)
+ return false;
+ return true;
+}
diff --git a/bittorrent/mbtstr/src/dtor.c b/bittorrent/mbtstr/src/dtor.c
new file mode 100644
index 0000000..af35c9e
--- /dev/null
+++ b/bittorrent/mbtstr/src/dtor.c
@@ -0,0 +1,15 @@
+#include <stdbool.h>
+#include <stdlib.h>
+
+#include "mbtstr/str.h"
+
+void mbt_str_dtor(struct mbt_str *str)
+{
+ if (str->data != NULL)
+ {
+ free(str->data);
+ str->data = NULL;
+ }
+ str->size = 0;
+ str->capacity = 0;
+}
diff --git a/bittorrent/mbtstr/src/fprint.c b/bittorrent/mbtstr/src/fprint.c
new file mode 100644
index 0000000..d0ee309
--- /dev/null
+++ b/bittorrent/mbtstr/src/fprint.c
@@ -0,0 +1,17 @@
+#include <complex.h>
+#include <ctype.h>
+#include <stdbool.h>
+
+#include "mbtstr/utils.h"
+#include "mbtstr/view.h"
+
+void mbt_cview_fprint(struct mbt_cview view, FILE *stream)
+{
+ for (size_t i = 0; i < view.size; i++)
+ {
+ if (isprint(view.data[i]))
+ fputc(view.data[i], stream);
+ else
+ fprintf(stream, "U+%04X", (view.data[i] + 256) % 256);
+ }
+}
diff --git a/bittorrent/mbtstr/src/free.c b/bittorrent/mbtstr/src/free.c
new file mode 100644
index 0000000..fccf803
--- /dev/null
+++ b/bittorrent/mbtstr/src/free.c
@@ -0,0 +1,11 @@
+#include <stdbool.h>
+#include <stdlib.h>
+
+#include "mbtstr/str.h"
+
+void mbt_str_free(struct mbt_str *str)
+{
+ free(str->data);
+ mbt_str_dtor(str);
+ free(str);
+}
diff --git a/bittorrent/mbtstr/src/init.c b/bittorrent/mbtstr/src/init.c
new file mode 100644
index 0000000..bf8a107
--- /dev/null
+++ b/bittorrent/mbtstr/src/init.c
@@ -0,0 +1,12 @@
+#include <stdbool.h>
+#include <stdlib.h>
+
+#include "mbtstr/str.h"
+
+struct mbt_str *mbt_str_init(size_t capacity)
+{
+ struct mbt_str *s = calloc(sizeof(struct mbt_str), 1);
+ if (!mbt_str_ctor(s, capacity))
+ return NULL;
+ return s;
+}
diff --git a/bittorrent/mbtstr/src/pushc.c b/bittorrent/mbtstr/src/pushc.c
new file mode 100644
index 0000000..d63d3d0
--- /dev/null
+++ b/bittorrent/mbtstr/src/pushc.c
@@ -0,0 +1,18 @@
+#include <stdlib.h>
+
+#include "mbtstr/str.h"
+
+bool mbt_str_pushc(struct mbt_str *str, char c)
+{
+ str->size += 1;
+ if (str->size >= str->capacity)
+ {
+ str->capacity = str->size;
+ str->data = realloc(str->data, str->capacity + 1);
+ if (str->data == NULL)
+ return false;
+ }
+ str->data[str->size - 1] = c;
+ str->data[str->size] = 0;
+ return true;
+}
diff --git a/bittorrent/mbtstr/src/pushcstr.c b/bittorrent/mbtstr/src/pushcstr.c
new file mode 100644
index 0000000..d15776e
--- /dev/null
+++ b/bittorrent/mbtstr/src/pushcstr.c
@@ -0,0 +1,11 @@
+#include "mbtstr/str.h"
+
+bool mbt_str_pushcstr(struct mbt_str *str, const char *cstr)
+{
+ if (cstr == NULL)
+ return false;
+ for (size_t i = 0; cstr[i]; i++)
+ if (!mbt_str_pushc(str, cstr[i]))
+ return false;
+ return true;
+}
diff --git a/bittorrent/mbtstr/src/pushcv.c b/bittorrent/mbtstr/src/pushcv.c
new file mode 100644
index 0000000..7f21562
--- /dev/null
+++ b/bittorrent/mbtstr/src/pushcv.c
@@ -0,0 +1,10 @@
+#include "mbtstr/str.h"
+#include "mbtstr/view.h"
+
+bool mbt_str_pushcv(struct mbt_str *str, struct mbt_cview view)
+{
+ for (size_t i = 0; i < view.size; i++)
+ if (!mbt_str_pushc(str, view.data[i]))
+ return false;
+ return true;
+}