50 lines
1.2 KiB
C
50 lines
1.2 KiB
C
#ifndef SORM_STR_H
|
|
#define SORM_STR_H
|
|
#include <rlib.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
typedef struct sormstr_t {
|
|
char *content;
|
|
size_t length;
|
|
size_t buffer_size;
|
|
size_t size;
|
|
} sormstr_t;
|
|
|
|
sormstr_t *sormstrn(size_t buffer_size) {
|
|
sormstr_t *result = (sormstr_t *)malloc(sizeof(sormstr_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 sormstra(sormstr_t *str, const char *to_append) {
|
|
size_t required_new_length = str->length + strlen(to_append);
|
|
str->length += strlen(to_append);
|
|
if (required_new_length > str->size) {
|
|
str->size += required_new_length + str->buffer_size;
|
|
str->content = realloc(str->content, str->size + 1);
|
|
} else {
|
|
// printf("NO NDEED\n");
|
|
}
|
|
strcat(str->content, to_append);
|
|
str->content[str->length] = 0;
|
|
}
|
|
void sormstrd(sormstr_t *str) {
|
|
if (str->content) {
|
|
free(str->content);
|
|
}
|
|
free(str);
|
|
}
|
|
char *sormstrc(sormstr_t *str) {
|
|
// sorm str convert
|
|
char *content = str->content;
|
|
str->content = NULL;
|
|
sormstrd(str);
|
|
return content;
|
|
}
|
|
|
|
#endif |