completed initial implementation of qse_env_t

This commit is contained in:
hyung-hwan 2011-08-07 09:41:27 +00:00
parent c73a51dbbc
commit 6f7f368a91
3 changed files with 132 additions and 18 deletions

View File

@ -1,5 +1,5 @@
/*
* $Id: pio.h 455 2011-05-09 16:11:13Z hyunghwan.chung $
* $Id$
*
Copyright 2006-2011 Chung, Hyung-Hwan.
This file is part of QSE.
@ -24,11 +24,19 @@
#include <qse/types.h>
#include <qse/macros.h>
/** @file
* This file defines data types and functions that you can use to build
* an environment block.
*/
/**
* The qse_env_t type defines a cross-platform environment block.
*/
typedef struct qse_env_t qse_env_t;
struct qse_env_t
{
QSE_DEFINE_COMMON_FIELDS(pio)
QSE_DEFINE_COMMON_FIELDS(env)
struct
{

View File

@ -1,5 +1,5 @@
/*
* $Id: pio.h 455 2011-05-09 16:11:13Z hyunghwan.chung $
* $Id$
*
Copyright 2006-2011 Chung, Hyung-Hwan.
This file is part of QSE.
@ -86,7 +86,7 @@ void qse_env_clear (qse_env_t* env)
}
if (env->arr.ptr)
{
env->arr.ptr = QSE_NULL;
env->arr.ptr[0] = QSE_NULL;
env->arr.len = 0;
}
}
@ -103,23 +103,37 @@ static int expandarr (qse_env_t* env)
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;
qse_size_t ncapa;
ncapa = (inc > STRSIZE)? (ncapa + inc): (ncapa + STRSIZE);
ncapa = (inc > STRSIZE)?
(env->str.capa + inc): (env->str.capa + 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;
if (tmp != env->str.ptr)
{
/* reallocation relocated the string buffer.
* the pointers in the pointer array have to be adjusted */
qse_size_t i;
for (i = 0; i < env->arr.len; i++)
{
env->arr.ptr[i] = tmp + (env->arr.ptr[i] - env->str.ptr);
}
}
env->str.ptr = tmp;
env->str.capa = ncapa;
return 0;
}
@ -151,28 +165,46 @@ int qse_env_insert (
int qse_env_delete (qse_env_t* env, const qse_char_t* name)
{
const qse_char_t* p = env->str.ptr;
qse_size_t i;
for (i = 0; i < env->arr.len; i++)
{
const qse_char_t* eq;
const qse_char_t* vp;
qse_char_t* vp;
vp = env->arr.ptr[i];
//qse_printf (QSE_T("comparing [%s] []\n"), vp);
eq = qse_strbeg (vp, name);
if (eq && *eq == QSE_T('='))
{
#if 0
/* bingo */
qse_size_t len, rem;
/*
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
A B C = D E F \0 X = Y Y \0 Z = T T \0 \0
env->str.len = 18
env->str.ptr = 5
vp = 13
len = 4 + 1
*/
len = qse_strlen (vp) + 1;
QSE_MEMCPY (vp, vp + len, ... );
#endif
rem = env->str.len - (vp + len - env->str.ptr) + 1;
QSE_MEMCPY (vp, vp + len, rem * QSE_SIZEOF(qse_char_t));
env->str.len -= len;
env->arr.len--;
for (; i < env->arr.len; i++)
env->arr.ptr[i] = env->arr.ptr[i+1] - len;
env->arr.ptr[i] = QSE_NULL;
return 0;
}
}
return 0;
return -1;
}
static int add_envstr (qse_env_t* env, const qse_char_t* nv)

View File

@ -12,17 +12,90 @@ static int test1 (void)
{
qse_env_t* env;
const qse_char_t* envstr;
qse_char_t** envarr;
env = qse_env_open (QSE_NULL, 0);
env = qse_env_open (QSE_NULL, 0, 0);
qse_env_addvar (env, QSE_T("alice"), QSE_T("wonderland"));
qse_env_addvar (env, QSE_T("cool"), QSE_T("mint"));
qse_env_clear (env);
qse_env_insert (env, QSE_T("alice"), QSE_T("wonderland"));
qse_env_insert (env, QSE_T("cool"), QSE_T("mint"));
qse_env_insert (env, QSE_T("smurf"), QSE_T("happy song"));
qse_env_insert (env, QSE_T("donkey"), QSE_T("mule"));
qse_env_insert (env, QSE_T("lily"), QSE_T("rose"));
qse_env_clear (env);
qse_env_insert (env, QSE_T("alice"), QSE_T("wonderland"));
qse_env_insert (env, QSE_T("cool"), QSE_T("mint"));
qse_env_insert (env, QSE_T("smurf"), QSE_T("happy song"));
qse_env_insert (env, QSE_T("donkey"), QSE_T("mule"));
qse_env_insert (env, QSE_T("lily"), QSE_T("rose"));
qse_env_delete (env, QSE_T("cool"));
qse_env_insert (env, QSE_T("spider"), QSE_T("man"));
envstr = qse_env_getstr (env);
while (*envstr != QSE_T('\0'))
if (envstr)
{
qse_printf (QSE_T("%s\n"), envstr);
envstr += qse_strlen(envstr) + 1;
while (*envstr != QSE_T('\0'))
{
qse_printf (QSE_T("%p [%s]\n"), envstr, envstr);
envstr += qse_strlen(envstr) + 1;
}
}
qse_printf (QSE_T("=====\n"));
envarr = qse_env_getarr (env);
if (envarr)
{
while (*envarr)
{
qse_printf (QSE_T("%p [%s]\n"), *envarr, *envarr);
envarr++;
}
}
qse_env_close (env);
return 0;
}
static int test2 (void)
{
qse_env_t* env;
const qse_char_t* envstr;
qse_char_t** envarr;
env = qse_env_open (QSE_NULL, 0, 1);
qse_printf (QSE_T("DELETING HOME => %s\n"),
(qse_env_delete (env, QSE_T("HOME")) == 0?
QSE_T("SUCCESS"): QSE_T("FAILURE"))
);
qse_printf (QSE_T("DELETING wolf => %s\n"),
(qse_env_delete (env, QSE_T("wolf")) == 0?
QSE_T("SUCCESS"): QSE_T("FAILURE"))
);
envstr = qse_env_getstr (env);
if (envstr)
{
while (*envstr != QSE_T('\0'))
{
qse_printf (QSE_T("%p [%s]\n"), envstr, envstr);
envstr += qse_strlen(envstr) + 1;
}
}
qse_printf (QSE_T("=====\n"));
envarr = qse_env_getarr (env);
if (envarr)
{
while (*envarr)
{
qse_printf (QSE_T("%p [%s]\n"), *envarr, *envarr);
envarr++;
}
}
qse_env_close (env);
@ -32,5 +105,6 @@ static int test1 (void)
int main ()
{
R (test1);
R (test2);
return 0;
}