add missing config.h includes
[libfirm] / ir / libcore / lc_appendable.c
1 /*
2   libcore: library for basic data structures and algorithms.
3   Copyright (C) 2005  IPD Goos, Universit"at Karlsruhe, Germany
4
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.
9
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.
14
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
18 */
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26
27 #include "lc_common_t.h"
28 #include "lc_defines.h"
29 #include "lc_printf.h"
30
31 /* Default appendable implementations */
32
33 int lc_appendable_snwadd(lc_appendable_t *app, const char *str, size_t len,
34                 unsigned int width, int left_just, char pad)
35 {
36         int res = 0;
37         int i;
38         int to_pad = width - len;
39
40         /* If not left justified, pad left */
41         for(i = 0; !left_just && i < to_pad; ++i)
42                 res += lc_appendable_chadd(app, pad);
43
44         /* Send the visible portion of the string to the output. */
45         res += lc_appendable_snadd(app, str, len);
46
47         /* If left justified, pad right. */
48         for(i = 0; left_just && i < to_pad; ++i)
49                 res += lc_appendable_chadd(app, pad);
50
51         return res;
52 }
53
54
55 void lc_appendable_init(lc_appendable_t *env, const lc_appendable_funcs_t *app,
56                 void *obj, size_t limit)
57 {
58         env->obj = obj;
59         env->limit = limit;
60         env->written = 0;
61         env->app =app;
62
63         app->init(env);
64 }
65
66 static void default_init(UNUSED(lc_appendable_t *env))
67 {
68 }
69
70 static void default_finish(UNUSED(lc_appendable_t *env))
71 {
72 }
73
74 /*
75  * File appendable.
76  */
77
78 static int file_snadd(lc_appendable_t *obj, const char *str, size_t n)
79 {
80         obj->written += n;
81         fwrite(str, sizeof(char), n, obj->obj);
82         return n;
83 }
84
85 static int file_chadd(lc_appendable_t *obj, int ch)
86 {
87         fputc(ch, obj->obj);
88         obj->written++;
89         return 1;
90 }
91
92 static lc_appendable_funcs_t app_file = {
93         default_init,
94         default_finish,
95         file_snadd,
96         file_chadd
97 };
98
99 const lc_appendable_funcs_t *lc_appendable_file = &app_file;
100
101
102 /*
103  * String appendable.
104  */
105
106 static void str_init(lc_appendable_t *obj)
107 {
108         strncpy(obj->obj, "", obj->limit);
109 }
110
111 static int str_snadd(lc_appendable_t *obj, const char *str, size_t n)
112 {
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;
117         return to_write;
118 }
119
120 static int str_chadd(lc_appendable_t *obj, int ch)
121 {
122         if(obj->limit - obj->written > 1) {
123                 char *tgt = obj->obj;
124                 tgt[obj->written++] = (char) ch;
125                 return 1;
126         }
127
128         return 0;
129 }
130
131 static void str_finish(lc_appendable_t *obj)
132 {
133         char *str = obj->obj;
134         str[obj->written] = '\0';
135 }
136
137 static lc_appendable_funcs_t app_string = {
138         str_init,
139         str_finish,
140         str_snadd,
141         str_chadd
142 };
143
144 const lc_appendable_funcs_t *lc_appendable_string = &app_string;
145
146 /*
147  * Obstack appendable
148  */
149
150 static int obst_snadd(lc_appendable_t *obj, const char *str, size_t n)
151 {
152         struct obstack *obst = obj->obj;
153         obj->written += n;
154         obstack_grow(obst, str, n);
155         return n;
156 }
157
158 static int obst_chadd(lc_appendable_t *obj, int ch)
159 {
160         struct obstack *obst = obj->obj;
161         obstack_1grow(obst, (char) ch);
162         obj->written++;
163         return 1;
164 }
165
166 static lc_appendable_funcs_t app_obstack = {
167         default_init,
168         default_finish,
169         obst_snadd,
170         obst_chadd
171 };
172
173 const lc_appendable_funcs_t *lc_appendable_obstack = &app_obstack;