home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 8 / CDASC08.ISO / VRAC / GT1800S.ZIP / DRAM4.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-21  |  6.6 KB  |  265 lines

  1. #include "define.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <dir.h>
  5. #include <dos.h>
  6. #include <fcntl.h>
  7. #include <io.h>
  8. #include <string.h>
  9. #include <ctype.h>
  10. #include <alloc.h>
  11. #include <sys\stat.h>
  12. #include <errno.h>
  13. #include "futil.h"
  14.  
  15. #ifdef DEBUG
  16.     extern void stack_probe(void);
  17. #endif
  18.  
  19. #ifdef HOST
  20. extern INTEGER crc_reg_hi;
  21. extern INTEGER begin_prime;
  22. #endif
  23. extern INTEGER hash_value;
  24. extern INTEGER delta_hash;
  25. extern INTEGER hash_first;
  26. extern INTEGER hash_crc;
  27. extern INTEGER search;
  28. extern INTEGER hash_max;
  29.  
  30. extern STRING dat_file[];
  31. extern STRING idx_file[];
  32. extern STRING dat_rec[];
  33.  
  34. /*****
  35.  
  36.     The function 'o_mode()' returns an O_FLAG to be used with an
  37.     _open command.  If DOS 2.xx is in use, then the O_FLAG returned
  38.     unchanged, otherwise it is OR'd with SH_DENYNO, then returned.
  39.  
  40.        omd ..... requested O_FLAG.
  41.  
  42. *****/
  43. int o_mode(int omd)
  44. {
  45.     int md;
  46.  
  47.     md = omd;
  48.     if (_osmajor > 2)
  49.     md |= O_DENYNONE;
  50.     return(md);
  51. }
  52.  
  53. /*****
  54.  
  55.     The function 'crc_calc' takes an open DRAM fcb and calculates
  56.     the CRC-16 from the 'string_key' contained in the fcb.
  57.  
  58.        fcb ..... a open DRAM fcb which must have a valid string_key
  59.              in it.
  60.  
  61. *****/
  62. #ifdef HOST
  63. INTEGER crc_calc(DRAM far *fcb)
  64. {
  65.     REGISTER k;
  66.     STRING far *s;
  67.  
  68.     s = fcb->string_key;
  69.     crc_reg_lo =
  70.       crc_reg_hi = 0;
  71.     FARstrupr((char far *) s);
  72.     for (k = 0; s[k]; k++)
  73.     ccitt_crc_calc(s[k]);
  74.     ccitt_crc_calc(0);
  75.     ccitt_crc_calc(0);
  76.     return(crc_reg_hi);
  77. }
  78.  
  79. void dram_slasher(DRAM far *fcb)
  80. {
  81.     REGISTER k;
  82.  
  83.     if (fcb->dram_path[0]) {
  84.     k = FARstrlen((char far *) (fcb->dram_path)) - 1;
  85.     if (k > 0) {
  86.         if (fcb->dram_path[k] != '\\')
  87.         FARstrcat((char far *) (fcb->dram_path),(char far *) "\\");
  88.     }
  89.     }
  90. }
  91.  
  92. /*****
  93.  
  94.     The function 'build_filenames()' takes a DRAM fcb and
  95.     constructs full pathname file names for both the IDX and CTL
  96.     files.  The fcb must have the filenames and required pathname
  97.     filled in prior to call.  The fcb should not be open, but it
  98.     should be allocated and required information supplied.
  99.  
  100.        fcb ..... an allocated, but unopened, DRAM fcb, which must
  101.              contain valid pathname and filename information.
  102.  
  103. *****/
  104. void build_filenames(DRAM far *fcb)
  105. {
  106.     dram_slasher(fcb);
  107.     FARstrcpy((char far *)dat_file,(char far *)fcb->dram_path);
  108.     FARstrcat((char far *)dat_file,(char far *)fcb->dram_fname);
  109.     strcpy(idx_file,dat_file);
  110.     strcat(dat_file,".CTL");
  111.     strcat(idx_file,".IDX");
  112. }
  113.  
  114. /*****
  115.  
  116.     The function 'open_files()' is used to open both the IDX and CTL
  117.     files based on a valid DRAM fcb.
  118.  
  119.        fcb ..... an allocated, but unopened, DRAM fcb.  The function
  120.              'build_filenames()' must have been called prior to
  121.              this function.
  122.  
  123. *****/
  124. int open_files(DRAM far *fcb)
  125. {
  126.     fcb->idx_handle = _open(idx_file,o_mode(O_RDWR));
  127.     fcb->dat_handle = _open(dat_file,o_mode(O_RDWR));
  128.     if ((fcb->dat_handle > 0) && (fcb->idx_handle > 0)) {
  129.     fcb->dram_status = 1;
  130.     return(0);
  131.     }
  132.     return(1);
  133. }
  134.  
  135. /*****
  136.  
  137.     The function 'dram_create()' will create new CTL and IDX files,
  138.     and then open the fcb prior to returning to the caller.  If files
  139.     are prior existing, this function will error.
  140.  
  141.        fcb ..... an allocated, but unopen, DRAM fcb.  Must have
  142.              concerning the files filled in.  Such as the names,
  143.              pathname, key-type, etc.  See SYSOP.C for example
  144.              of usage.
  145.  
  146. *****/
  147. int dram_create(DRAM far *fcb,int func)
  148. {
  149. //                         unsigned j;
  150.     long idx_size;
  151.     int chunk_size;
  152.     IDX_RECORD far *xp;
  153.     char buffer[2050];
  154.  
  155.     xp = fcb->idx_rec;
  156.     if (! fcb->dram_status) {
  157.     build_filenames(fcb);
  158.     if (_osmajor > 2) {
  159.         fcb->dat_handle = creatnew(dat_file,0);
  160.         fcb->idx_handle = creatnew(idx_file,0);
  161.     }
  162.     else {
  163.         fcb->dat_handle = _creat(dat_file,0);
  164.         fcb->idx_handle = _creat(idx_file,0);
  165.     }
  166.     if ((fcb->dat_handle > 0) && (fcb->idx_handle > 0)) {
  167.         chunk_size = 2048;
  168.         idx_size =
  169.         ((long)(begin_prime + 1)) * ((long)(sizeof(IDX_RECORD)));
  170.         memset(buffer,0,chunk_size);
  171.         FARmemset((char far *) xp,0,sizeof(IDX_RECORD));
  172.         while (idx_size >= chunk_size) {
  173.         writefile(fcb->idx_handle,(STRING far *) buffer,chunk_size);
  174.         idx_size -= chunk_size;
  175.         }
  176.         if (idx_size > 0) {
  177.         chunk_size = (int) idx_size;
  178.         writefile(fcb->idx_handle,(STRING far *) buffer,chunk_size);
  179.         }
  180. //          for (j = 0; j <= begin_prime; j++)
  181. //              writefile(fcb->idx_handle,(STRING far *) xp,sizeof(IDX_RECORD));
  182.         memset(dat_rec,0,fcb->dat_recsize);
  183.         writefile(fcb->dat_handle,(STRING far *) dat_rec,fcb->dat_recsize);
  184.         closefile(fcb->idx_handle,idx_file);
  185.         closefile(fcb->dat_handle,dat_file);
  186.         return((open_files(fcb)));
  187.     }
  188.     }
  189.     return(2);
  190. }
  191.  
  192. /*****
  193.  
  194.     The function 'dram_open()' will open a DRAM fcb which has been
  195.     prior allocated and initialized.
  196.  
  197.        fcb ..... an allocated, but unopen, DRAM fcb.  Must have
  198.              concerning the files filled in.  Such as the names,
  199.              pathname, key-type, etc.  See SYSOP.C for example
  200.              of usage.
  201.  
  202. *****/
  203. int dram_open(DRAM far *fcb,int func)
  204. {
  205.     if (! fcb->dram_status) {
  206.     build_filenames(fcb);
  207.     return((open_files(fcb)));
  208.     }
  209.     return(3);
  210. }
  211.  
  212. /*****
  213.  
  214.     The DRAM technique is based upon HASH TABLE lookup.  The function
  215.     'delta_calc()' is used to calculate the offset used when a collison
  216.     is detected in a hash chain.
  217.  
  218.     Before calling this routine, the 'hash_crc' and 'hash_max' must
  219.     have been set.  The 'hash_crc' contains the CRC-16 of the base
  220.     key, and 'hash_max' contains the size of the HASH TABLE (which
  221.     must be a prime number).
  222.  
  223. *****/
  224. INTEGER delta_calc(void)
  225. {
  226.     INTEGER k;
  227.  
  228.     if (!(k = (hash_crc >> 8) % hash_max))
  229.     k = 1;
  230.     return(k);
  231. }
  232.  
  233. /*****
  234.  
  235.     Read the comments above for the function 'delta_calc()', much of
  236.     this is pertinent for this function also.  The 'start_hash()'
  237.     function is used to initialize some variables, valid values must
  238.     be provided for 'hash_crc' and 'hash_max' prior to entry.
  239.  
  240. *****/
  241. void start_hash(void)
  242. {
  243.     hash_first = hash_value = hash_crc % hash_max;
  244.     delta_hash = delta_calc();
  245.     search = 1;
  246. }
  247.  
  248. /*****
  249.  
  250.     The function 'next_hash()' is used when a collison occurs.  It
  251.     calculates the next position in the HASH TABLE to be examined.
  252.  
  253.     The functions 'start_hash()' must have been executed properly
  254.     before entry to this routine.
  255.  
  256. *****/
  257. INTEGER next_hash(void)
  258. {
  259.     hash_value = (hash_value + delta_hash) % hash_max;
  260.     if (hash_first == hash_value)
  261.     return(0);
  262.     return(1);
  263. }
  264. #endif
  265.