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