groff before CVS: release 1.05
[s-roff.git] / lib / errarg.c
blob9ade9995498aef18080dd3b983b961fcabd14920
1 // -*- C++ -*-
2 /* Copyright (C) 1989, 1990, 1991 Free Software Foundation, Inc.
3 Written by James Clark (jjc@jclark.uucp)
5 This file is part of groff.
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 1, or (at your option) any later
10 version.
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.
17 You should have received a copy of the GNU General Public License along
18 with groff; see the file LICENSE. If not, write to the Free Software
19 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 #include <stdio.h>
22 #include "assert.h"
23 #include "errarg.h"
25 errarg::errarg(const char *p) : s(p == 0 ? "(null)" : p), type(STRING)
29 errarg::errarg() : type(EMPTY)
33 errarg::errarg(unsigned char cc) : type(CHAR)
35 c = cc;
38 errarg::errarg(int nn) : type(INTEGER), n(nn)
42 errarg::errarg(char cc) : type(CHAR), c(cc)
46 errarg::errarg(double dd) : type(DOUBLE), d(dd)
50 int errarg::empty() const
52 return type == EMPTY;
55 extern "C" {
56 const char *itoa(int);
59 void errarg::print() const
61 switch (type) {
62 case INTEGER:
63 fputs(itoa(n), stderr);
64 break;
65 case CHAR:
66 putc(c, stderr);
67 break;
68 case STRING:
69 fputs(s, stderr);
70 break;
71 case DOUBLE:
72 fprintf(stderr, "%g", d);
73 break;
74 case EMPTY:
75 break;
79 errarg empty_errarg;
81 void errprint(const char *format,
82 const errarg &arg1,
83 const errarg &arg2,
84 const errarg &arg3)
86 assert(format != 0);
87 char c;
88 while ((c = *format++) != '\0') {
89 if (c == '%') {
90 c = *format++;
91 switch(c) {
92 case '%':
93 fputc('%', stderr);
94 break;
95 case '1':
96 assert(!arg1.empty());
97 arg1.print();
98 break;
99 case '2':
100 assert(!arg2.empty());
101 arg2.print();
102 break;
103 case '3':
104 assert(!arg3.empty());
105 arg3.print();
106 break;
107 default:
108 assert(0);
111 else
112 putc(c, stderr);