pad.c
4.2 KB
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
/**********************************************************************/
/* */
/* FILENAME: pad.c */
/* BY: Jonathan G. Fiscus */
/* NATIONAL INSTITUTE OF STANDARDS AND TECHNOLOGY */
/* SPEECH RECOGNITION GROUP */
/* */
/* DESC: This is file contains routines to set 'pad' to */
/* a certain length. `pad` is a character array used*/
/* for centering output, when it's set to a length, */
/* n spaces are written into the array and terminated*/
/* with a NULL_CHAR so when written to output, there */
/* will be n spaces before the next formatted item */
/* */
/* ** These functions make use of a static variable to */
/* tell the routines how long a single output line */
/* can be. */
/* */
/**********************************************************************/
#include "sctk.h"
static int pad_print_out_width=SCREEN_WIDTH;
/*************************************************************/
/* init the utilities to the proper printout dimensions */
/*************************************************************/
void init_pad_util(int pr_width)
{
pad_print_out_width = pr_width;
}
/*************************************************************/
/* return the value of the print_out_width */
/*************************************************************/
int pad_pr_width(void)
{
return(pad_print_out_width);
}
/*************************************************************/
/* pad manipulating routines */
/*************************************************************/
/* set the pad to center the passed in string */
void set_pad(char *pad, char *str, int max)
{
int i, len;
len = (pad_print_out_width - strlen(str) )/2;
for (i=0; i<MIN(len, max-1);i++)
pad[i] = ' ';
if (i>0)
pad[i-1] = '\0';
else
pad[0] = '\0';
}
/*************************************************************/
/* set the pad to n spaces */
void set_pad_n(char *pad, int n, int max)
{
int i;
for (i=0;i<MIN(n,max-1);i++)
pad[i]=' ';
pad[i] = '\0';
}
/*************************************************************/
/* set the pad to center a string of length `len` */
void set_pad_cent_n(char *pad, int len, int max)
{
int i;
for (i=0;i<MIN(((pad_print_out_width - len) / 2),max-1);i++)
pad[i]=' ';
if (i>0)
pad[i-1] = '\0';
else
pad[0] = '\0';
}
/************************************************************/
/* Return a pointer to a string with a centered version of */
/* str to len characters */
/************************************************************/
char *center(char *str, int len)
{
static char desc[2000], *ptr;
int il, ft, bk, i;
if (len >= 2000){
fprintf(stderr,"Error: center utility in pad.c failed, buffer should be larger than %d\n",
len);
exit(1);
}
il = strlen(str);
ft = (len - il) / 2;
bk = len - (il+ft);
ptr=desc;
for (i=0; i<ft; i++)
*ptr++ = ' ';
for (i=0; i<il; i++)
*ptr++ = str[i];
for (i=0; i<bk; i++)
*ptr++ = ' ';
*ptr = '\0';
return(desc);
}
/***************************************************************/
/* copy the string if the len of from<len, the pad with char */
/***************************************************************/
void strncpy_pad(char *to, char *from, int len, int max, char chr)
{
int i;
for (i=0; i<MIN(len,max-1); i++){
if (*from != '\0')
*(to++) = *(from++);
else
*(to++) = chr;
}
*to = '\0';
}