CDB_File

 view release on metacpan or  search on metacpan

CDB_File.xs  view on Meta::CPAN

    cdb *        this

    CODE:
        /* here we dup the filehandle, because perl space will try and close
           it when it goes out of scope */
        RETVAL = PerlIO_fdopen(PerlIO_fileno(this->fh), "r");
    OUTPUT:
        RETVAL

U32
cdb_datalen(db)
    cdb *db

    CODE:
        RETVAL = cdb_datalen(db);

    OUTPUT:
        RETVAL

U32
cdb_datapos(db)
    cdb *db

    CODE:
        RETVAL = cdb_datapos(db);

    OUTPUT:
        RETVAL

cdb *
cdb_TIEHASH(CLASS, filename, option_key="", is_utf8=FALSE)
    char *CLASS
    char *filename
    char *option_key
    bool  is_utf8

    PREINIT:
        PerlIO *f;
        bool  utf8_chosen = FALSE;

    CODE:
        if(strlen(option_key) == 4 && strnEQ("utf8", option_key, 4) && is_utf8 )
#ifdef CDB_FILE_HAS_UTF8_HASH_MACROS
            croak("utf8 CDB_Files are not supported below Perl 5.14");
#else
            utf8_chosen = TRUE;
#endif

        Newxz(RETVAL, 1, cdb);
        RETVAL->fh = f = PerlIO_open(filename, "rb");
        RETVAL->is_utf8 = utf8_chosen;

        if (!f)
            XSRETURN_NO;
#ifdef HASMMAP
        {
            struct stat st;
            int fd = PerlIO_fileno(f);

            RETVAL->map = 0;
            if (fstat(fd, &st) == 0) {
                if (st.st_size <= 0xffffffff) {
                    char *x;

                    x = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
                    if (x != (char *)-1) {
                        RETVAL->size = st.st_size;
                        RETVAL->map = x;
                    }
                }
            }
        }
#endif
    OUTPUT:
        RETVAL

SV *
cdb_FETCH(this, k)
    cdb *this
    SV  *k

    PREINIT:
        char buf[8];
        int found;
        string_finder to_find;

    CODE:
        if (!SvOK(k)) {
            XSRETURN_UNDEF;
        }

        to_find.pv = this->is_utf8 ? SvPVutf8(k, to_find.len) : SvPV(k, to_find.len);
        to_find.hash = 0;
        to_find.is_utf8 = this->is_utf8 && SvUTF8(k);

        /* Already advanced to the key we need. */
        if (this->end && cdb_key_eq(&this->curkey, &to_find)) {
            if (cdb_read(this, buf, 8, this->curpos) == -1)
                readerror();
            uint32_unpack(buf + 4, &this->dlen);
            this->dpos = this->curpos + 8 + to_find.len;
            if (this->fetch_advance) {
                iter_advance(this);
                if (!iter_key(this)) {
                    iter_end(this);
                }
            }
            found = 1;
        } else {
            /* Need to find the key first.. */
            cdb_findstart(this);
            found = cdb_findnext(this, &to_find);
            if ((found != 0) && (found != 1)) readerror();
        }

        if (found) {
            U32 dlen;
            dlen = cdb_datalen(this);
            RETVAL = sv_from_datapos(this, dlen);
        }
        else {



( run in 1.133 second using v1.01-cache-2.11-cpan-39bf76dae61 )