Initial import of binutils 2.22 on the new vendor branch
[dragonfly.git] / contrib / groff / src / libs / libgroff / errarg.cpp
1 // -*- C++ -*-
2 /* Copyright (C) 1989, 1990, 1991, 1992, 2000, 2002, 2009
3    Free Software Foundation, Inc.
4      Written by James Clark (jjc@jclark.com)
5
6 This file is part of groff.
7
8 groff is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 groff is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 #include <stdio.h>
22 #include "assert.h"
23 #include "errarg.h"
24
25 errarg::errarg(const char *p) : type(STRING)
26 {
27   s = p ? p : "(null)";
28 }
29
30 errarg::errarg() : type(EMPTY)
31 {
32 }
33
34 errarg::errarg(int nn) : type(INTEGER)
35 {
36   n = nn;
37 }
38
39 errarg::errarg(unsigned int uu) : type(UNSIGNED_INTEGER)
40 {
41   u = uu;
42 }
43
44 errarg::errarg(char cc) : type(CHAR)
45 {
46   c = cc;
47 }
48
49 errarg::errarg(unsigned char cc) : type(CHAR)
50 {
51   c = cc;
52 }
53
54 errarg::errarg(double dd) : type(DOUBLE)
55 {
56   d = dd;
57 }
58
59 int errarg::empty() const
60 {
61   return type == EMPTY;
62 }
63
64 extern "C" {
65   const char *i_to_a(int);
66   const char *ui_to_a(unsigned int);
67 }
68             
69 void 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
92 errarg empty_errarg;
93
94 void 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 }