1114402Sru// -*- C++ -*- 2114402Sru/* Copyright (C) 1989, 1990, 1991, 1992, 2000, 2002 3114402Sru Free Software Foundation, Inc. 4114402Sru Written by James Clark (jjc@jclark.com) 5114402Sru 6114402SruThis file is part of groff. 7114402Sru 8114402Srugroff is free software; you can redistribute it and/or modify it under 9114402Sruthe terms of the GNU General Public License as published by the Free 10114402SruSoftware Foundation; either version 2, or (at your option) any later 11114402Sruversion. 12114402Sru 13114402Srugroff is distributed in the hope that it will be useful, but WITHOUT ANY 14114402SruWARRANTY; without even the implied warranty of MERCHANTABILITY or 15114402SruFITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 16114402Srufor more details. 17114402Sru 18114402SruYou should have received a copy of the GNU General Public License along 19114402Sruwith groff; see the file COPYING. If not, write to the Free Software 20151497SruFoundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */ 21114402Sru 22114402Sru#include <stdio.h> 23114402Sru#include "assert.h" 24114402Sru#include "errarg.h" 25114402Sru 26114402Sruerrarg::errarg(const char *p) : type(STRING) 27114402Sru{ 28114402Sru s = p ? p : "(null)"; 29114402Sru} 30114402Sru 31114402Sruerrarg::errarg() : type(EMPTY) 32114402Sru{ 33114402Sru} 34114402Sru 35114402Sruerrarg::errarg(int nn) : type(INTEGER) 36114402Sru{ 37114402Sru n = nn; 38114402Sru} 39114402Sru 40114402Sruerrarg::errarg(unsigned int uu) : type(UNSIGNED_INTEGER) 41114402Sru{ 42114402Sru u = uu; 43114402Sru} 44114402Sru 45114402Sruerrarg::errarg(char cc) : type(CHAR) 46114402Sru{ 47114402Sru c = cc; 48114402Sru} 49114402Sru 50114402Sruerrarg::errarg(unsigned char cc) : type(CHAR) 51114402Sru{ 52114402Sru c = cc; 53114402Sru} 54114402Sru 55114402Sruerrarg::errarg(double dd) : type(DOUBLE) 56114402Sru{ 57114402Sru d = dd; 58114402Sru} 59114402Sru 60114402Sruint errarg::empty() const 61114402Sru{ 62114402Sru return type == EMPTY; 63114402Sru} 64114402Sru 65114402Sruextern "C" { 66114402Sru const char *i_to_a(int); 67114402Sru const char *ui_to_a(unsigned int); 68114402Sru} 69114402Sru 70114402Sruvoid errarg::print() const 71114402Sru{ 72114402Sru switch (type) { 73114402Sru case INTEGER: 74114402Sru fputs(i_to_a(n), stderr); 75114402Sru break; 76114402Sru case UNSIGNED_INTEGER: 77114402Sru fputs(ui_to_a(u), stderr); 78114402Sru break; 79114402Sru case CHAR: 80114402Sru putc(c, stderr); 81114402Sru break; 82114402Sru case STRING: 83114402Sru fputs(s, stderr); 84114402Sru break; 85114402Sru case DOUBLE: 86114402Sru fprintf(stderr, "%g", d); 87114402Sru break; 88114402Sru case EMPTY: 89114402Sru break; 90114402Sru } 91114402Sru} 92114402Sru 93114402Sruerrarg empty_errarg; 94114402Sru 95114402Sruvoid errprint(const char *format, 96114402Sru const errarg &arg1, 97114402Sru const errarg &arg2, 98114402Sru const errarg &arg3) 99114402Sru{ 100114402Sru assert(format != 0); 101114402Sru char c; 102114402Sru while ((c = *format++) != '\0') { 103114402Sru if (c == '%') { 104114402Sru c = *format++; 105114402Sru switch(c) { 106114402Sru case '%': 107114402Sru fputc('%', stderr); 108114402Sru break; 109114402Sru case '1': 110114402Sru assert(!arg1.empty()); 111114402Sru arg1.print(); 112114402Sru break; 113114402Sru case '2': 114114402Sru assert(!arg2.empty()); 115114402Sru arg2.print(); 116114402Sru break; 117114402Sru case '3': 118114402Sru assert(!arg3.empty()); 119114402Sru arg3.print(); 120114402Sru break; 121114402Sru default: 122114402Sru assert(0); 123114402Sru } 124114402Sru } 125114402Sru else 126114402Sru putc(c, stderr); 127114402Sru } 128114402Sru} 129