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
|
/**
** @file padstr.c
**
** @author Numerous CSCI-452 classes
**
** @brief C implementations of common library functions
*/
#ifndef PADSTR_SRC_INC
#define PADSTR_SRC_INC
#include <common.h>
#include <lib.h>
/**
** padstr(dst,str,len,width,leftadjust,padchar - add padding characters
** to a string
**
** @param dst The destination buffer
** @param str The string to be padded
** @param len The string length, or -1
** @param width The desired final length of the string
** @param leftadjust Should the string be left-justified?
** @param padchar What character to pad with
**
** @return Pointer to the first byte after the padded string
**
** NOTE: does NOT NUL-terminate the buffer
*/
char *padstr(char *dst, char *str, int len, int width, int leftadjust,
int padchar)
{
int extra;
// determine the length of the string if we need to
if (len < 0) {
len = strlen(str);
}
// how much filler must we add?
extra = width - len;
// add filler on the left if we're not left-justifying
if (extra > 0 && !leftadjust) {
dst = pad(dst, extra, padchar);
}
// copy the string itself
for (int i = 0; i < len; ++i) {
*dst++ = str[i];
}
// add filler on the right if we are left-justifying
if (extra > 0 && leftadjust) {
dst = pad(dst, extra, padchar);
}
return dst;
}
#endif
|