home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / games / backgmmn.zip / MONEY.C < prev    next >
Text File  |  1987-12-24  |  10KB  |  367 lines

  1.  
  2. /* money.c, david c. oshel, 11/22/86
  3. */
  4.  
  5. #include <string.h>
  6. #include <ctype.h>
  7. #include <time.h>
  8. #include <malloc.h>
  9. #include <stdarg.h>
  10. #include <setjmp.h>
  11. #include "ciao.h"
  12.  
  13. #define ESC 27
  14. #define MINUS '-'
  15.  
  16. /* edit interrupt flags
  17. */
  18.  
  19. int ESCflag = 0, MINUSflag = 0;
  20.  
  21.  
  22. /* system date
  23. */
  24.  
  25. char date_mask[] = "00/00/00";   /* initialized from system date */
  26.  
  27.  
  28. /*
  29. **  dlrcvt -- return a pointer to dollar & cents string with commas
  30. **            input is a long value
  31. **            has trailing minus sign if negative input
  32. **            dollar sign not included
  33. **            leading spaces not included
  34. **            overflow shows ***,***,***.**
  35. */
  36.  
  37. char *dlrcvt( dval )
  38. long dval;
  39. {
  40.         register char *f, *q;
  41.         static char *p, dollars[16];
  42.         auto char buffer[34];
  43.         auto int sign;
  44.  
  45.         /* initialize receiving field */
  46.  
  47.                       /*  0....v....1....v */
  48.         strcpy( dollars, "   ,   ,  0.00 "); 
  49.  
  50.         /* fetch digits to be formatted */
  51.  
  52.         p = ltoa( dval, buffer, 10 );
  53.  
  54.         sign = (*p == '-');
  55.         if (sign) ++p;
  56.  
  57.  
  58.         /* format dollars field, right to left */
  59.  
  60.         q = strchr(p,'\0');        /* locate from end of source string */
  61.         q--;
  62.         f = dollars + 13;          /* locate to end of destination */
  63.         while (f >= dollars)
  64.         {
  65.              if ( q >= p )   /* is q to the right of first digit? */
  66.              {
  67.                  if (*f == ',' || *f == '.') --f;  /* jump left */
  68.                  *f = *q--;
  69.              }
  70.              else            /* no, no more digits remain in source */
  71.              {
  72.                  if (f < dollars + 10) 
  73.                  {
  74.                      *f = ' ';  /* fill destination left with blanks */
  75.                  }
  76.              }
  77.              --f;
  78.         }
  79.  
  80.         if (q > buffer)  /* OVERFLOW? does source still have digits? */
  81.         {
  82.                            /*  0....v....1....v */
  83.              strcpy( dollars, "***,***,***.** ");  /* overflow */
  84.         }
  85.  
  86.         if (sign) dollars[14] = '-';
  87.         f = dollars;
  88.         while (*f == ' ') f++;
  89.         return (f);
  90.  
  91. } /* dlrcvt() */
  92.  
  93.  
  94. /* Same, but minus sign is interpreted as trailing Cr, positive is Dr
  95. */
  96.  
  97. char *crdrform( x )
  98. long x;
  99. {
  100.      static char *q, r[20];
  101.  
  102.      strcpy( r, dlrcvt( x ) );
  103.      q = strchr( r, '\0' );
  104.      --q;
  105.      if ( x == 0L )      strcpy( q, "Dr" ); /* zero is positive */
  106.      else if (*q == '-') strcpy( q, "Cr" ); 
  107.      else                strcpy( q, "Dr" );
  108.      return (r);
  109. }
  110.  
  111.  
  112.  
  113. /* keyin with key click and Control C trap
  114. ** user must do  BREAKOFF()  in order to enable Control C trapping
  115. ** the macro is defined in "ciao.h"
  116. ** BREAK becomes ESC, but screen is still marred by "^C" message
  117. */
  118.  
  119. static jmp_buf l_onj;
  120. static void c_onj()
  121. {
  122.      longjmp( l_onj, -1 );
  123. }
  124.  
  125. int getkey()
  126. {
  127.       int n;
  128.       signal( SIGINT, c_onj );
  129.       if (setjmp( l_onj ) == 0) n = keyin( noop ); else n = ESC;
  130.       signal( SIGINT, SIG_DFL );
  131.       HIclack();
  132.       return (n);
  133. }
  134.  
  135.  
  136. /* edit masked numeric
  137. ** may only edit digits in the self-masking string, can't delete
  138. ** skips over (forward or backward) any edit character not a digit
  139. ** calls Function Key handler if user enters one
  140. ** the called Fn function is responsible for screen condition!
  141. ** returns 0 if successful 
  142. ** returns ESC if user interrupts with ESC and ESCflag is non-zero
  143. */
  144.  
  145. int edit_masked_numeric( buffptr, fnkey )
  146. char *buffptr;
  147. void (* fnkey)();
  148. {
  149.      register char *p; 
  150.      register int ch;
  151.      int len;
  152.  
  153.      /* highlight string to be edited */
  154.  
  155.      wprintf( "^2%s", buffptr );
  156.      len = strlen( buffptr );
  157.      for (ch = 0; ch < len; ch++ ) wink( '\b' );
  158.  
  159.      p = buffptr;
  160.      while (*p)
  161.      {
  162.          if ( isdigit( *p ) ) 
  163.          {
  164.               odd:  ch = getkey();
  165.               switch (ch)
  166.               {
  167.               case ESC:{ if (ESCflag != 0) return (ESC); break; }
  168.               case 13: { /* accept entry */
  169.                        while (*p) wink( *p++ );
  170.                        continue;
  171.                        break;
  172.                        }
  173.               case '\b':
  174.               case 203:{ /* left arrow */
  175.                        if (p > buffptr)
  176.                        {
  177.                           do
  178.                           {
  179.                              --p;
  180.                              wink( '\b' );
  181.                           }
  182.                           while ( (!isdigit( *p )) && (p > buffptr) );
  183.                        }
  184.                        continue;
  185.                        break;
  186.                        }
  187.               case 205:{ /* right arrow */
  188.                        wink( *p++ );
  189.                        continue;
  190.                        break;
  191.                        }
  192.               case 187:
  193.               case 188:
  194.               case 189:
  195.               case 190:
  196.               case 191:  /* was input a Fn key? */
  197.               case 192:
  198.               case 193:
  199.               case 194:
  200.               case 195:
  201.               case 196:{
  202.                        (* fnkey)( ch );
  203.                        continue;
  204.                        break;
  205.                        }
  206.               default: if ( isascii( ch ) && isdigit( ch ) )
  207.                        ;
  208.                        else { thurb(); goto odd; }
  209.               }
  210.          }
  211.          else ch = *p;        /* not digit mask, accept entry from buffptr */
  212.  
  213.          *p = ch;             /* put char in buffptr */
  214.          wink( *p++ );        /* echo it */
  215.  
  216.      }
  217.  
  218.      /* display string as edited, normal vid mode */
  219.  
  220.      for (ch = 0; ch < len; ch++ ) wink( '\b' );
  221.      wprintf( "^0%s", buffptr );
  222.      return ( 0 );
  223. }
  224.  
  225.  
  226.  
  227.  
  228. /* Edit String.
  229. ** calls Function Key handler if user enters one
  230. ** the called Fn function is responsible for screen condition!
  231. ** returns ESC if user interrupts with ESC and ESCFlag is nonzero
  232. ** returns MINUS if user interrupts with MINUS and MINUSFlag is nonzero
  233. ** returns 0 otherwise 
  234. */
  235.  
  236. int edit_string( buffptr, fnkey )
  237. char *buffptr;
  238. void (* fnkey)();
  239. {
  240.      register char *p; 
  241.      register int ch;
  242.      int len;
  243.  
  244.      /* highlight string to be edited */
  245.  
  246.      wprintf( "^2%s", buffptr );
  247.      len = strlen( buffptr );
  248.      for (ch = 0; ch < len; ch++ ) wink( '\b' );
  249.  
  250.      p = buffptr;
  251.      while (*p)
  252.      {
  253.           ch = getkey();
  254.           if ( isascii( ch ) )
  255.           {
  256.              switch (ch)
  257.              {
  258.              case ESC:{ if (ESCflag != 0) return (ESC); break; }
  259.              case '\r':{
  260.                       /* CR, accept input & exit */
  261.                       while (*p) wink( *p++ );
  262.                       break;
  263.                       }
  264.              case '\b':{                        /* backspace */
  265.                       if (p > buffptr)
  266.                       {
  267.                          --p;
  268.                          wink( '\b' );
  269.                       }
  270.                       break;
  271.                       }
  272.              case '-':{ if (MINUSflag != 0) return (MINUS); /* NO BREAK */ } 
  273.              default: {
  274.                       if ( isgraph( ch ) || ch == 0x20 ) 
  275.                       { 
  276.                           *p = ch;              /* put char in buffptr */
  277.                           wink( *p++ );         /* echo it */
  278.                       }
  279.                       else 
  280.                       {
  281.                           thurb();              /* reject unprintables */
  282.                       }
  283.                       break;
  284.                       }
  285.              }
  286.           }
  287.           else
  288.           {
  289.              switch (ch)
  290.              {
  291.              case 187:
  292.              case 188:
  293.              case 189:
  294.              case 190:
  295.              case 191:  /* was input a function key? */
  296.              case 192:
  297.              case 193:
  298.              case 194:
  299.              case 195:
  300.              case 196:{
  301.                       (* fnkey)( ch );
  302.                       break;
  303.                       }
  304.              case 203:{                         /* left arrow */
  305.                       if (p > buffptr)
  306.                       {
  307.                          --p;
  308.                          wink( '\b' );
  309.                       }
  310.                       break;
  311.                       }
  312.              case 205:{                         /* right arrow */
  313.                       wink( *p++ );
  314.                       break;
  315.                       }
  316.              default: {
  317.                       thurb();
  318.                       break;
  319.                       }
  320.              }
  321.           }
  322.      }
  323.  
  324.      /* display string as edited, normal vid mode */
  325.  
  326.      for (ch = 0; ch < len; ch++ ) wink( '\b' );
  327.      wprintf( "^0%s", buffptr );
  328.      return ( 0 );
  329. }
  330.  
  331.  
  332.  
  333. void set_date()
  334. {
  335.        long ltime;
  336.        struct tm *timevals;
  337.        int d,m,y;
  338.        time( <ime );
  339.        timevals = localtime( <ime );
  340.        d = timevals->tm_mday;
  341.        m = timevals->tm_mon + 1;
  342.        y = timevals->tm_year;
  343.        sprintf( date_mask, "%02u/%02u/%02u", m, d, y );
  344. }
  345.  
  346.  
  347.  
  348. /* message() has variable args, works like wprintf with gotoxy
  349. */
  350.  
  351. void message( x,y,p ) int x,y; char *p;  
  352. {
  353.      va_list arg_ptr;
  354.      char *buff;
  355.  
  356.      gotoxy( x,y ); wputs("^0"); clreol();  /* NOTE: Clears line! */
  357.      buff = malloc( 128 );
  358.      va_start(arg_ptr, p);
  359.      vsprintf( buff, p, arg_ptr );
  360.      va_end( arg_ptr );
  361.      wputs( buff );
  362.      free( buff );
  363. }
  364.  
  365.  
  366.  
  367.