added hawk_json_feeduchars()

This commit is contained in:
2025-06-08 21:51:11 +09:00
parent 7f9ac2047f
commit 63eb61c5c0
17 changed files with 710 additions and 577 deletions

View File

@ -41,7 +41,7 @@ static hawk_sio_t* open_sio (SedStd::Stream::Data& io, const hawk_ooch_t* file,
if (sio == HAWK_NULL) if (sio == HAWK_NULL)
{ {
const hawk_ooch_t* old_errmsg = hawk_sed_backuperrmsg((hawk_sed_t*)io); const hawk_ooch_t* old_errmsg = hawk_sed_backuperrmsg((hawk_sed_t*)io);
((Sed*)io)->formatError (HAWK_SED_EIOFIL, HAWK_NULL, "unable to open %js - %js", file, old_errmsg); ((Sed*)io)->formatError(HAWK_SED_EIOFIL, HAWK_NULL, "unable to open %js - %js", file, old_errmsg);
} }
return sio; return sio;
} }
@ -60,7 +60,7 @@ static hawk_sio_t* open_sio_std (SedStd::Stream::Data& io, hawk_sio_std_t std, i
if (sio == HAWK_NULL) if (sio == HAWK_NULL)
{ {
const hawk_ooch_t* old_errmsg = hawk_sed_backuperrmsg((hawk_sed_t*)io); const hawk_ooch_t* old_errmsg = hawk_sed_backuperrmsg((hawk_sed_t*)io);
((Sed*)io)->formatError (HAWK_SED_EIOFIL, HAWK_NULL, "unable to open %js - %js", std_names[std], old_errmsg); ((Sed*)io)->formatError(HAWK_SED_EIOFIL, HAWK_NULL, "unable to open %js - %js", std_names[std], old_errmsg);
} }
return sio; return sio;
} }
@ -113,7 +113,7 @@ int SedStd::FileStream::open (Data& io)
#if defined(HAWK_OOCH_IS_UCH) #if defined(HAWK_OOCH_IS_UCH)
this->file = hawk_gem_dupucstr(gem, tmp, HAWK_NULL); this->file = hawk_gem_dupucstr(gem, tmp, HAWK_NULL);
#else #else
this->file = hawk_duputobcstr(gem, tmp, HAWK_NULL); this->file = hawk_gem_duputobcstr(gem, tmp, HAWK_NULL);
#endif #endif
} }
else else
@ -183,7 +183,7 @@ hawk_ooi_t SedStd::FileStream::read (Data& io, hawk_ooch_t* buf, hawk_oow_t len)
if (!io.getName() && this->file) // io.getMode() must be READ if (!io.getName() && this->file) // io.getMode() must be READ
{ {
const hawk_ooch_t* old_errmsg = hawk_sed_backuperrmsg((hawk_sed_t*)io); const hawk_ooch_t* old_errmsg = hawk_sed_backuperrmsg((hawk_sed_t*)io);
((Sed*)io)->formatError (HAWK_SED_EIOFIL, HAWK_NULL, "unable to read %js - %js", this->file, old_errmsg); ((Sed*)io)->formatError(HAWK_SED_EIOFIL, HAWK_NULL, "unable to read %js - %js", this->file, old_errmsg);
} }
} }
@ -199,7 +199,7 @@ hawk_ooi_t SedStd::FileStream::write (Data& io, const hawk_ooch_t* buf, hawk_oow
if (!io.getName() && this->file) // io.getMode() must be WRITE if (!io.getName() && this->file) // io.getMode() must be WRITE
{ {
const hawk_ooch_t* old_errmsg = hawk_sed_backuperrmsg((hawk_sed_t*)io); const hawk_ooch_t* old_errmsg = hawk_sed_backuperrmsg((hawk_sed_t*)io);
((Sed*)io)->formatError (HAWK_SED_EIOFIL, HAWK_NULL, "unable to read %js - %js", this->file, old_errmsg); ((Sed*)io)->formatError(HAWK_SED_EIOFIL, HAWK_NULL, "unable to read %js - %js", this->file, old_errmsg);
} }
} }
@ -336,7 +336,7 @@ int SedStd::StringStream::open (Data& io)
} }
else else
{ {
((Sed*)io)->formatError (HAWK_EINVAL, HAWK_NULL, "no input data available to open"); ((Sed*)io)->formatError(HAWK_EINVAL, HAWK_NULL, "no input data available to open");
return -1; return -1;
} }
} }
@ -350,7 +350,7 @@ int SedStd::StringStream::open (Data& io)
#if defined(HAWK_OOCH_IS_UCH) #if defined(HAWK_OOCH_IS_UCH)
this->in.str = hawk_gem_dupucstr(gem, (const hawk_uch_t*)this->in._str, &len); this->in.str = hawk_gem_dupucstr(gem, (const hawk_uch_t*)this->in._str, &len);
#else #else
this->in.str = hawk_duputobcstr(gem, (const hawk_uch_t*)this->in._str, &len); this->in.str = hawk_gem_duputobcstr(gem, (const hawk_uch_t*)this->in._str, &len);
#endif #endif
} }
else else

