#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "db_headers.h"
#include "db_item.h"
#include "nisdb_mt.h"
#define HASHSHIFT 3
#define HASHMASK 0x1f
#ifdef TDRPC
#define LOWER(c) (isupper((c)) ? tolower((c)) : (c))
extern "C" {
int strncasecmp(const char *s1, const char *s2, int n);
};
#else
#define LOWER(c) (isupper((c)) ? _tolower((c)) : (c))
#endif
item::item(char *str, int n)
{
len = n;
if ((value = new char[len]) == NULL)
FATAL("item::item: cannot allocate space", DB_MEMORY_LIMIT);
(void) memcpy(value, str, len);
}
item::item(item *model)
{
len = model->len;
if ((value = new char[len]) == NULL)
FATAL(" item::item: cannot allocate space (2)",
DB_MEMORY_LIMIT);
(void) memcpy(value, model->value, len);
}
void
item::print()
{
int i;
for (i = 0; i < len; i++)
putchar(value[i]);
}
bool_t
item::equal(item* other, bool_t casein)
{
if (casein)
return ((len == other->len) &&
(!strncasecmp(value, other->value, len)));
else
return ((len == other->len) &&
(!memcmp(value, other->value, len)));
}
bool_t
item::equal(char* other, int olen, bool_t casein)
{
if (casein)
return ((len == olen) && (!strncasecmp(value, other, len)));
else
return ((len == olen) && (!memcmp(value, other, len)));
}
u_int
item::get_hashval(bool_t casein)
{
int i;
u_int hval = 0;
if (casein) {
for (i = 0; i < len; i++) {
hval = ((hval<<HASHSHIFT)^hval);
hval += (LOWER(value[i]) & HASHMASK);
}
} else {
for (i = 0; i < len; i++) {
hval = ((hval<<HASHSHIFT)^hval);
hval += (value[i] & HASHMASK);
}
}
return (hval);
}