Update to groff 1.19.2.
[dragonfly.git] / contrib / groff-1.19 / src / libs / libgroff / errarg.cpp
1 // -*- C++ -*-
2 /* Copyright (C) 1989, 1990, 1991, 1992, 2000, 2002
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 2, or (at your option) any later
11 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 along
19 with groff; see the file COPYING.  If not, write to the Free Software
20 Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */
21
22 #include <stdio.h>
23 #include "assert.h"
24 #include "errarg.h"
25
26 errarg::errarg(const char *p) : type(STRING)
27 {
28   s = p ? p : "(null)";
29 }
30
31 errarg::errarg() : type(EMPTY)
32 {
33 }
34
35 errarg::errarg(int nn) : type(INTEGER)
36 {
37   n = nn;
38 }
39
40 errarg::errarg(unsigned int uu) : type(UNSIGNED_INTEGER)
41 {
42   u = uu;
43 }
44
45 errarg::errarg(char cc) : type(CHAR)
46 {
47   c = cc;
48 }
49
50 errarg::errarg(unsigned char cc) : type(CHAR)
51 {
52   c = cc;
53 }
54
55 errarg::errarg(double dd) : type(DOUBLE)
56 {
57   d = dd;
58 }
59
60 int errarg::empty() const
61 {
62   return type == EMPTY;
63 }
64
65 extern "C" {
66   const char *i_to_a(int);
67   const char *ui_to_a(unsigned int);
68 }
69             
70 void errarg::print() const
71 {
72   switch (type) {
73   case INTEGER:
74     fputs(i_to_a(n), stderr);
75     break;
76   case UNSIGNED_INTEGER:
77     fputs(ui_to_a(u), stderr);
78     break;
79   case CHAR:
80     putc(c, stderr);
81     break;
82   case STRING:
83     fputs(s, stderr);
84     break;
85   case DOUBLE:
86     fprintf(stderr, "%g", d);
87     break;
88   case EMPTY:
89     break;
90   }
91 }
92
93 errarg empty_errarg;
94
95 void errprint(const char *format, 
96               const errarg &arg1,
97               const errarg &arg2,
98               const errarg &arg3)
99 {
100   assert(format != 0);
101   char c;
102   while ((c = *format++) != '\0') {
103     if (c == '%') {
104       c = *format++;
105       switch(c) {
106       case '%':
107         fputc('%', stderr);
108         break;
109       case '1':
110         assert(!arg1.empty());
111         arg1.print();
112         break;
113       case '2':
114         assert(!arg2.empty());
115         arg2.print();
116         break;
117       case '3':
118         assert(!arg3.empty());
119         arg3.print();
120         break;
121       default:
122         assert(0);
123       }
124     }
125     else
126       putc(c, stderr);
127   }
128 }