isspam/rstr.h

52 lines
1.2 KiB
C
Raw Normal View History

2024-11-28 17:39:34 +00:00
#ifndef RSTR_H
#define RSTR_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct str_t {
char *content;
size_t length;
size_t buffer_size;
size_t size;
} str_t;
str_t *strn(size_t buffer_size) {
str_t *result = (str_t *)malloc(sizeof(str_t));
result->length = 0;
result->size = buffer_size;
result->buffer_size = buffer_size;
result->content = (char *)malloc(buffer_size);
result->content[0] = 0;
return result;
}
void stra(str_t *str, const char *to_append) {
size_t required_new_length = str->length + strlen(to_append) + 1;
str->length += strlen(to_append);
if (required_new_length > str->size) {
str->size += required_new_length + str->buffer_size;
str->content = (char *)realloc(str->content, str->size + 1);
} else {
// printf("NO NDEED\n");
}
strcat(str->content, to_append);
str->content[str->length] = 0;
}
void strac(str_t *str, const char c) {
char to_append[] = {c, 0};
stra(str, to_append);
}
void strd(str_t *str) {
if (str->content) {
free(str->content);
}
free(str);
}
char *strc(str_t *str) {
char *content = str->content;
str->content = NULL;
strd(str);
return content;
}
#endif