add_tries.c revision 225736
11590Srgrimes/****************************************************************************
21590Srgrimes * Copyright (c) 1998-2005,2006 Free Software Foundation, Inc.              *
31590Srgrimes *                                                                          *
41590Srgrimes * Permission is hereby granted, free of charge, to any person obtaining a  *
51590Srgrimes * copy of this software and associated documentation files (the            *
61590Srgrimes * "Software"), to deal in the Software without restriction, including      *
71590Srgrimes * without limitation the rights to use, copy, modify, merge, publish,      *
81590Srgrimes * distribute, distribute with modifications, sublicense, and/or sell       *
91590Srgrimes * copies of the Software, and to permit persons to whom the Software is    *
101590Srgrimes * furnished to do so, subject to the following conditions:                 *
111590Srgrimes *                                                                          *
121590Srgrimes * The above copyright notice and this permission notice shall be included  *
131590Srgrimes * in all copies or substantial portions of the Software.                   *
141590Srgrimes *                                                                          *
151590Srgrimes * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
161590Srgrimes * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
171590Srgrimes * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
181590Srgrimes * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
191590Srgrimes * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
201590Srgrimes * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
211590Srgrimes * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
221590Srgrimes *                                                                          *
231590Srgrimes * Except as contained in this notice, the name(s) of the above copyright   *
241590Srgrimes * holders shall not be used in advertising or otherwise to promote the     *
251590Srgrimes * sale, use or other dealings in this Software without prior written       *
261590Srgrimes * authorization.                                                           *
271590Srgrimes ****************************************************************************/
281590Srgrimes
291590Srgrimes/****************************************************************************
301590Srgrimes *  Author: Thomas E. Dickey            1998-on                             *
311590Srgrimes ****************************************************************************/
321590Srgrimes
331590Srgrimes/*
341590Srgrimes**	add_tries.c
351590Srgrimes**
361590Srgrimes**	Add keycode/string to tries-tree.
371590Srgrimes**
381590Srgrimes*/
391590Srgrimes
401590Srgrimes#include <curses.priv.h>
411590Srgrimes
421590SrgrimesMODULE_ID("$Id: add_tries.c,v 1.8 2006/12/30 23:15:26 tom Exp $")
431590Srgrimes
441590Srgrimes#define SET_TRY(dst,src) if ((dst->ch = *src++) == 128) dst->ch = '\0'
451590Srgrimes#define CMP_TRY(a,b) ((a)? (a == b) : (b == 128))
461590Srgrimes
471590SrgrimesNCURSES_EXPORT(int)
481590Srgrimes_nc_add_to_try(TRIES ** tree, const char *str, unsigned code)
491590Srgrimes{
501590Srgrimes    TRIES *ptr, *savedptr;
511590Srgrimes    unsigned const char *txt = (unsigned const char *) str;
521590Srgrimes
531590Srgrimes    T((T_CALLED("_nc_add_to_try(%p, %s, %u)"), *tree, _nc_visbuf(str), code));
541590Srgrimes    if (txt == 0 || *txt == '\0' || code == 0)
551590Srgrimes	returnCode(ERR);
561590Srgrimes
571590Srgrimes    if ((*tree) != 0) {
581590Srgrimes	ptr = savedptr = (*tree);
591590Srgrimes
601590Srgrimes	for (;;) {
611590Srgrimes	    unsigned char cmp = *txt;
621590Srgrimes
631590Srgrimes	    while (!CMP_TRY(ptr->ch, cmp)
641590Srgrimes		   && ptr->sibling != 0)
651590Srgrimes		ptr = ptr->sibling;
661590Srgrimes
671590Srgrimes	    if (CMP_TRY(ptr->ch, cmp)) {
681590Srgrimes		if (*(++txt) == '\0') {
691590Srgrimes		    ptr->value = code;
701590Srgrimes		    returnCode(OK);
711590Srgrimes		}
721590Srgrimes		if (ptr->child != 0)
731590Srgrimes		    ptr = ptr->child;
741590Srgrimes		else
751590Srgrimes		    break;
761590Srgrimes	    } else {
771590Srgrimes		if ((ptr->sibling = typeCalloc(TRIES, 1)) == 0) {
781590Srgrimes		    returnCode(ERR);
791590Srgrimes		}
801590Srgrimes
811590Srgrimes		savedptr = ptr = ptr->sibling;
8218730Ssteve		SET_TRY(ptr, txt);
835814Sjkh		ptr->value = 0;
8418730Ssteve
8518730Ssteve		break;
861590Srgrimes	    }
871590Srgrimes	}			/* end for (;;) */
881590Srgrimes    } else {			/* (*tree) == 0 :: First sequence to be added */
891590Srgrimes	savedptr = ptr = (*tree) = typeCalloc(TRIES, 1);
901590Srgrimes
911590Srgrimes	if (ptr == 0) {
921590Srgrimes	    returnCode(ERR);
931590Srgrimes	}
941590Srgrimes
951590Srgrimes	SET_TRY(ptr, txt);
961590Srgrimes	ptr->value = 0;
971590Srgrimes    }
981590Srgrimes
991590Srgrimes    /* at this point, we are adding to the try.  ptr->child == 0 */
1001590Srgrimes
1011590Srgrimes    while (*txt) {
10218730Ssteve	ptr->child = typeCalloc(TRIES, 1);
1031590Srgrimes
1041590Srgrimes	ptr = ptr->child;
1051590Srgrimes
1061590Srgrimes	if (ptr == 0) {
1071590Srgrimes	    while ((ptr = savedptr) != 0) {
1081590Srgrimes		savedptr = ptr->child;
1091590Srgrimes		free(ptr);
1101590Srgrimes	    }
1111590Srgrimes	    returnCode(ERR);
1121590Srgrimes	}
11317193Sbde
11417193Sbde	SET_TRY(ptr, txt);
11518730Ssteve	ptr->value = 0;
1161590Srgrimes    }
1171590Srgrimes
1181590Srgrimes    ptr->value = code;
1191590Srgrimes    returnCode(OK);
1201590Srgrimes}
1211590Srgrimes