Skip to content

Commit

Permalink
update evar
Browse files Browse the repository at this point in the history
  • Loading branch information
ziyht committed Apr 24, 2019
1 parent 4352d72 commit d19e3a0
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/libs/etools/eobj_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ typedef enum _eo_type_map{
_XX(EQL),

}_eotype;

#undef _XX

//! ================================================
//! macro definition
Expand Down
168 changes: 161 additions & 7 deletions src/libs/etools/evar.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
#include "eutils.h"

#include "evar.h"
#include "estr.h"
#include "estr_p.h"

#define EVAR_VERISON "0.1.0"
#define EVAR_VERISON "evar 0.2.1"

//0 1 2 3 4 5 6 7 8 9 10 11 12 13
static const u8 _len_map[] = {0, 1, 2, 4, 8, 1, 2, 4, 8, 4, 8, 8, 8, 8};
static const u8 _len_map[] = __EVAR_ITEM_LEN_MAP;

#define _vp_pptr(vp, esize, i) ((char*)((vp->type & __ETYPEV_PTR_MASK) ? vp->v.p : &vp->v) + (esize * i))

evar evar_gen (etypev t, int cnt, int size)
{
Expand Down Expand Up @@ -139,11 +142,50 @@ evarp evar_new(etypev t, int cnt, int size)
return 0;
}

uint evar_clear(evarp vp)
{
estr* base; uint cnt;

is0_ret(vp || vp->type == E_NAV, 0);

cnt = 1;

if((vp->type & __ETYPEV_VAR_MASK) == __E_EVAR)
{
cnt = evar_free(vp->v.p);
*vp = EVAR_NAV;
}
else if(vp->type & __ETYPEV_PTR_MASK)
{
cnt = vp->cnt;
base = vp->v.p;
}
else if(vp->type & __ETYPEV_ARR_MASK)
{
cnt = vp->cnt;
base = vp->v.sa;
}

if((vp->type & __ETYPEV_VAR_MASK) == E_STR || (vp->type & __ETYPEV_VAR_MASK) == E_RAW)
{
uint i;

for(i = 0; i < vp->cnt; i++)
_s_free(base[i]);
}

memset(base, 0, vp->cnt * vp->esize);

return cnt;
}

