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 / M4THREE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-10-13  |  5.9 KB  |  262 lines

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