errarg.cpp revision 151497
1249259Sdim// -*- C++ -*- 2249259Sdim/* Copyright (C) 1989, 1990, 1991, 1992, 2000, 2002 3249259Sdim Free Software Foundation, Inc. 4249259Sdim Written by James Clark (jjc@jclark.com) 5249259Sdim 6249259SdimThis file is part of groff. 7249259Sdim 8249259Sdimgroff is free software; you can redistribute it and/or modify it under 9249259Sdimthe terms of the GNU General Public License as published by the Free 10249259SdimSoftware Foundation; either version 2, or (at your option) any later 11249259Sdimversion. 12249259Sdim 13249259Sdimgroff is distributed in the hope that it will be useful, but WITHOUT ANY 14249259SdimWARRANTY; without even the implied warranty of MERCHANTABILITY or 15249259SdimFITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 16249259Sdimfor more details. 17249259Sdim 18249259SdimYou should have received a copy of the GNU General Public License along 19249259Sdimwith groff; see the file COPYING. If not, write to the Free Software 20249259SdimFoundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */ 21249259Sdim 22249259Sdim#include <stdio.h> 23249259Sdim#include "assert.h" 24249259Sdim#include "errarg.h" 25249259Sdim 26249259Sdimerrarg::errarg(const char *p) : type(STRING) 27249259Sdim{ 28249259Sdim s = p ? p : "(null)"; 29249259Sdim} 30249259Sdim 31249259Sdimerrarg::errarg() : type(EMPTY) 32249259Sdim{ 33249259Sdim} 34249259Sdim 35249259Sdimerrarg::errarg(int nn) : type(INTEGER) 36249259Sdim{ 37249259Sdim n = nn; 38249259Sdim} 39249259Sdim 40249259Sdimerrarg::errarg(unsigned int uu) : type(UNSIGNED_INTEGER) 41249259Sdim{ 42249259Sdim u = uu; 43249259Sdim} 44249259Sdim 45249259Sdimerrarg::errarg(char cc) : type(CHAR) 46249259Sdim{ 47249259Sdim c = cc; 48249259Sdim} 49249259Sdim 50249259Sdimerrarg::errarg(unsigned char cc) : type(CHAR) 51249259Sdim{ 52249259Sdim c = cc; 53249259Sdim} 54249259Sdim 55249259Sdimerrarg::errarg(double dd) : type(DOUBLE) 56249259Sdim{ 57249259Sdim d = dd; 58249259Sdim} 59249259Sdim 60249259Sdimint errarg::empty() const 61249259Sdim{ 62249259Sdim return type == EMPTY; 63249259Sdim} 64249259Sdim 65249259Sdimextern "C" { 66249259Sdim const char *i_to_a(int); 67249259Sdim const char *ui_to_a(unsigned int); 68249259Sdim} 69263508Sdim 70263508Sdimvoid errarg::print() const 71263508Sdim{ 72263508Sdim switch (type) { 73263508Sdim case INTEGER: 74263508Sdim fputs(i_to_a(n), stderr); 75263508Sdim break; 76263508Sdim case UNSIGNED_INTEGER: 77263508Sdim fputs(ui_to_a(u), stderr); 78263508Sdim break; 79263508Sdim case CHAR: 80263508Sdim putc(c, stderr); 81263508Sdim break; 82263508Sdim case STRING: 83263508Sdim fputs(s, stderr); 84263508Sdim break; 85263508Sdim case DOUBLE: 86263508Sdim fprintf(stderr, "%g", d); 87263508Sdim break; 88263508Sdim case EMPTY: 89263508Sdim break; 90263508Sdim } 91263508Sdim} 92263508Sdim 93263508Sdimerrarg empty_errarg; 94249259Sdim 95249259Sdimvoid errprint(const char *format, 96249259Sdim const errarg &arg1, 97249259Sdim const errarg &arg2, 98249259Sdim const errarg &arg3) 99249259Sdim{ 100249259Sdim assert(format != 0); 101249259Sdim char c; 102263508Sdim while ((c = *format++) != '\0') { 103249259Sdim if (c == '%') { 104249259Sdim c = *format++; 105249259Sdim switch(c) { 106249259Sdim case '%': 107249259Sdim fputc('%', stderr); 108249259Sdim break; 109249259Sdim case '1': 110249259Sdim assert(!arg1.empty()); 111249259Sdim arg1.print(); 112249259Sdim break; 113249259Sdim case '2': 114249259Sdim assert(!arg2.empty()); 115249259Sdim arg2.print(); 116249259Sdim break; 117249259Sdim case '3': 118249259Sdim assert(!arg3.empty()); 119249259Sdim arg3.print(); 120249259Sdim break; 121263508Sdim default: 122249259Sdim assert(0); 123249259Sdim } 124249259Sdim } 125249259Sdim else 126249259Sdim putc(c, stderr); 127249259Sdim } 128249259Sdim} 129249259Sdim