Example #1
0
link search_btree(link p, char key)
{
	if (!p)
		return NULL;

	if (p->item == key)
		return p;

	link tmp = search_btree(p->l, key);
	if (tmp)
		return tmp;

	tmp = search_btree(p->r, key);
	if (tmp)
		return tmp;

	return NULL;
}
Example #2
0
File: mwdb.c Project: ntj/rockbox
int mwdb_findarticle(const char* filename,
                     char* artnme,
                     uint16_t artnmelen,
                     uint32_t * res_lo,
                     uint32_t * res_hi,
                     bool progress,
                     bool promptname,
                     bool casesense)
{
    int fd;
    char fnbuf[rb->strlen(filename)+6];

    if(promptname)
    {
#ifdef HAVE_TOUCHSCREEN
    /* this is a hack till proper touchscreen keyboard input arrives */
    rb->touchscreen_set_mode(TOUCHSCREEN_BUTTON);
#endif
        if (rb->kbd_input(artnme,artnmelen))
        {
            artnme[0]='\0';
#ifdef HAVE_TOUCHSCREEN
    rb->touchscreen_set_mode(TOUCHSCREEN_POINT);
#endif
            return false;  /* proper abort on keyboard abort */
        }
#ifdef HAVE_TOUCHSCREEN
    rb->touchscreen_set_mode(TOUCHSCREEN_POINT);
#endif
    }


    rb->snprintf(fnbuf,rb->strlen(filename)+6,"%s.wwi",filename);
    fd = rb->open(fnbuf, 0);

    if (fd==-1)
        return false;

    if(progress)
    {
        mwdb_p_xtpt=0;
        rb->lcd_clear_display();
        mwdb_nprintf("Searching...");
    }

    search_btree((void*)fd,artnme,rb->strlen(artnme),0,res_lo,res_hi,casesense);
    rb->close(fd);

    if(*res_hi==0)   /* not found */
        return false;
    else if(progress)
        mwdb_nprintf("Found:%d%d",*res_lo,*res_hi);

    return true;
}
Example #3
0
int main(int argc,char * argv[])
{
    if(argc<3)
        printf("Usage: btsearch <file> <key>\n");
    else
    {
        FILE *fd=fopen(argv[1],"r");
        uint32_t res_lo;
        uint32_t res_hi;
        fseek(fd,0,SEEK_SET);
        search_btree(fd, argv[2], strlen(argv[2]), 0, &res_lo, &res_hi, false);
        printf("Result: %d,%d\n",res_lo,res_hi);
    }

    return 0;
}
Example #4
0
int main(void)
{
	printf("hello, link! \n");

	root = make_node('4');
	root->l = make_node('2');
	root->r = make_node('5');

	root->l->l = make_node('1');
	root->l->r = make_node('3');
	root->r->r = make_node('6');
	root->r->l = make_node('7');

	travel(root);

	printf("count root = %d\n", count(root));
	printf("depth root = %d\n", depth(root));

	char str[] = "1231215";
	int i = 0;

	while (str[i])
	{
		char key = str[i];

		link p = search_btree(root, key);
		if (p)	// p: not NULL, then key is on the tree
		{
			p->counter++;
			printf("found key: %c (%d)\n", key, p->counter);
		}
		else
			printf("not found key: %c \n", key);

		i++;
	}

	travel(root);

	destroy(root);
	printf("destroy root ok!\n");

	return 0;
}
Example #5
0
void search_btree(void* file, const char* key, uint16_t rkeylen, uint32_t globoffs,
                  uint32_t* res_lo, uint32_t* res_hi, const bool casesense)
{
    unsigned char nd_key[KEY_MAXLEN];
    uint8_t node_flags;
    uint16_t node_nr_active,i,keylen;
    uint32_t chldptr;
    uint64_t dtaptr_lo,dtaptr_hi;
    fread(&node_flags,sizeof(uint8_t),1,file);
    fread(&node_nr_active,sizeof(uint16_t),1,file);
    node_nr_active=letoh16(node_nr_active);
    if(node_nr_active<1)   /* error */
        goto err;

    for(i=0; i<node_nr_active; i++)
    {
        fread(&dtaptr_lo,sizeof(uint32_t),1,file);
        dtaptr_lo=letoh32(dtaptr_lo);
        fread(&dtaptr_hi,sizeof(uint32_t),1,file);
        dtaptr_hi=letoh32(dtaptr_hi);
        fread(&chldptr,sizeof(uint32_t),1,file);
        chldptr=letoh32(chldptr);
        fread(&keylen,sizeof(uint16_t),1,file);
        keylen=letoh32(keylen);
        fread(&nd_key,sizeof(unsigned char),(keylen<KEY_MAXLEN) ? keylen : KEY_MAXLEN,file);
        if(keylen-((keylen<KEY_MAXLEN) ? keylen : KEY_MAXLEN)>0)
            fseek(file,keylen-((keylen<KEY_MAXLEN) ? keylen : KEY_MAXLEN),SEEK_CUR);

        keylen=(keylen<KEY_MAXLEN) ? keylen : KEY_MAXLEN;

        nd_key[keylen]=0;
        DEBUGF("CMP: %s, %s\n", key, nd_key);
        if(utf8strcnmp(((const unsigned char*)key),((const unsigned char*)nd_key),rkeylen,keylen,casesense)>0)
            continue;

        if(utf8strcnmp(((const unsigned char*)key),((const unsigned char*)nd_key),rkeylen,keylen,casesense)==0)
        {
            DEBUGF("Found! %s\n", nd_key);
            *res_lo=dtaptr_lo;
            *res_hi=dtaptr_hi;
            return;
        }

        if(chldptr==0||node_flags==1)
            goto err;

        fseek(file,globoffs+chldptr,SEEK_SET);
        search_btree(file,key,rkeylen,globoffs,res_lo,res_hi,casesense);
        return;
    }

    if(node_flags!=1)   /* node not leaf */
    {
        fread(&chldptr,sizeof(uint32_t),1,file);
        chldptr=letoh32(chldptr);
        if(chldptr==0)   /* leaf */
            goto err;

        fseek(file,globoffs+chldptr,SEEK_SET);
        search_btree(file,key,rkeylen,globoffs,res_lo,res_hi,casesense);
    }

    return;
err:
    *res_lo=*res_hi=0;
}