1
0

work on fatstr impl

This commit is contained in:
dtb 2023-08-29 21:07:42 -04:00
parent a5413c68a7
commit 87bc04b75a
3 changed files with 69 additions and 0 deletions

2
libfatstr/Makefile Normal file
View File

@ -0,0 +1,2 @@
libfatstr.o: libfatstr.c libfatstr.h
$(CC) -g -o libfatstr.o libfatstr.c

52
libfatstr/libfatstr.c Normal file
View File

@ -0,0 +1,52 @@
#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(){
}

15
libfatstr/libfatstr.h Normal file
View File

@ -0,0 +1,15 @@
typedef FatStr_size_t size_t;
typedef FatStr_scalar_t int;
typedef FatStr_vector_t FatStr_scalar_t *;
struct FatStr{
FatStr_size_t a; /* allocation */
FatStr_size_t s; /* actual size */
FatStr_vector_t v; /* vector */
};
struct FatStr *FatStr_append(struct FatStr *p, FatStr_scalar_t c);
FatStr_vector_t FatStr_convert(struct FatStr *p);
struct FatStr *FatStr_destruct(struct FatStr *p);
struct FatStr *FatStr_initialize(struct FatStr *p);
struct FatStr *FatStr_trimfront(