removed ase_str_forfeit() and added ase_str_yield()

This commit is contained in:
hyung-hwan 2008-09-01 04:55:59 +00:00
parent 91159085ed
commit ae2f49f1a1
2 changed files with 88 additions and 29 deletions

View File

@ -1,5 +1,5 @@
/* /*
* $Id: str.h 223 2008-06-26 06:44:41Z baconevi $ * $Id: str.h 352 2008-08-31 10:55:59Z baconevi $
* *
* {License} * {License}
*/ */
@ -20,21 +20,20 @@ typedef struct ase_str_t ase_str_t;
struct ase_str_t struct ase_str_t
{ {
ase_mmgr_t* mmgr;
ase_char_t* buf; ase_char_t* buf;
ase_size_t size; ase_size_t size;
ase_size_t capa; ase_size_t capa;
ase_mmgr_t* mmgr;
ase_bool_t __dynamic;
}; };
/* int ase_chartonum (ase_char_t c, int base) */ /* int ase_chartonum (ase_char_t c, int base) */
#define ASE_CHARTONUM(c,base) \ #define ASE_CHAR_TO_NUM(c,base) \
((c>=ASE_T('0') && c<=ASE_T('9'))? ((c-ASE_T('0')<base)? (c-ASE_T('0')): base): \ ((c>=ASE_T('0') && c<=ASE_T('9'))? ((c-ASE_T('0')<base)? (c-ASE_T('0')): base): \
(c>=ASE_T('A') && c<=ASE_T('Z'))? ((c-ASE_T('A')+10<base)? (c-ASE_T('A')+10): base): \ (c>=ASE_T('A') && c<=ASE_T('Z'))? ((c-ASE_T('A')+10<base)? (c-ASE_T('A')+10): base): \
(c>=ASE_T('a') && c<=ASE_T('z'))? ((c-ASE_T('a')+10<base)? (c-ASE_T('a')+10): base): base) (c>=ASE_T('a') && c<=ASE_T('z'))? ((c-ASE_T('a')+10<base)? (c-ASE_T('a')+10): base): base)
/* ase_strtonum (const ase_char_t* nptr, ase_char_t** endptr, int base) */ /* ase_strtonum (const ase_char_t* nptr, ase_char_t** endptr, int base) */
#define ASE_STRTONUM(value,nptr,endptr,base) \ #define ASE_STR_TO_NUM(value,nptr,endptr,base) \
{ \ { \
int __ston_f = 0, __ston_v; \ int __ston_f = 0, __ston_v; \
const ase_char_t* __ston_ptr = nptr; \ const ase_char_t* __ston_ptr = nptr; \
@ -46,7 +45,7 @@ struct ase_str_t
if (__ston_c == ASE_T('+')) { __ston_ptr++; } \ if (__ston_c == ASE_T('+')) { __ston_ptr++; } \
break; \ break; \
} \ } \
for (value = 0; (__ston_v = ASE_CHARTONUM(*__ston_ptr, base)) < base; __ston_ptr++) { \ for (value = 0; (__ston_v = ASE_CHAR_TO_NUM(*__ston_ptr, base)) < base; __ston_ptr++) { \
value = value * base + __ston_v; \ value = value * base + __ston_v; \
} \ } \
if (endptr != ASE_NULL) *((const ase_char_t**)endptr) = __ston_ptr; \ if (endptr != ASE_NULL) *((const ase_char_t**)endptr) = __ston_ptr; \
@ -54,7 +53,7 @@ struct ase_str_t
} }
/* ase_strxtonum (const ase_char_t* nptr, ase_size_t len, ase_char_char** endptr, int base) */ /* ase_strxtonum (const ase_char_t* nptr, ase_size_t len, ase_char_char** endptr, int base) */
#define ASE_STRXTONUM(value,nptr,len,endptr,base) \ #define ASE_STRX_TO_NUM(value,nptr,len,endptr,base) \
{ \ { \
int __ston_f = 0, __ston_v; \ int __ston_f = 0, __ston_v; \
const ase_char_t* __ston_ptr = nptr; \ const ase_char_t* __ston_ptr = nptr; \
@ -70,7 +69,7 @@ struct ase_str_t
break; \ break; \
} \ } \
for (value = 0; __ston_ptr < __ston_end && \ for (value = 0; __ston_ptr < __ston_end && \
(__ston_v = ASE_CHARTONUM(*__ston_ptr, base)) != base; __ston_ptr++) { \ (__ston_v = ASE_CHAR_TO_NUM(*__ston_ptr, base)) != base; __ston_ptr++) { \
value = value * base + __ston_v; \ value = value * base + __ston_v; \
} \ } \
if (endptr != ASE_NULL) *((const ase_char_t**)endptr) = __ston_ptr; \ if (endptr != ASE_NULL) *((const ase_char_t**)endptr) = __ston_ptr; \
@ -178,11 +177,46 @@ ase_ulong_t ase_strxtoulong (const ase_char_t* str, ase_size_t len);
* dynamic string * dynamic string
*/ */
ase_str_t* ase_str_open (ase_str_t* str, ase_size_t capa, ase_mmgr_t* mmgr); ase_str_t* ase_str_open (
void ase_str_close (ase_str_t* str); ase_mmgr_t* mmgr,
void ase_str_clear (ase_str_t* str); ase_size_t ext,
ase_size_t capa
);
void ase_str_forfeit (ase_str_t* str); void ase_str_close (
ase_str_t* str
);
/*
* If capa is 0, it doesn't allocate the buffer in advance.
*/
ase_str_t* ase_str_init (
ase_str_t* str,
ase_mmgr_t* mmgr,
ase_size_t capa
);
void ase_str_fini (
ase_str_t* str
);
/*
* NAME: yield the buffer
*
* DESCRIPTION:
* The ase_str_yield() function assigns the buffer to an variable of the
* ase_cstr_t type and recreate a new buffer of the new_capa capacity.
* The function fails if it fails to allocate a new buffer.
*
* RETURNS: 0 on success, -1 on failure.
*/
int ase_str_yield (
ase_str_t* str /* a dynamic string */,
ase_cstr_t* buf /* the pointer to a ase_cstr_t variable */,
int new_capa /* new capacity in number of characters */
);
void ase_str_clear (ase_str_t* str);
void ase_str_swap (ase_str_t* str, ase_str_t* str2); 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_cpy (ase_str_t* str, const ase_char_t* s);

