summaryrefslogtreecommitdiff
path: root/src/tet3/dtet2lib/ftoa.c
blob: 42527c92f2ffbbc45406b497f40c1ba8fd67af40 (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
/*
 *      SCCS:  @(#)ftoa.c	1.9 (98/08/28) 
 *
 *	UniSoft Ltd., London, England
 *
 * (C) Copyright 1992 X/Open Company Limited
 *
 * All rights reserved.  No part of this source code may be reproduced,
 * stored in a retrieval system, or transmitted, in any form or by any
 * means, electronic, mechanical, photocopying, recording or otherwise,
 * except as stated in the end-user licence agreement, without the prior
 * permission of the copyright owners.
 *
 * X/Open and the 'X' symbol are trademarks of X/Open Company Limited in
 * the UK and other countries.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

/************************************************************************

SCCS:   	@(#)ftoa.c	1.9 98/08/28 TETware release 3.3
NAME:		ftoa.c
PRODUCT:	TETware
AUTHOR:		Andrew Dingwall, UniSoft Ltd.
DATE CREATED:	June 1992

DESCRIPTION:
	function to return a printable representation of a set of flags

MODIFICATIONS:
	Andrew Dingwall, UniSoft Ltd., August 1996
	add a pool of re-usable buffers so that more than one tet_f2a()
	value may be passed to a reporting function

************************************************************************/

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include "dtmac.h"
#include "dtmsg.h"
#include "ptab.h"
#include "ltoa.h"
#include "ftoa.h"
#include "dtetlib.h"

/*
**	tet_f2a() - return printable representation of ptab pt_flags value
**
**	the return value points to a static area whose contents are
**	overwritten after NFBUF calls
*/

char *tet_f2a(fval, flags, nflags)
int fval, nflags;
struct flags flags[];
{
	static struct {
		char *bp;
		int buflen;
	} bufstruct[NFBUF];
	static int count;
	char **bpp;
	int *blp;
	register struct flags *fp;
	register char *p1, *p2;
	register unsigned ftmp;
	register int n, needlen;

	if (++count >= NFBUF)
		count = 0;
	bpp = &bufstruct[count].bp;
	blp = &bufstruct[count].buflen;

	/* work out the required output buffer size */
	for (needlen = 0, ftmp = fval, n = 0; ftmp; ftmp >>= 1, n++) {
		if (!(ftmp & 1))
			continue;
		for (fp = &flags[nflags - 1]; fp >= flags; fp--)
			if (fp->fl_value == (1 << n)) {
				needlen += strlen(fp->fl_name) + 1;
				break;
			}
		if (fp < flags)
			needlen += strlen(tet_i2o(1 << n)) + 1;
	}

	/* get the buffer to put the flag names in */
	if (BUFCHK(bpp, blp, TET_MAX(needlen, 2)) < 0)
		return("");

	/* copy the flag names in to the buffer */
	for (p1 = *bpp, ftmp = fval, n = 0; ftmp; ftmp >>= 1, n++) {
		if (!(ftmp & 1))
			continue;
		for (fp = &flags[nflags - 1]; fp >= flags; fp--)
			if (fp->fl_value == (1 << n)) {
				for (p2 = fp->fl_name; *p2; p2++)
					*p1++ = *p2;
				break;
			}
		if (fp < flags)
			for (p2 = tet_i2o(1 << n); *p2; p2++)
				*p1++ = *p2;
		if (ftmp & ~1)
			*p1++ = '|';
	}

	if (p1 == *bpp)
		*p1++ = '0';

	*p1 = '\0';

	return(*bpp);
}