groff: update vendor branch to v1.20.1
[dragonfly.git] / contrib / groff / src / libs / libgroff / errarg.cpp
CommitLineData
92d0a6a6 1// -*- C++ -*-
4d3e9548 2/* Copyright (C) 1989, 1990, 1991, 1992, 2000, 2002, 2009
92d0a6a6
JR
3 Free Software Foundation, Inc.
4 Written by James Clark (jjc@jclark.com)
5
6This file is part of groff.
7
8groff is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
4d3e9548
JL
10Software Foundation, either version 3 of the License, or
11(at your option) any later version.
92d0a6a6
JR
12
13groff is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
17
4d3e9548
JL
18You should have received a copy of the GNU General Public License
19along with this program. If not, see <http://www.gnu.org/licenses/>. */
92d0a6a6
JR
20
21#include <stdio.h>
22#include "assert.h"
23#include "errarg.h"
24
25errarg::errarg(const char *p) : type(STRING)
26{
27 s = p ? p : "(null)";
28}
29
30errarg::errarg() : type(EMPTY)
31{
32}
33
34errarg::errarg(int nn) : type(INTEGER)
35{
36 n = nn;
37}
38
39errarg::errarg(unsigned int uu) : type(UNSIGNED_INTEGER)
40{
41 u = uu;
42}
43
44errarg::errarg(char cc) : type(CHAR)
45{
46 c = cc;
47}
48
49errarg::errarg(unsigned char cc) : type(CHAR)
50{
51 c = cc;
52}
53
54errarg::errarg(double dd) : type(DOUBLE)
55{
56 d = dd;
57}
58
59int errarg::empty() const
60{
61 return type == EMPTY;
62}
63
64extern "C" {
65 const char *i_to_a(int);
66 const char *ui_to_a(unsigned int);
67}
68
69void errarg::print() const
70{
71 switch (type) {
72 case INTEGER:
73 fputs(i_to_a(n), stderr);
74 break;
75 case UNSIGNED_INTEGER:
76 fputs(ui_to_a(u), stderr);
77 break;
78 case CHAR:
79 putc(c, stderr);
80 break;
81 case STRING:
82 fputs(s, stderr);
83 break;
84 case DOUBLE:
85 fprintf(stderr, "%g", d);
86 break;
87 case EMPTY:
88 break;
89 }
90}
91
92errarg empty_errarg;
93
94void errprint(const char *format,
95 const errarg &arg1,
96 const errarg &arg2,
97 const errarg &arg3)
98{
99 assert(format != 0);
100 char c;
101 while ((c = *format++) != '\0') {
102 if (c == '%') {
103 c = *format++;
104 switch(c) {
105 case '%':
106 fputc('%', stderr);
107 break;
108 case '1':
109 assert(!arg1.empty());
110 arg1.print();
111 break;
112 case '2':
113 assert(!arg2.empty());
114 arg2.print();
115 break;
116 case '3':
117 assert(!arg3.empty());
118 arg3.print();
119 break;
120 default:
121 assert(0);
122 }
123 }
124 else
125 putc(c, stderr);
126 }
127}