home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / ixemul-45.0-src.tgz / tar.out / contrib / ixemul / db / recno / rec_get.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  7KB  |  309 lines

  1. /*    $NetBSD: rec_get.c,v 1.7 1995/02/27 13:24:57 cgd Exp $    */
  2.  
  3. /*-
  4.  * Copyright (c) 1990, 1993, 1994
  5.  *    The Regents of the University of California.  All rights reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions
  9.  * are met:
  10.  * 1. Redistributions of source code must retain the above copyright
  11.  *    notice, this list of conditions and the following disclaimer.
  12.  * 2. Redistributions in binary form must reproduce the above copyright
  13.  *    notice, this list of conditions and the following disclaimer in the
  14.  *    documentation and/or other materials provided with the distribution.
  15.  * 3. All advertising materials mentioning features or use of this software
  16.  *    must display the following acknowledgement:
  17.  *    This product includes software developed by the University of
  18.  *    California, Berkeley and its contributors.
  19.  * 4. Neither the name of the University nor the names of its contributors
  20.  *    may be used to endorse or promote products derived from this software
  21.  *    without specific prior written permission.
  22.  *
  23.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  24.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  27.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  29.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  30.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  32.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  33.  * SUCH DAMAGE.
  34.  */
  35.  
  36. #if defined(LIBC_SCCS) && !defined(lint)
  37. #if 0
  38. static char sccsid[] = "@(#)rec_get.c    8.5 (Berkeley) 6/20/94";
  39. #else
  40. static char rcsid[] = "$NetBSD: rec_get.c,v 1.7 1995/02/27 13:24:57 cgd Exp $";
  41. #endif
  42. #endif /* LIBC_SCCS and not lint */
  43.  
  44. #include <sys/types.h>
  45.  
  46. #include <errno.h>
  47. #include <stddef.h>
  48. #include <stdio.h>
  49. #include <stdlib.h>
  50. #include <string.h>
  51. #include <unistd.h>
  52.  
  53. #include <db.h>
  54. #include "recno.h"
  55.  
  56. /*
  57.  * __REC_GET -- Get a record from the btree.
  58.  *
  59.  * Parameters:
  60.  *    dbp:    pointer to access method
  61.  *    key:    key to find
  62.  *    data:    data to return
  63.  *    flag:    currently unused
  64.  *
  65.  * Returns:
  66.  *    RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found.
  67.  */
  68. int
  69. __rec_get(dbp, key, data, flags)
  70.     const DB *dbp;
  71.     const DBT *key;
  72.     DBT *data;
  73.     u_int flags;
  74. {
  75.     BTREE *t;
  76.     EPG *e;
  77.     recno_t nrec;
  78.     int status;
  79.  
  80.     t = dbp->internal;
  81.  
  82.     /* Toss any page pinned across calls. */
  83.     if (t->bt_pinned != NULL) {
  84.         mpool_put(t->bt_mp, t->bt_pinned, 0);
  85.         t->bt_pinned = NULL;
  86.     }
  87.  
  88.     /* Get currently doesn't take any flags, and keys of 0 are illegal. */
  89.     if (flags || (nrec = *(recno_t *)key->data) == 0) {
  90.         errno = EINVAL;
  91.         return (RET_ERROR);
  92.     }
  93.  
  94.     /*
  95.      * If we haven't seen this record yet, try to find it in the
  96.      * original file.
  97.      */
  98.     if (nrec > t->bt_nrecs) {
  99.         if (ISSET(t, R_EOF | R_INMEM))
  100.             return (RET_SPECIAL);
  101.         if ((status = t->bt_irec(t, nrec)) != RET_SUCCESS)
  102.             return (status);
  103.     }
  104.  
  105.     --nrec;
  106.     if ((e = __rec_search(t, nrec, SEARCH)) == NULL)
  107.         return (RET_ERROR);
  108.  
  109.     status = __rec_ret(t, e, 0, NULL, data);
  110.     if (ISSET(t, B_DB_LOCK))
  111.         mpool_put(t->bt_mp, e->page, 0);
  112.     else
  113.         t->bt_pinned = e->page;
  114.     return (status);
  115. }
  116.  
  117. /*
  118.  * __REC_FPIPE -- Get fixed length records from a pipe.
  119.  *
  120.  * Parameters:
  121.  *    t:    tree
  122.  *    cnt:    records to read
  123.  *
  124.  * Returns:
  125.  *    RET_ERROR, RET_SUCCESS
  126.  */
  127. int
  128. __rec_fpipe(t, top)
  129.     BTREE *t;
  130.     recno_t top;
  131. {
  132.     DBT data;
  133.     recno_t nrec;
  134.     size_t len;
  135.     int ch;
  136.     char *p;
  137.  
  138.     if (t->bt_dbufsz < t->bt_reclen) {
  139.         t->bt_dbuf = (char *)(t->bt_dbuf == NULL ?
  140.             malloc(t->bt_reclen) : realloc(t->bt_dbuf, t->bt_reclen));
  141.         if (t->bt_dbuf == NULL)
  142.             return (RET_ERROR);
  143.         t->bt_dbufsz = t->bt_reclen;
  144.     }
  145.     data.data = t->bt_dbuf;
  146.     data.size = t->bt_reclen;
  147.  
  148.     for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
  149.         len = t->bt_reclen;
  150.         for (p = t->bt_dbuf;; *p++ = ch)
  151.             if ((ch = getc(t->bt_rfp)) == EOF || !len--) {
  152.                 if (__rec_iput(t, nrec, &data, 0)
  153.                     != RET_SUCCESS)
  154.                     return (RET_ERROR);
  155.                 break;
  156.             }
  157.         if (ch == EOF)
  158.             break;
  159.     }
  160.     if (nrec < top) {
  161.         SET(t, R_EOF);
  162.         return (RET_SPECIAL);
  163.     }
  164.     return (RET_SUCCESS);
  165. }
  166.  
  167. /*
  168.  * __REC_VPIPE -- Get variable length records from a pipe.
  169.  *
  170.  * Parameters:
  171.  *    t:    tree
  172.  *    cnt:    records to read
  173.  *
  174.  * Returns:
  175.  *    RET_ERROR, RET_SUCCESS
  176.  */
  177. int
  178. __rec_vpipe(t, top)
  179.     BTREE *t;
  180.     recno_t top;
  181. {
  182.     DBT data;
  183.     recno_t nrec;
  184.     indx_t len;
  185.     size_t sz;
  186.     int bval, ch;
  187.     char *p;
  188.  
  189.     bval = t->bt_bval;
  190.     for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
  191.         for (p = t->bt_dbuf, sz = t->bt_dbufsz;; *p++ = ch, --sz) {
  192.             if ((ch = getc(t->bt_rfp)) == EOF || ch == bval) {
  193.                 data.data = t->bt_dbuf;
  194.                 data.size = p - t->bt_dbuf;
  195.                 if (ch == EOF && data.size == 0)
  196.                     break;
  197.                 if (__rec_iput(t, nrec, &data, 0)
  198.                     != RET_SUCCESS)
  199.                     return (RET_ERROR);
  200.                 break;
  201.             }
  202.             if (sz == 0) {
  203.                 len = p - t->bt_dbuf;
  204.                 t->bt_dbufsz += (sz = 256);
  205.                 t->bt_dbuf = (char *)(t->bt_dbuf == NULL ?
  206.                     malloc(t->bt_dbufsz) :
  207.                     realloc(t->bt_dbuf, t->bt_dbufsz));
  208.                 if (t->bt_dbuf == NULL)
  209.                     return (RET_ERROR);
  210.                 p = t->bt_dbuf + len;
  211.             }
  212.         }
  213.         if (ch == EOF)
  214.             break;
  215.     }
  216.     if (nrec < top) {
  217.         SET(t, R_EOF);
  218.         return (RET_SPECIAL);
  219.     }
  220.     return (RET_SUCCESS);
  221. }
  222.  
  223. /*
  224.  * __REC_FMAP -- Get fixed length records from a file.
  225.  *
  226.  * Parameters:
  227.  *    t:    tree
  228.  *    cnt:    records to read
  229.  *
  230.  * Returns:
  231.  *    RET_ERROR, RET_SUCCESS
  232.  */
  233. int
  234. __rec_fmap(t, top)
  235.     BTREE *t;
  236.     recno_t top;
  237. {
  238.     DBT data;
  239.     recno_t nrec;
  240.     caddr_t sp, ep;
  241.     size_t len;
  242.     char *p;
  243.  
  244.     if (t->bt_dbufsz < t->bt_reclen) {
  245.         t->bt_dbuf = (char *)(t->bt_dbuf == NULL ?
  246.             malloc(t->bt_reclen) : realloc(t->bt_dbuf, t->bt_reclen));
  247.         if (t->bt_dbuf == NULL)
  248.             return (RET_ERROR);
  249.         t->bt_dbufsz = t->bt_reclen;
  250.     }
  251.     data.data = t->bt_dbuf;
  252.     data.size = t->bt_reclen;
  253.  
  254.     sp = t->bt_cmap;
  255.     ep = t->bt_emap;
  256.     for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
  257.         if (sp >= ep) {
  258.             SET(t, R_EOF);
  259.             return (RET_SPECIAL);
  260.         }
  261.         len = t->bt_reclen;
  262.         for (p = t->bt_dbuf; sp < ep && len--; *p++ = *sp++);
  263.         memset(p, t->bt_bval, len);
  264.         if (__rec_iput(t, nrec, &data, 0) != RET_SUCCESS)
  265.             return (RET_ERROR);
  266.     }
  267.     t->bt_cmap = sp;
  268.     return (RET_SUCCESS);
  269. }
  270.  
  271. /*
  272.  * __REC_VMAP -- Get variable length records from a file.
  273.  *
  274.  * Parameters:
  275.  *    t:    tree
  276.  *    cnt:    records to read
  277.  *
  278.  * Returns:
  279.  *    RET_ERROR, RET_SUCCESS
  280.  */
  281. int
  282. __rec_vmap(t, top)
  283.     BTREE *t;
  284.     recno_t top;
  285. {
  286.     DBT data;
  287.     caddr_t sp, ep;
  288.     recno_t nrec;
  289.     int bval;
  290.  
  291.     sp = t->bt_cmap;
  292.     ep = t->bt_emap;
  293.     bval = t->bt_bval;
  294.  
  295.     for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
  296.         if (sp >= ep) {
  297.             SET(t, R_EOF);
  298.             return (RET_SPECIAL);
  299.         }
  300.         for (data.data = sp; sp < ep && *sp != bval; ++sp);
  301.         data.size = sp - (caddr_t)data.data;
  302.         if (__rec_iput(t, nrec, &data, 0) != RET_SUCCESS)
  303.             return (RET_ERROR);
  304.         ++sp;
  305.     }
  306.     t->bt_cmap = sp;
  307.     return (RET_SUCCESS);
  308. }
  309.