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