Imported Upstream version 1.22.4
[platform/upstream/groff.git] / src / libs / libgroff / errarg.cpp
1 // -*- C++ -*-
2 /* Copyright (C) 1989-2018 Free Software Foundation, Inc.
3      Written by James Clark (jjc@jclark.com)
4
5 This file is part of groff.
6
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 3 of the License, or
10 (at your option) any later version.
11
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.
16
17 You should have received a copy of the GNU General Public License
18 along with this program.  If not, see <http://www.gnu.org/licenses/>. */
19
20 #include <stdio.h>
21 #include "assert.h"
22 #include "errarg.h"
23
24 errarg::errarg(const char *p) : type(STRING)
25 {
26   s = p ? p : "(null)";
27 }
28
29 errarg::errarg() : type(EMPTY)
30 {
31 }
32
33 errarg::errarg(int nn) : type(INTEGER)
34 {
35   n = nn;
36 }
37
38 errarg::errarg(unsigned int uu) : type(UNSIGNED_INTEGER)
39 {
40   u = uu;
41 }
42
43 errarg::errarg(char cc) : type(CHAR)
44 {
45   c = cc;
46 }
47
48 errarg::errarg(unsigned char cc) : type(CHAR)
49 {
50   c = cc;
51 }
52
53 errarg::errarg(double dd) : type(DOUBLE)
54 {
55   d = dd;
56 }
57
58 int errarg::empty() const
59 {
60   return type == EMPTY;
61 }
62
63 extern "C" {
64   const char *i_to_a(int);
65   const char *ui_to_a(unsigned int);
66 }
67             
68 void errarg::print() const
69 {
70   switch (type) {
71   case INTEGER:
72     fputs(i_to_a(n), stderr);
73     break;
74   case UNSIGNED_INTEGER:
75     fputs(ui_to_a(u), stderr);
76     break;
77   case CHAR:
78     putc(c, stderr);
79     break;
80   case STRING:
81     fputs(s, stderr);
82     break;
83   case DOUBLE:
84     fprintf(stderr, "%g", d);
85     break;
86   case EMPTY:
87     break;
88   }
89 }
90
91 errarg empty_errarg;
92
93 void errprint(const char *format, 
94               const errarg &arg1,
95               const errarg &arg2,
96               const errarg &arg3)
97 {
98   assert(format != 0);
99   char c;
100   while ((c = *format++) != '\0') {
101     if (c == '%') {
102       c = *format++;
103       switch(c) {
104       case '%':
105         fputc('%', stderr);
106         break;
107       case '1':
108         assert(!arg1.empty());
109         arg1.print();
110         break;
111       case '2':
112         assert(!arg2.empty());
113         arg2.print();
114         break;
115       case '3':
116         assert(!arg3.empty());
117         arg3.print();
118         break;
119       default:
120         assert(0);
121       }
122     }
123     else
124       putc(c, stderr);
125   }
126 }