File: obuf_sign_pad.c

package info (click to toggle)
bglibs 2.04%2Bdfsg-8
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 3,468 kB
  • sloc: ansic: 15,821; perl: 674; sh: 63; makefile: 29
file content (37 lines) | stat: -rw-r--r-- 958 bytes parent folder | download | duplicates (4)
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
#include "obuf.h"

/** Pad the output with \c width instances of the single character \c
    ch, preceded by an optional negative sign at an appropriate place.

If the pad character is \c '0' then any negative sign is placed as the
first character, followed by padding.  Otherwise, it is preceded by the
padding.
*/
int obuf_sign_pad(obuf* out, int sign, unsigned width, char pad)
{
  if (!width) return !sign || obuf_putc(out, '-');
  if (pad != '0' && !obuf_pad(out, width, pad)) return 0;
  if (sign && !obuf_putc(out, '-')) return 0;
  if (pad == '0' && !obuf_pad(out, width, pad)) return 0;
  return 1;
}

#ifdef SELFTEST_MAIN
MAIN
{
  obuf_sign_pad(&outbuf, 0, 0,   0); NL();
  obuf_sign_pad(&outbuf, 1, 0,   0); NL();
  obuf_sign_pad(&outbuf, 0, 4, 'x'); NL();
  obuf_sign_pad(&outbuf, 1, 4, 'x'); NL();
  obuf_sign_pad(&outbuf, 0, 4, '0'); NL();
  obuf_sign_pad(&outbuf, 1, 4, '0'); NL();
}
#endif
#ifdef SELFTEST_EXP

-
xxxx
xxxx-
0000
-0000
#endif