cstring.h: a string utils for C
Copyleft 2003, 2004, matt<matt@techsing.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program (look for the file called COPYING);
if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef __CSTRING_H_
#define __CSTRING_H_
#ifdef __cplusplus
extern "C"{
#endif /* __cplusplus */
#include <string.h>
#include <stdlib.h>
typedef struct cstring {
void *data; /* either 'char *' or 'int *' */
int size; /* real size in bytes */
int size_allocated; /* ref size for mm allocation */
void *data_allocated; /* if non NULL, data has been malloced */
} cstring;
/* cstring handling */
static inline int cstr_realloc(cstring *cstr, int new_size)
{
int size;
void *data;
size = cstr->size_allocated;
if (size == 0)
size = 8; /* no need to allocate a too small first string */
while (size < new_size)
size = size * 2;
data = realloc(cstr->data_allocated, size);
if (!data){
return -1;
}
cstr->data_allocated = data;
cstr->size_allocated = size;
cstr->data = data;
}
static inline int cstr_alloc(cstring* cstr, int size)
{
cstr_reset(cstr);
return cstr_realloc(cstr,size);
}
static inline int cstr_malloc(cstring* cstr, int size)
{
cstr_reset(cstr);
return cstr_realloc(cstr,size);
}
/* add a byte */
static inline void cstr_ccat(cstring *cstr, int ch)
{
int size;
size = cstr->size + 1;
if (size > cstr->size_allocated)
cstr_realloc(cstr, size);
((unsigned char *)cstr->data)[size - 1] = ch;
cstr->size = size;
}
static inline void cstr_cat(cstring *cstr,const char* str)
{
int len,size;
len = strlen(str);
size = cstr->size + len;
cstr_realloc(cstr,size);
strcat(cstr->data,str);
cstr->size = size;
}
static inline void cstr_cpy(cstring* cstr, const char* str)
{
char* p;
p = (char*)cstr->data;
while((*p++ = *str++) != '/0') // to avoid warning: = '/0'
;
cstr->size = p - (char*)cstr->data;
}
static inline void cstr_asign(cstring* cstr, const char* str)
{
cstr_cpy(cstr,str);
}
/* add a wide char */
static inline void cstr_wccat(cstring *cstr, int ch)
{
int size;
size = cstr->size + sizeof(int);
if (size > cstr->size_allocated)
cstr_realloc(cstr, size);
*(int *)(((unsigned char *)cstr->data) + size - sizeof(int)) = ch;
cstr->size = size;
}
static inline void cstr_reset(cstring *cstr)
{
memset(cstr, 0, sizeof(cstring));
}
#define cstr_init(ptr) cstr_reset(ptr)
/* free string and reset it to NULL */
static inline void cstr_free(cstring *cstr)
{
free(cstr->data_allocated);
cstr_reset(cstr);
}
#define cstr_destroy(ptr) cstr_free(ptr)
static inline cstring *cstr_dup(cstring *cstr1)
{
cstring *cstr;
int size;
cstr = malloc(sizeof(cstring));
size = cstr1->size;
cstr->size = size;
cstr->size_allocated = size;
cstr->data_allocated = malloc(size);
cstr->data = cstr->data_allocated;
memcpy(cstr->data_allocated, cstr1->data_allocated, size);
return cstr;
}
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* __CSTRING_H_ */
Copyleft 2003, 2004, matt<matt@techsing.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program (look for the file called COPYING);
if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef __CSTRING_H_
#define __CSTRING_H_
#ifdef __cplusplus
extern "C"{
#endif /* __cplusplus */
#include <string.h>
#include <stdlib.h>
typedef struct cstring {
void *data; /* either 'char *' or 'int *' */
int size; /* real size in bytes */
int size_allocated; /* ref size for mm allocation */
void *data_allocated; /* if non NULL, data has been malloced */
} cstring;
/* cstring handling */
static inline int cstr_realloc(cstring *cstr, int new_size)
{
int size;
void *data;
size = cstr->size_allocated;
if (size == 0)
size = 8; /* no need to allocate a too small first string */
while (size < new_size)
size = size * 2;
data = realloc(cstr->data_allocated, size);
if (!data){
return -1;
}
cstr->data_allocated = data;
cstr->size_allocated = size;
cstr->data = data;
}
static inline int cstr_alloc(cstring* cstr, int size)
{
cstr_reset(cstr);
return cstr_realloc(cstr,size);
}
static inline int cstr_malloc(cstring* cstr, int size)
{
cstr_reset(cstr);
return cstr_realloc(cstr,size);
}
/* add a byte */
static inline void cstr_ccat(cstring *cstr, int ch)
{
int size;
size = cstr->size + 1;
if (size > cstr->size_allocated)
cstr_realloc(cstr, size);
((unsigned char *)cstr->data)[size - 1] = ch;
cstr->size = size;
}
static inline void cstr_cat(cstring *cstr,const char* str)
{
int len,size;
len = strlen(str);
size = cstr->size + len;
cstr_realloc(cstr,size);
strcat(cstr->data,str);
cstr->size = size;
}
static inline void cstr_cpy(cstring* cstr, const char* str)
{
char* p;
p = (char*)cstr->data;
while((*p++ = *str++) != '/0') // to avoid warning: = '/0'
;
cstr->size = p - (char*)cstr->data;
}
static inline void cstr_asign(cstring* cstr, const char* str)
{
cstr_cpy(cstr,str);
}
/* add a wide char */
static inline void cstr_wccat(cstring *cstr, int ch)
{
int size;
size = cstr->size + sizeof(int);
if (size > cstr->size_allocated)
cstr_realloc(cstr, size);
*(int *)(((unsigned char *)cstr->data) + size - sizeof(int)) = ch;
cstr->size = size;
}
static inline void cstr_reset(cstring *cstr)
{
memset(cstr, 0, sizeof(cstring));
}
#define cstr_init(ptr) cstr_reset(ptr)
/* free string and reset it to NULL */
static inline void cstr_free(cstring *cstr)
{
free(cstr->data_allocated);
cstr_reset(cstr);
}
#define cstr_destroy(ptr) cstr_free(ptr)
static inline cstring *cstr_dup(cstring *cstr1)
{
cstring *cstr;
int size;
cstr = malloc(sizeof(cstring));
size = cstr1->size;
cstr->size = size;
cstr->size_allocated = size;
cstr->data_allocated = malloc(size);
cstr->data = cstr->data_allocated;
memcpy(cstr->data_allocated, cstr1->data_allocated, size);
return cstr;
}
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* __CSTRING_H_ */