summaryrefslogtreecommitdiffhomepage
path: root/ir/libcore/lc_appendable.c
blob: a68946da6c3be8b4124480203cb3210e90c182a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
 * This file is part of libFirm.
 * Copyright (C) 2012 IPD Goos, Universit"at Karlsruhe, Germany
 */
#include "lc_printf.h"
#include "util.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* Default appendable implementations */

int lc_appendable_snwadd(lc_appendable_t *app, const char *str, size_t len,
                         unsigned int width, int left_just, char pad)
{
	int res = 0;
	size_t i;
	size_t to_pad = width > len ? width - len : 0;

	/* If not left justified, pad left */
	for (i = 0; !left_just && i < to_pad; ++i)
		res += lc_appendable_chadd(app, pad);

	/* Send the visible portion of the string to the output. */
	res += lc_appendable_snadd(app, str, len);

	/* If left justified, pad right. */
	for (i = 0; left_just && i < to_pad; ++i)
		res += lc_appendable_chadd(app, pad);

	return res;
}


void lc_appendable_init(lc_appendable_t *env, const lc_appendable_funcs_t *app,
                        void *obj, size_t limit)
{
	env->obj = obj;
	env->limit = limit;
	env->written = 0;
	env->app =app;

	app->init(env);
}

static void default_init(lc_appendable_t *env)
{
	(void) env;
}

static void default_finish(lc_appendable_t *env)
{
	(void) env;
}

/*
 * File appendable.
 */

static int file_snadd(lc_appendable_t *obj, const char *str, size_t n)
{
	obj->written += n;
	fwrite(str, sizeof(char), n, (FILE*)obj->obj);
	return n;
}

static int file_chadd(lc_appendable_t *obj, int ch)
{
	fputc(ch, (FILE*)obj->obj);
	obj->written++;
	return 1;
}

static lc_appendable_funcs_t app_file = {
	default_init,
	default_finish,
	file_snadd,
	file_chadd
};

const lc_appendable_funcs_t *lc_appendable_file = &app_file;


/*
 * String appendable.
 */

static void str_init(lc_appendable_t *obj)
{
	strncpy((char*)obj->obj, "", obj->limit);
}

static int str_snadd(lc_appendable_t *obj, const char *str, size_t n)
{
	size_t to_write = MIN(obj->limit - obj->written - 1, n);
	char *tgt = (char*)obj->obj;
	strncpy(tgt + obj->written, str, to_write);
	obj->written += to_write;
	return to_write;
}

static int str_chadd(lc_appendable_t *obj, int ch)
{
	if (obj->limit - obj->written > 1) {
		char *tgt = (char*)obj->obj;
		tgt[obj->written++] = (char) ch;
		return 1;
	}

	return 0;
}

static void str_finish(lc_appendable_t *obj)
{
	char *str = (char*)obj->obj;
	str[obj->written] = '\0';
}

static lc_appendable_funcs_t app_string = {
	str_init,
	str_finish,
	str_snadd,
	str_chadd
};

const lc_appendable_funcs_t *lc_appendable_string = &app_string;

/*
 * Obstack appendable
 */

static int obst_snadd(lc_appendable_t *obj, const char *str, size_t n)
{
	struct obstack *obst = (struct obstack*)obj->obj;
	obj->written += n;
	obstack_grow(obst, str, n);
	return n;
}

static int obst_chadd(lc_appendable_t *obj, int ch)
{
	struct obstack *obst = (struct obstack*)obj->obj;
	obstack_1grow(obst, (char) ch);
	obj->written++;
	return 1;
}

static lc_appendable_funcs_t app_obstack = {
	default_init,
	default_finish,
	obst_snadd,
	obst_chadd
};

const lc_appendable_funcs_t *lc_appendable_obstack = &app_obstack;