home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.bin / groff / libgroff / errarg.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-08  |  2.3 KB  |  123 lines

  1. /*-
  2.  * This code is derived from software copyrighted by the Free Software
  3.  * Foundation.
  4.  *
  5.  * Modified 1991 by Donn Seeley at UUNET Technologies, Inc.
  6.  */
  7.  
  8. #ifndef lint
  9. static char sccsid[] = "@(#)errarg.cc    6.3 (Berkeley) 5/8/91";
  10. #endif /* not lint */
  11.  
  12. // -*- C++ -*-
  13. /* Copyright (C) 1989, 1990 Free Software Foundation, Inc.
  14.      Written by James Clark (jjc@jclark.uucp)
  15.  
  16. This file is part of groff.
  17.  
  18. groff is free software; you can redistribute it and/or modify it under
  19. the terms of the GNU General Public License as published by the Free
  20. Software Foundation; either version 1, or (at your option) any later
  21. version.
  22.  
  23. groff is distributed in the hope that it will be useful, but WITHOUT ANY
  24. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  25. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  26. for more details.
  27.  
  28. You should have received a copy of the GNU General Public License along
  29. with groff; see the file LICENSE.  If not, write to the Free Software
  30. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  31.  
  32. #include <stdio.h>
  33. #include "assert.h"
  34. #include "errarg.h"
  35. #include "lib.h"
  36.  
  37. errarg::errarg(const char *p) : s(p == 0 ? "(null)" : p), type(STRING)
  38. {
  39. }
  40.  
  41. errarg::errarg() : type(EMPTY)
  42. {
  43. }
  44.  
  45. errarg::errarg(unsigned char cc) : type(CHAR)
  46. {
  47.   c = cc;
  48. }
  49.  
  50. errarg::errarg(int nn) : type(INTEGER), n(nn)
  51. {
  52. }
  53.  
  54. errarg::errarg(char cc) : type(CHAR), c(cc)
  55. {
  56. }
  57.  
  58. errarg::errarg(double dd) : type(DOUBLE), d(dd)
  59. {
  60. }
  61.  
  62. int errarg::empty() const
  63. {
  64.   return type == EMPTY;
  65. }
  66.  
  67. void errarg::print() const
  68. {
  69.   switch (type) {
  70.   case INTEGER:
  71.     fputs(itoa(n), stderr);
  72.     break;
  73.   case CHAR:
  74.     putc(c, stderr);
  75.     break;
  76.   case STRING:
  77.     fputs(s, stderr);
  78.     break;
  79.   case DOUBLE:
  80.     fprintf(stderr, "%g", d);
  81.     break;
  82.   case EMPTY:
  83.     break;
  84.   }
  85. }
  86.  
  87. errarg empty_errarg;
  88.  
  89. void errprint(const char *format, 
  90.           const errarg &arg1,
  91.           const errarg &arg2,
  92.           const errarg &arg3)
  93. {
  94.   assert(format != 0);
  95.   char c;
  96.   while ((c = *format++) != '\0') {
  97.     if (c == '%') {
  98.       c = *format++;
  99.       switch(c) {
  100.       case '%':
  101.     fputc('%', stderr);
  102.     break;
  103.       case '1':
  104.     assert(!arg1.empty());
  105.     arg1.print();
  106.     break;
  107.       case '2':
  108.     assert(!arg2.empty());
  109.     arg2.print();
  110.     break;
  111.       case '3':
  112.     assert(!arg3.empty());
  113.     arg3.print();
  114.     break;
  115.       default:
  116.     assert(0);
  117.       }
  118.     }
  119.     else
  120.       putc(c, stderr);
  121.   }
  122. }
  123.