diff options
| author | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:07:58 +0200 |
|---|---|---|
| committer | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:07:58 +0200 |
| commit | 967be9e750221ab2ab783f95df79bb26d290a45e (patch) | |
| tree | 6802900a5e975f9f68b169f0f503f040056d6952 /bittorrent/mbtstr/src | |
Diffstat (limited to 'bittorrent/mbtstr/src')
| -rw-r--r-- | bittorrent/mbtstr/src/cmp.c | 18 | ||||
| -rw-r--r-- | bittorrent/mbtstr/src/contains.c | 16 | ||||
| -rw-r--r-- | bittorrent/mbtstr/src/ctor.c | 19 | ||||
| -rw-r--r-- | bittorrent/mbtstr/src/dtor.c | 15 | ||||
| -rw-r--r-- | bittorrent/mbtstr/src/fprint.c | 17 | ||||
| -rw-r--r-- | bittorrent/mbtstr/src/free.c | 11 | ||||
| -rw-r--r-- | bittorrent/mbtstr/src/init.c | 12 | ||||
| -rw-r--r-- | bittorrent/mbtstr/src/pushc.c | 18 | ||||
| -rw-r--r-- | bittorrent/mbtstr/src/pushcstr.c | 11 | ||||
| -rw-r--r-- | bittorrent/mbtstr/src/pushcv.c | 10 |
10 files changed, 147 insertions, 0 deletions
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; +} |
