Category : C Source Code
Archive   : BTREEC.ZIP
Filename : ADDKEY.C

 
Output of file : ADDKEY.C contained in archive : BTREEC.ZIP
/*
* ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
* º Btree Implementation for Turbo C v2.0 º
* º ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ º
* º Copyright (C) 1989 º
* º Author: Fook H. Eng, 428-B South Ramona Ave, º
* º Monterey Park, CA 91754, 818 572-9220 º
* º º
* ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ
*/
#include
#include "cstruct.h"
#include
#include "access.h"

static taitem pitem;
static tapage * addptr;
static int passup;
static void find_ins(indexfile * nxf, int pgref, int datref, char * pkey);
static void insert(indexfile * nxf, int pgref, int r);

static void find_ins(indexfile * nxf, int pgref, int datref, char * pkey)
{
int nextpgref, c, mid, lft, rgt;
tapage * pgptr;

if (pgref == 0) {
passup = TRUE;
strcpy(pitem.key, pkey);
pitem.dataref = datref;
pitem.pageref = 0;
}
else {
pgptr = (tapage *) tagetpage(nxf, pgref);
lft = 0;
rgt = pgptr->itemsonpage - 1;
do {
mid = (lft + rgt) / 2;
if ((c = tacompkeys(pkey, pgptr->itemarray[mid].key, datref,
pgptr->itemarray[mid].dataref, nxf->allowduplkeys)) <= 0)
rgt = mid - 1;
if (c >= 0)
lft = mid + 1;
} while (rgt >= lft);
if (lft - rgt > 1) { /* failure to add. already there! */
passup = FALSE;
addptr = NULL;
}
else {
nextpgref = (rgt == -1) ? pgptr->bckwpageref :
pgptr->itemarray[rgt].pageref;
find_ins(nxf, nextpgref, datref, pkey);
if (passup)
insert(nxf, pgref, rgt);
}
}
}

static void insert(indexfile * nxf, int pgref, int r)
/*
* insert pitem on pgref as itemarray[r+1]. if not possible because
* of excess items, get new page and split the items, then define a
* new pitem to pass back to search fcn.
* the argument list of this function properly includes pgref instead
* of pgptr. recursion is ongoing, we can never be sure that pgptr is
* pointing where we think it should after several recursive rounds.
* for similar reasons we don't declare pgptr or pgref as global (not
* even static) objects.
*/
{
int i;
tapage * pgptr;

pgptr = (tapage *) tagetpage(nxf, pgref);

if (pgptr->itemsonpage < PAGESIZE) {
for (i = pgptr->itemsonpage - 1; i >= r+1; i--)
pgptr->itemarray[i+1] = pgptr->itemarray[i];
pgptr->itemarray[r+1] = pitem;
pgptr->itemsonpage++;
passup = FALSE;
addptr = pgptr;
}
else {
tapage * pg2ptr;
taitem qitem;

pg2ptr = (tapage *) (tanewpage(nxf));
if (r == ORDER - 1) {
qitem = pitem;
for (i = 0; i < ORDER; i++)
pg2ptr->itemarray[i] = pgptr->itemarray[i + ORDER];
}
else if (r < ORDER - 1) {
qitem = pgptr->itemarray[ORDER - 1];
for (i = ORDER - 1; i >= r + 2; i--)
pgptr->itemarray[i] = pgptr->itemarray[i - 1];
pgptr->itemarray[r + 1] = pitem;
for (i = 0; i < ORDER; i++)
pg2ptr->itemarray[i] = pgptr->itemarray[i + ORDER];
}
else {
qitem = pgptr->itemarray[ORDER];
r -= ORDER;
for (i = 0; i < r; i++)
pg2ptr->itemarray[i] = pgptr->itemarray[i + ORDER + 1];
pg2ptr->itemarray[r] = pitem;
for (i = r + 1; i < ORDER; i++)
pg2ptr->itemarray[i] = pgptr->itemarray[i + ORDER];
}
pgptr->itemsonpage = ORDER;
pg2ptr->itemsonpage = ORDER;
pg2ptr->bckwpageref = qitem.pageref;
qitem.pageref = ((tapagerec * ) pg2ptr)->pageref;
pitem = qitem;
((tapagerec *) pg2ptr)->updated = TRUE;
}
((tapagerec *) pgptr)->updated = TRUE;
}

tapage * addkey(indexfile * nxf, int prcdatref, char * prockey)
{
int roottmp;
tapagerec * pgrcptr;
tapage * rootptr;

taxkey(prockey, nxf->keyl);
find_ins(nxf, nxf->rootref, prcdatref, prockey);
if (passup) {
roottmp = nxf->rootref;
rootptr = (tapage *) (pgrcptr = tanewpage(nxf));
rootptr->itemsonpage = 1;
rootptr->bckwpageref = roottmp;
rootptr->itemarray[0] = pitem;
nxf->rootref = pgrcptr->pageref;
pgrcptr->updated = TRUE;
addptr = rootptr;
}
nxf->pnx = -1;
return (addptr);
}

  3 Responses to “Category : C Source Code
Archive   : BTREEC.ZIP
Filename : ADDKEY.C

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/