more better
This commit is contained in:
parent
ac638c48eb
commit
bacd306020
@ -1,42 +1,35 @@
|
|||||||
#include <stdlib.h> /* free(3), malloc(3), realloc(3) */
|
#include <stdlib.h> /* free(3), malloc(3), realloc(3) */
|
||||||
#include <string.h> /* strncpy(3) */
|
#include <string.h> /* strncpy(3), size_t */
|
||||||
#include "libfatstr.h"
|
#include "libfatstr.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. */
|
||||||
#define GROW_INTERVAL 100
|
static size_t FatStr_growinterval = 100; /* in sizeof chars */
|
||||||
|
|
||||||
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 *
|
struct FatStr *
|
||||||
FatStr_append(struct FatStr *p, FatStr_scalar_t c){
|
FatStr_append(struct FatStr *p, FatStr_scalar_t c){
|
||||||
FatStr_vector_t t;
|
FatStr_vector_t t;
|
||||||
|
|
||||||
if(p->s + 1 > p->a){
|
if(p == NULL || (p->s + (sizeof *(p->v)) > p->a
|
||||||
if((t = realloc(p->v, (sizeof *(p->v)) * (p->a + GROW_INTERVAL)))
|
&& FatStr_grow(p, FatStr_growinterval) == NULL))
|
||||||
== NULL)
|
return NULL;
|
||||||
return NULL;
|
|
||||||
p->a += GROW_INTERVAL;
|
|
||||||
}
|
|
||||||
(p->v)[(p->s)++] = c;
|
(p->v)[(p->s)++] = c;
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct FatStr *
|
||||||
|
FatStr_construct(struct FatStr *p){
|
||||||
|
|
||||||
|
return FatStr_grow(FatStr_initialize(p), FatStr_growinterval);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* retval will need to be freed */
|
|
||||||
char *
|
char *
|
||||||
FatStr_convert(struct FatStr *p){
|
FatStr_convert(struct FatStr *p){
|
||||||
char *r;
|
char *r;
|
||||||
|
|
||||||
/* allocation might be wrong */
|
/* allocation might be wrong */
|
||||||
return ((r = malloc(p->s * (sizeof *(p->v)))) == NULL)
|
return (p == NULL || (r = malloc(p->s * (sizeof *(p->v)))) == NULL)
|
||||||
? NULL
|
? NULL
|
||||||
: strncpy(r, p->v, p->s)
|
: strncpy(r, p->v, p->s)
|
||||||
;
|
;
|
||||||
@ -44,10 +37,36 @@ FatStr_convert(struct FatStr *p){
|
|||||||
|
|
||||||
struct FatStr *
|
struct FatStr *
|
||||||
FatStr_destruct(struct FatStr *p){
|
FatStr_destruct(struct FatStr *p){
|
||||||
free(p->v);
|
|
||||||
|
if(p != NULL){
|
||||||
|
free(p->v);
|
||||||
|
FatStr_initialize(p);
|
||||||
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct FatStr *
|
struct FatStr *
|
||||||
FatStr_trimfront(){
|
FatStr_grow(struct FatStr *p, size_t units){
|
||||||
|
FatStr_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 FatStr *
|
||||||
|
FatStr_initialize(struct FatStr *p){
|
||||||
|
|
||||||
|
if(p != NULL){
|
||||||
|
p->a = 0;
|
||||||
|
p->s = 0;
|
||||||
|
p->v = NULL;
|
||||||
|
}
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct FatStr *
|
||||||
|
FatStr_trimfront(struct FatStr *p, size_t units){
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user