From a5e86b984ca174ecce9d03b10bcd8ea5fe061fee Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Sat, 6 Aug 2011 11:08:21 +0000 Subject: [PATCH] added a few functions for qse_env_t --- qse/include/qse/cmn/env.h | 35 +++++--- qse/include/qse/cmn/str.h | 6 +- qse/lib/cmn/env.c | 159 ++++++++++++++++++++++++++++++++---- qse/samples/cmn/Makefile.am | 3 +- qse/samples/cmn/Makefile.in | 23 ++++-- qse/samples/cmn/env.c | 36 ++++++++ 6 files changed, 223 insertions(+), 39 deletions(-) create mode 100644 qse/samples/cmn/env.c diff --git a/qse/include/qse/cmn/env.h b/qse/include/qse/cmn/env.h index 3a3f1006..e9e49b2c 100644 --- a/qse/include/qse/cmn/env.h +++ b/qse/include/qse/cmn/env.h @@ -34,14 +34,14 @@ struct qse_env_t { qse_size_t capa; qse_size_t len; - void* ptr; - } buf; + qse_char_t* ptr; + } str; struct { - qse_size_t capa; - qse_size_t len; - void** ptr; + qse_size_t capa; + qse_size_t len; + qse_char_t** ptr; } arr; }; @@ -70,15 +70,26 @@ void qse_env_fini ( qse_env_t* env ); -/* -void* qse_env_getstring (); -void* qse_env_getarray (); -*/ +void qse_env_clear ( + qse_env_t* env +); -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, - const void* name, - const void* value + const qse_char_t* name, + 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 diff --git a/qse/include/qse/cmn/str.h b/qse/include/qse/cmn/str.h index 50d874e5..64d2dab0 100644 --- a/qse/include/qse/cmn/str.h +++ b/qse/include/qse/cmn/str.h @@ -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. This file is part of QSE. @@ -104,7 +104,7 @@ struct qse_mbs_t { QSE_DEFINE_COMMON_FIELDS (mbs) 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 */ }; @@ -2302,7 +2302,6 @@ void qse_mbs_setsizer ( qse_mbs_t* str, qse_mbs_sizer_t sizer ); -/******/ /** * The qse_mbs_getcapa() function returns the current capacity. @@ -2472,7 +2471,6 @@ void qse_wcs_setsizer ( qse_wcs_t* str, qse_wcs_sizer_t sizer ); -/******/ /** * The qse_wcs_getcapa() function returns the current capacity. diff --git a/qse/lib/cmn/env.c b/qse/lib/cmn/env.c index 93d088ac..57f599c9 100644 --- a/qse/lib/cmn/env.c +++ b/qse/lib/cmn/env.c @@ -20,8 +20,12 @@ #include +#include #include "mem.h" +#define STRSIZE 4096 +#define ARRSIZE 128 + QSE_IMPLEMENT_COMMON_FUNCTIONS(env) 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_MEMSET (env, 0, QSE_SIZEOF(*env)); + env->mmgr = mmgr; return env; } void qse_env_fini (qse_env_t* env) { 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); } - -static int expand_buffer (qse_env_t* env) +void qse_env_clear (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')) { - } - return -1; -} + if (qse_env_addraw (env, envstr) <= -1) + { + ret = -1; + goto done; + } + envstr += qse_strlen(lpszVariable) + 1; + } -static int expand_array (qse_env_t* env) -{ - if (env->arr.ptr == QSE_NULL) +done: + FreeEnvironmentStrings (envstr); + 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; -} diff --git a/qse/samples/cmn/Makefile.am b/qse/samples/cmn/Makefile.am index fa2f34e7..9beb03a0 100644 --- a/qse/samples/cmn/Makefile.am +++ b/qse/samples/cmn/Makefile.am @@ -6,7 +6,7 @@ AM_CPPFLAGS = \ -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 LDADD = -lqsecmn @@ -28,6 +28,7 @@ time_SOURCES = time.c main_SOURCES = main.c main2_SOURCES = main2.c rex01_SOURCES = rex01.c +env_SOURCES = env.c if ENABLE_CXX diff --git a/qse/samples/cmn/Makefile.in b/qse/samples/cmn/Makefile.in index 6a640c3b..222560ff 100644 --- a/qse/samples/cmn/Makefile.in +++ b/qse/samples/cmn/Makefile.in @@ -38,7 +38,7 @@ bin_PROGRAMS = xma$(EXEEXT) fma$(EXEEXT) chr$(EXEEXT) str$(EXEEXT) \ sll$(EXEEXT) dll$(EXEEXT) lda$(EXEEXT) oht$(EXEEXT) \ htb$(EXEEXT) rbt$(EXEEXT) fio$(EXEEXT) pio$(EXEEXT) \ sio$(EXEEXT) time$(EXEEXT) main$(EXEEXT) main2$(EXEEXT) \ - rex01$(EXEEXT) + rex01$(EXEEXT) env$(EXEEXT) subdir = samples/cmn DIST_COMMON = $(srcdir)/Makefile.am $(srcdir)/Makefile.in ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 @@ -62,6 +62,10 @@ am_dll_OBJECTS = dll.$(OBJEXT) dll_OBJECTS = $(am_dll_OBJECTS) dll_LDADD = $(LDADD) dll_DEPENDENCIES = +am_env_OBJECTS = env.$(OBJEXT) +env_OBJECTS = $(am_env_OBJECTS) +env_LDADD = $(LDADD) +env_DEPENDENCIES = am_fio_OBJECTS = fio.$(OBJEXT) fio_OBJECTS = $(am_fio_OBJECTS) fio_LDADD = $(LDADD) @@ -135,16 +139,16 @@ CCLD = $(CC) LINK = $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) \ --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) $(AM_LDFLAGS) \ $(LDFLAGS) -o $@ -SOURCES = $(chr_SOURCES) $(dll_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) -DIST_SOURCES = $(chr_SOURCES) $(dll_SOURCES) $(fio_SOURCES) \ +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) +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 CTAGS = ctags DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) @@ -313,6 +317,7 @@ time_SOURCES = time.c main_SOURCES = main.c main2_SOURCES = main2.c rex01_SOURCES = rex01.c +env_SOURCES = env.c all: all-am .SUFFIXES: @@ -396,6 +401,9 @@ chr$(EXEEXT): $(chr_OBJECTS) $(chr_DEPENDENCIES) dll$(EXEEXT): $(dll_OBJECTS) $(dll_DEPENDENCIES) @rm -f dll$(EXEEXT) $(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) @rm -f fio$(EXEEXT) $(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)/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)/fma.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/htb.Po@am__quote@ diff --git a/qse/samples/cmn/env.c b/qse/samples/cmn/env.c new file mode 100644 index 00000000..0f73071d --- /dev/null +++ b/qse/samples/cmn/env.c @@ -0,0 +1,36 @@ +#include +#include +#include + +#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; +}