2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License, Version 1.0 only
2N/A * (the "License"). You may not use this file except in compliance
2N/A * with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * db_item.cc
2N/A *
2N/A * Copyright (c) 1988-2000 by Sun Microsystems, Inc.
2N/A * All Rights Reserved.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A#include <stdio.h>
2N/A#include <string.h>
2N/A#include <ctype.h>
2N/A
2N/A#include "db_headers.h"
2N/A#include "db_item.h"
2N/A#include "nisdb_mt.h"
2N/A
2N/A#define HASHSHIFT 3
2N/A#define HASHMASK 0x1f
2N/A
2N/A#ifdef TDRPC
2N/A#define LOWER(c) (isupper((c)) ? tolower((c)) : (c))
2N/Aextern "C" {
2N/A int strncasecmp(const char *s1, const char *s2, int n);
2N/A};
2N/A#else
2N/A#define LOWER(c) (isupper((c)) ? _tolower((c)) : (c))
2N/A#endif
2N/A
2N/A
2N/A/* Constructor: creates item using given character sequence and length */
2N/Aitem::item(char *str, int n)
2N/A{
2N/A len = n;
2N/A if ((value = new char[len]) == NULL)
2N/A FATAL("item::item: cannot allocate space", DB_MEMORY_LIMIT);
2N/A
2N/A (void) memcpy(value, str, len);
2N/A}
2N/A
2N/A
2N/A/* Constructor: creates item by copying given item */
2N/Aitem::item(item *model)
2N/A{
2N/A len = model->len;
2N/A if ((value = new char[len]) == NULL)
2N/A FATAL(" item::item: cannot allocate space (2)",
2N/A DB_MEMORY_LIMIT);
2N/A
2N/A (void) memcpy(value, model->value, len);
2N/A}
2N/A
2N/A/* Prints contents of item to stdout */
2N/Avoid
2N/Aitem::print()
2N/A{
2N/A int i;
2N/A for (i = 0; i < len; i++)
2N/A putchar(value[i]);
2N/A}
2N/A
2N/A/* Equality test. 'casein' TRUE means case insensitive test. */
2N/Abool_t
2N/Aitem::equal(item* other, bool_t casein)
2N/A{
2N/A if (casein) // case-insensitive
2N/A return ((len == other->len) &&
2N/A (!strncasecmp(value, other->value, len)));
2N/A else // case sensitive
2N/A return ((len == other->len) &&
2N/A (!memcmp(value, other->value, len)));
2N/A}
2N/A
2N/Abool_t
2N/Aitem::equal(char* other, int olen, bool_t casein)
2N/A{
2N/A if (casein) // case-insensitive
2N/A return ((len == olen) && (!strncasecmp(value, other, len)));
2N/A else // case sensitive
2N/A return ((len == olen) && (!memcmp(value, other, len)));
2N/A}
2N/A
2N/A/* Return hash value. 'casein' TRUE means case insensitive test. */
2N/Au_int
2N/Aitem::get_hashval(bool_t casein)
2N/A{
2N/A int i;
2N/A u_int hval = 0;
2N/A
2N/A // we want to separate the cases so that we don't needlessly do
2N/A // an extra test for the case-sensitive branch in the for loop
2N/A if (casein) { // case insensitive
2N/A for (i = 0; i < len; i++) {
2N/A hval = ((hval<<HASHSHIFT)^hval);
2N/A hval += (LOWER(value[i]) & HASHMASK);
2N/A }
2N/A } else { // case sensitive
2N/A for (i = 0; i < len; i++) {
2N/A hval = ((hval<<HASHSHIFT)^hval);
2N/A hval += (value[i] & HASHMASK);
2N/A }
2N/A }
2N/A
2N/A return (hval);
2N/A}