sxt: rdb added few new options for print/escan to make life ease;

master
Alexander Vdolainen 9 years ago
parent b632f406ee
commit 65e0837fdd

@ -121,6 +121,9 @@ uint32_t sxtrdb_print_wot(sxtrdb_t *b, const char *fmt, int argc, ...);
/* returns length of used space in the buffer */ /* returns length of used space in the buffer */
uint32_t sxtrdb_length(sxtrdb_t *); uint32_t sxtrdb_length(sxtrdb_t *);
/* set used length */
int sxtrdb_setlength(sxtrdb_t *, uint32_t);
/* returns length of allocated space in the buffer */ /* returns length of allocated space in the buffer */
uint32_t sxtrdb_alength(sxtrdb_t *); uint32_t sxtrdb_alength(sxtrdb_t *);

@ -320,6 +320,7 @@ int sxtrdb_escan_va(sxtrdb_t *b, const char *fmt, int argc, va_list ap)
uint64_t *_u64; uint64_t *_u64;
void **_dp; void **_dp;
char **_cstr; char **_cstr;
sxtrdb_t **_rdb;
} d; } d;
const char *t, *last; const char *t, *last;
uint32_t len, clen; uint32_t len, clen;
@ -396,6 +397,50 @@ int sxtrdb_escan_va(sxtrdb_t *b, const char *fmt, int argc, va_list ap)
d._dp = NULL; d._dp = NULL;
r = SXT_SUCCESS; r = SXT_SUCCESS;
break; break;
case 'P':
d._dp = va_arg(ap, void **);
*d._dp = NULL;
len = sxtrdb_read_u32(b, &clen);
if(len != sizeof(uint32_t)) {
r = SXT_ERDBRD;
break;
}
if((*d._dp = malloc(clen)) == NULL) {
r = SXT_ENOMEM;
break;
}
len = sxtrdb_read_raw(b, *d._dp, clen);
if(len != clen) {
free(*d._dp);
goto __errrbread;
}
d._dp = NULL;
r = SXT_SUCCESS;
break;
case 'R':
d._rdb = va_arg(ap, sxtrdb_t **);
*d._rdb = NULL;
len = sxtrdb_read_u32(b, &clen);
if(len != sizeof(uint32_t)) {
r = SXT_ERDBRD;
break;
}
/* allocate rdb and write to it */
if((*d._rdb = sxtrdb_new_palloc(clen)) == NULL) {
r = SXT_ENOMEM;
break;
} else sxtrdb_setflags(*d._rdb, SXTRDB_BURN);
len = sxtrdb_read_raw(b, sxtrdb_rdata(*d._rdb), clen);
if(len != clen) {
sxtrdb_free(*d._dp);
goto __errrbread;
} else sxtrdb_setlength(*d._rdb, clen);
d._rdb = NULL;
r = SXT_SUCCESS;
break;
} }
if(r != SXT_SUCCESS) break; if(r != SXT_SUCCESS) break;
@ -429,8 +474,13 @@ int sxtrdb_escan_va(sxtrdb_t *b, const char *fmt, int argc, va_list ap)
free(*d._cstr); free(*d._cstr);
} }
break; break;
case 'R':
d._rdb = va_arg(ap_copy, sxtrdb_t **);
if(*d._rdb) sxtrdb_free(*d._rdb);
break;
case 'p': case 'p':
(void)va_arg(ap_copy, size_t); (void)va_arg(ap_copy, size_t);
case 'P':
d._dp = va_arg(ap_copy, void **); d._dp = va_arg(ap_copy, void **);
if(*d._dp) free(*d._dp); if(*d._dp) free(*d._dp);
break; break;
@ -462,6 +512,7 @@ uint32_t sxtrdb_print_va(sxtrdb_t *b, const char *fmt, int argc, va_list ap)
{ {
const char *t; const char *t;
char *cstr; char *cstr;
sxtrdb_t *rdb;
union { union {
uint8_t _u8; uint8_t _u8;
uint16_t _u16; uint16_t _u16;
@ -509,6 +560,27 @@ uint32_t sxtrdb_print_va(sxtrdb_t *b, const char *fmt, int argc, va_list ap)
if(r != clen) return 0; if(r != clen) return 0;
else len += clen; else len += clen;
break; break;
case 'R':
rdb = (sxtrdb_t *)va_arg(ap, sxtrdb_t *);
clen = sxtrdb_length(rdb);
r = sxtrdb_write_u32(b, (uint32_t)clen);
if(r != sizeof(uint32_t)) return 0;
else len += r;
r = sxtrdb_write_raw(b, sxtrdb_rdata(rdb), (uint32_t)clen);
if(r != clen) return 0;
else len += clen;
break;
case 'P':
clen = va_arg(ap, size_t);
d._dp = va_arg(ap, void *);
count++;
r = sxtrdb_write_u32(b, (uint32_t)clen);
if(r != sizeof(uint32_t)) return 0;
else len += r;
r = sxtrdb_write_raw(b, d._dp, (uint32_t)clen);
if(r != clen) return 0;
else len += clen;
break;
case 'p': case 'p':
clen = va_arg(ap, size_t); clen = va_arg(ap, size_t);
d._dp = va_arg(ap, void *); d._dp = va_arg(ap, void *);
@ -610,6 +682,16 @@ void sxtrdb_exflags(sxtrdb_t *b, int nf)
return; return;
} }
int sxtrdb_setlength(sxtrdb_t *b, uint32_t s)
{
if(!b) return SXT_EINVAL;
if(s > b->rlength) return SXT_EINVAL;
else b->ulength = s;
return SXT_SUCCESS;
}
/* let the buffer to use actually used bytes, not all allocated space /* let the buffer to use actually used bytes, not all allocated space
* will return SXT_SUCCESS on success (or in case if it doesn't required), * will return SXT_SUCCESS on success (or in case if it doesn't required),
* SXT_ENOMEM or other error if fails * SXT_ENOMEM or other error if fails

Loading…
Cancel
Save