150276Speter/**************************************************************************** 2174993Srafan * Copyright (c) 1998-2005,2006 Free Software Foundation, Inc. * 350276Speter * * 450276Speter * Permission is hereby granted, free of charge, to any person obtaining a * 550276Speter * copy of this software and associated documentation files (the * 650276Speter * "Software"), to deal in the Software without restriction, including * 750276Speter * without limitation the rights to use, copy, modify, merge, publish, * 850276Speter * distribute, distribute with modifications, sublicense, and/or sell * 950276Speter * copies of the Software, and to permit persons to whom the Software is * 1050276Speter * furnished to do so, subject to the following conditions: * 1150276Speter * * 1250276Speter * The above copyright notice and this permission notice shall be included * 1350276Speter * in all copies or substantial portions of the Software. * 1450276Speter * * 1550276Speter * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * 1650276Speter * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * 1750276Speter * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * 1850276Speter * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 1950276Speter * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 2050276Speter * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * 2150276Speter * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * 2250276Speter * * 2350276Speter * Except as contained in this notice, the name(s) of the above copyright * 2450276Speter * holders shall not be used in advertising or otherwise to promote the * 2550276Speter * sale, use or other dealings in this Software without prior written * 2650276Speter * authorization. * 2750276Speter ****************************************************************************/ 2850276Speter 2950276Speter/**************************************************************************** 30174993Srafan * Author: Thomas E. Dickey 1998-on * 3150276Speter ****************************************************************************/ 3250276Speter 3350276Speter/* 3450276Speter** add_tries.c 3550276Speter** 3650276Speter** Add keycode/string to tries-tree. 3750276Speter** 3850276Speter*/ 3950276Speter 4050276Speter#include <curses.priv.h> 4150276Speter 42174993SrafanMODULE_ID("$Id: add_tries.c,v 1.8 2006/12/30 23:15:26 tom Exp $") 4350276Speter 4450276Speter#define SET_TRY(dst,src) if ((dst->ch = *src++) == 128) dst->ch = '\0' 4550276Speter#define CMP_TRY(a,b) ((a)? (a == b) : (b == 128)) 4650276Speter 47174993SrafanNCURSES_EXPORT(int) 48174993Srafan_nc_add_to_try(TRIES ** tree, const char *str, unsigned code) 4950276Speter{ 50174993Srafan TRIES *ptr, *savedptr; 5162449Speter unsigned const char *txt = (unsigned const char *) str; 5250276Speter 53174993Srafan T((T_CALLED("_nc_add_to_try(%p, %s, %u)"), *tree, _nc_visbuf(str), code)); 54174993Srafan if (txt == 0 || *txt == '\0' || code == 0) 55174993Srafan returnCode(ERR); 5650276Speter 5762449Speter if ((*tree) != 0) { 5862449Speter ptr = savedptr = (*tree); 5950276Speter 6062449Speter for (;;) { 6162449Speter unsigned char cmp = *txt; 6250276Speter 6362449Speter while (!CMP_TRY(ptr->ch, cmp) 6476726Speter && ptr->sibling != 0) 6562449Speter ptr = ptr->sibling; 6650276Speter 6762449Speter if (CMP_TRY(ptr->ch, cmp)) { 6862449Speter if (*(++txt) == '\0') { 6962449Speter ptr->value = code; 70174993Srafan returnCode(OK); 7162449Speter } 7262449Speter if (ptr->child != 0) 7362449Speter ptr = ptr->child; 7462449Speter else 7562449Speter break; 7662449Speter } else { 77174993Srafan if ((ptr->sibling = typeCalloc(TRIES, 1)) == 0) { 78174993Srafan returnCode(ERR); 7962449Speter } 8050276Speter 8162449Speter savedptr = ptr = ptr->sibling; 8262449Speter SET_TRY(ptr, txt); 8362449Speter ptr->value = 0; 8450276Speter 8562449Speter break; 8662449Speter } 8762449Speter } /* end for (;;) */ 8862449Speter } else { /* (*tree) == 0 :: First sequence to be added */ 89174993Srafan savedptr = ptr = (*tree) = typeCalloc(TRIES, 1); 9050276Speter 9162449Speter if (ptr == 0) { 92174993Srafan returnCode(ERR); 9350276Speter } 9450276Speter 9562449Speter SET_TRY(ptr, txt); 9662449Speter ptr->value = 0; 9762449Speter } 9850276Speter 9962449Speter /* at this point, we are adding to the try. ptr->child == 0 */ 10050276Speter 10162449Speter while (*txt) { 102174993Srafan ptr->child = typeCalloc(TRIES, 1); 10350276Speter 10462449Speter ptr = ptr->child; 10550276Speter 10662449Speter if (ptr == 0) { 10762449Speter while ((ptr = savedptr) != 0) { 10862449Speter savedptr = ptr->child; 10962449Speter free(ptr); 11062449Speter } 111174993Srafan returnCode(ERR); 11250276Speter } 11350276Speter 11462449Speter SET_TRY(ptr, txt); 11562449Speter ptr->value = 0; 11662449Speter } 11762449Speter 11862449Speter ptr->value = code; 119174993Srafan returnCode(OK); 12050276Speter} 121