1
/*
2
 * Copyright 2008-2011 Various Authors
3
 * Copyright 2004 Timo Hirvonen
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License as
7
 * published by the Free Software Foundation; either version 2 of the
8
 * License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful, but
11
 * WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
#ifndef _FORMAT_PRINT_H
20
#define _FORMAT_PRINT_H
21
22
struct format_option {
23
	union {
24
		/* NULL is treated like "" */
25
		const char *fo_str;
26
		int fo_int;
27
		/* [h:]mm:ss. can be negative */
28
		int fo_time;
29
		double fo_double;
30
	};
31
	/* set to 1 if you want to disable printing */
32
	unsigned int empty : 1;
33
	/* set to 1 if zero padding is allowed */
34
	unsigned int pad_zero : 1;
35
	enum { FO_STR = 1, FO_INT, FO_TIME, FO_DOUBLE } type;
36
	char ch;
37
	const char *str;
38
};
39
40
/* gcc < 4.6 and icc < 12.0 can't properly initialize anonymous unions */
41
#if (defined(__GNUC__) && defined(__GNUC_MINOR__) && (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 6))) || \
42
	(defined(__INTEL_COMPILER) && __INTEL_COMPILER < 1200)
43
#define UNION_INIT(f, v) { .f = v }
44
#else
45
#define UNION_INIT(f, v) .f = v
46
#endif
47
48
#define DEF_FO_STR(c, s, z)    { UNION_INIT(fo_str,  NULL), .type = FO_STR,    .pad_zero = z, .ch = c, .str = s }
49
#define DEF_FO_INT(c, s, z)    { UNION_INIT(fo_int,  0),    .type = FO_INT,    .pad_zero = z, .ch = c, .str = s }
50
#define DEF_FO_TIME(c, s, z)   { UNION_INIT(fo_time, 0),    .type = FO_TIME,   .pad_zero = z, .ch = c, .str = s }
51
#define DEF_FO_DOUBLE(c, s, z) { UNION_INIT(fo_double, 0.), .type = FO_DOUBLE, .pad_zero = z, .ch = c, .str = s }
52
#define DEF_FO_END             { .type = 0 }
53
54
int format_print(char *str, int width, const char *format, const struct format_option *fopts);
55
int format_valid(const char *format, const struct format_option *fopts);
56
57
#endif