1
0

libfatstr -> libfatptr

This commit is contained in:
dtb
2023-08-30 19:28:01 -04:00
parent 565ef686c5
commit 8905d938fe
5 changed files with 61 additions and 58 deletions

1
libfatptr/Makefile Normal file
View File

@@ -0,0 +1 @@
libfatptr.o:

72
libfatptr/libfatptr.c Normal file
View File

@@ -0,0 +1,72 @@
#include <stdlib.h> /* free(3), malloc(3), realloc(3) */
#include <string.h> /* strncpy(3), size_t */
#include "libfatptr.h"
/* quick and dirty ASCII edition */
/* Size by which to grow memory allocations. */
static size_t FatPtr_growinterval = 100; /* in sizeof chars */
struct FatPtr *
FatPtr_append(struct FatPtr *p, FatPtr_scalar_t c){
FatPtr_vector_t t;
if(p == NULL || (p->s + (sizeof *(p->v)) > p->a
&& FatPtr_grow(p, FatPtr_growinterval) == NULL))
return NULL;
(p->v)[(p->s)++] = c;
return p;
}
struct FatPtr *
FatPtr_construct(struct FatPtr *p){
return FatPtr_grow(FatPtr_initialize(p), FatPtr_growinterval);
}
char *
FatPtr_convert(struct FatPtr *p){
char *r;
/* allocation might be wrong */
return (p == NULL || (r = malloc(p->s * (sizeof *(p->v)))) == NULL)
? NULL
: strncpy(r, p->v, p->s)
;
}
struct FatPtr *
FatPtr_destruct(struct FatPtr *p){
if(p != NULL){
free(p->v);
FatPtr_initialize(p);
}
return NULL;
}
struct FatPtr *
FatPtr_grow(struct FatPtr *p, size_t units){
FatPtr_vector_t t;
if(p == NULL || (t = realloc(p->v, p->a + units)) == NULL)
return NULL;
p->a += units;
p->v = t;
return p;
}
struct FatPtr *
FatPtr_initialize(struct FatPtr *p){
if(p != NULL){
p->a = 0;
p->s = 0;
p->v = NULL;
}
return p;
}
struct FatPtr *
FatPtr_trimfront(struct FatPtr *p, size_t units){
}

40
libfatptr/libfatptr.h Normal file
View File

@@ -0,0 +1,40 @@
/* #include <stdlib.h> /* size_t */
#if !defined FatPtr_size_t
# define FatPtr_size_t size_t
#endif
#if !defined FatPtr_scalar_t
# define FatPtr_scalar_t int
#endif
#define FatPtr_vector_t FatPtr_scalar_t *
struct FatPtr{
FatPtr_size_t a; /* allocation in sizeof char */
FatPtr_size_t s; /* actual size in sizeof *v */
FatPtr_vector_t v; /* vector */
};
/* FatPtr_append
* - FatPtr *p: object to which to append
* - FatPtr_scalar_t c: scalar to be appended
* Returns NULL if the operation failed (due to malloc(3)) or if p was NULL.
* Otherwise returns p. */
struct FatPtr *FatPtr_append(struct FatPtr *p, FatPtr_scalar_t c);
struct FatPtr *FatPtr_construct(struct FatPtr *p);
/* FatPtr_convert
* - FatPtr *p: object to convert
* Returns NULL if the operation failed (due to malloc(3)) or if p was NULL.
* Otherwise returns a malloc(3)d char * with the current content of p, which
* should be free(3)d when no longer in use. */
char *FatPtr_convert(struct FatPtr *p);
/* FatPtr_destruct
* - FatPtr *p: object to destruct
* Returns NULL. */
struct FatPtr *FatPtr_destruct(struct FatPtr *p);
struct FatPtr *FatPtr_grow(struct FatPtr *p, size_t units);
struct FatPtr *FatPtr_initialize(struct FatPtr *p);
struct FatPtr *FatPtr_trimfront(struct FatPtr *p, size_t units);