added a few functions for qse_env_t

This commit is contained in:
hyung-hwan 2011-08-06 11:08:21 +00:00
parent 5b92edfe50
commit a5e86b984c
6 changed files with 223 additions and 39 deletions

View File

@ -34,14 +34,14 @@ struct qse_env_t
{ {
qse_size_t capa; qse_size_t capa;
qse_size_t len; qse_size_t len;
void* ptr; qse_char_t* ptr;
} buf; } str;
struct struct
{ {
qse_size_t capa; qse_size_t capa;
qse_size_t len; qse_size_t len;
void** ptr; qse_char_t** ptr;
} arr; } arr;
}; };
@ -70,15 +70,26 @@ void qse_env_fini (
qse_env_t* env qse_env_t* env
); );
/* void qse_env_clear (
void* qse_env_getstring (); qse_env_t* env
void* qse_env_getarray (); );
*/
int qse_env_add ( #define qse_env_getstr(env) ((env)->str.ptr)
#define qse_env_getarr(env) ((env)->arr.ptr)
int qse_env_addvar (
qse_env_t* env, qse_env_t* env,
const void* name, const qse_char_t* name,
const void* value const qse_char_t* value
);
int qse_env_addraw (
qse_env_t* env, /**< env */
const qse_char_t* raw /**< name=value */
);
int qse_env_loadcurvars (
qse_env_t* env
); );
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -1,5 +1,5 @@
/* /*
* $Id: str.h 533 2011-08-04 15:43:28Z hyunghwan.chung $ * $Id: str.h 535 2011-08-05 17:08:21Z hyunghwan.chung $
* *
Copyright 2006-2011 Chung, Hyung-Hwan. Copyright 2006-2011 Chung, Hyung-Hwan.
This file is part of QSE. This file is part of QSE.
@ -104,7 +104,7 @@ struct qse_mbs_t
{ {
QSE_DEFINE_COMMON_FIELDS (mbs) QSE_DEFINE_COMMON_FIELDS (mbs)
qse_mbs_sizer_t sizer; /**< buffer resizer function */ qse_mbs_sizer_t sizer; /**< buffer resizer function */
qse_mxstr_t val; /**< buffer/string pointer and lengh */ qse_mxstr_t val; /**< buffer/string pointer and lengh */
qse_size_t capa; /**< buffer capacity */ qse_size_t capa; /**< buffer capacity */
}; };
@ -2302,7 +2302,6 @@ void qse_mbs_setsizer (
qse_mbs_t* str, qse_mbs_t* str,
qse_mbs_sizer_t sizer qse_mbs_sizer_t sizer
); );
/******/
/** /**
* The qse_mbs_getcapa() function returns the current capacity. * The qse_mbs_getcapa() function returns the current capacity.
@ -2472,7 +2471,6 @@ void qse_wcs_setsizer (
qse_wcs_t* str, qse_wcs_t* str,
qse_wcs_sizer_t sizer qse_wcs_sizer_t sizer
); );
/******/
/** /**
* The qse_wcs_getcapa() function returns the current capacity. * The qse_wcs_getcapa() function returns the current capacity.

View File

@ -20,8 +20,12 @@
#include <qse/cmn/env.h> #include <qse/cmn/env.h>
#include <qse/cmn/str.h>
#include "mem.h" #include "mem.h"
#define STRSIZE 4096
#define ARRSIZE 128
QSE_IMPLEMENT_COMMON_FUNCTIONS(env) QSE_IMPLEMENT_COMMON_FUNCTIONS(env)
qse_env_t* qse_env_open (qse_mmgr_t* mmgr, qse_size_t xtnsize) qse_env_t* qse_env_open (qse_mmgr_t* mmgr, qse_size_t xtnsize)
@ -59,33 +63,158 @@ void qse_env_close (qse_env_t* env)
qse_env_t* qse_env_init (qse_env_t* env, qse_mmgr_t* mmgr) qse_env_t* qse_env_init (qse_env_t* env, qse_mmgr_t* mmgr)
{ {
QSE_MEMSET (env, 0, QSE_SIZEOF(*env)); QSE_MEMSET (env, 0, QSE_SIZEOF(*env));
env->mmgr = mmgr;
return env; return env;
} }
void qse_env_fini (qse_env_t* env) void qse_env_fini (qse_env_t* env)
{ {
if (env->arr.ptr) QSE_MMGR_FREE (env->mmgr, env->arr.ptr); if (env->arr.ptr) QSE_MMGR_FREE (env->mmgr, env->arr.ptr);
if (env->buf.ptr) QSE_MMGR_FREE (env->mmgr, env->buf.ptr); if (env->str.ptr) QSE_MMGR_FREE (env->mmgr, env->str.ptr);
} }
void qse_env_clear (qse_env_t* env)
static int expand_buffer (qse_env_t* env)
{ {
if (env->buf.ptr == QSE_NULL) if (env->str.ptr) env->str.ptr[0] = QSE_T('\0');
if (env->arr.ptr) env->arr.ptr = QSE_NULL;
}
static int expandarr (qse_env_t* env)
{
qse_char_t** tmp;
qse_size_t ncapa = env->arr.capa + ARRSIZE;
tmp = (qse_char_t**) QSE_MMGR_REALLOC (
env->mmgr, env->arr.ptr,
QSE_SIZEOF(qse_char_t*) * (ncapa + 1));
if (tmp == QSE_NULL) return -1;
env->arr.ptr = tmp;
env->arr.capa = ncapa;
return 0;
}
static int expandstr (qse_env_t* env, qse_size_t inc)
{
qse_char_t* tmp;
qse_size_t ncapa = env->str.capa;
ncapa = (inc > STRSIZE)? (ncapa + inc): (ncapa + STRSIZE);
tmp = (qse_char_t*) QSE_MMGR_REALLOC (
env->mmgr, env->str.ptr,
QSE_SIZEOF(qse_char_t) * (ncapa + 1));
if (tmp == QSE_NULL) return -1;
env->str.ptr = tmp;
env->str.capa = ncapa;
return 0;
}
int qse_env_addvar (qse_env_t* env, const qse_char_t* name, const qse_char_t* value)
{
qse_size_t nl, vl, tl;
nl = qse_strlen (name);
vl = qse_strlen (value);
if (env->arr.len >= env->arr.capa &&
expandarr(env) <= -1) return -1;
tl = nl + 1 + vl + 1; /* name = value '\0' */
if (env->str.len + tl > env->str.capa &&
expandstr (env, tl) <= -1) return -1;
env->arr.ptr[env->arr.len++] = &env->str.ptr[env->str.len];
env->arr.ptr[env->arr.len] = QSE_NULL;
env->str.len += qse_strcpy (&env->str.ptr[env->str.len], name);
env->str.ptr[env->str.len++] = QSE_T('=');
env->str.len += qse_strcpy (&env->str.ptr[env->str.len], value);
env->str.ptr[++env->str.len] = QSE_T('\0');
return -1;
}
int qse_env_addraw (qse_env_t* env, const qse_char_t* nv)
{
qse_size_t tl;
if (env->arr.len >= env->arr.capa &&
expandarr(env) <= -1) return -1;
tl = qse_strlen(nv) + 1;
if (env->str.len + tl > env->str.capa &&
expandstr (env, tl) <= -1) return -1;
env->arr.ptr[env->arr.len++] = &env->str.ptr[env->str.len];
env->arr.ptr[env->arr.len] = QSE_NULL;
env->str.len += qse_strcpy (&env->str.ptr[env->str.len], nv);
env->str.ptr[++env->str.len] = QSE_T('\0');
return 0;
}
/* add current environment variables */
int qse_env_loadcurvars (qse_env_t* env)
{
#if defined(_WIN32)
qse_char_t* envstr;
int ret = 0;
envstr = GetEnvironmentStrings ();
if (envstr == QSE_NULL) return -1;
while (*envstr != QSE_T('\0'))
{ {
} if (qse_env_addraw (env, envstr) <= -1)
return -1; {
} ret = -1;
goto done;
}
envstr += qse_strlen(lpszVariable) + 1;
}
static int expand_array (qse_env_t* env) done:
{ FreeEnvironmentStrings (envstr);
if (env->arr.ptr == QSE_NULL) return ret;
#elif defined(__OS2__)
/* TODO: */
return -1;
#elif defined(__DOS__)
/* TODO: */
return -1;
#else
extern char** environ;
char** p = environ;
#if defined(QSE_CHAR_IS_MCHAR)
while (*p)
{ {
if (qse_env_addraw (env, *p) <= -1) return -1;
p++;
} }
return -1;
#else
while (*p)
{
int n;
qse_char_t* x;
x = qse_mbstowcsdup (*p, env->mmgr);
if (x == QSE_NULL) return -1;
n = qse_env_addraw (env, x);
QSE_MMGR_FREE (env->mmgr, x);
if (n <= -1) return -1;
p++;
}
#endif
return 0;
#endif
} }
int qse_env_add (qse_env_t* env, const void* name, const void* value)
{
return -1;
}

