NEWS: Welcome to my new homepage! <3

feat: Initial commit - libstr - Unnamed repository; edit this file 'description' to name the repository.

libstr

Unnamed repository; edit this file 'description' to name the repository.
git clone git://192.168.2.2/libstr
Log | Files | Refs | README

commit fd80a2a08462fc3f9b0bbbbed96fb53bd73e11da
Author: Sophie <sophie@aest.me>
Date:   Wed, 25 Sep 2024 11:47:51 +0200

feat: Initial commit

Diffstat:
AMakefile | 18++++++++++++++++++
AREADME.md | 9+++++++++
Alibstr.c | 46++++++++++++++++++++++++++++++++++++++++++++++
Alibstr.h | 6++++++
4 files changed, 79 insertions(+), 0 deletions(-)

diff --git a/Makefile b/Makefile @@ -0,0 +1,18 @@ +default: build + +build: + cc -o libstr.so -shared -fPIC -lpthread -lssl -Wall -Wextra libstr.c + +install: + cc -o libstr.so -shared -fPIC libstr.c + sudo cp libstr.h /usr/local/include/ + sudo cp libstr.so /usr/local/lib/ + sudo ldconfig + rm libstr.so + +uninstall: + sudo rm /usr/local/include/libstr.h + sudo rm /usr/local/lib/libstr.so + +clean: + rm libstr.so diff --git a/README.md b/README.md @@ -0,0 +1,9 @@ +# libstr + +A util library for dealing with strings in C + +## Installation + +``` +sudo make install +``` diff --git a/libstr.c b/libstr.c @@ -0,0 +1,46 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <stdarg.h> + +#include "libstr.h" + +void str_append_len(char **buffer, char *value, int len) { + int buffer_len = 0; + if (*buffer != NULL) { + buffer_len = strlen(*buffer); + } + *buffer = realloc(*buffer, sizeof(char) * (buffer_len + len + 1)); + memcpy(*buffer + buffer_len, value, len); + (*buffer)[buffer_len + len] = '\0'; +} + +void str_append(char **buffer, char *value) { + int len = strlen(value); + str_append_len(buffer, value, len); +} + +void str_appendf(char **buffer, const char *format, ...) { + va_list args; + va_start(args, format); + char str[1000]; + vsnprintf(str, sizeof(str), format, args); + str_append(buffer, str); + va_end(args); +} + +char *str_replace_all(char *buffer, char *str, char *replacement) { + char *result = NULL; + int str_len = strlen(str); + for (int i = 0; i < (int) strlen(buffer); i++) { + char *ptr = &buffer[i]; + if (strncmp(ptr, str, str_len) == 0) { + str_append(&result, replacement); + i += str_len - 1; + } + else { + str_append_len(&result, &buffer[i], 1); + } + } + return result; +} diff --git a/libstr.h b/libstr.h @@ -0,0 +1,6 @@ +#pragma once + +void str_append_len(char **buffer, char *value, int len); +void str_append(char **buffer, char *value); +void str_appendf(char **buffer, const char *format, ...); +char *str_replace_all(char *buffer, char *str, char *replacement);