50 lines
1.2 KiB
C
Raw Normal View History

2024-11-22 13:45:03 +00:00
#ifndef SORM_STR_H
#define SORM_STR_H
#include <rlib.h>
#include <stdio.h>
#include <stdlib.h>
2024-11-22 14:51:47 +00:00
#include <string.h>
2024-11-22 13:45:03 +00:00
typedef struct sormstr_t {
2024-11-22 14:51:47 +00:00
char *content;
2024-11-22 13:45:03 +00:00
size_t length;
size_t buffer_size;
size_t size;
} sormstr_t;
2024-11-22 14:51:47 +00:00
sormstr_t *sormstrn(size_t buffer_size) {
sormstr_t *result = (sormstr_t *)malloc(sizeof(sormstr_t));
2024-11-22 13:45:03 +00:00
result->length = 0;
result->size = buffer_size;
result->buffer_size = buffer_size;
result->content = (char *)malloc(buffer_size);
result->content[0] = 0;
return result;
}
2024-11-22 14:51:47 +00:00
void sormstra(sormstr_t *str, const char *to_append) {
2024-11-22 13:45:03 +00:00
size_t required_new_length = str->length + strlen(to_append);
str->length += strlen(to_append);
2024-11-22 14:51:47 +00:00
if (required_new_length > str->size) {
2024-11-22 13:45:03 +00:00
str->size += required_new_length + str->buffer_size;
2024-11-22 14:51:47 +00:00
str->content = realloc(str->content, str->size + 1);
} else {
// printf("NO NDEED\n");
2024-11-22 13:45:03 +00:00
}
2024-11-22 14:51:47 +00:00
strcat(str->content, to_append);
2024-11-22 13:45:03 +00:00
str->content[str->length] = 0;
}
2024-11-22 14:51:47 +00:00
void sormstrd(sormstr_t *str) {
if (str->content) {
2024-11-22 13:45:03 +00:00
free(str->content);
}
free(str);
}
2024-11-22 14:51:47 +00:00
char *sormstrc(sormstr_t *str) {
2024-11-22 13:45:03 +00:00
// sorm str convert
2024-11-22 14:51:47 +00:00
char *content = str->content;
2024-11-22 13:45:03 +00:00
str->content = NULL;
sormstrd(str);
return content;
}
#endif