Beispiel #1
0
static VALUE
fdbm_fetch(VALUE obj, VALUE keystr, VALUE ifnone)
{
    datum key, value;
    struct dbmdata *dbmp;
    DBM *dbm;
    long len;

    ExportStringValue(keystr);
    len = RSTRING_LEN(keystr);
    if (TOO_LONG(len)) goto not_found;
    key.dptr = RSTRING_PTR(keystr);
    key.dsize = (DSIZE_TYPE)len;

    GetDBM2(obj, dbmp, dbm);
    value = dbm_fetch(dbm, key);
    if (value.dptr == 0) {
      not_found:
	if (NIL_P(ifnone) && rb_block_given_p()) {
	    keystr = rb_str_dup(keystr);
	    OBJ_TAINT(keystr);
	    return rb_yield(keystr);
	}
	return ifnone;
    }
    return rb_tainted_str_new(value.dptr, value.dsize);
}
Beispiel #2
0
static VALUE
rb_gdbm_fetch2(GDBM_FILE dbm, VALUE keystr)
{
    datum key;
    long len;

    StringValue(keystr);
    len = RSTRING_LEN(keystr);
    if (TOO_LONG(len)) return Qnil;
    key.dptr = RSTRING_PTR(keystr);
    key.dsize = (int)len;

    return rb_gdbm_fetch(dbm, key);
}
Beispiel #3
0
static VALUE
rb_gdbm_nextkey(GDBM_FILE dbm, VALUE keystr)
{
    datum key, key2;
    VALUE str;
    long len;

    len = RSTRING_LEN(keystr);
    if (TOO_LONG(len)) return Qnil;
    key.dptr = RSTRING_PTR(keystr);
    key.dsize = (int)len;
    key2 = gdbm_nextkey(dbm, key);
    if (key2.dptr == 0)
        return Qnil;

    str = rb_str_new(key2.dptr, key2.dsize);
    free(key2.dptr);
    OBJ_TAINT(str);
    return str;
}
Beispiel #4
0
static Py_ssize_t
conn_recv_string(Connection *conn, char *buffer, 
                 size_t buflength, char **newbuffer)
{
    DWORD left, length, full_length, err;
    
    *newbuffer = NULL;

    if (ReadFile(conn->handle, buffer, buflength, &length, NULL))
        return length;
    
    err = GetLastError();
    if (err != ERROR_MORE_DATA) {
        if (err == ERROR_BROKEN_PIPE)
            return END_OF_FILE;
        return STANDARD_ERROR;
    }
    
    if (!PeekNamedPipe(conn->handle, NULL, 0, NULL, NULL, &left))
        return STANDARD_ERROR;

    full_length = length + left;
    if (TOO_LONG(full_length))
        return BAD_MESSAGE_LENGTH;

    *newbuffer = PyMem_Malloc(full_length);
    if (*newbuffer == NULL)
        return MEMORY_ERROR;
    
    memcpy(*newbuffer, buffer, length);

    if (ReadFile(conn->handle, *newbuffer+length, left, &length, NULL)) {
        assert(length == left);
        return full_length;
    } else {
        PyMem_Free(*newbuffer);
        return STANDARD_ERROR;
    }
}
Beispiel #5
0
/*
 * call-seq:
 *   dbm.key(value) -> string
 *
 * Returns the key for the specified value.
 */
static VALUE
fdbm_key(VALUE obj, VALUE valstr)
{
    datum key, val;
    struct dbmdata *dbmp;
    DBM *dbm;
    long len;

    ExportStringValue(valstr);
    len = RSTRING_LEN(valstr);
    if (TOO_LONG(len)) return Qnil;
    val.dptr = RSTRING_PTR(valstr);
    val.dsize = (DSIZE_TYPE)len;

    GetDBM2(obj, dbmp, dbm);
    for (key = dbm_firstkey(dbm); key.dptr; key = dbm_nextkey(dbm)) {
	val = dbm_fetch(dbm, key);
	if ((long)val.dsize == RSTRING_LEN(valstr) &&
	    memcmp(val.dptr, RSTRING_PTR(valstr), val.dsize) == 0) {
	    return rb_tainted_str_new(key.dptr, key.dsize);
	}
    }
    return Qnil;
}