Lines Matching refs:state

60  * Common state
63 struct state {
69 int (*append_char)(struct state *, unsigned char);
70 int (*reserve)(struct state *, size_t);
76 sn_reserve (struct state *state, size_t n)
78 return state->s + n > state->theend;
82 sn_append_char (struct state *state, unsigned char c)
84 if (sn_reserve (state, 1)) {
87 *state->s++ = c;
95 as_reserve (struct state *state, size_t n)
97 if (state->s + n > state->theend) {
98 int off = state->s - state->str;
101 if (state->max_sz && state->sz >= state->max_sz)
104 state->sz = max(state->sz * 2, state->sz + n);
105 if (state->max_sz)
106 state->sz = min(state->sz, state->max_sz);
107 tmp = realloc (state->str, state->sz);
110 state->str = tmp;
111 state->s = state->str + off;
112 state->theend = state->str + state->sz - 1;
118 as_append_char (struct state *state, unsigned char c)
120 if(as_reserve (state, 1))
123 *state->s++ = c;
130 append_number(struct state *state,
146 if((*state->append_char)(state, rep[num % base]))
154 if((*state->append_char)(state, '0'))
167 if((*state->append_char)(state, '0'))
175 if((*state->append_char)(state, rep[10] + 23)) /* XXX */
177 if((*state->append_char)(state, '0'))
182 if((*state->append_char)(state, '-'))
186 if((*state->append_char)(state, '+'))
190 if((*state->append_char)(state, ' '))
197 char c = state->s[-i-1];
198 state->s[-i-1] = state->s[-len+i];
199 state->s[-len+i] = c;
203 if((*state->append_char)(state, ' '))
210 char c = state->s[-i-1];
211 state->s[-i-1] = state->s[-len+i];
212 state->s[-len+i] = c;
219 append_string (struct state *state,
231 if((*state->append_char) (state, ' '))
235 if ((*state->append_char) (state, *arg++))
239 if ((*state->append_char) (state, *arg++))
244 if((*state->append_char) (state, ' '))
250 append_char(struct state *state,
256 if((*state->append_char) (state, ' '))
259 if((*state->append_char) (state, arg))
262 if((*state->append_char) (state, ' '))
285 xyzprintf (struct state *state, const char *char_format, va_list ap)
358 if(append_char(state, va_arg(ap, int), width, flags))
362 if (append_string(state,
383 if (append_number (state, num, 10, "0123456789",
393 if (append_number (state, arg, 10, "0123456789",
403 if (append_number (state, arg, 010, "01234567",
413 if (append_number (state, arg, 0x10, "0123456789abcdef",
423 if (append_number (state, arg, 0x10, "0123456789ABCDEF",
431 if (append_number (state, arg, 0x10, "0123456789ABCDEF",
438 *arg = state->s - state->str;
445 if ((*state->append_char)(state, c))
449 if ( (*state->append_char)(state, '%')
450 || (*state->append_char)(state, c))
455 if ((*state->append_char) (state, c))
570 struct state state;
572 state.max_sz = max_sz;
573 state.sz = 1;
574 state.str = malloc(state.sz);
575 if (state.str == NULL) {
579 state.s = state.str;
580 state.theend = state.s + state.sz - 1;
581 state.append_char = as_append_char;
582 state.reserve = as_reserve;
584 st = xyzprintf (&state, format, args);
586 free (state.str);
592 *state.s = '\0';
593 len = state.s - state.str;
594 tmp = realloc (state.str, len+1);
596 free (state.str);
611 struct state state;
615 state.max_sz = 0;
616 state.sz = sz;
617 state.str = ustr;
618 state.s = ustr;
619 state.theend = ustr + sz - 1;
620 state.append_char = sn_append_char;
621 state.reserve = sn_reserve;
623 ret = xyzprintf (&state, format, args);
624 *state.s = '\0';
628 return state.s - state.str;