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