Gama C Library
Gama C API Documentation
str.h
Go to the documentation of this file.
1/**
2 * @file str.h
3 * @brief Defines a dynamic string structure and provides utility functions for its manipulation.
4 *
5 * This file offers a simple dynamic string (`gmStr`) implementation that can
6 * grow as needed, making it suitable for building strings without fixed-size
7 * buffer limitations.
8 */
9#pragma once
10
11#include "_malloc.h" // Assuming this is where custom malloc/realloc are defined
12
13#include <stddef.h>
14#include <string.h>
15/**
16 * @brief Represents a dynamic, heap-allocated string.
17 *
18 * This structure stores both the current length of the string and a pointer
19 * to its content, allowing for efficient string manipulation and growth.
20 */
21typedef struct {
22 size_t length; /**< The current length of the string (excluding null terminator). */
23 char *content; /**< Pointer to the heap-allocated string content (null-terminated). */
24} gmStr;
25
26/**
27 * @brief Initializes an empty `gmStr` struct.
28 * @return A new `gmStr` instance with length 0 and NULL content.
29 */
30gmStr gm_str() { return (gmStr){.length = 0, .content = NULL}; }
31
32/**
33 * @brief Appends a specified number of characters from a C string to a `gmStr`.
34 *
35 * This function reallocates memory for the `gmStr`'s content if necessary
36 * to accommodate the new characters.
37 *
38 * @param s A pointer to the `gmStr` to append to.
39 * @param n The number of characters from `txt` to append.
40 * @param txt The null-terminated C string to append.
41 * @return 0 on success, -1 on invalid input, -5 on reallocation failure.
42 */
43int gm_str_appendn(gmStr *s, size_t n, const char *txt) {
44 if (!s || !txt || n == 0)
45 return -1;
46 if (s->length == 0) {
47 s->length = 0;
48 s->content = malloc(n + 1);
49 s->content[n] = '\0'; // Ensure null-termination
50 } else {
51 size_t newcap = s->length + n + 1;
52 char *newtxt = realloc(s->content, newcap);
53 newtxt[newcap - 1] = '\0';
54 if (!newtxt)
55 return -5;
56 s->content = newtxt;
57 }
58 for (size_t i = 0; i < n; i++) {
59 s->content[s->length] = txt[i];
60 s->length++;
61 }
62 return 0;
63}
64
65/**
66 * @brief Appends a null-terminated C string to a `gmStr`.
67 * @param s A pointer to the `gmStr` to append to.
68 * @param txt The null-terminated C string to append.
69 * @return 0 on success, or an error code from `gm_str_appendn`.
70 */
71static inline int gm_str_append(gmStr *s, const char *txt) {
72 return gm_str_appendn(s, strlen(txt), txt);
73}
74
75/**
76 * @brief Frees the heap-allocated content of a `gmStr` and resets its length to 0.
77 *
78 * The `gmStr` struct itself is not freed, only its internal buffer.
79 *
80 * @param str A pointer to the `gmStr` to clear.
81 */
82void gm_str_clear(gmStr *str) {
83 if (str && str->content) {
84 free(str->content);
85 str->content = NULL;
86 }
87 if (str) {
88 str->length = 0;
89 }
90}
void * malloc(size_t size)
Custom implementation of malloc using a static memory pool.
Definition malloc.h:144
void * realloc(void *ptr, size_t size)
Custom implementation of realloc for memory allocated by malloc (this custom version).
Definition malloc.h:236
void free(void *ptr)
Custom implementation of free for memory allocated by malloc (this custom version).
Definition malloc.h:189
void gm_str_clear(gmStr *str)
Frees the heap-allocated content of a gmStr and resets its length to 0.
Definition str.h:82
gmStr gm_str()
Initializes an empty gmStr struct.
Definition str.h:30
int gm_str_appendn(gmStr *s, size_t n, const char *txt)
Appends a specified number of characters from a C string to a gmStr.
Definition str.h:43
Represents a dynamic, heap-allocated string.
Definition str.h:21
char * content
Definition str.h:23
size_t length
Definition str.h:22