refactoring server and add myclib

This commit is contained in:
2025-08-02 13:35:56 +02:00
parent fa964b2620
commit 2918aeadcb
14 changed files with 505 additions and 292 deletions

View File

@@ -0,0 +1,119 @@
#include "mystring.h"
#include <math.h>
#include <stdlib.h>
#include <string.h>
mcl_string *mcl_string_new(const char *text, long initial_capacity) {
if (!text) {
return NULL;
}
/* Allocate string struct */
mcl_string *str = malloc(sizeof(mcl_string));
if (!str) {
return NULL;
}
/* Calculate size and capacity */
str->size = strlen(text);
size_t capacity = initial_capacity;
if (capacity != -1 && capacity - 1 < str->size) {
return NULL;
}
if (capacity == -1) {
capacity = (unsigned long)pow(2, (unsigned)log2(str->size) + 1);
}
str->capacity = capacity;
/* Allocate data buffer */
str->data = malloc(sizeof(char) * str->capacity);
if (!str->data) {
free(str);
return NULL;
}
/* Copy the text and ensure null termination */
memset(str->data, 0, str->capacity);
memcpy(str->data, text, str->size);
str->data[str->size] = '\0';
return str;
}
int mcl_string_append(mcl_string *string, const char *text) {
if (!string || !text) {
return -1;
}
/* Handle empty case */
size_t text_len = strlen(text);
if (text_len == 0) {
return 0;
}
size_t new_size = text_len + string->size;
/* Check if we need to resize */
if (new_size + 1 > string->capacity) {
size_t new_capacity = (unsigned long)pow(2, (unsigned)log2(new_size) + 1);
/* Reallocate the buffer */
void *new_data = realloc(string->data, sizeof(char) * new_capacity);
if (!new_data) {
return -1;
}
string->data = new_data;
string->capacity = new_capacity;
/* Init to 0 the new capacity */
memset(string->data + string->size, 0, string->capacity - string->size);
}
/* Append text */
memcpy(string->data + string->size, text, text_len);
string->size = new_size;
string->data[string->size] = '\0';
return 0;
}
void mcl_string_free(mcl_string *string) {
if (!string) {
return;
}
if (string->data) {
free(string->data);
}
free(string);
}
size_t mcl_string_length(const mcl_string *string) {
if (!string) {
return 0;
}
return string->size;
}
size_t mcl_string_capacity(const mcl_string *string) {
if (!string) {
return 0;
}
return string->capacity;
}
const char *mcl_string_cstr(const mcl_string *string) {
if (!string || !string->data) {
return "";
}
return string->data;
}

View File

@@ -0,0 +1,73 @@
#ifndef MYCLIB_STRING_H
#define MYCLIB_STRING_H
#include <stddef.h>
/**
* @brief String structure
*/
typedef struct mcl_string_t {
size_t size; /**< Length of the string (excluding null terminator) */
size_t capacity; /**< Total allocated capacity */
char *data; /**< Pointer to the string data */
} mcl_string;
/**
* @brief Create a new string
*
* @param text The text to initialize from
* @param initial_capacity The initial capacity, pass -1 to retrieve it from the text
* @return Pointer to the new string, or NULL on failure
*
* @note The caller is responsible for freeing the returned string with mcl_string_free() and to pass the right inital_capacity
*/
mcl_string *mcl_string_new(const char *text, long initial_capacity);
/**
* @brief Append text to an existing string
*
* @param string The string to append to
* @param text The string to append (can be empty)
* @return 0 on success, -1 on failure
*
* @note If it fails, the original string remains unchanged
*/
int mcl_string_append(mcl_string *string, const char *text);
/**
* @brief Free a string
*
* @param string The string to free
*
* @note This function is safe to call with NULL pointers
*/
void mcl_string_free(mcl_string *string);
/**
* @brief Get the current length of the string
*
* @param string The string to query
* @return The length of the string, or 0 if string is NULL
*/
size_t mcl_string_length(const mcl_string *string);
/**
* @brief Get the current capacity of the string
*
* @param string The string to query
* @return The capacity of the string buffer, or 0 if string is NULL
*/
size_t mcl_string_capacity(const mcl_string *string);
/**
* @brief Get a read-only string representation
*
* @param string The string to access
* @return Pointer to null-terminated string data, or empty string "" if string is NULL
*
* @warning The returned pointer should not be modified directly and may become
* invalid after any modification operation on the string
*/
const char *mcl_string_cstr(const mcl_string *string);
#endif /* MYCLIB_STRING_H */