View File

@ -126,13 +126,26 @@ HAWK_EXPORT void hawk_json_reset (
hawk_json_t* json hawk_json_t* json
); );
HAWK_EXPORT int hawk_json_feed ( HAWK_EXPORT int hawk_json_feedbchars (
hawk_json_t* json, hawk_json_t* json,
const void* ptr, const hawk_bch_t* ptr,
hawk_oow_t len, hawk_oow_t len,
hawk_oow_t* xlen hawk_oow_t* xlen
); );
HAWK_EXPORT int hawk_json_feeduchars (
hawk_json_t* json,
const hawk_uch_t* ptr,
hawk_oow_t len,
hawk_oow_t* xlen
);
#if defined(HAWK_OOCH_IS_UCH)
# define hawk_json_feed hawk_json_feeduchars
#else
# define hawk_json_feed hawk_json_feedbchars
#endif
HAWK_EXPORT hawk_json_state_t hawk_json_getstate ( HAWK_EXPORT hawk_json_state_t hawk_json_getstate (
hawk_json_t* json hawk_json_t* json
); );
@ -208,12 +221,12 @@ static HAWK_INLINE void hawk_json_geterror (hawk_json_t* json, hawk_errnum_t* er
#define hawk_json_geterror(json, errnum, errmsg, errloc) (hawk_gem_geterror(hawk_json_getgem(json), errnum, errmsg, errloc)) #define hawk_json_geterror(json, errnum, errmsg, errloc) (hawk_gem_geterror(hawk_json_getgem(json), errnum, errmsg, errloc))
#endif #endif
#if defined(HAWK_OOCH_IS_BCH) #if defined(HAWK_OOCH_IS_UCH)
# define hawk_json_geterrmsg hawk_json_geterrbmsg
# define hawk_json_geterrinf hawk_json_geterrbinf
#else
# define hawk_json_geterrmsg hawk_json_geterrumsg # define hawk_json_geterrmsg hawk_json_geterrumsg
# define hawk_json_geterrinf hawk_json_geterruinf # define hawk_json_geterrinf hawk_json_geterruinf
#else
# define hawk_json_geterrmsg hawk_json_geterrbmsg
# define hawk_json_geterrinf hawk_json_geterrbinf
#endif #endif

View File

@ -143,6 +143,35 @@ HAWK_EXPORT int hawk_comp_bchars_bcstr (
/* ------------------------------------ */ /* ------------------------------------ */
HAWK_EXPORT int hawk_comp_ucstr_bcstr (
const hawk_uch_t* str1,
const hawk_bch_t* str2,
int ignorecase
);
HAWK_EXPORT int hawk_comp_ucstr_bcstr_limited (
const hawk_uch_t* str1,
const hawk_bch_t* str2,
hawk_oow_t maxlen,
int ignorecase
);
HAWK_EXPORT int hawk_comp_uchars_bcstr (
const hawk_uch_t* str1,
hawk_oow_t len,
const hawk_bch_t* str2,
int ignorecase
);
HAWK_EXPORT int hawk_comp_bchars_ucstr (
const hawk_bch_t* str1,
hawk_oow_t len,
const hawk_uch_t* str2,
int ignorecase
);
/* ------------------------------------ */
HAWK_EXPORT hawk_oow_t hawk_concat_uchars_to_ucstr ( HAWK_EXPORT hawk_oow_t hawk_concat_uchars_to_ucstr (
hawk_uch_t* buf, hawk_uch_t* buf,
hawk_oow_t bsz, hawk_oow_t bsz,

View File

@ -143,6 +143,35 @@ HAWK_EXPORT int hawk_comp_bchars_bcstr (
/* ------------------------------------ */ /* ------------------------------------ */
HAWK_EXPORT int hawk_comp_ucstr_bcstr (
const hawk_uch_t* str1,
const hawk_bch_t* str2,
int ignorecase
);
HAWK_EXPORT int hawk_comp_ucstr_bcstr_limited (
const hawk_uch_t* str1,
const hawk_bch_t* str2,
hawk_oow_t maxlen,
int ignorecase
);
HAWK_EXPORT int hawk_comp_uchars_bcstr (
const hawk_uch_t* str1,
hawk_oow_t len,
const hawk_bch_t* str2,
int ignorecase
);
HAWK_EXPORT int hawk_comp_bchars_ucstr (
const hawk_bch_t* str1,
hawk_oow_t len,
const hawk_uch_t* str2,
int ignorecase
);
/* ------------------------------------ */
HAWK_EXPORT hawk_oow_t hawk_concat_uchars_to_ucstr ( HAWK_EXPORT hawk_oow_t hawk_concat_uchars_to_ucstr (
hawk_uch_t* buf, hawk_uch_t* buf,
hawk_oow_t bsz, hawk_oow_t bsz,

View File

@ -374,35 +374,6 @@ HAWK_EXPORT hawk_oow_t hawk_hash_bytes_ (
* STRING * STRING
* ========================================================================= */ * ========================================================================= */
HAWK_EXPORT int hawk_comp_ucstr_bcstr (
const hawk_uch_t* str1,
const hawk_bch_t* str2,
int ignorecase
);
HAWK_EXPORT int hawk_comp_ucstr_bcstr_limited (
const hawk_uch_t* str1,
const hawk_bch_t* str2,
hawk_oow_t maxlen,
int ignorecase
);
HAWK_EXPORT int hawk_comp_uchars_bcstr (
const hawk_uch_t* str1,
hawk_oow_t len,
const hawk_bch_t* str2,
int ignorecase
);
HAWK_EXPORT int hawk_comp_bchars_ucstr (
const hawk_bch_t* str1,
hawk_oow_t len,
const hawk_uch_t* str2,
int ignorecase
);
/* ------------------------------------------------------------------------- */
HAWK_EXPORT hawk_oow_t hawk_subst_for_uchars_to_ucstr ( HAWK_EXPORT hawk_oow_t hawk_subst_for_uchars_to_ucstr (
hawk_uch_t* buf, hawk_uch_t* buf,
hawk_oow_t bsz, hawk_oow_t bsz,

View File

@ -114,7 +114,7 @@ static void pop_state (hawk_json_t* json)
hawk_json_state_node_t* ss; hawk_json_state_node_t* ss;
ss = json->state_stack; ss = json->state_stack;
HAWK_ASSERT (ss != HAWK_NULL && ss != &json->state_top); HAWK_ASSERT(ss != HAWK_NULL && ss != &json->state_top);
json->state_stack = ss->next; json->state_stack = ss->next;
if (json->state_stack->state == HAWK_JSON_STATE_IN_ARRAY) if (json->state_stack->state == HAWK_JSON_STATE_IN_ARRAY)
@ -143,7 +143,7 @@ static int invoke_data_inst (hawk_json_t* json, hawk_json_inst_t inst)
{ {
if (inst != HAWK_JSON_INST_STRING) if (inst != HAWK_JSON_INST_STRING)
{ {
hawk_json_seterrbfmt (json, HAWK_NULL, HAWK_EINVAL, "dictionary key not a string - %.*js", json->tok.len, json->tok.ptr); hawk_json_seterrbfmt(json, HAWK_NULL, HAWK_EINVAL, "dictionary key not a string - %.*js", json->tok.len, json->tok.ptr);
return -1; return -1;
} }
@ -208,7 +208,7 @@ static int handle_string_value_char (hawk_json_t* json, hawk_ooci_t c)
if (n == 0 || n > HAWK_COUNTOF(bcsbuf)) if (n == 0 || n > HAWK_COUNTOF(bcsbuf))
{ {
/* illegal character or buffer to small */ /* illegal character or buffer to small */
hawk_json_seterrbfmt (json, HAWK_NULL, HAWK_EECERR, "unable to convert %jc", json->state_stack->u.sv.acc); hawk_json_seterrbfmt(json, HAWK_NULL, HAWK_EECERR, "unable to convert %jc", json->state_stack->u.sv.acc);
return -1; return -1;
} }
@ -356,7 +356,7 @@ static int handle_character_value_char (hawk_json_t* json, hawk_ooci_t c)
if (json->tok.len < 1) if (json->tok.len < 1)
{ {
hawk_json_seterrbfmt (json, HAWK_NULL, HAWK_EINVAL, "no character in a character literal"); hawk_json_seterrbfmt(json, HAWK_NULL, HAWK_EINVAL, "no character in a character literal");
return -1; return -1;
} }
if (invoke_data_inst(json, HAWK_JSON_INST_CHARACTER) <= -1) return -1; if (invoke_data_inst(json, HAWK_JSON_INST_CHARACTER) <= -1) return -1;
@ -368,7 +368,7 @@ static int handle_character_value_char (hawk_json_t* json, hawk_ooci_t c)
if (json->tok.len > 1) if (json->tok.len > 1)
{ {
hawk_json_seterrbfmt (json, HAWK_NULL, HAWK_EINVAL, "too many characters in a character literal - %.*js", (int)json->tok.len, json->tok.ptr); hawk_json_seterrbfmt(json, HAWK_NULL, HAWK_EINVAL, "too many characters in a character literal - %.*js", (int)json->tok.len, json->tok.ptr);
return -1; return -1;
} }
@ -392,10 +392,10 @@ static int handle_numeric_value_char (hawk_json_t* json, hawk_ooci_t c)
pop_state (json); pop_state (json);
HAWK_ASSERT (json->tok.len > 0); HAWK_ASSERT(json->tok.len > 0);
if (!hawk_is_ooch_digit(json->tok.ptr[json->tok.len - 1])) if (!hawk_is_ooch_digit(json->tok.ptr[json->tok.len - 1]))
{ {
hawk_json_seterrbfmt (json, HAWK_NULL, HAWK_EINVAL, "invalid numeric value - %.*js", (int)json->tok.len, json->tok.ptr); hawk_json_seterrbfmt(json, HAWK_NULL, HAWK_EINVAL, "invalid numeric value - %.*js", (int)json->tok.len, json->tok.ptr);
return -1; return -1;
} }
if (invoke_data_inst(json, HAWK_JSON_INST_NUMBER) <= -1) return -1; if (invoke_data_inst(json, HAWK_JSON_INST_NUMBER) <= -1) return -1;
@ -419,7 +419,7 @@ static int handle_word_value_char (hawk_json_t* json, hawk_ooci_t c)
else if (hawk_comp_oochars_bcstr(json->tok.ptr, json->tok.len, "false", 0) == 0) inst = HAWK_JSON_INST_FALSE; else if (hawk_comp_oochars_bcstr(json->tok.ptr, json->tok.len, "false", 0) == 0) inst = HAWK_JSON_INST_FALSE;
else else
{ {
hawk_json_seterrbfmt (json, HAWK_NULL, HAWK_EINVAL, "invalid word value - %.*js", json->tok.len, json->tok.ptr); hawk_json_seterrbfmt(json, HAWK_NULL, HAWK_EINVAL, "invalid word value - %.*js", json->tok.len, json->tok.ptr);
return -1; return -1;
} }
@ -452,7 +452,7 @@ static int handle_start_char (hawk_json_t* json, hawk_ooci_t c)
} }
else else
{ {
hawk_json_seterrbfmt (json, HAWK_NULL, HAWK_EINVAL, "not starting with [ or { - %jc", (hawk_ooch_t)c); hawk_json_seterrbfmt(json, HAWK_NULL, HAWK_EINVAL, "not starting with [ or { - %jc", (hawk_ooch_t)c);
return -1; return -1;
} }
} }
@ -469,7 +469,7 @@ static int handle_char_in_array (hawk_json_t* json, hawk_ooci_t c)
{ {
if (!json->state_stack->u.ia.got_value) if (!json->state_stack->u.ia.got_value)
{ {
hawk_json_seterrbfmt (json, HAWK_NULL, HAWK_EINVAL, "redundant comma in array - %jc", (hawk_ooch_t)c); hawk_json_seterrbfmt(json, HAWK_NULL, HAWK_EINVAL, "redundant comma in array - %jc", (hawk_ooch_t)c);
return -1; return -1;
} }
json->state_stack->u.ia.got_value = 0; json->state_stack->u.ia.got_value = 0;
@ -484,7 +484,7 @@ static int handle_char_in_array (hawk_json_t* json, hawk_ooci_t c)
{ {
if (json->state_stack->u.ia.got_value) if (json->state_stack->u.ia.got_value)
{ {
hawk_json_seterrbfmt (json, HAWK_NULL, HAWK_EINVAL, "comma required in array - %jc", (hawk_ooch_t)c); hawk_json_seterrbfmt(json, HAWK_NULL, HAWK_EINVAL, "comma required in array - %jc", (hawk_ooch_t)c);
return -1; return -1;
} }
@ -531,7 +531,7 @@ static int handle_char_in_array (hawk_json_t* json, hawk_ooci_t c)
} }
else else
{ {
hawk_json_seterrbfmt (json, HAWK_NULL, HAWK_EINVAL, "wrong character inside array - %jc[%d]", (hawk_ooch_t)c, (int)c); hawk_json_seterrbfmt(json, HAWK_NULL, HAWK_EINVAL, "wrong character inside array - %jc[%d]", (hawk_ooch_t)c, (int)c);
return -1; return -1;
} }
} }
@ -549,7 +549,7 @@ static int handle_char_in_dic (hawk_json_t* json, hawk_ooci_t c)
{ {
if (json->state_stack->u.id.state != 1) if (json->state_stack->u.id.state != 1)
{ {
hawk_json_seterrbfmt (json, HAWK_NULL, HAWK_EINVAL, "redundant colon in dictionary - %jc", (hawk_ooch_t)c); hawk_json_seterrbfmt(json, HAWK_NULL, HAWK_EINVAL, "redundant colon in dictionary - %jc", (hawk_ooch_t)c);
return -1; return -1;
} }
json->state_stack->u.id.state++; json->state_stack->u.id.state++;
@ -559,7 +559,7 @@ static int handle_char_in_dic (hawk_json_t* json, hawk_ooci_t c)
{ {
if (json->state_stack->u.id.state != 3) if (json->state_stack->u.id.state != 3)
{ {
hawk_json_seterrbfmt (json, HAWK_NULL, HAWK_EINVAL, "redundant comma in dicitonary - %jc", (hawk_ooch_t)c); hawk_json_seterrbfmt(json, HAWK_NULL, HAWK_EINVAL, "redundant comma in dicitonary - %jc", (hawk_ooch_t)c);
return -1; return -1;
} }
json->state_stack->u.id.state = 0; json->state_stack->u.id.state = 0;
@ -574,12 +574,12 @@ static int handle_char_in_dic (hawk_json_t* json, hawk_ooci_t c)
{ {
if (json->state_stack->u.id.state == 1) if (json->state_stack->u.id.state == 1)
{ {
hawk_json_seterrbfmt (json, HAWK_NULL, HAWK_EINVAL, "colon required in dicitonary - %jc", (hawk_ooch_t)c); hawk_json_seterrbfmt(json, HAWK_NULL, HAWK_EINVAL, "colon required in dicitonary - %jc", (hawk_ooch_t)c);
return -1; return -1;
} }
else if (json->state_stack->u.id.state == 3) else if (json->state_stack->u.id.state == 3)
{ {
hawk_json_seterrbfmt (json, HAWK_NULL, HAWK_EINVAL, "comma required in dicitonary - %jc", (hawk_ooch_t)c); hawk_json_seterrbfmt(json, HAWK_NULL, HAWK_EINVAL, "comma required in dicitonary - %jc", (hawk_ooch_t)c);
return -1; return -1;
} }
@ -626,7 +626,7 @@ static int handle_char_in_dic (hawk_json_t* json, hawk_ooci_t c)
} }
else else
{ {
hawk_json_seterrbfmt (json, HAWK_NULL, HAWK_EINVAL, "wrong character inside dictionary - %jc[%d]", (hawk_ooch_t)c, (int)c); hawk_json_seterrbfmt(json, HAWK_NULL, HAWK_EINVAL, "wrong character inside dictionary - %jc[%d]", (hawk_ooch_t)c, (int)c);
return -1; return -1;
} }
} }
@ -648,7 +648,7 @@ start_over:
} }
else else
{ {
hawk_json_seterrnum (json, HAWK_NULL, HAWK_EEOF); hawk_json_seterrnum(json, HAWK_NULL, HAWK_EEOF);
return -1; return -1;
} }
} }
@ -684,7 +684,7 @@ start_over:
break; break;
default: default:
hawk_json_seterrbfmt (json, HAWK_NULL, HAWK_EINTERN, "internal error - must not be called for state %d", (int)json->state_stack->state); hawk_json_seterrbfmt(json, HAWK_NULL, HAWK_EINTERN, "internal error - must not be called for state %d", (int)json->state_stack->state);
return -1; return -1;
} }
@ -696,7 +696,7 @@ start_over:
/* ========================================================================= */ /* ========================================================================= */
static int feed_json_data (hawk_json_t* json, const hawk_bch_t* data, hawk_oow_t len, hawk_oow_t* xlen) static int feed_json_data_b (hawk_json_t* json, const hawk_bch_t* data, hawk_oow_t len, hawk_oow_t* xlen)
{ {
const hawk_bch_t* ptr; const hawk_bch_t* ptr;
const hawk_bch_t* end; const hawk_bch_t* end;
@ -747,9 +747,51 @@ oops:
return -1; return -1;
} }
/* ========================================================================= */
static int feed_json_data_u (hawk_json_t* json, const hawk_uch_t* data, hawk_oow_t len, hawk_oow_t* xlen)
{
const hawk_uch_t* ptr;
const hawk_uch_t* end;
ptr = data;
end = ptr + len;
while (ptr < end)
{
hawk_ooci_t c;
#if defined(HAWK_OOCH_IS_UCH)
c = *ptr++;
/* handle a single character */
if (handle_char(json, c) <= -1) goto oops;
#else
hawk_bch_t bcsbuf[HAWK_BCSIZE_MAX];
hawk_oow_t mlen = 0;
hawk_oow_t n, i;
n = json->_gem.cmgr->uctobc(*ptr++, bcsbuf, HAWK_COUNTOF(bcsbuf));
if (n == 0) goto oops; // illegal character
for (i = 0; i < n; i++)
{
if (handle_char(json, bcsbuf[i]) <= -1) goto oops;
}
#endif
}
*xlen = ptr - data;
return 1;
oops:
/* TODO: compute the number of processed bytes so far and return it via a parameter??? */
/*printf ("feed oops....\n");*/
return -1;
}
/* ========================================================================= */ /* ========================================================================= */
hawk_json_t* hawk_json_open (hawk_mmgr_t* mmgr, hawk_oow_t xtnsize, hawk_cmgr_t* cmgr, hawk_json_prim_t* prim, hawk_errnum_t* errnum) hawk_json_t* hawk_json_open (hawk_mmgr_t* mmgr, hawk_oow_t xtnsize, hawk_cmgr_t* cmgr, hawk_json_prim_t* prim, hawk_errnum_t* errnum)
{ {
hawk_json_t* json; hawk_json_t* json;
@ -761,7 +803,7 @@ hawk_json_t* hawk_json_open (hawk_mmgr_t* mmgr, hawk_oow_t xtnsize, hawk_cmgr_t*
return HAWK_NULL; return HAWK_NULL;
} }
HAWK_MEMSET (json, 0, HAWK_SIZEOF(*json) + xtnsize); HAWK_MEMSET(json, 0, HAWK_SIZEOF(*json) + xtnsize);
json->_instsize = HAWK_SIZEOF(*json); json->_instsize = HAWK_SIZEOF(*json);
json->_gem.mmgr = mmgr; json->_gem.mmgr = mmgr;
json->_gem.cmgr = cmgr; json->_gem.cmgr = cmgr;
@ -784,9 +826,9 @@ hawk_json_t* hawk_json_open (hawk_mmgr_t* mmgr, hawk_oow_t xtnsize, hawk_cmgr_t*
void hawk_json_close (hawk_json_t* json) void hawk_json_close (hawk_json_t* json)
{ {
pop_all_states (json); pop_all_states(json);
if (json->tok.ptr) hawk_json_freemem (json, json->tok.ptr); if (json->tok.ptr) hawk_json_freemem(json, json->tok.ptr);
HAWK_MMGR_FREE (hawk_json_getmmgr(json), json); HAWK_MMGR_FREE(hawk_json_getmmgr(json), json);
} }
int hawk_json_setoption (hawk_json_t* json, hawk_json_option_t id, const void* value) int hawk_json_setoption (hawk_json_t* json, hawk_json_option_t id, const void* value)
@ -798,7 +840,7 @@ int hawk_json_setoption (hawk_json_t* json, hawk_json_option_t id, const void* v
return 0; return 0;
} }
hawk_json_seterrnum (json, HAWK_NULL, HAWK_EINVAL); hawk_json_seterrnum(json, HAWK_NULL, HAWK_EINVAL);
return -1; return -1;
} }
@ -811,7 +853,7 @@ int hawk_json_getoption (hawk_json_t* json, hawk_json_option_t id, void* value)
return 0; return 0;
}; };
hawk_json_seterrnum (json, HAWK_NULL, HAWK_EINVAL); hawk_json_seterrnum(json, HAWK_NULL, HAWK_EINVAL);
return -1; return -1;
} }
@ -825,17 +867,17 @@ hawk_errstr_t hawk_json_geterrstr (hawk_json_t* json)
void hawk_json_seterrbfmt (hawk_json_t* json, const hawk_loc_t* errloc, hawk_errnum_t errnum, const hawk_bch_t* fmt, ...) void hawk_json_seterrbfmt (hawk_json_t* json, const hawk_loc_t* errloc, hawk_errnum_t errnum, const hawk_bch_t* fmt, ...)
{ {
va_list ap; va_list ap;
va_start (ap, fmt); va_start(ap, fmt);
hawk_gem_seterrbvfmt (hawk_json_getgem(json), errloc, errnum, fmt, ap); hawk_gem_seterrbvfmt(hawk_json_getgem(json), errloc, errnum, fmt, ap);
va_end (ap); va_end(ap);
} }
void hawk_json_seterrufmt (hawk_json_t* json, const hawk_loc_t* errloc, hawk_errnum_t errnum, const hawk_uch_t* fmt, ...) void hawk_json_seterrufmt (hawk_json_t* json, const hawk_loc_t* errloc, hawk_errnum_t errnum, const hawk_uch_t* fmt, ...)
{ {
va_list ap; va_list ap;
va_start (ap, fmt); va_start(ap, fmt);
hawk_gem_seterruvfmt (hawk_json_getgem(json), errloc, errnum, fmt, ap); hawk_gem_seterruvfmt(hawk_json_getgem(json), errloc, errnum, fmt, ap);
va_end (ap); va_end(ap);
} }
@ -855,7 +897,7 @@ void* hawk_json_allocmem (hawk_json_t* json, hawk_oow_t size)
void* ptr; void* ptr;
ptr = HAWK_MMGR_ALLOC(hawk_json_getmmgr(json), size); ptr = HAWK_MMGR_ALLOC(hawk_json_getmmgr(json), size);
if (!ptr) hawk_json_seterrnum (json, HAWK_NULL, HAWK_ENOMEM); if (!ptr) hawk_json_seterrnum(json, HAWK_NULL, HAWK_ENOMEM);
return ptr; return ptr;
} }
@ -864,21 +906,21 @@ void* hawk_json_callocmem (hawk_json_t* json, hawk_oow_t size)
void* ptr; void* ptr;
ptr = HAWK_MMGR_ALLOC(hawk_json_getmmgr(json), size); ptr = HAWK_MMGR_ALLOC(hawk_json_getmmgr(json), size);
if (!ptr) hawk_json_seterrnum (json, HAWK_NULL, HAWK_ENOMEM); if (!ptr) hawk_json_seterrnum(json, HAWK_NULL, HAWK_ENOMEM);
else HAWK_MEMSET (ptr, 0, size); else HAWK_MEMSET(ptr, 0, size);
return ptr; return ptr;
} }
void* hawk_json_reallocmem (hawk_json_t* json, void* ptr, hawk_oow_t size) void* hawk_json_reallocmem (hawk_json_t* json, void* ptr, hawk_oow_t size)
{ {
ptr = HAWK_MMGR_REALLOC(hawk_json_getmmgr(json), ptr, size); ptr = HAWK_MMGR_REALLOC(hawk_json_getmmgr(json), ptr, size);
if (!ptr) hawk_json_seterrnum (json, HAWK_NULL, HAWK_ENOMEM); if (!ptr) hawk_json_seterrnum(json, HAWK_NULL, HAWK_ENOMEM);
return ptr; return ptr;
} }
void hawk_json_freemem (hawk_json_t* json, void* ptr) void hawk_json_freemem (hawk_json_t* json, void* ptr)
{ {
HAWK_MMGR_FREE (hawk_json_getmmgr(json), ptr); HAWK_MMGR_FREE(hawk_json_getmmgr(json), ptr);
} }
/* ========================================================================= */ /* ========================================================================= */
@ -892,21 +934,38 @@ void hawk_json_reset (hawk_json_t* json)
{ {
/* TODO: reset XXXXXXXXXXXXXXXXXXXXXXXXXXXxxxxx */ /* TODO: reset XXXXXXXXXXXXXXXXXXXXXXXXXXXxxxxx */
pop_all_states (json); pop_all_states (json);
HAWK_ASSERT (json->state_stack == &json->state_top); HAWK_ASSERT(json->state_stack == &json->state_top);
json->state_stack->state = HAWK_JSON_STATE_START; json->state_stack->state = HAWK_JSON_STATE_START;
} }
int hawk_json_feed (hawk_json_t* json, const void* ptr, hawk_oow_t len, hawk_oow_t* xlen) int hawk_json_feedbchars (hawk_json_t* json, const hawk_bch_t* ptr, hawk_oow_t len, hawk_oow_t* xlen)
{ {
int x; int x;
hawk_oow_t total, ylen; hawk_oow_t total, ylen;
const hawk_bch_t* buf;
buf = (const hawk_bch_t*)ptr;
total = 0; total = 0;
while (total < len) while (total < len)
{ {
x = feed_json_data(json, &buf[total], len - total, &ylen); x = feed_json_data_b(json, &ptr[total], len - total, &ylen);
if (x <= -1) return -1;
total += ylen;
if (x == 0) break; /* incomplete sequence encountered */
}
*xlen = total;
return 0;
}
int hawk_json_feeduchars (hawk_json_t* json, const hawk_uch_t* ptr, hawk_oow_t len, hawk_oow_t* xlen)
{
int x;
hawk_oow_t total, ylen;
total = 0;
while (total < len)
{
x = feed_json_data_u(json, &ptr[total], len - total, &ylen);
if (x <= -1) return -1; if (x <= -1) return -1;
total += ylen; total += ylen;

View File

@ -1,11 +1,39 @@
#include <hawk-json.h> #include <hawk-json.h>
#include <hawk-str.h>
#include <stdio.h> #include <stdio.h>
#include "tap.h" #include "tap.h"
#define OK_X(test) OK(test, #test)
static int on_json_element (hawk_json_t* json, hawk_json_inst_t inst, const hawk_oocs_t* str) static int on_json_element (hawk_json_t* json, hawk_json_inst_t inst, const hawk_oocs_t* str)
{ {
printf ("%d\n", inst); static int phase = 0;
if (phase == 0) OK_X(inst == HAWK_JSON_INST_START_DIC);
if (phase == 1)
{
OK_X(inst == HAWK_JSON_INST_KEY);
OK_X(hawk_comp_oochars_bcstr(str->ptr, str->len, "hello", 0) == 0);
}
if (phase == 2)
{
OK_X(inst == HAWK_JSON_INST_STRING);
OK_X(hawk_comp_oochars_bcstr(str->ptr, str->len, "world", 0) == 0);
}
if (phase == 3)
{
OK_X(inst == HAWK_JSON_INST_KEY);
OK_X(hawk_comp_oochars_bcstr(str->ptr, str->len, "key1", 0) == 0);
}
if (phase == 4)
{
OK_X(inst == HAWK_JSON_INST_NUMBER);
OK_X(hawk_comp_oochars_bcstr(str->ptr, str->len, "12345", 0) == 0);
}
if (phase == 5) OK_X(inst == HAWK_JSON_INST_END_DIC);
phase++;
return 0; return 0;
} }
@ -13,7 +41,9 @@ int main ()
{ {
hawk_json_t* json; hawk_json_t* json;
hawk_json_prim_t prim; hawk_json_prim_t prim;
/* const hawk_ooch_t* TODO */ hawk_oow_t xlen;
int n;
no_plan(); no_plan();
prim.instcb = on_json_element; prim.instcb = on_json_element;
@ -21,7 +51,9 @@ int main ()
json = hawk_json_openstd(0, &prim, HAWK_NULL); json = hawk_json_openstd(0, &prim, HAWK_NULL);
OK (json != HAWK_NULL, "instantiation must be successful"); OK (json != HAWK_NULL, "instantiation must be successful");
/* hawk_json_feed(json, "{\"hello\": \"world\"}", & TODO */ n = hawk_json_feed(json, HAWK_T("{\"hello\": \"world\", \"key1\": 12345}"), 33, &xlen);
OK_X(n == 0);
OK_X(xlen == 33);
hawk_json_close(json); hawk_json_close(json);
return exit_status(); return exit_status();