home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / online / source / c / compilers / Tickle-4.0.sit.hqx / Tickle-4.0 / cbtree / src / cbtest.c < prev    next >
C/C++ Source or Header  |  1992-05-09  |  8KB  |  422 lines

  1.  
  2. #include <ioctl.h>
  3. #include <fcntl.h>
  4. #include <stdio.h>
  5.  
  6. #include <db.h>
  7. #include <btree.h>
  8.  
  9. #ifdef DOC
  10.  
  11. typedef struct {
  12.     char *dptr;
  13.     int dsize;
  14.     } datum;
  15.  
  16. cc ... -ldbm ...
  17.  
  18. #endif
  19.  
  20. extern char *malloc();
  21.  
  22. DB            *the_db;
  23. char        dirfile[256];
  24. char        datafile[256];
  25. char        command[256];
  26. char        cmd_str[256];
  27. char        arg1[256];
  28. char        arg2[256];
  29.  
  30. int
  31. my_btree_cmp(p1, p2)
  32. char    *p1, *p2;
  33.     {
  34.     /*fprintf(stderr, "my_btree_cmp: p1 x%lx '%s' p2 x%lx '%s'\n", p1, p1, p2, p2);*/
  35.     return strcmp(p1, p2);
  36.     }
  37.  
  38. main(argc, argv)
  39. int        argc;
  40. char    *argv[];
  41. {
  42. int        done,
  43.         count,
  44.         args,
  45.         strind,
  46.         result,
  47.         promptlen;
  48. DBT        key,
  49.         data;
  50. char    *prompt = "[f/i/l/r/d/o/q] ",
  51.         prefix[128];
  52. BTREEINFO openinfo;
  53.  
  54. #pragma unused (argc, argv)
  55.  
  56.     strcpy(datafile, argv[1]);
  57.     
  58.     openinfo.flags = R_DUP;
  59.     openinfo.cachesize = 0;
  60.     openinfo.compare = my_btree_cmp;    /* use strcmp() */
  61.     openinfo.lorder = BIG_ENDIAN;
  62.     openinfo.psize = 4096;
  63.     
  64.     the_db = btree_open(datafile, O_RDWR | O_CREAT, 0666, &openinfo);
  65.     if (the_db == NULL) {
  66.         fprintf(stderr, "ERROR: opening DB '%s'\n", datafile);
  67.         exit(2);
  68.         }
  69.     
  70.     promptlen = strlen(prompt);
  71.     
  72.     for (done=0; ! done; ) {
  73.     
  74.         printf(prompt); fflush(stdout);
  75.         if (gets(command) == NULL)
  76.             break;
  77.         
  78.         strind = 0;
  79.         if (strncmp(command, prompt, promptlen) == 0)
  80.             strind = promptlen;
  81.         
  82.         args = sscanf(&command[strind], "%s%s%s", cmd_str, arg1, arg2);
  83.         
  84.         switch (cmd_str[0]) {
  85.         
  86.             case 'q': case 'Q':
  87.                 done = 1;
  88.                 break;
  89.             
  90.             case 'o': case 'O':
  91.             
  92.                 do_other_commands();
  93.                 
  94.                 break;
  95.             
  96.             case 'f': case 'F':
  97.                 key.data = arg1;
  98.                 key.size = strlen(arg1);
  99.                 result = (* the_db->get)(the_db, &key, &data, 0);
  100.                 if (result != 0)
  101.                     fprintf(stderr, "Key '%s' fetch failed (%d).\n", arg1, result);
  102.                 else
  103.                     print_datum("FIND", arg1, &data);
  104.                 break;
  105.             
  106.             case 'i': case 'I':
  107.                 key.data = arg1;
  108.                 key.size = strlen(arg1) + 1;
  109.                 data.data = arg2;
  110.                 data.size = strlen(arg2) + 1;
  111.                 result = (* the_db->put)(the_db, &key, &data, R_PUT);
  112.                 if (result != 0)
  113.                     fprintf(stderr, "Key '%s' insert failed (%d).\n", arg1, result);
  114.                 else
  115.                     printf("Key '%s' inserted.\n", arg1);
  116.                 break;
  117.                 
  118.             case 'd': case 'D':
  119.                 key.data = arg1;
  120.                 key.size = strlen(arg1);
  121.                 result = (* the_db->del)(the_db, &key, R_PUT);
  122.                 if (result != 0)
  123.                     fprintf(stderr, "Key '%s' insert failed (%d).\n", arg1, result);
  124.                 else
  125.                     printf("Key '%s' inserted.\n", arg1);
  126.                 break;
  127.  
  128.             case 'r': case 'R':
  129.                 key.data = arg1;
  130.                 key.size = strlen(arg1) + 1;
  131.                 data.data = arg2;
  132.                 data.size = strlen(arg2) + 1;
  133.                 result = (* the_db->put)(the_db, &key, &data, R_PUT);
  134.                 if (result != 0)
  135.                     fprintf(stderr, "Key '%s' insert failed (%d).\n", arg1, result);
  136.                 else
  137.                     printf("Key '%s' inserted.\n", arg1);
  138.                 break;
  139.  
  140.             case 'l': case 'L':
  141.                 result = (* the_db->seq) (the_db, &key, &data, R_FIRST);
  142.                 if (result < 0)
  143.                     {
  144.                     fprintf(stderr, "Error reading first key.\n");
  145.                     }
  146.                 else if (result > 0)
  147.                     {
  148.                     fprintf(stderr, "The database contains no keys.\n");
  149.                     }
  150.                 else
  151.                     {
  152.                     for (count=1; result == 0 ; )
  153.                         {
  154.                         sprintf(prefix, "Item #%02d", count++);
  155.                         print_datum(prefix, key.data, &data);
  156.                         
  157.                         key.size = 0;
  158.                         key.data = "";
  159.                         result = (* the_db->seq) (the_db, &key, &data, R_NEXT);
  160.                         }
  161.                     }
  162.                 break;
  163.             
  164.             }
  165.         
  166.         fflush(stdout);
  167.         }
  168.     
  169.     if ((* the_db->close)(the_db) == -1)
  170.         fprintf(stderr, "ERROR closing DB.\n");
  171.     
  172.     }
  173.  
  174.  
  175. print_datum(message, key, data)
  176. char        *message;
  177. char        *key;
  178. DBT            *data;
  179. {
  180.     printf( "%s%s%s%s ", message,
  181.             ( key==NULL ? ":"    : ": <" ),
  182.             ( key==NULL ? ""    : key ),
  183.             ( key==NULL ? " "    : "> " )
  184.             );
  185.     
  186.     printf("[%d]<%.*s>\n", data->size, data->size, data->data);
  187.     }
  188.  
  189. do_other_commands()
  190.     {
  191.     int        done,
  192.             args,
  193.             strind,
  194.             promptlen;
  195.     DBT        key,
  196.             data;
  197.     char    *prompt = "[h/f/q] ";
  198.  
  199.     promptlen = strlen(prompt);
  200.     
  201.     for (done=0; ! done; ) {
  202.     
  203.         printf(prompt); fflush(stdout);
  204.         if (gets(command) == NULL)
  205.             break;
  206.         
  207.         strind = 0;
  208.         if (strncmp(command, prompt, promptlen) == 0)
  209.             strind = promptlen;
  210.         
  211.         args = sscanf(&command[strind], "%s%s%s", cmd_str, arg1, arg2);
  212.         
  213.         switch (cmd_str[0]) {
  214.         
  215.             case 'q': case 'Q':
  216.                 done = 1;
  217.                 break;
  218.             
  219. #ifdef NEVER_DEFINED
  220.  
  221. #ifdef SDBM
  222.             case 'h': case 'H':
  223.                 hash = 0;
  224.                 printf("NEED TO IMPLEMENT\n");
  225.                 break;
  226. #else
  227.             case 'h': case 'H':
  228.                 data.dptr = arg1;
  229.                 data.dsize = strlen(arg1);
  230.                 hash = dcalchash(data);
  231.                 printf("dcalchash(%s) = %d[x%08lX]\n", arg1, hash, hash);
  232.                 break;
  233. #endif
  234.             
  235. #endif
  236.             
  237.             case 'f': case 'F':
  238.                 {
  239.                 int        limit, i, result;
  240.                 char    keystr[32];
  241.                 char    datastr[64];
  242.                 
  243.                 printf("FILLING DB...\n"); fflush(stdout);
  244.                 
  245.                 limit = atoi(arg2);
  246.                 for (i=atoi(arg1); i<=limit; ++i)
  247.                     {
  248.                     sprintf(keystr, "Key%d", i);
  249.                     sprintf(datastr, "DATA --KEY--> <Key%d>", i);
  250.                     
  251.                     key.data = keystr;
  252.                     key.size = strlen(keystr) + 1;
  253.                     data.data = datastr;
  254.                     data.size = strlen(datastr) + 1;
  255.                     result = (* the_db->put)(the_db, &key, &data, R_PUT);
  256.                     if (result != 0)
  257.                         {
  258.                         fprintf(stderr, "FAILURE[%d]: Key '%s' store.\n", result, keystr);
  259.                         break;
  260.                         }
  261.                     }
  262.  
  263.                 printf("FILLING COMPLETED.\n");
  264.                 }
  265.                 break;
  266.  
  267.             }
  268.         
  269.         fflush(stdout);
  270.         }
  271.     
  272.     }
  273.  
  274. typedef struct {
  275.     char    *ptr;
  276.     char    *file;
  277.     int        line;
  278.     } MALLOC_ITEM;
  279.  
  280. static int            mptr = 0;
  281. static MALLOC_ITEM    mstk[1024];
  282. static int            fptr = 0;
  283. static MALLOC_ITEM    fstk[1024];
  284.  
  285. char *
  286. Malloc(size, file, line)
  287. int        size;
  288. char    *file;
  289. int        line;
  290.     {
  291.     char    *ptr;
  292. #pragma unused (file, line)
  293.  
  294.     ptr = malloc(size);
  295.  
  296.     /*fprintf(stderr, "MALLOC: x%08lX %d bytes %s:%d\n", ptr, size, file, line);*/
  297.     
  298. #ifdef NEVER_DEFINED
  299.     if (ptr != NULL)
  300.         {
  301.         mstk[mptr].ptr = ptr;
  302.         mstk[mptr].file = file;
  303.         mstk[mptr].line = line;
  304.         ++mptr;
  305.         memset(ptr, 0, size);
  306.         }
  307. #endif
  308.     
  309.     return ptr;
  310.     }
  311.  
  312. Free(ptr, file, line)
  313. char    *ptr;
  314. char    *file;
  315. int        line;
  316.     {
  317. #pragma unused (file, line)
  318.     /*fprintf(stderr, "  FREE: x%08lX %s:%d\n", ptr, file, line);*/
  319.     
  320.     free(ptr);
  321.     
  322. #ifdef NEVER_DEFINED
  323.     int        i;
  324.     
  325.     for (i=0; i < mptr; ++i)
  326.         if (mstk[i].ptr == ptr)
  327.             break;
  328.     
  329.     if (i >= mptr)
  330.         {
  331.         fprintf(stderr, "FREE: x%08lX %s:%d NOT ALLOCATED!!\n", ptr, file, line);
  332.         }
  333.     else
  334.         {
  335.         if (i < mptr - 1)
  336.             {
  337.             memmove(&mstk[i], mstk[i+1], ((mptr - (i + 1)) * sizeof(MALLOC_ITEM)) );
  338.             }
  339.         --mptr;
  340.  
  341.         free(ptr);
  342.         }
  343. #endif
  344.     
  345.     }
  346.  
  347. #ifdef NEVER_DEFINED
  348.  
  349. cd {src}bsd-cbtree:
  350. search printf ≈.c
  351. C -mbg full -b3 big.c
  352. C -mbg full -b3 btree.c
  353. C -mbg full -b3 delete.c
  354. C -mbg full -b3 insert.c
  355. C -mbg full -b3 lrucache.c
  356. C -mbg full -b3 lruhash.c
  357. C -mbg full -b3 lrutils.c
  358. C -mbg full -b3 search.c
  359. C -mbg full -b3 seq.c
  360. C -mbg full -b3 split.c
  361. C -mbg full -b3 storage.c
  362. C -mbg full -b3 updutils.c
  363. C -mbg full -b3 utils.c
  364. Lib -o cbtree.o                ∂
  365.     -sg CBTREE=Main            ∂
  366.     big.c.o                    ∂
  367.     btree.c.o                ∂
  368.     delete.c.o                ∂
  369.     insert.c.o                ∂
  370.     lrucache.c.o            ∂
  371.     lruhash.c.o                ∂
  372.     lrutils.c.o                ∂
  373.     search.c.o                ∂
  374.     seq.c.o                    ∂
  375.     split.c.o                ∂
  376.     storage.c.o                ∂
  377.     updutils.c.o            ∂
  378.     utils.c.o
  379. cp cbtree.o {clibraries}cbtree.o
  380. cp cbtree.o {clibraries}cbtreeMDBG.o
  381.  
  382. search db.h ≈.c
  383.  
  384. C -mbg full -b3 big.c
  385. C -mbg full -b3 btree.c
  386. C -mbg full -b3 delete.c
  387. C -mbg full -b3 insert.c
  388. C -mbg full -b3 lrucache.c
  389. C -mbg full -b3 lruhash.c
  390. C -mbg full -b3 lrutils.c
  391. C -mbg full -b3 search.c
  392. C -mbg full -b3 seq.c
  393. C -mbg full -b3 split.c
  394. C -mbg full -b3 storage.c
  395. C -mbg full -b3 updutils.c
  396. C -mbg full -b3 utils.c
  397. C -mbg full -b3 {active}
  398. Link -o cbtest -w            ∂
  399.     -t MPST -c 'MPS '        ∂
  400.     {active}.o                ∂
  401.     big.c.o                    ∂
  402.     btree.c.o                ∂
  403.     delete.c.o                ∂
  404.     insert.c.o                ∂
  405.     lrucache.c.o            ∂
  406.     lruhash.c.o                ∂
  407.     lrutils.c.o                ∂
  408.     search.c.o                ∂
  409.     seq.c.o                    ∂
  410.     split.c.o                ∂
  411.     storage.c.o                ∂
  412.     updutils.c.o            ∂
  413.     utils.c.o                ∂
  414.     {clibraries}dprintf_drvr.o    ∂
  415.     {clibraries}stat.o        ∂
  416.     {libraries}runtime.o    ∂
  417.     {libraries}interface.o    ∂
  418.     {clibraries}stdclib.o
  419. cbtest testME5
  420.  
  421. #endif
  422.