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:

View File

@ -1,31 +1,31 @@
#include <stdlib.h> /* free(3), malloc(3), realloc(3) */ #include <stdlib.h> /* free(3), malloc(3), realloc(3) */
#include <string.h> /* strncpy(3), size_t */ #include <string.h> /* strncpy(3), size_t */
#include "libfatstr.h" #include "libfatptr.h"
/* quick and dirty ASCII edition */ /* quick and dirty ASCII edition */
/* Size by which to grow memory allocations. */ /* Size by which to grow memory allocations. */
static size_t FatStr_growinterval = 100; /* in sizeof chars */ static size_t FatPtr_growinterval = 100; /* in sizeof chars */
struct FatStr * struct FatPtr *
FatStr_append(struct FatStr *p, FatStr_scalar_t c){ FatPtr_append(struct FatPtr *p, FatPtr_scalar_t c){
FatStr_vector_t t; FatPtr_vector_t t;
if(p == NULL || (p->s + (sizeof *(p->v)) > p->a if(p == NULL || (p->s + (sizeof *(p->v)) > p->a
&& FatStr_grow(p, FatStr_growinterval) == NULL)) && FatPtr_grow(p, FatPtr_growinterval) == NULL))
return NULL; return NULL;
(p->v)[(p->s)++] = c; (p->v)[(p->s)++] = c;
return p; return p;
} }
struct FatStr * struct FatPtr *
FatStr_construct(struct FatStr *p){ FatPtr_construct(struct FatPtr *p){
return FatStr_grow(FatStr_initialize(p), FatStr_growinterval); return FatPtr_grow(FatPtr_initialize(p), FatPtr_growinterval);
} }
char * char *
FatStr_convert(struct FatStr *p){ FatPtr_convert(struct FatPtr *p){
char *r; char *r;
/* allocation might be wrong */ /* allocation might be wrong */
@ -35,19 +35,19 @@ FatStr_convert(struct FatStr *p){
; ;
} }
struct FatStr * struct FatPtr *
FatStr_destruct(struct FatStr *p){ FatPtr_destruct(struct FatPtr *p){
if(p != NULL){ if(p != NULL){
free(p->v); free(p->v);
FatStr_initialize(p); FatPtr_initialize(p);
} }
return NULL; return NULL;
} }
struct FatStr * struct FatPtr *
FatStr_grow(struct FatStr *p, size_t units){ FatPtr_grow(struct FatPtr *p, size_t units){
FatStr_vector_t t; FatPtr_vector_t t;
if(p == NULL || (t = realloc(p->v, p->a + units)) == NULL) if(p == NULL || (t = realloc(p->v, p->a + units)) == NULL)
return NULL; return NULL;
@ -56,8 +56,8 @@ FatStr_grow(struct FatStr *p, size_t units){
return p; return p;
} }
struct FatStr * struct FatPtr *
FatStr_initialize(struct FatStr *p){ FatPtr_initialize(struct FatPtr *p){
if(p != NULL){ if(p != NULL){
p->a = 0; p->a = 0;
@ -67,6 +67,6 @@ FatStr_initialize(struct FatStr *p){
return p; return p;
} }
struct FatStr * struct FatPtr *
FatStr_trimfront(struct FatStr *p, size_t units){ 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);

View File

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

View File

@ -1,37 +0,0 @@
/* #include <stdlib.h> /* size_t */
/* These definitions aren't set in stone. */
typedef size_t FatStr_size_t;
typedef int FatStr_scalar_t;
typedef FatStr_scalar_t * FatStr_vector_t;
/* This definition is dependable. */
struct FatStr{
FatStr_size_t a; /* allocation in sizeof char */
FatStr_size_t s; /* actual size in sizeof *v */
FatStr_vector_t v; /* vector */
};
/* FatStr_append
* - FatStr *p: object to which to append
* - FatStr_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 FatStr *FatStr_append(struct FatStr *p, FatStr_scalar_t c);
struct FatStr *FatStr_construct(struct FatStr *p);
/* FatStr_convert
* - FatStr *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 *FatStr_convert(struct FatStr *p);
/* FatStr_destruct
* - FatStr *p: object to destruct
* Returns NULL. */
struct FatStr *FatStr_destruct(struct FatStr *p);
struct FatStr *FatStr_grow(struct FatStr *p, size_t units);
struct FatStr *FatStr_initialize(struct FatStr *p);
struct FatStr *FatStr_trimfront(struct FatStr *p, size_t units);