uint evar_free(evarp vp)
{
estr* base; uint cnt;

is0_ret(vp || vp->type == E_NAV, 0);

uint cnt = 1;
cnt = 1;

if((vp->type & __ETYPEV_VAR_MASK) == __E_EVAR)
{
Expand All @@ -152,14 +194,26 @@ uint evar_free(evarp vp)
}
else if(vp->type & __ETYPEV_PTR_MASK)
{
cnt = vp->cnt;
efree(vp->v.p);
cnt = vp->cnt;
base = vp->v.p;
}
else if(vp->type & __ETYPEV_ARR_MASK)
{
cnt = vp->cnt;
cnt = vp->cnt;
base = vp->v.sa;
}

if((vp->type & __ETYPEV_VAR_MASK) == E_STR || (vp->type & __ETYPEV_VAR_MASK) == E_RAW)
{
uint i;

for(i = 0; i < vp->cnt; i++)
_s_free(base[i]);
}

if(vp->type & __ETYPEV_PTR_MASK)
efree(vp->v.p);

if(vp->type & __ETYPEV_NEW_MASK)
efree(vp);
else
Expand All @@ -176,3 +230,103 @@ etypev evar_type( evarp vp) { return vp ? vp->type : E_NAV;}
bool evar_isArr(evarp vp) { return vp ? vp->type & __ETYPEV_ARR_MASK : 0; }
bool evar_isPtr(evarp vp) { return vp ? vp->type & __ETYPEV_PTR_MASK : 0; }

bool evar_set(evarp vp, uint idx, conptr in, int ilen)
{
is1_ret(!vp || vp->cnt <= idx, false);

switch (vp->type & __ETYPEV_VAR_MASK)
{

case E_I8 :
case E_I16:
case E_I32:
case E_I64:

case E_U8 :
case E_U16:
case E_U32:
case E_U64:

case E_F32:
case E_F64:

case E_PTR:
case E_USER:
is1_ret(ilen != vp->esize, false);

memcpy(_vp_pptr(vp, vp->esize, idx), in, vp->esize);

return true;

case E_STR:
case E_RAW: __estr_wrtB((estr*)_vp_pptr(vp, vp->esize, idx), in, ilen);

return true;

}

return false;
}

bool evar_setI(evarp vp, uint idx, i64 val) { is1_ret(!vp || (vp->type & __ETYPEV_VAR_MASK) != E_I64 || vp->cnt <= idx, false); *(i64 *)_vp_pptr(vp, 8, idx) = val; return true; }
bool evar_setF(evarp vp, uint idx, f64 val) { is1_ret(!vp || (vp->type & __ETYPEV_VAR_MASK) != E_F64 || vp->cnt <= idx, false); *(f64 *)_vp_pptr(vp, 8, idx) = val; return true; }
bool evar_setS(evarp vp, uint idx, constr str) { is1_ret(!vp || (vp->type & __ETYPEV_VAR_MASK) != E_STR || vp->cnt <= idx, false); __estr_wrtS((estr *)_vp_pptr(vp, 8, idx), str); return true; }
bool evar_setP(evarp vp, uint idx, conptr ptr) { is1_ret(!vp || (vp->type & __ETYPEV_VAR_MASK) != E_PTR || vp->cnt <= idx, false); *(conptr*)_vp_pptr(vp, 8, idx) = ptr; return true; }
bool evar_setR(evarp vp, uint idx, conptr in, int ilen){ is1_ret(!vp || (vp->type & __ETYPEV_VAR_MASK) != E_RAW || vp->cnt <= idx, false); __estr_wrtB((estr * )_vp_pptr(vp, 8, idx), in, ilen); return true; }

evar evar_at (evarp vp, uint idx)
{
evar out;

is1_ret(!vp || vp->cnt <= idx, EVAR_NAV);

out.type = (vp->type & __ETYPEV_VAR_MASK) | __ETYPEV_PTR_MASK;
out.esize = vp->esize;
out.cnt = 1;
out.v.p = _vp_pptr(vp, vp->esize, idx);

return out;
}

evar evar_val (evarp vp, uint idx)
{
evar out;

is1_ret(!vp || vp->cnt <= idx, EVAR_NAV);

out.type = (vp->type & __ETYPEV_VAR_MASK) | __ETYPEV_PTR_MASK;
out.esize = vp->esize;
out.cnt = 1;
out.v.p = _vp_pptr(vp, vp->esize, idx);

return out;

#if 0
switch (vp->type & __ETYPEV_VAR_MASK)
{
case E_I8 :
case E_I16 :
case E_I32 :
case E_I64 :

case E_U8 :
case E_U16 :
case E_U32 :
case E_U64 :

case E_F32 :
case E_F64 :

case E_PTR :
case E_USER: return EVAR_NAV;

}

#endif
}

i64 evar_valI(evarp vp, uint idx) { is1_ret(!vp || (vp->type & __ETYPEV_VAR_MASK) != E_I64 || vp->cnt <= idx, 0); return *(i64 *)_vp_pptr(vp, 8, idx); }
f64 evar_valF(evarp vp, uint idx) { is1_ret(!vp || (vp->type & __ETYPEV_VAR_MASK) != E_F64 || vp->cnt <= idx, 0); return *(f64 *)_vp_pptr(vp, 8, idx); }
cstr evar_valS(evarp vp, uint idx) { is1_ret(!vp || (vp->type & __ETYPEV_VAR_MASK) != E_STR || vp->cnt <= idx, 0); return *(cstr *)_vp_pptr(vp, 8, idx); }
cptr evar_valP(evarp vp, uint idx) { is1_ret(!vp || (vp->type & __ETYPEV_VAR_MASK) != E_PTR || vp->cnt <= idx, 0); return *(cptr *)_vp_pptr(vp, 8, idx); }
cptr evar_valR(evarp vp, uint idx) { is1_ret(!vp || (vp->type & __ETYPEV_VAR_MASK) != E_RAW || vp->cnt <= idx, 0); return *(cptr *)_vp_pptr(vp, 8, idx); }
20 changes: 10 additions & 10 deletions src/libs/etools/evar.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ typedef enum etypev_s{
//! val
E_NAV = 0, // not a var

#undef _XX
#define _XX(type, v) \
E_ ## type = v, \
E_ ## type ## _a = v | __ETYPEV_ARR_MASK, \
Expand Down Expand Up @@ -64,6 +63,7 @@ typedef enum etypev_s{
_XX(USER, 14), // E_USER

}etypev;
#undef _XX

#define __EVAR_ITEM_LEN_MAP {0, 1, 2, 4, 8, 1, 2, 4, 8, 4, 8, 8, 8, 8}

Expand Down Expand Up @@ -134,17 +134,17 @@ bool evar_setS(evarp vp, uint idx, constr str);
bool evar_setP(evarp vp, uint idx, conptr ptr);
bool evar_setR(evarp vp, uint idx, conptr in, int ilen);

evar evar_at (evarp v, uint idx);
evar evar_at (evarp vp, uint idx);

evar evar_val (evarp v, uint idx);
i64 evar_valI(evarp v, uint idx);
f64 evar_valF(evarp v, uint idx);
cstr evar_valS(evarp v, uint idx);
cptr evar_valP(evarp v, uint idx);
cptr evar_valR(evarp v, uint idx);
evar evar_val (evarp vp, uint idx); // do not free the returned var
i64 evar_valI(evarp vp, uint idx);
f64 evar_valF(evarp vp, uint idx);
cstr evar_valS(evarp vp, uint idx);
cptr evar_valP(evarp vp, uint idx);
cptr evar_valR(evarp vp, uint idx);

uint evar_lenS(evarp v, uint idx);
uint evar_lenR(evarp v, uint idx);
uint evar_lenS(evarp vp, uint idx);
uint evar_lenR(evarp vp, uint idx);


#ifdef __cplusplus
Expand Down

0 comments on commit d19e3a0

Please sign in to comment.