home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c163 / 3.ddi / SCRE_SOU.EXE / U4UPGRAD.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-29  |  24.9 KB  |  1,066 lines

  1.  
  2. /* u4upgrade.c (c)Copyright Sequiter Software Inc., 1990-1991.  All rights reserved.
  3. */
  4.  
  5. #include "u4upgrad.h"
  6.  
  7. C4CODE c4code ;
  8.  
  9. /* Generic reference number allocation and deallocation */
  10.  
  11. static void u4free_ref( int ref, void ***array_pointer, int *num_ref_used )
  12. {
  13.    (*array_pointer)[ref] =  0 ;
  14.    (*num_ref_used)-- ;
  15. }
  16.  
  17. const  int  extra_ref =  10 ;
  18.  
  19. static int u4alloc_ref( void ***array_pointer, int *num_ref_used, int *num_ref )
  20. {
  21.    int new_ref ;
  22.    void **new_list;
  23.  
  24.  
  25.    if ( *num_ref_used < *num_ref )
  26.    {
  27.       for ( new_ref = 0; new_ref < *num_ref; new_ref++ )
  28.       {
  29.          if ( (*array_pointer)[new_ref] == 0 )
  30.             break ;
  31.       }
  32.    }
  33.    else
  34.    {
  35.       new_ref =  *num_ref ;
  36.       new_list = (void **)u4alloc(sizeof(D4DATA)*(*num_ref+extra_ref));
  37.       if (new_list == 0)
  38.          e4severe(e4memory, (char *) 0);
  39.       if ( *array_pointer != 0 )
  40.       {
  41.          memcpy( new_list, *array_pointer, sizeof(void *) * *num_ref ) ;
  42.          u4free( *array_pointer ) ;
  43.       }
  44.       *array_pointer =  new_list ;
  45.       *num_ref +=  extra_ref ;
  46.    }
  47.  
  48.    (*num_ref_used)++ ;
  49.    return new_ref ;
  50. }
  51.  
  52. /* Variables for Index file allocation/deallocation */
  53. static I4INDEX **i4index_list =  0 ;
  54. static int  num_index  =  0 ;
  55. static int  num_index_used  =  0 ;
  56.  
  57.  
  58. /* Data allocation and deallocation */
  59.  
  60. static D4DATA **d4data_list =  0 ;
  61. static int  num_data  =  0 ;
  62. static int  num_data_used =  0 ;
  63. static int  v4cur_base =  -1 ;
  64.  
  65. static char buffer[255];
  66.  
  67. /* Define converted CodeBase 4.2 routines */
  68.  
  69. /* Conversion Routines ----------------------------------------------------- */
  70. double c45atod(char *char_string, int string_len)
  71. {
  72.    return c4atod(char_string,string_len);
  73. }
  74.  
  75. int c45atoi(char *ptr, int n)
  76. {
  77.    return c4atoi(ptr,n);
  78. }
  79.  
  80. long c45atol(char *ptr, int n)
  81. {
  82.    return c4atol(ptr,n);
  83. }
  84.  
  85. char *c45dtoa(double doub_value,int len,int dec)
  86. {
  87.    c4dtoa45(doub_value,buffer,len,dec);
  88.    return buffer;
  89. }
  90.  
  91. char *c45dt_format(char *str_date,char *picture)
  92. {
  93.    a4format(str_date,buffer,picture);
  94.    return buffer;
  95. }
  96.  
  97. int c45dt_julian(char *str_date,double *julian_date)
  98. {
  99.    if ((*julian_date = a4format_mdx(str_date)) == (double)1.0E300) return -2;
  100.    return 0;
  101. }
  102.  
  103. void c45dt_str( char *dbf_date, double *index_date)
  104. {
  105.    c4dt_str(dbf_date,index_date);
  106. }
  107.  
  108. char *c45dt_unformat(char *date_data,char *picture)
  109. {
  110.    a4init(buffer,date_data,picture);
  111.    return buffer;
  112. }
  113.  
  114. void c45encode(char *to,char *from,char *t_to,char *t_from)
  115. {
  116.    c4encode(to,from,t_to,t_from);
  117. }
  118.  
  119. char *c45ltoa(long long_value,char *result_ptr,int result_len)
  120. {
  121.    c4ltoa45(long_value,result_ptr,result_len);
  122.    return result_ptr;
  123. }
  124.  
  125. void c45trim_n(char *str,int n_ch)
  126. {
  127.    c4trim_n(str,n_ch);
  128. }
  129.  
  130. /* Database Routines ------------------------------------------------------- */
  131. static  int  d4alloc_data_file(D4DATA *ptr)
  132. {
  133.    int i_data;
  134.    i_data =  u4alloc_ref( (void ***) &d4data_list, &num_data_used, &num_data ) ;
  135.    d4data_list[i_data] =  ptr;
  136.    return i_data ;
  137. }
  138.  
  139. /* Routine to ensure a data file is selected. */
  140. static void d4select_test(void)
  141. {
  142.    if ( v4cur_base < 0 )
  143.       e4severe( e4result, "No Data File Selected", (char *) 0 ) ;
  144.    if ( d4data_list[v4cur_base] == 0 )
  145.       e4severe( e4result, "No Data File Selected", (char *) 0 ) ;
  146. }
  147.  
  148. D4DATA *d4data_ptr( int ref )
  149. {
  150.    if ( ref < 0 )
  151.    {
  152.       d4select_test() ;
  153.       return d4data_list[v4cur_base] ;
  154.    }
  155.    else
  156.       return d4data_list[ref] ;
  157. }
  158.  
  159. int d45append(void)
  160. {
  161.    int rc;
  162.    d4select_test() ;
  163.    (*d4data_list[v4cur_base]).record_changed = 0;
  164.    rc = d4append_start(d4data_list[v4cur_base],0) ;
  165.    if (rc < 0) return rc;
  166.    if (rc == r4locked) return -2;
  167.    if (rc == r4unique) return -3;
  168.    rc = d4append(d4data_list[v4cur_base]) ;
  169.    if (rc == r4unique) return -3;
  170.    return -(rc<0);
  171. }
  172.  
  173. int d45append_blank()
  174. {
  175.    int rc;
  176.    d4select_test() ;
  177.    rc = d4append_blank(d4data_list[v4cur_base]) ;
  178.    if (rc == r4unique) return -3;
  179.    if (rc == r4locked) return -2;
  180.    return -(rc<0);
  181. }
  182.  
  183. int d45bof()
  184. {
  185.    d4select_test() ;
  186.    return d4bof(d4data_list[v4cur_base]) ;
  187. }
  188.  
  189. int d45bottom()
  190. {
  191.    int rc;
  192.    d4select_test() ;
  193.    rc = d4bottom(d4data_list[v4cur_base]) ;
  194.    if (rc == r4eof) return 3;
  195.    return -(rc!=0);
  196. }
  197.  
  198. long d45buf_init(long start_try,long end_try,long ch_try)
  199. {
  200.    ch_try = ch_try;
  201.    end_try = end_try;
  202.    c4code.mem_size_sort_pool = (unsigned)(start_try > 0xF000 ? 0xF000 : start_try);
  203.    return 0 ;
  204. }
  205.  
  206. int d45buf_total(long n_recs,int max_buffers,int may_lend)
  207. {
  208.    n_recs = n_recs;
  209.    max_buffers = max_buffers;
  210.    may_lend = may_lend;
  211.    return 0;
  212. }
  213.  
  214. int d45buf_unit(long n_recs)
  215. {
  216.    n_recs = n_recs;
  217.    return 0;
  218. }
  219.  
  220. int  d45close(void)
  221. {
  222.    int index_ref, rc=0;
  223.    d4select_test() ;
  224.    /* Close any Index Files, for the data file, opened with 'i4open()' */
  225.    for ( index_ref = 0; index_ref < num_index; index_ref++ )
  226.       if ( i4index_list[index_ref] != 0 )
  227.          if ( i4index_list[index_ref]->data == d4data_list[v4cur_base] )
  228.             if (i45close(index_ref) != 0) rc = -1;
  229.    if (d4close(d4data_list[v4cur_base]) != 0) rc = -1;
  230.    u4free_ref( v4cur_base, (void ***) &d4data_list, &num_data_used ) ;
  231.    v4cur_base =  -1 ;
  232.    return rc;
  233. }
  234.  
  235. int d45close_all()
  236. {
  237.    int i, rc=0;
  238.    for ( i = 0; i < num_data; i++ )
  239.    {
  240.       v4cur_base =  i ;
  241.       if ( d4data_list[v4cur_base] == 0 )  continue ;
  242.       if (d45close() != 0) rc = -1;
  243.    }
  244.    return rc;
  245. }
  246.  
  247. int d45create( char *name, int n_fields, FIELD *fields, int safety )
  248. {
  249.    int temp, i_field;
  250.    F4FIELD_INFO *field_info;
  251.    D4DATA *result;
  252.  
  253.    temp = c4code.safety;
  254.    c4code.safety = safety ;
  255.    field_info =  (F4FIELD_INFO *)u4alloc( sizeof(F4FIELD_INFO) * (n_fields+1));
  256.    if ( field_info == 0 ) {
  257.       e4error( &c4code, e4memory, "Sorting data file", (char *) 0 ) ;
  258.       return(-1);
  259.    }
  260.    for ( i_field = 0; i_field < n_fields; i_field++ )
  261.    {
  262.       field_info[i_field].name =  fields[i_field].name ;
  263.       field_info[i_field].type =  fields[i_field].type ;
  264.  
  265.       if ( fields[i_field].type == 'M' )
  266.          field_info[i_field].len =  10 ;
  267.       else
  268.          field_info[i_field].len =   fields[i_field].width ;
  269.       field_info[i_field].dec =  fields[i_field].decimals ;
  270.    }
  271.  
  272.    if ((result = (D4DATA *)d4create(&c4code, name, field_info,0)) == 0) {
  273.       if (safety == 1 && c4code.error_code == r4no_create) {
  274.          c4code.safety = temp;
  275.          return -1;
  276.       }
  277.       e4error( &c4code, e4create, name, (char *) 0);
  278.       return(-1);
  279.    }
  280.    c4code.safety = temp;
  281.    u4free(field_info);
  282.    return(v4cur_base = d4alloc_data_file(result));
  283. }
  284.  
  285. int d45delete( long rec )
  286. {
  287.    d4go(d4data_list[v4cur_base], rec ) ;
  288.    d4delete(d4data_list[v4cur_base]) ;
  289.    return 0 ;
  290. }
  291.  
  292. int d45deleted()
  293. {
  294.    d4select_test() ;
  295.    return (d4deleted(d4data_list[v4cur_base]) > 0) ;
  296. }
  297.  
  298. int d45eof()
  299. {
  300.    d4select_test() ;
  301.    return d4eof(d4data_list[v4cur_base]) ;
  302. }
  303.  
  304. int d45flush(int ref)
  305. {
  306.    d45select( ref ) ;
  307.    return d4flush_all(d4data_list[v4cur_base]) ;
  308. }
  309.  
  310. int d45go( long rec )
  311. {
  312.    int rc;
  313.    if ( rec < 1 )
  314.    {
  315.       d4blank(d4data_list[v4cur_base]) ;
  316.       return 1 ;
  317.    }
  318.    d4select_test() ;
  319.    rc = d4go(d4data_list[v4cur_base], rec ) ;
  320.    if (rc == r4locked) return -2;
  321.    return -(rc!=0);
  322. }
  323.  
  324. int  d45init()
  325. {
  326.    d4init(&c4code);
  327.    c4code.auto_open = 0;
  328.    return 0 ;
  329. }
  330.  
  331. int  d45initialize( int num_base, int num_index, int num_blocks, int eval_space, long buf_bytes )
  332. {
  333.    d4init(&c4code);
  334.    c4code.auto_open = 0;
  335.    eval_space = eval_space; /* No corresponding setting in C4CODE structure */
  336.    c4code.mem_start_data = num_base;
  337.    c4code.mem_start_index = num_index;
  338.    c4code.mem_start_block = num_blocks;
  339.    c4code.mem_size_sort_pool = (unsigned)(buf_bytes > 0xF000 ? 0xF000 : buf_bytes);
  340.    return 0 ;
  341. }
  342.  
  343. int d45init_undo()
  344. {
  345.    return d4init_undo(&c4code);
  346. }
  347.  
  348. int  d45lock( long lock_code, int do_wait )
  349. {
  350.    int temp, rc;
  351.    d4select_test() ;
  352.    temp = c4code.wait;
  353.    c4code.wait =  do_wait ;
  354.    if ( lock_code > 0 )
  355.       rc =  d4lock(d4data_list[v4cur_base],lock_code) ;
  356.    else if ( lock_code < 0 )
  357.            rc =  d4lock_file(d4data_list[v4cur_base]) ;
  358.         else
  359.            rc = d4lock_append(d4data_list[v4cur_base]);
  360.    c4code.wait = temp;
  361.    if (rc == r4locked) return -2;
  362.    return -(rc<0);
  363. }
  364.  
  365. int  d45locked( long rec_code )
  366. {
  367.    d4select_test() ;
  368.    if ( rec_code > 0 )
  369.       return d4lock_test(d4data_list[v4cur_base], rec_code ) ;
  370.    if ( rec_code < 0 )
  371.       return d4lock_test_file(d4data_list[v4cur_base]) ;
  372.    return d4lock_test_append(d4data_list[v4cur_base]) ;
  373. }
  374.  
  375. int d45lock_all(int do_wait,int free_buffers)
  376. {
  377.    int temp, rc;
  378.    free_buffers = free_buffers; /* Tag file blocks are always flushed */
  379.    temp = c4code.wait;
  380.    c4code.wait = do_wait;
  381.    rc = d4lock_file(d4data_list[v4cur_base]);
  382.    c4code.wait = temp;
  383.    if (rc == r4locked) return -2;
  384.    if (rc < 0) return -1;
  385.    c4code.wait = do_wait;
  386.    rc = d4lock_index(d4data_list[v4cur_base]);
  387.    c4code.wait = temp;
  388.    if (rc == r4locked) return -2;
  389.    if (rc < 0) return -1;
  390.    return 0;
  391. }
  392.  
  393. int d45lock_code( int lock_code )
  394. {
  395.    int return_value =  c4code.read_lock;
  396.    switch( lock_code )
  397.    {
  398.       case 1:
  399.          c4code.read_lock =  1 ;
  400.          break ;
  401.  
  402.       case 2:
  403.          c4code.read_lock =  0 ;
  404.          break ;
  405.  
  406.       case -2:
  407.          break ;
  408.  
  409.       #ifdef DEBUG
  410.       default:
  411.     c4code.error( e4parm, "This operation is not supported under CodeBase 4.5", (char *) 0 ) ;
  412.       #endif
  413.    }
  414.    return return_value ;
  415. }
  416.  
  417. int d45lock_wait( int do_wait )
  418. {
  419.    int return_wait_code = c4code.wait ;
  420.    if ( do_wait >= 0 )
  421.       c4code.wait = do_wait ;
  422.    return return_wait_code ;
  423. }
  424.  
  425. int  d45lseek( long rec )
  426. {
  427.    d4select_test() ;
  428.    return (int)d4rec_pos(d4data_list[v4cur_base], rec ) ;
  429. }
  430.  
  431. int  d45pack()
  432. {
  433.    int rc;
  434.    d4select_test() ;
  435.    rc = d4pack(d4data_list[v4cur_base]);
  436.    if (rc == r4locked) return -2;
  437.    return -(rc!=0);
  438. }
  439.  
  440. int  d45recall( long rec )
  441. {
  442.    d4select_test();
  443.    d45go( rec ) ;
  444.    d4recall(d4data_list[v4cur_base]) ;
  445.    d45write( rec ) ;
  446.    return 0 ;
  447. }
  448.  
  449. long  d45reccount()
  450. {
  451.    d4select_test() ;
  452.    return d4reccount(d4data_list[v4cur_base]) ;
  453. }
  454.  
  455. long  d45recno()
  456. {
  457.    d4select_test() ;
  458.    return d4recno(d4data_list[v4cur_base]);
  459. }
  460.  
  461. int  d45ref( char *name )
  462. {
  463.    int i_data;
  464.    char *temp_ptr, *temp;
  465.    temp_ptr = temp = (char *)u4alloc(strlen(name)+1);
  466.    for ( i_data = 0; i_data< num_data; i_data++ )
  467.       if ( d4data_list[i_data] != 0 )
  468.       {
  469.          memcpy(temp,name,strlen(name));
  470.          *(temp+strlen(name)) = 0;
  471.          while (*temp_ptr != ' ' && *temp_ptr) temp_ptr++;
  472.          *temp_ptr = 0;
  473.          temp_ptr = d4alias(d4data_list[i_data]);
  474.          if (strlen(temp) == strlen(temp_ptr))
  475.             if (!memcmp(temp_ptr,temp,(size_t)strlen(temp))) {
  476.                u4free(temp);
  477.                return i_data ;
  478.             }
  479.       }
  480.    u4free(temp);
  481.    return -1 ;
  482. }
  483.  
  484. int  d45seek_double( double d )
  485. {
  486.    int rc;
  487.    d4select_test() ;
  488.    rc = d4seek_double(d4data_list[v4cur_base], d ) ;
  489.    if (rc == r4entry || rc == r4locked) return -1;
  490.    return rc;
  491. }
  492.  
  493. int  d45seek_str( char *str )
  494. {
  495.    int rc;
  496.    d4select_test() ;
  497.    rc = d4seek(d4data_list[v4cur_base], str ) ;
  498.    if (rc == r4entry || rc == r4locked) return -1;
  499.    return rc;
  500. }
  501.  
  502. int  d45select( int base_ref )
  503. {
  504.    int return_ref;
  505.    if ( base_ref < 0 )
  506.       return v4cur_base ;
  507.    return_ref =  v4cur_base ;
  508.    v4cur_base =  base_ref ;
  509.    d4select_test() ;
  510.    return return_ref ;
  511. }
  512.  
  513. int  d45skip( long num_rec )
  514. {
  515.    int rc;
  516.    d4select_test() ;
  517.    rc = d4skip(d4data_list[v4cur_base], num_rec ) ;
  518.    if (rc == r4bof) return 1;
  519.    if (rc == r4locked) return -2;
  520.    if (rc == r4entry) return -3;
  521.    return rc;
  522. }
  523.  
  524. int  d45top()
  525. {
  526.    int rc ;
  527.    d4select_test() ;
  528.    rc = d4top(d4data_list[v4cur_base]) ;
  529.    if ( rc == r4locked ) return -2 ;
  530.    return rc ;
  531. }
  532.  
  533. int  d45unlock( long lock_code )
  534. {
  535.    d4select_test() ;
  536.    if ( lock_code > 0 )
  537.       return d4unlock_records(d4data_list[v4cur_base]) ;
  538.    if ( lock_code < 0 )
  539.       return -(d4unlock_all(d4data_list[v4cur_base]) != 0) ;
  540.    return d4unlock_append(d4data_list[v4cur_base]) ;
  541. }
  542.  
  543. int  d45use( char *name )
  544. {
  545.    D4DATA *result;
  546.    if ((result = d4open(&c4code, name)) == 0)
  547.       return -1;
  548.    v4cur_base =  d4alloc_data_file(result) ;
  549.    return  v4cur_base ;
  550. }
  551.  
  552. int  d45use_excl( char *name )
  553. {
  554.    int rc ;
  555.    if ( (rc = d45use(name)) < 0 ) return rc ;
  556.    if ( d45lock(-1L,1) < 0 )  return -1 ;
  557.    return rc ;
  558. }
  559.  
  560. int  d45write( long rec )
  561. {
  562.    int rc;
  563.    d4select_test() ;
  564.    if ( rec <= 0 ) return d45append() ;
  565.    rc = d4write(d4data_list[v4cur_base], rec ) ;
  566.    if (rc == r4locked) return -2;
  567.    if (rc == r4unique) return -3;
  568.    return -(rc<0);
  569. }
  570.  
  571. int  d45zap( long start, long end )
  572. {
  573.    int rc;
  574.    d4select_test() ;
  575.    rc = d4zap(d4data_list[v4cur_base], start, end ) ;
  576.    if (rc == r4locked) return -2;
  577.    return -(rc!=0);
  578. }
  579.  
  580. /* Expression Evaluation Routines ------------------------------------------ */
  581. static int e4last_length;
  582. static int e4last_type;
  583.  
  584. void *e45eval( char *expr_ptr )
  585. {
  586.    E4EXPR *expr;
  587.    char **result_ptr_ptr = 0;
  588.    d4select_test() ;
  589.    expr = e4parse(d4data_list[v4cur_base],expr_ptr) ;
  590.    if (expr == 0) {
  591.       e4free(expr);
  592.       return 0;
  593.    }
  594.    if ((e4last_length = e4vary(expr,result_ptr_ptr)) < 0) {
  595.       e4free(expr);
  596.       return 0;
  597.    }
  598.    e4last_type = e4type(expr);
  599.    e4free(expr);
  600.    return *result_ptr_ptr;
  601. }
  602.  
  603. void *e45exec(char *compile_ptr)
  604. {
  605.    char **result_ptr_ptr = 0;
  606.    if ((e4last_length = e4vary((E4EXPR *)compile_ptr,result_ptr_ptr)) < 0) return 0;
  607.    e4last_type = e4type((E4EXPR *)compile_ptr);
  608.    return *result_ptr_ptr;
  609. }
  610.  
  611. int e45length()
  612. {
  613.    return e4last_length;
  614. }
  615.  
  616. int e45parse(char *expr_ptr,char **compile_ptr)
  617. {
  618.    *compile_ptr = (char *)e4parse(d4data_list[v4cur_base],expr_ptr);
  619.    if (*compile_ptr == 0) return -1;
  620.    return strlen(((E4EXPR *)*compile_ptr)->parsed);
  621. }
  622.  
  623. char *e45string(char *compile_ptr)
  624. {
  625.    char **result_ptr_ptr = 0;
  626.    if ((e4last_length = e4vary((E4EXPR *)compile_ptr,result_ptr_ptr)) < 0) return 0;
  627.    e4last_type = e4type((E4EXPR *)compile_ptr);
  628.    return *result_ptr_ptr;
  629. }
  630.  
  631. char e45type()
  632. {
  633.    if (e4last_type == t4num_str) return 'n';
  634.    if (e4last_type == t4num_doub) return 'N';
  635.    if (e4last_type == t4date_doub) return 'd';
  636.    if (e4last_type == t4date_str) return 'D';
  637.    if (e4last_type == t4str) return 'C';
  638.    return 'L';
  639. }
  640.  
  641. /* Field Routines ---------------------------------------------------------- */
  642. F4FIELD *f4field_ptr( long field_ref )
  643. {
  644.    int    f_num, b_ref ;
  645.    b_ref = (int) (field_ref>>16) ;
  646.    if (b_ref < 0 ) return( (F4FIELD *) 0 ) ;
  647.    f_num =  (int) (field_ref & 0xFFFF) ;
  648.    return (d4data_list[b_ref]->fields)+f_num ;
  649. }
  650.  
  651. long  f45ref( char *field_name )
  652. {
  653.    int f_num;
  654.    d4select_test() ;
  655.    f_num =  d4field_number(d4data_list[v4cur_base], field_name ) - 1;
  656.    if ( f_num < 0 )  return -1L ;
  657.    return( ((long)f_num) | (((long)v4cur_base)<<16) ) ;
  658. }
  659.  
  660. char  f45char( long f_ref )
  661. {
  662.    return((char)f4char(f4field_ptr(f_ref))) ;
  663. }
  664.  
  665. void f45r_char( long f_ref, char chr )
  666. {
  667.    f4assign_char(f4field_ptr(f_ref),chr);
  668. }
  669.         
  670. int f45decimals( long f_ref)
  671. {
  672.    return f4decimals(f4field_ptr(f_ref)) ;
  673. }
  674.  
  675. double  f45double( long f_ref )
  676. {
  677.    return f4double(f4field_ptr(f_ref)) ;
  678. }
  679.  
  680. void f45r_double( long f_ref, double d_value )
  681. {
  682.    f4assign_double(f4field_ptr(f_ref),d_value) ;
  683. }
  684.  
  685. int  f45int( long f_ref )
  686. {
  687.    return f4int( f4field_ptr(f_ref) ) ;
  688. }
  689.  
  690. void f45r_int( long f_ref, int i_value )
  691. {
  692.    f4assign_int(f4field_ptr(f_ref),i_value) ;
  693. }
  694.  
  695. long  f45long( long f_ref )
  696. {
  697.    return f4long( f4field_ptr(f_ref) ) ;
  698. }
  699.  
  700. void f45r_long( long f_ref, long l_value )
  701. {
  702.    f4assign_long(f4field_ptr(f_ref), l_value) ;
  703. }
  704.  
  705. char *f45name( long f_ref )
  706. {
  707.    return f4name(f4field_ptr(f_ref)) ;
  708. }
  709.  
  710. int f45num_fields()
  711. {
  712.    d4select_test() ;
  713.    return d4num_fields(d4data_list[v4cur_base]) ;
  714. }
  715.  
  716. char * f45ptr( long f_ref)
  717. {
  718.    d4select_test();
  719.    return f4ptr(f4field_ptr(f_ref));
  720. }
  721.  
  722. void *  f45record()
  723. {
  724.    d4select_test() ;
  725.    return d4record(d4data_list[v4cur_base]) ;
  726. }
  727.  
  728. int  f45record_width()
  729. {
  730.    d4select_test() ;
  731.    return (int)d4record_width(d4data_list[v4cur_base]) ;
  732. }
  733.  
  734. long  f45j_ref( int j_ref )
  735. {
  736.    return( (((long) v4cur_base) << 16)  |   (long) j_ref-1 ) ;
  737. }
  738.  
  739. char * f45str( long f_ref)
  740. {
  741.    return f4str(f4field_ptr(f_ref));
  742. }
  743.  
  744. int  f45ncpy( long f_ref, char *ptr, int n )
  745. {
  746.    return f4ncpy(f4field_ptr(f_ref),ptr,n);
  747. }
  748.  
  749. void  f45r_str( long f_ref, char *str )
  750. {
  751.    f4assign(f4field_ptr(f_ref),str) ;
  752. }
  753.         
  754. int f45true( long f_ref)
  755. {
  756.    return f4true(f4field_ptr(f_ref)) ;
  757. }
  758.         
  759. char f45type( long f_ref)
  760. {
  761.    return (char)f4type(f4field_ptr(f_ref)) ;
  762. }
  763.  
  764. int f45width( long f_ref)
  765. {
  766.    return (int)f4len(f4field_ptr(f_ref)) ;
  767. }
  768.  
  769. /* Memory Handling Routines ------------------------------------------------ */
  770. char *h45alloc(unsigned int num_bytes)
  771. {
  772.    return (char *)u4alloc(num_bytes);
  773. }
  774.  
  775. void h45free_memory(void *ptr)
  776. {
  777.    u4free(ptr);
  778. }
  779.  
  780. /* Index Routines ---------------------------------------------------------- */
  781.  
  782. I4INDEX *i4index_ptr( int ref )
  783. {
  784.    return i4index_list[ref] ;
  785. }
  786.  
  787. static void i4exist_test(int ref)
  788. {
  789.    if ( ref < 0 )
  790.       e4severe( e4result, "Illegal Index File Reference Number", (char *) 0 ) ;
  791.    if ( i4index_list[ref] == 0 )
  792.       e4severe( e4result, "Illegal Index File Reference Number", (char *) 0 ) ;
  793. }
  794.  
  795. int i45add(int index_ref,char *key_ptr,long rec_num)
  796. {
  797.    int rc;
  798.    rc = i4lock(i4index_list[index_ref]);
  799.    if (rc == r4locked) return -2;
  800.    if (rc < 0) return -1;
  801.    rc = t4add((T4TAG *)i4index_list[index_ref]->tags.selected,key_ptr,rec_num);
  802.    if (rc == r4unique) return 1;
  803.    if (rc == 0) return 0;
  804.    return -1;
  805. }
  806.  
  807. int i45bottom(int index_ref)
  808. {
  809.    int rc;
  810.    rc = t4bottom((T4TAG *)i4index_list[index_ref]->tags.selected);
  811.    if (t4eof((T4TAG *)i4index_list[index_ref]->tags.selected)) return 3;
  812.    if (rc == r4locked) return -2;
  813.    return -(rc<0);
  814. }
  815.  
  816. long i45check(int index_ref)
  817. {
  818.    int rc;
  819.    rc = i4check((I4INDEX *)i4index_list[index_ref]);
  820.    if (rc == r4locked) return -2;
  821.    return -(rc!=0);
  822. }
  823.  
  824. int  i45close( int ref )
  825. {
  826.    int rc;
  827.    i4exist_test(ref) ;
  828.    rc = i4close(i4index_list[ref]);
  829.    d4data_list[v4cur_base]->indexes.selected = 0;
  830.    u4free_ref( ref, (void ***) &i4index_list, &num_index_used ) ;
  831.    return rc ;
  832. }
  833.  
  834. char *i45eval(int index_ref)
  835. {
  836.    char **result_ptr_ptr = 0;
  837.    T4TAG *temp;
  838.    temp = (T4TAG *) i4index_list[index_ref]->tags.selected;
  839.    e4vary((E4EXPR *)temp->expr,result_ptr_ptr);
  840.    return *result_ptr_ptr;
  841. }
  842.  
  843. int i45free(int index_ref)
  844. {
  845.    int rc;
  846.    if (i4lock(i4index_list[index_ref]) != 0) return -1;
  847.    rc = t4free_all((T4TAG *)i4index_list[index_ref]->tags.selected);
  848.    if (i4unlock(i4index_list[index_ref]) != 0) return -1;
  849.    return -(rc<0);
  850. }
  851.  
  852. int i45go(int index_ref,char *key_ptr,long record_number)
  853. {
  854.    int rc;
  855.    rc = t4go((T4TAG *)i4index_list[index_ref]->tags.selected,key_ptr,record_number);
  856.    if (rc == r4after) return 2;
  857.    if (rc == r4found) return 1;
  858.    if (rc == r4locked) return -2;
  859.    return -(rc<0);
  860. }
  861.  
  862. int i45index( char *name, char *expr, int unique, int safety )
  863. {
  864.    int ref, temp;
  865.    T4TAG_INFO *tag_info;
  866.    char buf[13];
  867.    d4select_test() ;
  868.    ref =  u4alloc_ref( (void ***) &i4index_list, &num_index_used, &num_index ) ;
  869.    tag_info = (T4TAG_INFO *)u4alloc(sizeof(T4TAG_INFO) * 2);
  870.    memset( tag_info, 0, sizeof(T4TAG_INFO)*2 ) ;
  871.    u4name_piece(buf, sizeof(buf), name, 0, 1);
  872.    tag_info[0].name = buf ;
  873.    tag_info[0].expression =  expr ;
  874.    tag_info[0].unique =  e4unique * unique ;
  875.    temp = c4code.safety;
  876.    c4code.safety = safety;
  877.    i4index_list[ref] = (I4INDEX *)i4create(d4data_list[v4cur_base],name,tag_info);
  878.    c4code.safety = temp;
  879.    u4free(tag_info);
  880.    if (i4index_list[ref] == 0)
  881.       if (c4code.error_code == r4locked) return -2;
  882.       else return -1;
  883.    d4data_list[v4cur_base]->indexes.selected = i4index_list[ref];
  884.    return ref ;
  885. }
  886.  
  887. int i45lock( int ref, int wait )
  888. {
  889.    int rc, temp;
  890.    i4exist_test(ref) ;
  891.    temp = c4code.wait;
  892.    c4code.wait = wait;
  893.    rc = i4lock(i4index_list[ref]) ;
  894.    c4code.wait = temp;
  895.    if ( rc == r4locked )
  896.       return -2 ;
  897.    return rc ;
  898. }
  899.  
  900. int  i45open( char *name )
  901. {
  902.    int ref;
  903.    d4select_test() ;
  904.    ref =  u4alloc_ref( (void ***) &i4index_list, &num_index_used, &num_index ) ;
  905.    i4index_list[ref] = (I4INDEX *)i4open(d4data_list[v4cur_base],name);
  906.    if ( i4index_list[ref] == 0 ) return -1;
  907.    d4data_list[v4cur_base]->indexes.selected = i4index_list[ref];
  908.    return ref ;
  909. }
  910.  
  911. int i45ref( char *name )
  912. {
  913.    int i_index;
  914.    char *temp_ptr, *temp;
  915.    temp_ptr = temp = (char *)u4alloc(strlen(name)+1);
  916.    for ( i_index = 0; i_index< num_index; i_index++ )
  917.       if ( i4index_list[i_index] != 0 )
  918.       {
  919.          memcpy(temp,name,strlen(name));
  920.          *(temp+strlen(name)) = 0;
  921.          while (*temp_ptr != ' ' && *temp_ptr) temp_ptr++;
  922.          *temp_ptr = 0;
  923.          temp_ptr = i4index_list[i_index]->file.name;
  924.          if (strlen(temp) == strlen(temp_ptr))
  925.  
  926.             if (!memcmp(temp_ptr,temp,(size_t)strlen(temp))) {
  927.                u4free(temp);
  928.                return i_index ;
  929.             }
  930.       }
  931.    u4free(temp);
  932.    return -1 ;
  933. }
  934.  
  935. int i45reindex( int ref )
  936. {
  937.    int rc;
  938.    i4exist_test(ref) ;
  939.    rc = i4reindex(i4index_list[ref]) ;
  940.    if (rc == r4locked) return -2;
  941.    return -(rc!=0);
  942. }
  943.  
  944. int i45remove(int index_ref,char *key_ptr,long rec_num)
  945. {
  946.    int rc;
  947.    rc = i4lock(i4index_list[index_ref]);
  948.    if (rc == r4locked) return -2;
  949.    if (rc < 0) return -1;
  950.    rc = t4remove((T4TAG *)i4index_list[index_ref]->tags.selected,key_ptr,rec_num);
  951.    if (rc == r4entry) return 1;
  952.    return -(rc<0);
  953. }
  954.  
  955. int i45seek(int index_ref,void *key_ptr)
  956. {
  957.    int rc;
  958.    rc = t4seek((T4TAG *)i4index_list[index_ref]->tags.selected,key_ptr,strlen((char *)key_ptr));
  959.    if (t4eof((T4TAG *)i4index_list[index_ref]->tags.selected)) return 3;
  960.    if (rc == r4after) return 2;
  961.    if (rc == r4locked) return -2;
  962.    return -(rc<0);
  963. }
  964.  
  965. int i45seek_ref()
  966. {
  967.    I4INDEX *selected;
  968.    d4select_test() ;
  969.    selected =  (I4INDEX *) d4data_list[v4cur_base]->indexes.selected ;
  970.    if ( selected == 0 )  return -1 ;
  971.    return i45ref(selected->file.name);
  972. }
  973.  
  974. int i45select( int ref )
  975. {
  976.    I4INDEX *i_ptr, *was_selected;
  977.    if (ref == -1)
  978.       i_ptr = 0;
  979.    else {
  980.       i4exist_test(ref) ;
  981.       i_ptr = i4index_list[ref] ;
  982.    }
  983.    was_selected = (I4INDEX *)d4data_list[v4cur_base]->indexes.selected;
  984.    d4data_list[v4cur_base]->indexes.selected = i_ptr;
  985.    if ( was_selected == 0 )  return -1 ;
  986.    return i45ref( was_selected->file.name ) ;
  987. }
  988.  
  989. long i45skip(int index_ref,long n)
  990. {
  991.    long rc;
  992.    if (n == 0) return 0;
  993.    rc = t4skip((T4TAG *)i4index_list[index_ref]->tags.selected,n);
  994.    if (c4code.error_code < 0) return -n;
  995.    return rc;
  996. }
  997.  
  998. int i45top(int index_ref)
  999. {
  1000.    int rc;
  1001.    rc = t4top((T4TAG *)i4index_list[index_ref]->tags.selected);
  1002.    if (rc == r4locked) return -2;
  1003.    if (t4eof((T4TAG *)i4index_list[index_ref]->tags.selected)) return 3;
  1004.    return rc;
  1005. }
  1006.  
  1007. char i45type( int ref )
  1008. {
  1009.    int i_type;
  1010.    i4exist_test(ref) ;
  1011.    i_type = ((T4TAG *)(i4index_list[ref]->tags.selected))->expr->type;
  1012.    if (i_type == t4num_doub) return 'N';
  1013.    if (i_type == t4date_doub) return 'D';
  1014.    return 'C';
  1015. }
  1016.  
  1017. int i45unlock( int ref )
  1018. {
  1019.    i4exist_test(ref) ;
  1020.    return i4unlock(i4index_list[ref]) ;
  1021. }
  1022.  
  1023. void i45unselect()
  1024. {
  1025.    d4select_test() ;
  1026.    d4data_list[v4cur_base]->indexes.selected = 0;
  1027. }
  1028.  
  1029. /* Memo Routines ----------------------------------------------------------- */
  1030. int m45read(long field_ref,long rec_num,char *str,int str_len)
  1031. {
  1032.    int rc;
  1033.    if (rec_num > d4reccount(d4data_list[v4cur_base])) {
  1034.       *str = '\0';
  1035.       d4blank(d4data_list[v4cur_base]);
  1036.       return 0;
  1037.    }
  1038.    rc = d4go(d4data_list[v4cur_base],rec_num);
  1039.    if (rc == r4locked) return -2;
  1040.    if (rc != 0) return -1;
  1041.    rc = m4ncpy(f4field_ptr(field_ref),str,str_len);
  1042.    if (rc > 0) return rc;
  1043.    if (c4code.error_code == 0) return 0;
  1044.    if (c4code.error_code == r4locked) return -2;
  1045.    return -1;
  1046. }
  1047.  
  1048. int m45write(long field_ref,long rec_num,char *str,int str_len)
  1049. {
  1050.    int rc;
  1051.    if (rec_num > d4reccount(d4data_list[v4cur_base]))
  1052.       rc = d4append_start(d4data_list[v4cur_base],0);
  1053.    else
  1054.       rc = d4go(d4data_list[v4cur_base],rec_num);
  1055.    if (rc == r4locked) return -2;
  1056.    if (rc != 0) return -1;
  1057.    rc = m4assign_n(f4field_ptr(field_ref),str,str_len);
  1058.    if (rc != 0) return -1;
  1059.    if (rec_num > d4reccount(d4data_list[v4cur_base])) {
  1060.       rc = d4append(d4data_list[v4cur_base]);
  1061.       if (rc == r4locked) return -2;
  1062.    }
  1063.    if (rc < 0) return -1;
  1064.    return m4len(f4field_ptr(field_ref));
  1065. }
  1066.