1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
typedef struct _PAWVA_FUNC_TEXT { paws str; pawu pos, end; } PAWVA_FUNC_TEXT;
...
PAW_API pawu pawInitStringv( PAWS *str, paws args, va_list va )
{
bool cont = true;
PAWBUF UDV = {{0}};
PAWVA_FUNC **formats = pawStdFormats;
pawu leng = strlen(args);
pawzu need = leng;
PAWVA_FUNC_TEXT _txt = {NULL};
pawzu split;
pawhhu *udv;
pawu i = 0, f, took;
char *tmp;
pawd ate = 0;
/* Check if we're using standard or custom specifiers*/
if ( strstr( args, "%!" ) == args )
{
formats = va_arg( va, PAWVA_FUNC** );
_txt.pos = _txt.end = i = 2;
}
/* Get max ud size */
for ( f = 0; formats[f]; ++f )
{
pawzu size = 0;
PAWVA_FUNC *F = formats[f];
F->chk( NULL, &size, NULL, va );
split = (size > split) ? size : split;
}
split += sizeof(_txt);
/* Assign enough potential UDs that we don't need to reallocate later */
split += !split;
udv = pawInitBuffert( &UDV, split, PAW_O_CLEAR_NEW, leng / 2 );
if ( !udv )
return 0;
/* Collect arguments into UDs */
_txt.str = args + i;
for ( ; args[i]; ++i )
{
if ( args[i] != '%' )
{
_txt.end = i;
continue;
}
if ( args[i+1] == '%' )
_txt.end = i;
if ( _txt.end > _txt.pos )
{
memcpy( udv, &_txt, sizeof(_txt) );
UDV.Slots.taken += split;
udv += split;
UDV.count++;
}
_txt.str = args + i + 1;
_txt.pos = i + 1;
_txt.end = i + 1;
if ( args[i+1] == '%' )
{
++i;
continue;
}
for ( f = 0; formats[f]; ++f )
{
PAWVA_FUNC_TEXT *txt = (PAWVA_FUNC_TEXT*)udv;
PAWVA_FUNC *F = formats[f];
pawzu size = 0;
pawd ate;
memset( udv, 0, split );
ate = F->chk( txt + 1, &size, args + i, va );
if ( ate < 0 )
continue;
txt->str = (char*)F;
UDV.Slots.taken += split;
need += size;
udv += split;
UDV.count++;
i += ate;
break;
}
assert( formats[f] );
}
if ( _txt.end > _txt.pos )
{
memcpy( udv, &_txt, sizeof(_txt) );
UDV.Slots.taken += split;
udv += split;
UDV.count++;
}
/* Allocate enough space for all arguments to be placed in the
* temporary buffer */
tmp = pawInitStringz( str, need );
if ( !tmp )
{
pawTermBuffer( &UDV );
return 0;
}
/* Use collected UDs */
udv = pawSeekBufferHead( &UDV );
for ( i = 0; i < UDV.count; ++i, udv += split )
{
PAWVA_FUNC_TEXT *txt = (PAWVA_FUNC_TEXT*)udv;
if ( txt->end > txt->pos )
pawJoinStringn( str, txt->str, txt->end - txt->pos );
else
{
PAWVA_FUNC *F = ((PAWVA_FUNC*)(txt->str));
F->use( txt + 1, str );
}
}
/* Put generated string where it belongs */
pawTermBuffer( &UDV );
return took;
}
...
typedef struct _PAW_PUTF_INT
{
pawu min;
char ins;
paws sig;
paws pfx;
pawd len;
char num[bitsof(pawjd)];
paws sfx;
} PAW_PUTF_INT;
PAW_API pawd paw_putf_int_chk( void *ud, pawzu *size, paws args, va_list va )
{
if ( ud )
{
pawjd jd = 0;
pawju ju = 0;
pawu i, ate = 1;
bool nosig = false;
bool read_min = false;
PAW_PUTF_INT *cfg = ud;
paws base = PAW_BASE10;
char sig = args[ate++], mod = 0;
paws mods = "hljztv", sigs = "pboduixX";
cfg->min = 0;
cfg->sig = "";
cfg->pfx = "";
cfg->sfx = "";
cfg->ins = ' ';
if ( sig == '+' )
{
cfg->sig = "+";
sig = args[ate++];
}
if ( sig == '0' )
{
cfg->ins = '0';
sig = args[ate++];
}
if ( sig == '*' )
{
read_min = true;
sig = args[ate++];
}
else
{
for ( i = sig, i -= '0'; i < 10u; i = sig, i -= '0' )
{
cfg->min += i;
sig = args[ate++];
}
}
for ( i = 0; mods[i]; ++i )
{
if ( sig == mods[i] )
{
sig = args[ate++];
mod = mods[i];
break;
}
}
if ( mod && sig == mod )
{
if ( mod == 'h' )
mod = 'H';
#ifdef PAWLLD_MAX
else if ( mod == 'l' )
mod = 'L';
#endif
else
return -1;
sig = args[ate++];
}
for ( i = 0; sigs[i]; ++i )
{
if ( sig == sigs[i] )
break;
}
nosig = (sig == 'p' || (sig != 'd' && sig != 'i'));
if ( !(sigs[i]) || (sig == 'p' && mod) || (nosig && mod == 't'))
return -1;
if ( read_min )
cfg->min = va_arg( va, pawu );
if ( sig == 'p' )
{
if ( !(cfg->min) )
cfg->min = NEED_DIGITS(bitsof(void*),16) + 2;
ju = (pawvu)va_arg( va, void const * );
cfg->pfx = "0x";
base = PAW_BASE16X;
}
else if ( sig == 'd' || sig == 'i' )
{
switch ( mod )
{
#ifdef PAWLLD_MAX
case 'L': jd = va_arg( va, pawlld ); break;
#endif
case 'l': jd = va_arg( va, pawld ); break;
case 'j': jd = va_arg( va, pawjd ); break;
case 't': jd = va_arg( va, pawtd ); break;
case 'z': jd = va_arg( va, pawzd ); break;
case 'v': jd = va_arg( va, pawvd ); break;
default: jd = va_arg( va, pawd );
}
}
else
{
switch ( sig )
{
case 'b': base = PAW_BASE2; break;
case 'o': base = PAW_BASE8; break;
case 'u': break;
case 'x': base = PAW_BASE16x; break;
case 'X': base = PAW_BASE16X; break;
}
switch ( mod )
{
#ifdef PAWLLD_MAX
case 'L': ju = va_arg( va, pawllu ); break;
#endif
case 'l': ju = va_arg( va, pawlu ); break;
case 'j': ju = va_arg( va, pawju ); break;
case 'z': ju = va_arg( va, pawzu ); break;
case 'v': ju = va_arg( va, pawvu ); break;
default: ju = va_arg( va, pawu );
}
}
if ( nosig )
cfg->len = pawu2s( &(cfg->num), ju, base );
else
{
if ( jd < 0 )
{
cfg->sig = "-";
jd = -jd;
}
cfg->len = pawu2s( &(cfg->num), jd, base );
}
*size = cfg->len + pawsleng(cfg->pfx) + pawsleng(cfg->sig);
if ( *size < cfg->min )
*size = cfg->min;
return ate;
}
*size = sizeof(PAW_PUTF_INT);
return 0;
}
| |