1 /*
2 *
3 * C++ Portable Types Library (PTypes)
4 * Version 2.1.1 Released 27-Jun-2007
5 *
6 * Copyright (C) 2001-2007 Hovik Melikyan
7 *
8 * http://www.melikyan.com/ptypes/
9 *
10 */
11
12 #include <string.h>
13
14 #include "ptypes.h"
15
16
17 PTYPES_BEGIN
18
19
fill(int width,char pad)20 string ptdecl fill(int width, char pad)
21 {
22 string res;
23 if (width > 0) {
24 setlength(res, width);
25 memset(pchar(pconst(res)), pad, length(res));
26 }
27 return res;
28 }
29
30
pad(const string & s,int width,char c,bool left)31 string ptdecl pad(const string& s, int width, char c, bool left)
32 {
33 int len = length(s);
34 if (len < width && width > 0)
35 {
36 string res;
37 setlength(res, width);
38 if (left)
39 {
40 if (len > 0)
41 memcpy(pchar(pconst(res)), pconst(s), len);
42 memset(pchar(pconst(res)) + len, c, width - len);
43 }
44 else
45 {
46 memset(pchar(pconst(res)), c, width - len);
47 if (len > 0)
48 memcpy(pchar(pconst(res)) + width - len, pconst(s), len);
49 }
50 return res;
51 }
52 else
53 return s;
54 }
55
56
57 PTYPES_END
58