home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 318_02 / redutl.c < prev    next >
C/C++ Source or Header  |  1990-06-18  |  3KB  |  168 lines

  1. /*
  2.     RED general utilities -- Full C version
  3.  
  4.     Source:  redutl.c
  5.     Version: September 21, 1983; January 18, 1990.
  6.  
  7.     Written by
  8.     
  9.         Edward K. Ream
  10.         166 N. Prospect
  11.         Madison WI 53705
  12.         (608) 257-0802
  13.  
  14.  
  15.     PUBLIC DOMAIN SOFTWARE
  16.  
  17.     This software is in the public domain.
  18.  
  19.     See red.h for a disclaimer of warranties and other information.
  20. */
  21.  
  22. #include "red.h"
  23.  
  24. /*
  25.     Return TRUE if the first token in args is a number.
  26.     Return the value of the number in  *val.
  27. */
  28. int
  29. number(char *args, int  *val)
  30. {
  31.     TRACEPB("number", sl_lpout();
  32.         sl_sout(args); sl_csout();
  33.         sl_pout(val);  sl_rpout());
  34.  
  35.     if (!isdigit(args [0])) {
  36.         RETURN_INT("number", FALSE);
  37.     }
  38.     else {
  39.         *val = atoi(args);
  40.         RETURN_INT("number", TRUE);
  41.     }
  42. }
  43.  
  44. /*
  45.     Output adecimal integer n in field width >= w.
  46.     Left justify the number in the field.
  47. */
  48. void
  49. putdec(int n, int w)
  50. {
  51.     char chars[10];
  52.     int i,nd;
  53.  
  54.     TRACEPB("putdec", sl_lpout();
  55.         sl_iout(n); sl_csout();
  56.         sl_iout(w); sl_rpout());
  57.  
  58.     nd = itoc(n,chars,10);
  59.     i  = 0;
  60.     while (i < nd) {
  61.         outchar(chars[i++]);
  62.     }
  63.     i = nd;
  64.     while (i++ < w) {
  65.         outchar(' ');
  66.     }
  67.  
  68.     TICKX("putdec");
  69. }
  70.  
  71. /*
  72.     Convert integer n to character string in str.
  73. */
  74. int
  75. itoc(int n, char *str, int size)
  76. {
  77.     int absval;
  78.     int len;
  79.     int i,j,k;
  80.  
  81.     TRACEPB("itoc",  sl_lpout();
  82.         sl_iout(n);    sl_csout();
  83.         sl_sout(str);  sl_csout(); 
  84.         sl_iout(size); sl_rpout());
  85.  
  86.     absval = abs(n);
  87.  
  88.     /* Generate digits. */
  89.     str[0] = 0;
  90.     i = 1;
  91.     while (i < size) {
  92.         str[i++] = (absval % 10)+'0';
  93.         absval   = absval / 10;
  94.         if (absval == 0) {
  95.             break;
  96.         }
  97.     }
  98.  
  99.     /* Generate sign. */
  100.     if (i < size && n < 0) {
  101.         str[i++] = '-';
  102.     }
  103.     len = i-1;
  104.  
  105.     /* Reverse sign, digits. */
  106.     i--;
  107.     j = 0;
  108.     while (j < i) {
  109.         k      = str[i];
  110.         str[i] = str[j];
  111.         str[j] = k;
  112.         i--;
  113.         j++;
  114.     }
  115.  
  116.     RETURN_INT("itoc", len);
  117. }
  118.  
  119. /*
  120.     User error routine.
  121. */
  122. void
  123. error(char *message)
  124. {
  125.     int x, y;
  126.  
  127.     TRACEPB("error", sl_lpout(); sl_sout(message); sl_rpout());
  128.  
  129.     /* Save cursor. */
  130.     x = outx;
  131.     y = outy;
  132.  
  133.     pmtmess("Error: ",message);
  134.  
  135.     /* Wait for any key. */
  136.     syscin();
  137.  
  138.     /* Restore cursor. */
  139.     outxy(x, y);
  140.  
  141.     TICKX("error");
  142. }
  143.  
  144. /*
  145.     User warning routine.
  146. */
  147. void
  148. warning(char *message)
  149. {
  150.     int x, y;
  151.  
  152.     TRACEPB("warning", sl_lpout(); sl_sout(message); sl_rpout());
  153.  
  154.     /* Save cursor. */
  155.     x = outx;
  156.     y = outy;
  157.  
  158.     pmtmess("Warning: ",message);
  159.  
  160.     /* Wait for any key. */
  161.     syscin();
  162.  
  163.     /* Restore cursor. */
  164.     outxy(x, y);
  165.  
  166.     TICKX("warning");
  167. }
  168.