home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0010 - 0019 / ibm0010-0019 / ibm0010.tar / ibm0010 / CODE4-1.ZIP / SOURCE.ZIP / X4EDIT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-10-14  |  7.6 KB  |  348 lines

  1. /* x4edit.c  (c)Copyright Sequiter Software Inc., 1987, 1988, 1989.  All rights reserved. */
  2.  
  3. #include "d4base.h"
  4. #include "w4.h"
  5. #include "g4char.h"
  6.  
  7. #include <string.h>
  8.  
  9. extern    INDEX  *v4index ;
  10. extern    BASE   *v4base ;
  11.  
  12. static  int  edit(void), help(void), record_go(void), seek_record(void) ;
  13. static  int  do_command( unsigned int), setup(void), write_rec(void) ;
  14.  
  15. /* Edit the Current Database */
  16.  
  17. x4edit()
  18. {
  19.    int  rc, last_window, w_ref ;
  20.  
  21.    if ( d4select(-1) < 0 )   /* Make Sure a Database is Open */
  22.    {
  23.       w4display( " Edit is not Possible: ",
  24.          "No database has been opened.",
  25.          "Press any key to continue.", (char *) 0 ) ;
  26.       return -1 ;
  27.    }
  28.  
  29.    last_window =  w4select(-1) ;
  30.  
  31.    w_ref =  w4define( 0,0, 24,79 ) ;
  32.    w4popup() ;
  33.    w4activate( w_ref ) ;
  34.  
  35.    rc =  edit() ;
  36.  
  37.    w4deactivate( w_ref ) ;
  38.    w4close( w_ref ) ;
  39.    w4select( last_window ) ;
  40.  
  41.    return rc ;
  42. }
  43.  
  44.  
  45. static int  edit()
  46. {
  47.    int     command_char, i_ref, rc ;
  48.  
  49.    if ( d4recno() == 0 )
  50.       if ( x4top() < 0 )  return -1 ;
  51.  
  52.    if ( setup() < 0)  return -1 ;   /* Setup the Initial Gets */
  53.  
  54.    for ( ;; )     /* Loop Forever */
  55.    {
  56.       rc =  0 ;
  57.  
  58.       /* Display Status Information on Line 0 */
  59.  
  60.       w4( 0,0, "Record:" ) ;
  61.       w4long( 0,7, d4recno(), 6) ;
  62.  
  63.       if ( d4deleted() )
  64.      w4( 0,15, "DELETED" ) ;
  65.       else
  66.      w4( 0,15, "       " ) ;
  67.  
  68.       w4out( "  Database: " ) ;
  69.       w4out( d4ptr()->name ) ;
  70.  
  71.       i_ref =  i4seek_ref() ;
  72.       if ( i_ref >= 0 )
  73.       {
  74.      if ( w4col() < 60 )
  75.      {
  76.         w4out( "   Index File: " ) ;
  77.         w4out( v4index[i_ref].name ) ;
  78.      }
  79.       }
  80.  
  81.       /* Unlock the Database */
  82.       if ( d4unlock(-1L ) < 0 )  return -1 ;
  83.  
  84.       /* Get the Record Information */
  85.       command_char =  g4read() ;
  86.       if ( command_char == 0 )
  87.      command_char = ESC ;
  88.  
  89.       /* Process the Return Character */
  90.  
  91.       if ( command_char == F1 )
  92.      command_char =  help() ;
  93.       else
  94.       {
  95.      rc =  do_command( command_char ) ;  /* It Could be a Command */
  96.      if ( rc != 0 )   return  rc ;
  97.       }
  98.    }
  99. }
  100.  
  101.  
  102. static int  write_rec()
  103. {
  104.    if ( d4recno() > 0L )
  105.    {
  106.       if ( d4write( d4recno() ) < 0 )  return -1 ;
  107.       if ( d4unlock(-1L) < 0 )  return -1 ;
  108.    }
  109.    return 0 ;
  110. }
  111.  
  112.  
  113. static int  do_command( command_char )
  114. unsigned int  command_char ;
  115. {
  116.    long  rec ;
  117.    int     rc ;
  118.  
  119.    switch( command_char )
  120.    {
  121.       case ALT_A:   /* Add Blank Record */
  122.          if ( write_rec() < 0 )  return -1 ;
  123.      memset( d4ptr()->buffer, (int) ' ', d4ptr()->buffer_len ) ;
  124.      if ( d4write( 0L ) < 0 )  return -1 ;     /* Append Blank Record */
  125.      break ;
  126.  
  127.       case ALT_B:
  128.          if ( write_rec() < 0 )  return -1 ;
  129.      if ( x4bottom() < 0 )    return -1 ;
  130.      break ;
  131.  
  132.       case ALT_D:   /* Delete */
  133.          if ( write_rec() < 0 )  return -1 ;
  134.      if ( d4recno() > 0L )
  135.         if ( d4delete(d4recno()) < 0 )   return -1 ;
  136.      break ;
  137.  
  138.       case ALT_I:   /* Insert Record */
  139.          if ( write_rec() < 0 )  return -1 ;
  140.      memset( d4ptr()->buffer, (int) ' ', d4ptr()->buffer_len ) ;
  141.      if ( x4insert( d4recno() ) < 0 )  return -1 ;
  142.      break ;
  143.  
  144.       case ALT_R:   /* Go to Entered Record */
  145.          if ( write_rec() < 0 )  return -1 ;
  146.      if ( record_go() < 0 )  return -1 ;
  147.      break ;
  148.  
  149.       case ALT_S:
  150.          if ( write_rec() < 0 )  return -1 ;
  151.      if ( seek_record() < 0 )  return -1 ;
  152.      break ;
  153.  
  154.       case ALT_T:   /* Top Record */
  155.          if ( write_rec() < 0 )  return -1 ;
  156.      if ( x4top() < 0 )  return -1 ;
  157.      break ;
  158.  
  159.       case ALT_U:   /* Undo the Record Deletion */
  160.          if ( write_rec() < 0 )  return -1 ;
  161.      if ( d4recno() > 0L )
  162.         if ( d4recall( d4recno() )    < 0 )  return -1 ;
  163.      break ;
  164.  
  165.       case PGUP:    /* Previous Record */
  166.          if ( write_rec() < 0 )  return -1 ;
  167.      if ( x4skip(-1L) < 0 )  return -1 ;
  168.      break ;
  169.  
  170.       case PGDN:    /* Next Record */
  171.          if ( write_rec() < 0 )  return -1 ;
  172.  
  173.      rec =    d4recno() ;
  174.      if ( (rc = x4skip(1L)) < 0 )  return -1 ;
  175.      if ( rc == 3 )
  176.         if ( rec > 0 )
  177.            if ( x4go(rec) < 0 )  return -1 ;
  178.      break ;
  179.  
  180.       case CTRL_W:  /* Write the Current Record */
  181.      if ( d4write( d4recno() ) < 0 )  return -1 ;
  182.      return CTRL_W ;
  183.  
  184.       case ESC:     /* Exit */
  185.      g4release(1) ;
  186.      return  ESC ;
  187.    }
  188.    return 0 ;
  189. }
  190.  
  191.  
  192. static int  setup()
  193. {
  194.    int    i,r,c ;
  195.    long field_ref ;
  196.  
  197.    w4clear( 0 ) ;    /* Clear the Screen */
  198.  
  199.    w4( 24,0, "Press <F1> for Help" ) ;
  200.  
  201.    g4release(1) ;
  202.  
  203.    /* For each Field */
  204.    for ( i=1, r=2, c=5; i<= f4num_fields(); i++ )
  205.    {
  206.       field_ref =  f4j_ref(i) ;
  207.       if ( f4type(field_ref) == 'M' )  continue ;
  208.  
  209.       if ( r == 23 )
  210.       {
  211.      if ( c == 5 )
  212.      {
  213.         r =  2 ;
  214.         c = 45 ;
  215.      }
  216.      else
  217.         break ;
  218.       }
  219.  
  220.       w4( r,c, f4name(field_ref) ) ;   /* Say the Field Name */
  221.       g4field( r,c+12, field_ref ) ;   /* Schedule a Get */
  222.       if ( f4type(field_ref) == 'C' )
  223.      g4width( 0,18) ;           /* Max. Width of 18 */
  224.  
  225.       r++ ;
  226.    }
  227.  
  228.    g4release(0) ;   /* Do not Release the Gets after a read */
  229.  
  230.    return 0 ;
  231. }
  232.  
  233.  
  234. static int  help()     /* Display Help Information */
  235. {
  236.    int    r,c, rc ;
  237.  
  238.    w4clear( 0 ) ;
  239.  
  240.    w4centre( 1, "Edit Help Screen" ) ;
  241.  
  242.    r =    4 ;
  243.    c = 25 ;
  244.  
  245.    w4( r++,c-3,     "Enter Command: " ) ;
  246.    w4cursor( w4row(), w4col() ) ;
  247.  
  248.    w4( ++r, c,    "<Alt A>  - Add Record" ) ;
  249.    w4( ++r, c,    "<Alt B>  - Bottom Record" ) ;
  250.    w4( ++r, c,    "<Alt D>  - Delete Recrord" ) ;
  251.    w4( ++r, c,    "<Alt I>  - Insert Record" ) ;
  252.    w4( ++r, c,    "<Alt R>  - Record Number" ) ;
  253.    w4( ++r, c,    "<Alt S>  - Seek Record" ) ;
  254.    w4( ++r, c,    "<Alt T>  - Top Record" ) ;
  255.    w4( ++r, c,    "<Alt U>  - Undo Delete Record" ) ;
  256.    w4( ++r, c,    "<PgUp>   - Previous Record" ) ;
  257.    w4( ++r, c,    "<PgDn>   - Next Record" ) ;
  258.    w4( ++r, c,    "<Ctrl W> - Write Record and Exit" ) ;
  259.    w4( ++r, c,    "<Esc>    - Exit" ) ;
  260.    ++r ;
  261.    w4( ++r, c-3,  "Press any key to continue ..." ) ;
  262.    ++r ;
  263.  
  264.    rc =  g4char() ;
  265.  
  266.    g4release(1) ;
  267.    if ( setup() < 0 )  return -1 ;
  268.  
  269.    return  rc ;
  270. }
  271.  
  272.  
  273. static int  record_go()
  274. {
  275.    long  rec ;
  276.    int     rc, w_ref ;
  277.  
  278.    rec =  d4recno() ;
  279.  
  280.    w_ref =  w4define( 9,27,14,52 ) ;
  281.    w4popup() ;
  282.    w4title( 0,-1, " Record Number Command ", F_WHITE ) ;
  283.    w4activate( w_ref ) ;
  284.  
  285.    w4( 1,2, "Records in Database:" ) ;
  286.    w4long( w4row(),w4col(), d4reccount(), 6 ) ;
  287.  
  288.    w4( 2,2, "Enter Record Number: " ) ;
  289.    g4long( w4row(), w4col(), &rec ) ;
  290.    rc = g4read() ;     /* Read the Record */
  291.  
  292.    w4deactivate( w_ref ) ;
  293.    w4close( w_ref ) ;
  294.  
  295.    if ( rc != ESC && rec > 0 && rec <= d4reccount() )
  296.       if ( x4go(rec) < 0) return -1 ;     /* Go to the Record */
  297.  
  298.    return 0 ;
  299. }
  300.  
  301.  
  302. static int  seek_record()
  303. {
  304.    int    index_ref, rc, w_ref ;
  305.    long rec ;
  306.    char seek_data[101] ;
  307.  
  308.    index_ref =    i4seek_ref() ;
  309.  
  310.    if ( index_ref < 0 )
  311.    {
  312.       w4display( " Operator Error: ", 
  313.          "The Database does not have an Index File.",
  314.          "",
  315.          "Press any key to continue.", (char *) 0 ) ;
  316.       return 1 ;
  317.    }
  318.  
  319.    w_ref =  w4define( 10,25,14,54 ) ;
  320.    w4popup() ;
  321.    w4title( 0,-1, " Enter the Seek Information: ", F_WHITE ) ;
  322.    w4activate( w_ref ) ;
  323.  
  324.    memset( seek_data, 0, (size_t) sizeof(seek_data) ) ;
  325.    g4release(1) ;
  326.    g4( 1,3, seek_data ) ;
  327.    g4width( 24, 100 ) ;
  328.    rc =  g4read() ;
  329.  
  330.    w4deactivate( w_ref ) ;
  331.    w4close( w_ref ) ;
  332.  
  333.    if ( rc != ESC )
  334.    {
  335.       rec =  d4recno() ;
  336.       if ( (rc =  x4seek(seek_data))< 0)  return -1 ;
  337.  
  338.       if ( rc == 3 )
  339.       {
  340.      c4trim_n( seek_data, (int) sizeof(seek_data) ) ;
  341.      w4display( " Seek Data not Found: ", seek_data, (char *) 0 ) ;
  342.      if ( x4go(rec) < 0 )  return -1 ;
  343.       }
  344.    }
  345.  
  346.    return 0 ;
  347. }
  348.