diff --git a/ase/cmn/str.c b/ase/cmn/str.c new file mode 100644 index 00000000..7db6772d --- /dev/null +++ b/ase/cmn/str.c @@ -0,0 +1,165 @@ +/* + * $Id: str.c,v 1.1 2007-02-22 14:32:08 bacon Exp $ + * + * {License} + */ + +#include + +ase_str_t* ase_str_open (ase_str_t* str, ase_size_t capa) +{ + if (str == ASE_NULL) + { + str = (ase_str_t*) + ASE_AWK_MALLOC (awk, ASE_SIZEOF(ase_str_t)); + if (str == ASE_NULL) return ASE_NULL; + str->__dynamic = ase_true; + } + else str->__dynamic = ase_false; + + str->awk = awk; + str->buf = (ase_char_t*) ASE_AWK_MALLOC ( + awk, ASE_SIZEOF(ase_char_t) * (capa + 1)); + if (str->buf == ASE_NULL) + { + if (str->__dynamic) ASE_AWK_FREE (awk, str); + return ASE_NULL; + } + + str->size = 0; + str->capa = capa; + str->buf[0] = ASE_T('\0'); + + return str; +} + +void ase_str_close (ase_str_t* str) +{ + ASE_AWK_FREE (str->awk, str->buf); + if (str->__dynamic) ASE_AWK_FREE (str->awk, str); +} + +void ase_str_clear (ase_str_t* str) +{ + str->size = 0; + str->buf[0] = ASE_T('\0'); +} + +void ase_str_forfeit (ase_str_t* str) +{ + if (str->__dynamic) ASE_AWK_FREE (str->awk, str); +} + +void ase_str_swap (ase_str_t* str, ase_str_t* str1) +{ + ase_str_t tmp; + + tmp.buf = str->buf; + tmp.size = str->size; + tmp.capa = str->capa; + tmp.awk = str->awk; + + str->buf = str1->buf; + str->size = str1->size; + str->capa = str1->capa; + str->awk = str1->awk; + + str1->buf = tmp.buf; + str1->size = tmp.size; + str1->capa = tmp.capa; + str1->awk = tmp.awk; +} + +ase_size_t ase_str_cpy (ase_str_t* str, const ase_char_t* s) +{ + /* TODO: improve it */ + return ase_str_ncpy (str, s, ase_strlen(s)); +} + +ase_size_t ase_str_ncpy (ase_str_t* str, const ase_char_t* s, ase_size_t len) +{ + ase_char_t* buf; + + if (len > str->capa) + { + buf = (ase_char_t*) ASE_AWK_MALLOC ( + str->awk, ASE_SIZEOF(ase_char_t) * (len + 1)); + if (buf == ASE_NULL) return (ase_size_t)-1; + + ASE_AWK_FREE (str->awk, str->buf); + str->capa = len; + str->buf = buf; + } + + str->size = ase_strncpy (str->buf, s, len); + str->buf[str->size] = ASE_T('\0'); + return str->size; +} + +ase_size_t ase_str_cat (ase_str_t* str, const ase_char_t* s) +{ + /* TODO: improve it */ + return ase_str_ncat (str, s, ase_strlen(s)); +} + +ase_size_t ase_str_ncat (ase_str_t* str, const ase_char_t* s, ase_size_t len) +{ + if (len > str->capa - str->size) + { + ase_char_t* tmp; + ase_size_t capa; + + capa = str->size + len; + + /* double the capa if necessary for concatenation */ + if (capa < str->capa * 2) capa = str->capa * 2; + + if (str->awk->prmfns.realloc != ASE_NULL) + { + tmp = (ase_char_t*) ASE_AWK_REALLOC ( + str->awk, str->buf, + ASE_SIZEOF(ase_char_t) * (capa + 1)); + if (tmp == ASE_NULL) return (ase_size_t)-1; + } + else + { + tmp = (ase_char_t*) ASE_AWK_MALLOC ( + str->awk, ASE_SIZEOF(ase_char_t) * (capa + 1)); + if (tmp == ASE_NULL) return (ase_size_t)-1; + if (str->buf != ASE_NULL) + { + ASE_AWK_MEMCPY (str->awk, tmp, str->buf, + ASE_SIZEOF(ase_char_t) * (str->capa + 1)); + ASE_AWK_FREE (str->awk, str->buf); + } + } + + str->capa = capa; + str->buf = tmp; + } + + str->size += ase_strncpy (&str->buf[str->size], s, len); + str->buf[str->size] = ASE_T('\0'); + return str->size; +} + +ase_size_t ase_str_ccat (ase_str_t* str, ase_char_t c) +{ + return ase_str_ncat (str, &c, 1); +} + +ase_size_t ase_str_nccat (ase_str_t* str, ase_char_t c, ase_size_t len) +{ + while (len > 0) + { + if (ase_str_ncat (str, &c, 1) == (ase_size_t)-1) + { + return (ase_size_t)-1; + } + + len--; + } + return str->size; +} + + diff --git a/ase/cmn/str.h b/ase/cmn/str.h new file mode 100644 index 00000000..31e05fb6 --- /dev/null +++ b/ase/cmn/str.h @@ -0,0 +1,52 @@ +/* + * $Id: str.h,v 1.1 2007-02-22 14:32:08 bacon Exp $ + * + * {License} + */ + +#ifndef _ASE_CMN_STR_H_ +#define _ASE_CMN_STR_H_ + +#include +#include + +#define ASE_STR_LEN(x) ((x)->size) +#define ASE_STR_SIZE(x) ((x)->size + 1) +#define ASE_STR_CAPA(x) ((x)->capa) +#define ASE_STR_BUF(x) ((x)->buf) +#define ASE_STR_CHAR(x,idx) ((x)->buf[idx]) + +typedef struct ase_str_t ase_str_t; + +struct ase_str_t +{ + ase_char_t* buf; + ase_size_t size; + ase_size_t capa; + ase_bool_t __dynamic; +}; + +#ifdef __cplusplus +extern "C" { +#endif + +ase_str_t* ase_str_open (ase_str_t* str, ase_size_t capa, ase_t* awk); +void ase_str_close (ase_str_t* str); +void ase_str_clear (ase_str_t* str); + +void ase_str_forfeit (ase_str_t* str); +void ase_str_swap (ase_str_t* str, ase_str_t* str2); + +ase_size_t ase_str_cpy (ase_str_t* str, const ase_char_t* s); +ase_size_t ase_str_ncpy (ase_str_t* str, const ase_char_t* s, ase_size_t len); + +ase_size_t ase_str_cat (ase_str_t* str, const ase_char_t* s); +ase_size_t ase_str_ncat (ase_str_t* str, const ase_char_t* s, ase_size_t len); +ase_size_t ase_str_ccat (ase_str_t* str, ase_char_t c); +ase_size_t ase_str_nccat (ase_str_t* str, ase_char_t c, ase_size_t len); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/ase/utl/ctype.h b/ase/utl/ctype.h index f382b705..ff7ba998 100644 --- a/ase/utl/ctype.h +++ b/ase/utl/ctype.h @@ -2,8 +2,8 @@ * $id$ */ -#ifndef _ASE_CTYPE_H_ -#define _ASE_CTYPE_H_ +#ifndef _ASE_UTL_CTYPE_H_ +#define _ASE_UTL_CTYPE_H_ #include #include diff --git a/ase/utl/main.h b/ase/utl/main.h index 9cff1fc1..20ab61a1 100644 --- a/ase/utl/main.h +++ b/ase/utl/main.h @@ -1,9 +1,9 @@ /* - * $Id: main.h,v 1.1 2007-02-20 14:04:21 bacon Exp $ + * $Id: main.h,v 1.2 2007-02-22 14:32:08 bacon Exp $ */ -#ifndef _ASE_MAIN_H_ -#define _ASE_MAIN_H_ +#ifndef _ASE_UTL_MAIN_H_ +#define _ASE_UTL_MAIN_H_ #include #include diff --git a/ase/utl/stdio.h b/ase/utl/stdio.h index 7ef44fcd..13779821 100644 --- a/ase/utl/stdio.h +++ b/ase/utl/stdio.h @@ -1,9 +1,9 @@ /* - * $Id: stdio.h,v 1.2 2007-02-21 04:09:28 bacon Exp $ + * $Id: stdio.h,v 1.3 2007-02-22 14:32:08 bacon Exp $ */ -#ifndef _ASE_STDIO_H_ -#define _ASE_STDIO_H_ +#ifndef _ASE_UTL_STDIO_H_ +#define _ASE_UTL_STDIO_H_ #include #include