View File

@ -1,5 +1,5 @@
/* /*
* $Id: str_dyn.c 350 2008-08-29 14:51:04Z baconevi $ * $Id: str_dyn.c 352 2008-08-31 10:55:59Z baconevi $
* *
* {License} * {License}
*/ */
@ -34,20 +34,51 @@ ase_str_t* ase_str_init (ase_str_t* str, ase_mmgr_t* mmgr, ase_size_t capa)
ASE_MEMSET (str, 0, sizeof(ase_str_t)); ASE_MEMSET (str, 0, sizeof(ase_str_t));
str->mmgr = mmgr; str->mmgr = mmgr;
if (capa == 0) str->buf = ASE_NULL;
else
{
str->buf = (ase_char_t*) ASE_MMGR_ALLOC ( str->buf = (ase_char_t*) ASE_MMGR_ALLOC (
mmgr, sizeof(ase_char_t) * (capa + 1)); mmgr, sizeof(ase_char_t) * (capa + 1));
if (str->buf == ASE_NULL) return ASE_NULL; if (str->buf == ASE_NULL) return ASE_NULL;
str->buf[0] = ASE_T('\0');
}
str->size = 0; str->size = 0;
str->capa = capa; str->capa = capa;
str->buf[0] = ASE_T('\0');
return str; return str;
} }
void ase_str_fini (ase_str_t* str) void ase_str_fini (ase_str_t* str)
{ {
ASE_MMGR_FREE (str->mmgr, str->buf); if (str->buf != ASE_NULL) ASE_MMGR_FREE (str->mmgr, str->buf);
}
int ase_str_yield (ase_str_t* str, ase_cstr_t* buf, int new_capa)
{
ase_char_t* tmp;
if (new_capa == 0) tmp = ASE_NULL;
else
{
tmp = (ase_char_t*) ASE_MMGR_ALLOC (
str->mmgr, sizeof(ase_char_t) * (new_capa + 1));
if (tmp == ASE_NULL) return -1;
tmp[0] = ASE_T('\0');
}
if (buf != ASE_NULL)
{
buf->ptr = str->buf;
buf->len = str->size;
}
str->buf = tmp;
str->size = 0;
str->capa = new_capa;
return 0;
} }
void ase_str_clear (ase_str_t* str) void ase_str_clear (ase_str_t* str)
@ -56,12 +87,6 @@ void ase_str_clear (ase_str_t* str)
str->buf[0] = ASE_T('\0'); str->buf[0] = ASE_T('\0');
} }
void ase_str_forfeit (ase_str_t* str)
{
// TODO: how to handle this??????????????????????
if (str->__dynamic) ASE_MMGR_FREE (str->mmgr, str);
}
void ase_str_swap (ase_str_t* str, ase_str_t* str1) void ase_str_swap (ase_str_t* str, ase_str_t* str1)
{ {
ase_str_t tmp; ase_str_t tmp;
@ -92,13 +117,13 @@ ase_size_t ase_str_ncpy (ase_str_t* str, const ase_char_t* s, ase_size_t len)
{ {
ase_char_t* buf; ase_char_t* buf;
if (len > str->capa) if (len > str->capa || str->buf == ASE_NULL)
{ {
buf = (ase_char_t*) ASE_MMGR_ALLOC ( buf = (ase_char_t*) ASE_MMGR_ALLOC (
str->mmgr, sizeof(ase_char_t) * (len + 1)); str->mmgr, sizeof(ase_char_t) * (len + 1));
if (buf == ASE_NULL) return (ase_size_t)-1; if (buf == ASE_NULL) return (ase_size_t)-1;
ASE_MMGR_FREE (str->mmgr, str->buf); if (str->buf != ASE_NULL) ASE_MMGR_FREE (str->mmgr, str->buf);
str->capa = len; str->capa = len;
str->buf = buf; str->buf = buf;
} }
@ -126,11 +151,11 @@ ase_size_t ase_str_ncat (ase_str_t* str, const ase_char_t* s, ase_size_t len)
/* double the capa if necessary for concatenation */ /* double the capa if necessary for concatenation */
if (capa < str->capa * 2) capa = str->capa * 2; if (capa < str->capa * 2) capa = str->capa * 2;
if (str->mmgr->realloc != ASE_NULL) if (str->mmgr->realloc != ASE_NULL && str->buf != ASE_NULL)
{ {
tmp = (ase_char_t*) ASE_REALLOC ( tmp = (ase_char_t*) ASE_REALLOC (
str->mmgr, str->buf, str->mmgr, str->buf,
sizeof(ase_char_t) * (capa + 1)); sizeof(ase_char_t)*(capa+1));
if (tmp == ASE_NULL) return (ase_size_t)-1; if (tmp == ASE_NULL) return (ase_size_t)-1;
} }
else else