home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c160 / 1.ddi / SOURCE / M4THREE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-19  |  5.8 KB  |  246 lines

  1. /*  m4three.c,  for Code Base 4
  2.     (c)Copyright Sequiter Software Inc., 1987-1990.  All rights reserved.
  3. */
  4.  
  5. #include "p4misc.h"
  6. #include "d4all.h"
  7. #include "u4error.h"
  8. #include "m4.h"
  9.  
  10. #include <string.h>
  11. #ifndef UNIX
  12. #include <process.h>
  13. #include <io.h>
  14. #endif
  15.  
  16. #define   MEMO_SIZE   0x200
  17.  
  18. extern  BASE  *v4base ;
  19. extern  int    v4cur_base, v4lock_wait ;
  20.  
  21. static  int  m3open(int) ;
  22.  
  23. static int  m3open( int base_ref ) 
  24. {
  25.    int    hand ;
  26.  
  27.    if ( (hand =  u4open( m4name(base_ref), 0 )) < 0 )  return -1 ;
  28.    v4base[base_ref].memo_file =  hand ;
  29.    v4base[base_ref].memo_size =  0x200 ;
  30.  
  31.    return  hand ;
  32. }
  33.  
  34. m3read( long field_ref, long rec_num, char *str, int str_len)
  35. {
  36.    int  new_base ; 
  37.  
  38.    new_base =  (int) (field_ref >> 16) ;
  39.  
  40.    if ( v4base[new_base].memo_file < 0 ) 
  41.       if ( m3open( new_base ) < 0 )    return -1 ;
  42.  
  43.    return ( m4read(field_ref, rec_num, str, str_len)  ) ;
  44. }
  45.  
  46. m3exist( long field_ref ) 
  47. {
  48.    return( m4exist(field_ref) ) ;
  49. }
  50.  
  51. m3edit( long field_ref, long rec_num, char *editor_name, int max_size )
  52. {
  53. #ifndef NO_SPAWNL
  54.    char *ptr ;
  55.    int   hand, num_read ;
  56.    char  memoedit_ptr[14] ;
  57.  
  58.    if ( max_size <= 0 )  return -1 ;
  59.  
  60.    ptr =  h4alloc( max_size ) ;
  61.    if ( ptr == (char *) 0 )   return 1 ;
  62.  
  63.    if ((num_read = m3read( field_ref, rec_num, ptr, max_size))  < 0 )
  64.    {
  65.       h4free_memory(ptr) ;
  66.       return  num_read ;
  67.    }
  68.  
  69.    strcpy( memoedit_ptr, "M4" ) ;
  70.    if ( (hand = u4temp_create(memoedit_ptr)) < 0 )
  71.    {
  72.       h4free_memory(ptr) ;
  73.       return -1 ;
  74.    }
  75.  
  76.    lseek( hand, 0L, 0 ) ;
  77.    if ( write( hand, ptr, num_read ) != num_read )
  78.    {
  79.       h4free_memory(ptr) ;
  80.       close(hand) ;
  81.       u4error( E_WRITE, memoedit_ptr, (char *) 0 ) ;
  82.       return -1 ;
  83.    }
  84.  
  85.    close( hand ) ;
  86.  
  87.    if ( spawnl( P_WAIT, editor_name, "", memoedit_ptr, (char *) 0 )  < 0)
  88.    {
  89.       h4free_memory(ptr) ;
  90.       close(hand) ;
  91.       u4error( E_EDITOR, editor_name, (char *) 0 ) ;
  92.       return -1 ;
  93.    }
  94.  
  95.    if ( (hand = u4open(memoedit_ptr,0)) < 0 )
  96.    {
  97.       h4free_memory(ptr) ;
  98.       return -1 ;
  99.    }
  100.  
  101.    lseek( hand, 0L, 0 ) ;
  102.    if ( (num_read = read(hand, ptr, max_size)) < 0 )
  103.    {
  104.       h4free_memory(ptr) ;
  105.       close(hand) ;
  106.       u4error( E_READ, memoedit_ptr, (char *) 0 ) ;
  107.       return -1 ;
  108.    }
  109.  
  110.    close(hand) ;
  111.  
  112.    u4remove( memoedit_ptr ) ;
  113.  
  114.    if ( m3write(field_ref, rec_num, ptr, num_read) < 0 )
  115.    {
  116.       h4free_memory(ptr) ;
  117.       return -1 ;
  118.    }
  119.  
  120.    h4free_memory( ptr ) ;
  121. #endif
  122.    return 0 ;
  123. }
  124.  
  125.  
  126. m3write( long field_ref, long rec_num, char *str, int str_len )
  127. {
  128.    BASE  *base_ptr ;
  129.    long   memo_num, new_start_num ;
  130.    int    len_read, rc, len_write, new_base, old_base ;
  131.  
  132.    old_base =  v4cur_base ;
  133.    new_base =  (int) (field_ref >> 16) ;
  134.    base_ptr =  v4base +  new_base ;
  135.  
  136.    if ( base_ptr->memo_file < 0 )
  137.       if ( m3open(new_base) < 0 )   return -1 ;
  138.  
  139.    d4select(new_base) ;
  140.  
  141.    if ( rec_num > d4reccount() )
  142.       d4blank() ;
  143.    else
  144.       if ( (rc = d4go(rec_num)) < 0 )  
  145.       {
  146.          d4select(old_base) ;
  147.          return rc ;
  148.       }
  149.  
  150.    memo_num =   c4atol( f4ptr(field_ref),  f4width(field_ref) ) ;
  151.  
  152.    rc = u4lock(base_ptr->memo_file, 0L, 0x7FFFFFFFL, v4lock_wait) ;
  153.    if ( rc < 0 )
  154.    {
  155.       d4select(old_base) ;
  156.       return rc ;
  157.    }
  158.    if ( str_len >= MEMO_SIZE  && memo_num > 0 )
  159.    {
  160.       char  buf[MEMO_SIZE] ;
  161.       int   read_size, i ;
  162.  
  163.       read_size =  0 ;
  164.  
  165.       lseek( base_ptr->memo_file, memo_num*MEMO_SIZE, 0 ) ;
  166.  
  167.       do
  168.       {
  169.      read_size +=  MEMO_SIZE ;
  170.  
  171.      len_read =  read( base_ptr->memo_file, buf, MEMO_SIZE ) ;
  172.      if ( len_read < 0 ) 
  173.      {
  174.         u4error( E_READ, m4name(new_base), (char *) 0 ) ;
  175.         u4unlock( base_ptr->memo_file, 0L, 0x7FFFFFFFL ) ;
  176.         d4select(old_base) ;
  177.         return -1 ;
  178.      }
  179.      for ( i=0; i< MEMO_SIZE; i++ )
  180.         if ( buf[i] == (char) 0x1A )  break ;
  181.  
  182.       } while ( i>= MEMO_SIZE ) ;  /* Continue if Esc is not located */
  183.  
  184.       if ( read_size <= str_len )  memo_num =  0 ;
  185.    }
  186.    if ( memo_num == 0L )
  187.    {
  188.       lseek( base_ptr->memo_file, 0L, 0 ) ;
  189.       rc =  read( base_ptr->memo_file, (char *) &memo_num,
  190.           (unsigned int) sizeof(memo_num)) ;
  191.       if ( rc != (int) sizeof(memo_num) ) 
  192.       {
  193.      u4error(E_READ, m4name(new_base), (char *) 0);
  194.      u4unlock( base_ptr->memo_file, 0L, 0x7FFFFFFFL ) ;
  195.      d4select(old_base) ;
  196.      return -1 ;
  197.       }
  198.       new_start_num =  memo_num+  (str_len+MEMO_SIZE-1) / MEMO_SIZE ;
  199.  
  200.       lseek( base_ptr->memo_file, 0L, 0 ) ;
  201.       rc =  write( base_ptr->memo_file, (char *) &new_start_num,
  202.            (unsigned int) sizeof(new_start_num)) ;
  203.       if ( rc != (int) sizeof(new_start_num) ||
  204.        write( base_ptr->memo_file, "\032", 1 ) != 1  ) 
  205.       {
  206.      u4error(E_WRITE, m4name(new_base), (char *) 0);
  207.      u4unlock( base_ptr->memo_file, 0L, 0x7FFFFFFFL ) ;
  208.      d4select(old_base) ;
  209.      return -1 ;
  210.       }
  211.    }
  212.  
  213.    lseek( base_ptr->memo_file, MEMO_SIZE* memo_num, 0 ) ;
  214.    len_write =  write( base_ptr->memo_file, str, str_len ) ;
  215.    if ( len_write == str_len)
  216.       rc =  write( base_ptr->memo_file, "\x1A", 1 ) ;
  217.  
  218.    if ( len_write !=  str_len  ||  rc != 1 )
  219.    {
  220.       u4error( E_WRITE, m4name(new_base), (char *) 0 ) ;
  221.       u4unlock( base_ptr->memo_file, 0L, 0x7FFFFFFFL ) ;
  222.       d4select(old_base) ;
  223.       return -1 ;
  224.    }
  225.  
  226.    c4ltoa( memo_num, f4ptr(field_ref), f4width(field_ref) ) ;
  227.  
  228.    if ( rec_num > d4reccount() )
  229.       rc =  d4append() ;
  230.    else
  231.       rc =  d4write(rec_num) ;
  232.  
  233.    if ( rc < 0 )
  234.    {
  235.       u4error( E_WRITE, m4name(new_base), (char *) 0 ) ;
  236.       u4unlock( base_ptr->memo_file, 0L, 0x7FFFFFFFL) ;
  237.       d4select(old_base) ;
  238.       return -1 ;
  239.    }
  240.  
  241.    d4select(old_base) ;
  242.    if ( u4unlock( base_ptr->memo_file, 0L, 0x7FFFFFFFL) < 0)  return -1 ;
  243.  
  244.    return str_len ;
  245. }
  246.