Using Source Nav. database API

Khamis Abuelkomboz khamis2@t-online.de
Thu Jan 3 15:58:00 GMT 2002


Your example is correct!
It works only if you specify a pattern, what the key begins with. The DB 
tool does only
provide "BEGINS" functionality (maybe I'm wrong). The problem was, that 
you didn't
hit any string combination, what the db key begins with :-)

However, here is your example that displays all the database contents if 
you specify an
empty pattern ("").

One more tip! the SN database files contents differ from one to other. 
You need to use your
example to study the contents of every file carefully.
The extensions of the databases tell you about the file is used for:

db.fil    contains all symbols in a file (largest file)
db.cl     contains all classes
db.f       contains all files
db.fu    contains all functions
...
and so on.

/*
 * dbquery.c
 *
 * Copyright (C) 1998 Cygnus Solutions
 *
 * Description:
 * This example shows how to query the Source-Navigator database in C.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>

#include "NTunixstubs.h"
#include "db.h"

int
main(int argc, char *argv[])
{
        DB      *db;
        DBT     data;
        DBT     key;
        int     flag;
        int     len;
        char    *pattern;

        if (argc != 3)
        {
                printf("usage: %s database pattern\n",argv[0]);
                exit(1);
        }
        if (!(db = dbopen(argv[1],O_RDONLY,0644,DB_BTREE,NULL)))
        {
                fprintf(stderr,"Could not open \"%s\",%s\n",argv[1],
                        strerror(errno));
                exit(2);
        }

        pattern = argv[2];
        len = strlen(pattern);

        if (len == 0)
        {
            key.data = "\0";
            key.size = 1;
        }
        else
        {
            key.data = (void *)pattern;
            key.size = len;
        }
        for(flag = R_CURSOR;
                db->seq(db, &key,&data,flag) == 0
                && strncmp(key.data,pattern,len) == 0
                ;
                flag = R_NEXT)
        {
                printf("%s\t%s\n", (char *) key.data, (char *) data.data);
        }
        db->close(db);

        exit (0);
}




More information about the Sourcenav mailing list