2 libcore: library for basic data structures and algorithms.
3 Copyright (C) 2005 IPD Goos, Universit"at Karlsruhe, Germany
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 #include "lc_common_t.h"
28 #include "lc_defines.h"
29 #include "lc_printf.h"
31 /* Default appendable implementations */
33 int lc_appendable_snwadd(lc_appendable_t *app, const char *str, size_t len,
34 unsigned int width, int left_just, char pad)
38 int to_pad = width - len;
40 /* If not left justified, pad left */
41 for(i = 0; !left_just && i < to_pad; ++i)
42 res += lc_appendable_chadd(app, pad);
44 /* Send the visible portion of the string to the output. */
45 res += lc_appendable_snadd(app, str, len);
47 /* If left justified, pad right. */
48 for(i = 0; left_just && i < to_pad; ++i)
49 res += lc_appendable_chadd(app, pad);
55 void lc_appendable_init(lc_appendable_t *env, const lc_appendable_funcs_t *app,
56 void *obj, size_t limit)
66 static void default_init(UNUSED(lc_appendable_t *env))
70 static void default_finish(UNUSED(lc_appendable_t *env))
78 static int file_snadd(lc_appendable_t *obj, const char *str, size_t n)
81 fwrite(str, sizeof(char), n, obj->obj);
85 static int file_chadd(lc_appendable_t *obj, int ch)
92 static lc_appendable_funcs_t app_file = {
99 const lc_appendable_funcs_t *lc_appendable_file = &app_file;
106 static void str_init(lc_appendable_t *obj)
108 strncpy(obj->obj, "", obj->limit);
111 static int str_snadd(lc_appendable_t *obj, const char *str, size_t n)
113 size_t to_write = LC_MIN(obj->limit - obj->written - 1, n);
114 char *tgt = obj->obj;
115 strncpy(tgt + obj->written, str, to_write);
116 obj->written += to_write;
120 static int str_chadd(lc_appendable_t *obj, int ch)
122 if(obj->limit - obj->written > 1) {
123 char *tgt = obj->obj;
124 tgt[obj->written++] = (char) ch;
131 static void str_finish(lc_appendable_t *obj)
133 char *str = obj->obj;
134 str[obj->written] = '\0';
137 static lc_appendable_funcs_t app_string = {
144 const lc_appendable_funcs_t *lc_appendable_string = &app_string;
150 static int obst_snadd(lc_appendable_t *obj, const char *str, size_t n)
152 struct obstack *obst = obj->obj;
154 obstack_grow(obst, str, n);
158 static int obst_chadd(lc_appendable_t *obj, int ch)
160 struct obstack *obst = obj->obj;
161 obstack_1grow(obst, (char) ch);
166 static lc_appendable_funcs_t app_obstack = {
173 const lc_appendable_funcs_t *lc_appendable_obstack = &app_obstack;