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