home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / BEEHIVE / UTILITYS / CHX8012B.ARC / CHECKB.C < prev    next >
Text File  |  1990-07-21  |  14KB  |  532 lines

  1. /*  checkb.c -- 3rd source file for check register program                   */
  2.  
  3. /*  copyright (c) 1986 by Jim Woolley and WoolleyWare, San Jose, CA          */
  4.  
  5. /*  vers. 1.0, 12/85 thru 5/86
  6.  *
  7.  *  vers. 1.2, 7/86
  8.  *  clrscr(): do Trmini plus scrolling
  9.  *  lindel(): for VT100, push up from row 24 instead of ROWS
  10.  *  linins(): avoid moving a line beyond ROWS and auto scrolling
  11.  *  integrated CLEARS using #ifdef
  12.  */
  13.  
  14. /*  this file contains:
  15.  *      display( i)
  16.  *      putrecord( i)
  17.  *      update( i)
  18.  *      newbalance( i)
  19.  *      newentry( i)
  20.  *      clrscr()
  21.  *      clreol( c)
  22.  *      dellin( r)
  23.  *      lindel( r)
  24.  *      inslin( r)
  25.  *      linins( r)
  26.  *      putscr( z)
  27.  *      godown( c)
  28.  *      shodown()
  29.  *      goup( c)
  30.  *      goright( c)
  31.  *      goleft( c)
  32.  *      gonext()
  33.  *      goprior()
  34.  *      gobottom()
  35.  *      gotop()
  36.  *      goupdown( c)
  37.  */
  38.  
  39. #include    "a:checks.h"
  40.  
  41. display( i)                             /*  display entries i thru Last      */
  42. int i;
  43. {
  44.     char c;
  45.     int firstrow, lastrow;
  46.  
  47.     c = 0;
  48.     i = max( i, First);
  49.     while ( i <= Last)
  50.     {
  51.         if ( !c && kbhit())             /*  look ahead for vertical motion   */
  52.         {
  53.             if ((( c = getchar()) == CTRLR && First) ||
  54.                 ( c == CTRLC && ( First + LAST) <= Maxentry))
  55.                 break;
  56.             if ( !( *Lindel) && (       /*  if no line delete control and    */
  57.                 ( c == CTRLX && Recno >= ( Last - 1) &&
  58.                     ( Last - First + HEAD) == ( ROWS - 1)) ||
  59.                 ( c == CTRLZ && First < Last)
  60.                 ))
  61.                 break;
  62.             if ( !( *Linins) &&         /*  if no line insert control and    */
  63.                 First && (( c == CTRLE && Recno == First) || c == CTRLW))
  64.                 break;
  65.         }
  66.         putrecord( i++);
  67.     }
  68.     i = Last + 1;
  69.     firstrow = First - HEAD;
  70.     lastrow = firstrow + ROWS;
  71.     while ( i < lastrow)
  72.     {
  73.         cursorto(( i - firstrow), 0);
  74.         clreol( 0);                     /*  clear bottom of screen           */
  75.         ++i;
  76.     }
  77.     if ( c)                             /*  restore keyboard char            */
  78.         ungetch( c);
  79. }
  80.  
  81. putrecord( i)                           /*  display Entry[ i]                */
  82. int i;
  83. {
  84.     char c, *p, s[ 10];
  85.     int count;
  86.     struct record *e;
  87.  
  88.     e = &Entry[ i];
  89.     cursorto(( i - First + HEAD), 0);
  90.     putdate( &( e->date));
  91.     count = PAYEESIZE - 1 - strlen( p = e->payee);
  92.     puts( p);
  93.     while ( count--)
  94.         putchar( PAYEEFILL);
  95.     putchar( ' ');
  96.     putchar( e->category & 0x7f);
  97.     putchar( ' ');
  98.     putmoney( &( e->amount));
  99.     puts( e->deposit ? "DEP " : DEPCLRFIL);
  100. #ifdef  CLEARS
  101.     puts( e->clear ? DEPCLRFIL : "<M> ");
  102. #else
  103.     puts( e->clear ? "CLR " : DEPCLRFIL);
  104. #endif
  105.     putmoney( &Balance[ i]);
  106. }
  107.  
  108. update( i)                              /*  update balance for entry >= i    */
  109. int i;
  110. {
  111.     int firstrow;
  112.  
  113.     firstrow = First - HEAD;
  114.     while ( i <= Maxentry)
  115.     {
  116.         newbalance( i);
  117.         if ( First <= i && i <= Last)
  118.         {
  119.             cursorto(( i - firstrow), BALCOL);
  120.             putmoney( &Balance[ i]);
  121.         }
  122.         ++i;
  123.     }
  124. }
  125.  
  126. newbalance( i)                          /*  update balance for entry i       */
  127. int i;
  128. {
  129.     struct record *e;
  130.     struct money m, *p;
  131.  
  132.     if ( i)
  133.         p = &Balance[ i - 1];
  134.     else
  135.     {
  136.         m.dollar = 0;
  137.         m.cent = 0;
  138.         p = &m;
  139.     }
  140.     e = &Entry[ i];
  141.     addmoney( &Balance[ i], p, &( e->amount), e->deposit);
  142. }
  143.  
  144. newentry( i)                            /*  create and initialize entry i    */
  145. int i;
  146. {
  147.     struct record *en, *ep;
  148.  
  149.     en = &Entry[ i];
  150. #ifndef  CLEARS
  151.     if ( Today.yy)
  152.     {
  153.         en->date.mm = Today.mm;
  154.         en->date.dd = Today.dd;
  155.         en->date.yy = Today.yy;
  156.     }
  157.     else if ( i)
  158. #else
  159.     if ( i)
  160. #endif
  161.     {
  162.         ep = &Entry[ i - 1];
  163.         en->date.mm = ep->date.mm;
  164.         en->date.dd = ep->date.dd;
  165.         en->date.yy = ep->date.yy;
  166.     }
  167.     en->payee[ 0] = '\0';
  168.     en->category = DEFCAT;
  169.     en->amount.dollar = 0;
  170.     en->amount.cent = 0;
  171.     en->deposit = FALSE;
  172. #ifdef  CLEARS
  173.     en->clear = TRUE;
  174. #else
  175.     en->clear = FALSE;
  176. #endif
  177.     newbalance( i);
  178.     Modified = TRUE;
  179. }
  180.  
  181. clrscr()                                /*  clear screen and home cursor     */
  182. {
  183.     int i;
  184.  
  185.     putscr( Trmini);                    /*  Trmini may initialize w/o clear  */
  186.     cursorto(( ROWS - 1), 0);
  187.     for (( i = ( ROWS + 2)); i; --i)    /*  make sure by scrolling           */
  188.         putchar( '\n');
  189.     cursorto( 0, 0);
  190. }
  191.  
  192. clreol( c)                              /*  clear column c to end-of-line    */
  193. int c;                                  /*  0 is left edge                   */
  194. {                                       /*  cursor must be set to column c   */
  195.     if ( *Eraeol)
  196.         putscr( Eraeol);
  197.     else for ( ; c < COLS; ++c)
  198.         putchar( ' ');
  199. }
  200.  
  201. dellin( r)                              /*  delete line at row r             */
  202. int r;                                  /*  move remaining lines up          */
  203. {                                       /*  display new last line            */
  204.     if ( lindel( r))
  205.         display( First + LAST);
  206.     else display( First + r - HEAD);
  207. }
  208.  
  209. lindel( r)                              /*  if *Lindel != 0,                 */
  210. int r;                                  /*  delete line at row r             */
  211. {                                       /*  move remaining lines up          */
  212.                                         /*  return TRUE; else, return FALSE  */
  213.                                         /*  final cursor pos. indeterminate  */
  214.     if ( *Lindel)
  215.     {
  216.         if ( *Lindel == 255)
  217.         {
  218.             puts( "\033[");             /*  special for VT100                */
  219.             printd( r + 1);
  220.             putchar( 'r');
  221.             cursorto( 23, 0);           /*  VT100 has 24 rows                */
  222.             puts( "\033D\033[1r");
  223.         }
  224.         else
  225.         {
  226.             cursorto( r, 0);
  227.             putscr( Lindel);
  228.         }
  229.         return ( TRUE);
  230.     }
  231.     return ( FALSE);
  232. }
  233.  
  234. inslin( r)                              /*  insert blank line at row r       */
  235. int r;                                  /*  move existing lines down         */
  236. {                                       /*  putrecord at row r               */
  237.  
  238.     if ( linins( r))
  239.         putrecord( First + r - HEAD);
  240.     else display( First + r - HEAD);
  241. }
  242.  
  243. linins( r)                              /*  if *Linins != 0,                 */
  244. int r;                                  /*  insert blank line at row r       */
  245. {                                       /*  move existing lines down         */
  246.                                         /*  position cursor at ( r, 0)       */
  247.                                         /*  return TRUE; else, return FALSE  */
  248.     if ( *Linins)
  249.     {
  250.         cursorto(( ROWS - 1), 0);       /*  first erase last line            */
  251.         clreol( 0);
  252.         if ( *Linins == 255)
  253.         {
  254.             puts( "\033[");             /*  special for VT100                */
  255.             printd( r + 1);
  256.             putchar( 'r');
  257.             cursorto( r, 0);
  258.             puts( "\033M\033[1r");
  259.             cursorto( r, 0);
  260.         }
  261.         else
  262.         {
  263.             cursorto( r, 0);
  264.             putscr( Linins);
  265.         }
  266.         return ( TRUE);
  267.     }
  268.     return ( FALSE);
  269. }
  270.  
  271. putscr( z)                              /*  send screen control chars        */
  272. char *z;                                /*  first char is count of chars     */
  273. {
  274.     char c;
  275.  
  276.     for ( c = *z++; c; --c)
  277.         putchar( *z++);
  278. }
  279.  
  280. godown( c)                              /*  go down one entry                */
  281. char c;
  282. {
  283.     if ( c == CTRLZ)
  284.     {
  285.         if ( Last <= First)
  286.             return;
  287.         ++First;                        /*  scroll up                        */
  288.         Recno = max( Recno, First);
  289.         Last = min(( Last + 1), Maxentry);
  290.         shodown();
  291.         return;
  292.     }
  293.     if ( ++Recno > ( Last - 1))         /*  at next to last on screen?       */
  294.     {
  295.         if ( Recno > Maxentry)          /*  at Maxentry?                     */
  296.         {
  297.             Recno = Maxentry + 1;
  298.             Field = 0;
  299. #ifndef CLEARS
  300.             if ( c == '\r')             /*  assume new entry desired         */
  301.             {
  302.                 if ( Maxentry < ( ENTRYSIZE - 1))
  303.                 {
  304.                     newentry( ++Maxentry);
  305.                     putrecord( Last = Maxentry);
  306.                     Today.yy = 0;       /*  use previous entry's date next   */
  307.                 }
  308.                 else
  309.                 {
  310.                     prompt( "Number of entries is maximum allowed");
  311.                     waitesc();
  312.                 }
  313.             }
  314. #endif
  315.         }
  316.         if (( Last - First + HEAD) == ( ROWS - 1))
  317.         {
  318.             ++First;                    /*  scroll up                        */
  319.             Last = min(( Last + 1), Maxentry);
  320.             shodown();
  321.         }
  322.     }
  323. }
  324.  
  325. shodown()                               /*  scroll display up to show down   */
  326. {
  327.     if ( !( *Lindel))                   /*  if no delete line control        */
  328.     {                                   /*      get a head start             */
  329.         if ( Last < ( First + LAST))
  330.         {
  331.             cursorput(( Last + 1), 0);
  332.             clreol( 0);
  333.         }
  334.         putrecord( Last);
  335.     }
  336.     dellin( HEAD);
  337. }
  338.  
  339. goup( c)                                /*  go up one entry                  */
  340. char c;
  341. {
  342.     if ( Recno != First && c != CTRLW)
  343.     {
  344.         --Recno;
  345.         return;
  346.     }
  347.     if ( !First)
  348.         return;
  349.     --First;
  350.     if ( !( *Linins))                   /*  if no insert line control        */
  351.         putrecord( First);              /*      get a head start             */
  352.     Last = min(( First + LAST), Maxentry);
  353.     if ( c == CTRLW)
  354.         Recno = min( Recno, ( First + ( LAST - 1)));
  355.     else --Recno;
  356.     inslin( HEAD);
  357. }
  358.  
  359. goright( c)                             /*  move right within current entry  */
  360. char c;
  361. {
  362.     char *p;
  363.     int edge;
  364.     struct record *e;
  365.  
  366.     if ( Recno > Maxentry)
  367.     {
  368.         godown( c);
  369.         return;
  370.     }
  371. #ifndef CLEARS
  372.     if ( Field == PAYFIELD && c != '\r')
  373.     {
  374.         e = &Entry[ Recno];
  375.         if ( Character < ( edge = min(( PAYEESIZE - 1), strlen( e->payee))))
  376.         {
  377.             if ( c == CTRLF || c == '\t')
  378.             {
  379.                 --edge;
  380.                 p = e->payee + Character;
  381.                 while ( Character < edge)
  382.                 {
  383.                     ++Character;
  384.                     ++p;
  385.                     if ( isspace( *( p - 1)) && !isspace( *p))
  386.                         return;
  387.                 }
  388.             }
  389.             ++Character;
  390.             return;
  391.         }
  392.     }
  393. #endif
  394.     if ( ++Field >= MAXFIELD)
  395.     {
  396.         Field = 0;
  397.         godown( c);
  398.     }
  399. }
  400.  
  401. goleft( c)                              /*  move left within current entry   */
  402. char c;
  403. {
  404.     char *p;
  405.  
  406. #ifndef CLEARS
  407.     p = Entry[ Recno].payee;
  408.     if ( Field == PAYFIELD && Character > 0)
  409.     {
  410.         if ( c == CTRLA)
  411.         {
  412.             p += Character;
  413.             while ( --Character)
  414.             {
  415.                 --p;
  416.                 if ( isspace( *( p - 1)) && !isspace( *p))
  417.                     return;
  418.             }
  419.         }
  420.         else --Character;
  421.     }
  422.     else if ( Field)
  423.     {
  424.         Character = PAYEESIZE;          /*  putcursor() will readjust        */
  425.         --Field;
  426.     }
  427. #else
  428.     if ( Field)
  429.         --Field;
  430. #endif
  431.     else if ( !Recno)
  432.         return;
  433.     else
  434.     {
  435.         Field = MAXFIELD - 1;
  436. #ifndef CLEARS
  437.         Character = PAYEESIZE;          /*  putcursor() will readjust        */
  438. #endif
  439.         goup( c);
  440.     }
  441. }
  442.  
  443. gonext()                                /*  display next page                */
  444. {
  445.     int i, n;
  446.  
  447.     if ( Recno > Maxentry)
  448.         return;
  449.     if (( First + LAST) > Maxentry)
  450.     {
  451.         Recno = Maxentry;
  452.         godown( 0);
  453.         return;
  454.     }
  455.     i = First + PAGE;
  456.     Last = min( Maxentry, ( i + LAST));
  457.     i = max( 0, min( i, ( Last - (LAST - PAGE))));
  458.     n = i - First;
  459.     First = i;
  460.     Recno = min(( Recno + n), ( Maxentry + 1));
  461.     if ( Recno <= Maxentry)
  462.         putrecord( Recno);              /*  get a head start                 */
  463.     display( First);
  464.     if ( Recno > Maxentry)
  465.         godown( 0);
  466. }
  467.  
  468. goprior()                               /*  display previous page            */
  469. {
  470.     int i, n;
  471.  
  472.     if ( !Recno)
  473.         return;
  474.     if ( !First)
  475.     {
  476.         Recno = 0;
  477.         return;
  478.     }
  479.     i = max(( First - PAGE), 0);
  480.     n = First - i;
  481.     First = i;
  482.     Last = min( Maxentry, ( First + LAST));
  483.     putrecord( Recno = max(( Recno - n), 0));  /*  get a head start          */
  484.     display( First);
  485. }
  486.  
  487. gobottom()                              /*  display last page                */
  488. {
  489.     Recno = max( Recno, ( Maxentry + ( 1 - PAGE)));
  490.     First = max( First, ( Recno - PAGE));
  491.     gonext();
  492. }
  493.  
  494. gotop()                                 /*  display first page               */
  495. {
  496.     First = min( First, 1);
  497.     Recno = min( Recno, 1);
  498.     goprior();
  499. }
  500.  
  501. goupdown( c)                            /*  continuous up or down            */
  502. char c;
  503. {
  504.     int i;
  505.  
  506.     prompt( "Press digit to change speed, or other key to stop");
  507.     c -= CTRLTOA;
  508.     FOREVER
  509.     {
  510.         if ( c == CTRLW)
  511.         {
  512.             if ( First)
  513.                 goup( c);
  514.             else return;
  515.         }
  516.         else
  517.         {
  518.             if ( Last != First)
  519.                 godown( c);
  520.             else return;
  521.         }
  522.         for ( i = Speed*Dloop; i && !kbhit(); --i)
  523.             continue;                   /*  wait Speed/10 sec for key        */
  524.         if ( i)                         /*  if key hit                       */
  525.         {
  526.             if ( isdigit( i = getchar()))
  527.                 Speed = ( i == '0' ? 10 : i - '0');
  528.             else return;
  529.         }
  530.     }
  531. }
  532.