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