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
6This file is part of groff.
7
8groff is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
10Software Foundation; either version 2, or (at your option) any later
11version.
12
13groff is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16for more details.
17
18You should have received a copy of the GNU General Public License along
19with groff; see the file COPYING.  If not, write to the Free Software
20Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */
21
22#include <stdio.h>
23#include "assert.h"
24#include "errarg.h"
25
26errarg::errarg(const char *p) : type(STRING)
27{
28  s = p ? p : "(null)";
29}
30
31errarg::errarg() : type(EMPTY)
32{
33}
34
35errarg::errarg(int nn) : type(INTEGER)
36{
37  n = nn;
38}
39
40errarg::errarg(unsigned int uu) : type(UNSIGNED_INTEGER)
41{
42  u = uu;
43}
44
45errarg::errarg(char cc) : type(CHAR)
46{
47  c = cc;
48}
49
50errarg::errarg(unsigned char cc) : type(CHAR)
51{
52  c = cc;
53}
54
55errarg::errarg(double dd) : type(DOUBLE)
56{
57  d = dd;
58}
59
60int errarg::empty() const
61{
62  return type == EMPTY;
63}
64
65extern "C" {
66  const char *i_to_a(int);
67  const char *ui_to_a(unsigned int);
68}
69
70void 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
93errarg empty_errarg;
94
95void 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}
129