2 * This file is part of libFirm.
3 * Copyright (C) 2012 IPD Goos, Universit"at Karlsruhe, Germany
12 #include "lc_printf.h"
14 /* Default appendable implementations */
16 int lc_appendable_snwadd(lc_appendable_t *app, const char *str, size_t len,
17 unsigned int width, int left_just, char pad)
21 size_t to_pad = width > len ? width - len : 0;
23 /* If not left justified, pad left */
24 for (i = 0; !left_just && i < to_pad; ++i)
25 res += lc_appendable_chadd(app, pad);
27 /* Send the visible portion of the string to the output. */
28 res += lc_appendable_snadd(app, str, len);
30 /* If left justified, pad right. */
31 for (i = 0; left_just && i < to_pad; ++i)
32 res += lc_appendable_chadd(app, pad);
38 void lc_appendable_init(lc_appendable_t *env, const lc_appendable_funcs_t *app,
39 void *obj, size_t limit)
49 static void default_init(lc_appendable_t *env)
54 static void default_finish(lc_appendable_t *env)
63 static int file_snadd(lc_appendable_t *obj, const char *str, size_t n)
66 fwrite(str, sizeof(char), n, (FILE*)obj->obj);
70 static int file_chadd(lc_appendable_t *obj, int ch)
72 fputc(ch, (FILE*)obj->obj);
77 static lc_appendable_funcs_t app_file = {
84 const lc_appendable_funcs_t *lc_appendable_file = &app_file;
91 static void str_init(lc_appendable_t *obj)
93 strncpy((char*)obj->obj, "", obj->limit);
96 static int str_snadd(lc_appendable_t *obj, const char *str, size_t n)
98 size_t to_write = MIN(obj->limit - obj->written - 1, n);
99 char *tgt = (char*)obj->obj;
100 strncpy(tgt + obj->written, str, to_write);
101 obj->written += to_write;
105 static int str_chadd(lc_appendable_t *obj, int ch)
107 if (obj->limit - obj->written > 1) {
108 char *tgt = (char*)obj->obj;
109 tgt[obj->written++] = (char) ch;
116 static void str_finish(lc_appendable_t *obj)
118 char *str = (char*)obj->obj;
119 str[obj->written] = '\0';
122 static lc_appendable_funcs_t app_string = {
129 const lc_appendable_funcs_t *lc_appendable_string = &app_string;
135 static int obst_snadd(lc_appendable_t *obj, const char *str, size_t n)
137 struct obstack *obst = (struct obstack*)obj->obj;
139 obstack_grow(obst, str, n);
143 static int obst_chadd(lc_appendable_t *obj, int ch)
145 struct obstack *obst = (struct obstack*)obj->obj;
146 obstack_1grow(obst, (char) ch);
151 static lc_appendable_funcs_t app_obstack = {
158 const lc_appendable_funcs_t *lc_appendable_obstack = &app_obstack;