Back to index

tetex-bin  3.0
add_tries.c
Go to the documentation of this file.
00001 /****************************************************************************
00002  * Copyright (c) 1998,2000 Free Software Foundation, Inc.                   *
00003  *                                                                          *
00004  * Permission is hereby granted, free of charge, to any person obtaining a  *
00005  * copy of this software and associated documentation files (the            *
00006  * "Software"), to deal in the Software without restriction, including      *
00007  * without limitation the rights to use, copy, modify, merge, publish,      *
00008  * distribute, distribute with modifications, sublicense, and/or sell       *
00009  * copies of the Software, and to permit persons to whom the Software is    *
00010  * furnished to do so, subject to the following conditions:                 *
00011  *                                                                          *
00012  * The above copyright notice and this permission notice shall be included  *
00013  * in all copies or substantial portions of the Software.                   *
00014  *                                                                          *
00015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
00016  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
00017  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
00018  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
00019  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
00020  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
00021  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
00022  *                                                                          *
00023  * Except as contained in this notice, the name(s) of the above copyright   *
00024  * holders shall not be used in advertising or otherwise to promote the     *
00025  * sale, use or other dealings in this Software without prior written       *
00026  * authorization.                                                           *
00027  ****************************************************************************/
00028 
00029 /****************************************************************************
00030  *  Author: Thomas E. Dickey <dickey@clark.net> 1998                        *
00031  ****************************************************************************/
00032 
00033 /*
00034 **     add_tries.c
00035 **
00036 **     Add keycode/string to tries-tree.
00037 **
00038 */
00039 
00040 #include <curses.priv.h>
00041 
00042 MODULE_ID("$Id: add_tries.c,v 1.4 2000/12/10 02:55:07 tom Exp $")
00043 
00044 #define SET_TRY(dst,src) if ((dst->ch = *src++) == 128) dst->ch = '\0'
00045 #define CMP_TRY(a,b) ((a)? (a == b) : (b == 128))
00046 
00047 NCURSES_EXPORT(void)
00048 _nc_add_to_try(struct tries **tree, const char *str, unsigned short code)
00049 {
00050     static bool out_of_memory = FALSE;
00051     struct tries *ptr, *savedptr;
00052     unsigned const char *txt = (unsigned const char *) str;
00053 
00054     if (txt == 0 || *txt == '\0' || out_of_memory || code == 0)
00055        return;
00056 
00057     if ((*tree) != 0) {
00058        ptr = savedptr = (*tree);
00059 
00060        for (;;) {
00061            unsigned char cmp = *txt;
00062 
00063            while (!CMP_TRY(ptr->ch, cmp)
00064                  && ptr->sibling != 0)
00065               ptr = ptr->sibling;
00066 
00067            if (CMP_TRY(ptr->ch, cmp)) {
00068               if (*(++txt) == '\0') {
00069                   ptr->value = code;
00070                   return;
00071               }
00072               if (ptr->child != 0)
00073                   ptr = ptr->child;
00074               else
00075                   break;
00076            } else {
00077               if ((ptr->sibling = typeCalloc(struct tries, 1)) == 0) {
00078                   out_of_memory = TRUE;
00079                   return;
00080               }
00081 
00082               savedptr = ptr = ptr->sibling;
00083               SET_TRY(ptr, txt);
00084               ptr->value = 0;
00085 
00086               break;
00087            }
00088        }                    /* end for (;;) */
00089     } else {                /* (*tree) == 0 :: First sequence to be added */
00090        savedptr = ptr = (*tree) = typeCalloc(struct tries, 1);
00091 
00092        if (ptr == 0) {
00093            out_of_memory = TRUE;
00094            return;
00095        }
00096 
00097        SET_TRY(ptr, txt);
00098        ptr->value = 0;
00099     }
00100 
00101     /* at this point, we are adding to the try.  ptr->child == 0 */
00102 
00103     while (*txt) {
00104        ptr->child = typeCalloc(struct tries, 1);
00105 
00106        ptr = ptr->child;
00107 
00108        if (ptr == 0) {
00109            out_of_memory = TRUE;
00110 
00111            while ((ptr = savedptr) != 0) {
00112               savedptr = ptr->child;
00113               free(ptr);
00114            }
00115 
00116            return;
00117        }
00118 
00119        SET_TRY(ptr, txt);
00120        ptr->value = 0;
00121     }
00122 
00123     ptr->value = code;
00124     return;
00125 }