aboutsummaryrefslogtreecommitdiff
path: root/sys/fmtio/fmtstr.x
blob: 197ea2bd01e090a01a14ee789b70e2b12066f311 (plain) (blame)
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
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.

include	<ctype.h>

# FMTSTR -- Place a string in a field of the given width, left or right
# justifying as indicated, and output to the named file.  The length of
# the text string may exceed the field width, in which case there is no
# filling.

procedure fmtstr (fd, str, col, fill_char, left_justify, maxch, width)
		    
int	fd			# output file
char	str[ARB]		# string to be output
int	col			# column: both input and output parameter
char	fill_char		# fill character, if right justify
int	left_justify		# YES or NO
int	maxch			# maximum string chars to output
int	width			# field width
int	nchars, nfill, ip
int	strlen()
errchk	putc, putci

begin
	if (fd <= 0)
	    return

	if (width > 0) {
	    nchars = min (maxch, strlen(str))
	    nfill = max (0, width - nchars)
	} else {
	    nchars = maxch
	    nfill = 0						# free format
	}

	if (left_justify == NO)					# fill at left
	    for (col=col+nfill;  nfill > 0;  nfill=nfill-1)
		call putc (fd, fill_char)

	for (ip=1;  str[ip] != EOS && ip <= nchars;  ip=ip+1) {	# put string
	    call putc (fd, str[ip])
	    if (IS_PRINT (str[ip]))
		col = col + 1
	    else
		call fmt_setcol (str[ip], col)
	}

	for (col=col+nfill;  nfill > 0;  nfill=nfill-1)		# fill at right
	    call putci (fd, ' ')
end