View File

@ -6,7 +6,7 @@ AM_CPPFLAGS = \
-I$(includedir) -I$(includedir)
bin_PROGRAMS = xma fma chr str sll dll lda oht htb rbt fio pio sio time main main2 rex01 bin_PROGRAMS = xma fma chr str sll dll lda oht htb rbt fio pio sio time main main2 rex01 env
LDFLAGS = -L../../lib/cmn LDFLAGS = -L../../lib/cmn
LDADD = -lqsecmn LDADD = -lqsecmn
@ -28,6 +28,7 @@ time_SOURCES = time.c
main_SOURCES = main.c main_SOURCES = main.c
main2_SOURCES = main2.c main2_SOURCES = main2.c
rex01_SOURCES = rex01.c rex01_SOURCES = rex01.c
env_SOURCES = env.c
if ENABLE_CXX if ENABLE_CXX

View File

@ -38,7 +38,7 @@ bin_PROGRAMS = xma$(EXEEXT) fma$(EXEEXT) chr$(EXEEXT) str$(EXEEXT) \
sll$(EXEEXT) dll$(EXEEXT) lda$(EXEEXT) oht$(EXEEXT) \ sll$(EXEEXT) dll$(EXEEXT) lda$(EXEEXT) oht$(EXEEXT) \
htb$(EXEEXT) rbt$(EXEEXT) fio$(EXEEXT) pio$(EXEEXT) \ htb$(EXEEXT) rbt$(EXEEXT) fio$(EXEEXT) pio$(EXEEXT) \
sio$(EXEEXT) time$(EXEEXT) main$(EXEEXT) main2$(EXEEXT) \ sio$(EXEEXT) time$(EXEEXT) main$(EXEEXT) main2$(EXEEXT) \
rex01$(EXEEXT) rex01$(EXEEXT) env$(EXEEXT)
subdir = samples/cmn subdir = samples/cmn
DIST_COMMON = $(srcdir)/Makefile.am $(srcdir)/Makefile.in DIST_COMMON = $(srcdir)/Makefile.am $(srcdir)/Makefile.in
ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
@ -62,6 +62,10 @@ am_dll_OBJECTS = dll.$(OBJEXT)
dll_OBJECTS = $(am_dll_OBJECTS) dll_OBJECTS = $(am_dll_OBJECTS)
dll_LDADD = $(LDADD) dll_LDADD = $(LDADD)
dll_DEPENDENCIES = dll_DEPENDENCIES =
am_env_OBJECTS = env.$(OBJEXT)
env_OBJECTS = $(am_env_OBJECTS)
env_LDADD = $(LDADD)
env_DEPENDENCIES =
am_fio_OBJECTS = fio.$(OBJEXT) am_fio_OBJECTS = fio.$(OBJEXT)
fio_OBJECTS = $(am_fio_OBJECTS) fio_OBJECTS = $(am_fio_OBJECTS)
fio_LDADD = $(LDADD) fio_LDADD = $(LDADD)
@ -135,16 +139,16 @@ CCLD = $(CC)
LINK = $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) \ LINK = $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) \
--mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) $(AM_LDFLAGS) \ --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) $(AM_LDFLAGS) \
$(LDFLAGS) -o $@ $(LDFLAGS) -o $@
SOURCES = $(chr_SOURCES) $(dll_SOURCES) $(fio_SOURCES) $(fma_SOURCES) \ SOURCES = $(chr_SOURCES) $(dll_SOURCES) $(env_SOURCES) $(fio_SOURCES) \
$(htb_SOURCES) $(lda_SOURCES) $(main_SOURCES) $(main2_SOURCES) \
$(oht_SOURCES) $(pio_SOURCES) $(rbt_SOURCES) $(rex01_SOURCES) \
$(sio_SOURCES) $(sll_SOURCES) $(str_SOURCES) $(time_SOURCES) \
$(xma_SOURCES)
DIST_SOURCES = $(chr_SOURCES) $(dll_SOURCES) $(fio_SOURCES) \
$(fma_SOURCES) $(htb_SOURCES) $(lda_SOURCES) $(main_SOURCES) \ $(fma_SOURCES) $(htb_SOURCES) $(lda_SOURCES) $(main_SOURCES) \
$(main2_SOURCES) $(oht_SOURCES) $(pio_SOURCES) $(rbt_SOURCES) \ $(main2_SOURCES) $(oht_SOURCES) $(pio_SOURCES) $(rbt_SOURCES) \
$(rex01_SOURCES) $(sio_SOURCES) $(sll_SOURCES) $(str_SOURCES) \ $(rex01_SOURCES) $(sio_SOURCES) $(sll_SOURCES) $(str_SOURCES) \
$(time_SOURCES) $(xma_SOURCES) $(time_SOURCES) $(xma_SOURCES)
DIST_SOURCES = $(chr_SOURCES) $(dll_SOURCES) $(env_SOURCES) \
$(fio_SOURCES) $(fma_SOURCES) $(htb_SOURCES) $(lda_SOURCES) \
$(main_SOURCES) $(main2_SOURCES) $(oht_SOURCES) $(pio_SOURCES) \
$(rbt_SOURCES) $(rex01_SOURCES) $(sio_SOURCES) $(sll_SOURCES) \
$(str_SOURCES) $(time_SOURCES) $(xma_SOURCES)
ETAGS = etags ETAGS = etags
CTAGS = ctags CTAGS = ctags
DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST)
@ -313,6 +317,7 @@ time_SOURCES = time.c
main_SOURCES = main.c main_SOURCES = main.c
main2_SOURCES = main2.c main2_SOURCES = main2.c
rex01_SOURCES = rex01.c rex01_SOURCES = rex01.c
env_SOURCES = env.c
all: all-am all: all-am
.SUFFIXES: .SUFFIXES:
@ -396,6 +401,9 @@ chr$(EXEEXT): $(chr_OBJECTS) $(chr_DEPENDENCIES)
dll$(EXEEXT): $(dll_OBJECTS) $(dll_DEPENDENCIES) dll$(EXEEXT): $(dll_OBJECTS) $(dll_DEPENDENCIES)
@rm -f dll$(EXEEXT) @rm -f dll$(EXEEXT)
$(LINK) $(dll_OBJECTS) $(dll_LDADD) $(LIBS) $(LINK) $(dll_OBJECTS) $(dll_LDADD) $(LIBS)
env$(EXEEXT): $(env_OBJECTS) $(env_DEPENDENCIES)
@rm -f env$(EXEEXT)
$(LINK) $(env_OBJECTS) $(env_LDADD) $(LIBS)
fio$(EXEEXT): $(fio_OBJECTS) $(fio_DEPENDENCIES) fio$(EXEEXT): $(fio_OBJECTS) $(fio_DEPENDENCIES)
@rm -f fio$(EXEEXT) @rm -f fio$(EXEEXT)
$(LINK) $(fio_OBJECTS) $(fio_LDADD) $(LIBS) $(LINK) $(fio_OBJECTS) $(fio_LDADD) $(LIBS)
@ -450,6 +458,7 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/chr.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/chr.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/dll.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/dll.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/env.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/fio.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/fio.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/fma.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/fma.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/htb.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/htb.Po@am__quote@

36
qse/samples/cmn/env.c Normal file
View File

@ -0,0 +1,36 @@
#include <qse/cmn/env.h>
#include <qse/cmn/str.h>
#include <qse/cmn/stdio.h>
#define R(f) \
do { \
qse_printf (QSE_T("== %s ==\n"), QSE_T(#f)); \
if (f() == -1) return -1; \
} while (0)
static int test1 (void)
{
qse_env_t* env;
const qse_char_t* envstr;
env = qse_env_open (QSE_NULL, 0);
qse_env_addvar (env, QSE_T("alice"), QSE_T("wonderland"));
qse_env_addvar (env, QSE_T("cool"), QSE_T("mint"));
envstr = qse_env_getstr (env);
while (*envstr != QSE_T('\0'))
{
qse_printf (QSE_T("%s\n"), envstr);
envstr += qse_strlen(envstr) + 1;
}
qse_env_close (env);
return 0;
}
int main ()
{
R (test1);
return 0;
}