1
0
src/libfatstr/libfatstr.c
2023-08-29 21:07:42 -04:00

53 lines
1017 B
C

#include <stdlib.h> /* free(3), malloc(3), realloc(3) */
#include <string.h> /* strncpy(3) */
#include "fatstr.h"
/* quick and dirty ASCII edition */
/* Size by which to grow memory allocations. */
#define GROW_INTERVAL 100
struct FatStr *
FatStr_initialize(struct FatStr *p){
if((p->v = malloc((sizeof *(p->v)) * GROW_INTERVAL)) == NULL)
return NULL; /* who knows */
p->a = GROW_INTERVAL;
p->s = 0;
return p;
}
struct FatStr *
FatStr_append(struct FatStr *p, FatStr_scalar_t c){
FatStr_vector_t t;
if(p->s + 1 > p->a){
if((t = realloc(p->v, (sizeof *(p->v)) * (p->a + GROW_INTERVAL)))
== NULL)
return NULL;
p->a += GROW_INTERVAL;
}
(p->v)[(p->s)++] = c;
}
/* retval will need to be freed */
char *
FatStr_convert(struct FatStr *p){
char *r;
if((r = malloc(p->s * (sizeof *(p->v)))) == NULL) /* might be buggy */
return NULL;
strncpy(r, p->v, p->s);
return r;
}
struct FatStr *
FatStr_destruct(struct FatStr *p){
free(p->v);
return NULL;
}
struct FatStr *
FatStr_trimfront(){
}