]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - ncurses/base/new_pair.c
ncurses 6.0 - patch 20180120
[ncurses.git] / ncurses / base / new_pair.c
index 271d494d8f884f3ad21102643b377541bfa5364b..f13d16f4575421c1cac5c9345b8c6ab06dfc257c 100644 (file)
@@ -60,7 +60,7 @@
 
 #endif
 
-MODULE_ID("$Id: new_pair.c,v 1.8 2017/03/10 09:22:50 tom Exp $")
+MODULE_ID("$Id: new_pair.c,v 1.14 2017/08/11 18:20:22 tom Exp $")
 
 #if USE_NEW_PAIR
 
@@ -118,7 +118,7 @@ dumpit(SCREEN *sp, int pair, const char *tag)
 
     if (next_len(sp, 0) != prev_len(sp, 0)) {
        endwin();
-       exit(1);
+       ExitProgram(EXIT_FAILURE);
     }
 }
 #else
@@ -144,7 +144,8 @@ _nc_find_color_pair(SCREEN *sp, int fg, int bg)
 
     find.fg = fg;
     find.bg = bg;
-    if ((pp = tfind(&find, &sp->_ordered_pairs, compare_data)) != 0) {
+    if (sp != 0 &&
+       (pp = tfind(&find, &sp->_ordered_pairs, compare_data)) != 0) {
        colorpair_t *temp = *(colorpair_t **) pp;
        result = (int) (temp - sp->_color_pairs);
     } else {
@@ -169,6 +170,20 @@ delink_color_pair(SCREEN *sp, int pair)
     }
 }
 
+/*
+ * Discard all nodes in the fast-index.
+ */
+NCURSES_EXPORT(void)
+_nc_free_ordered_pairs(SCREEN *sp)
+{
+    if (sp && sp->_ordered_pairs && sp->_pair_alloc) {
+       int n;
+       for (n = 0; n < sp->_pair_alloc; ++n) {
+           tdelete(&sp->_color_pairs[n], &sp->_ordered_pairs, compare_data);
+       }
+    }
+}
+
 /*
  * Use this call to update the fast-index when modifying an entry in the color
  * pair table.
@@ -176,8 +191,9 @@ delink_color_pair(SCREEN *sp, int pair)
 NCURSES_EXPORT(void)
 _nc_reset_color_pair(SCREEN *sp, int pair, colorpair_t * next)
 {
+    colorpair_t *last;
     if (ValidPair(sp, pair)) {
-       colorpair_t *last = &(sp->_color_pairs[pair]);
+       last = _nc_reserve_pairs(sp, pair);
        delink_color_pair(sp, pair);
        if (last->mode > cpFREE &&
            (last->fg != next->fg || last->bg != next->bg)) {
@@ -215,13 +231,31 @@ _nc_set_color_pair(SCREEN *sp, int pair, int mode)
     }
 }
 
+/*
+ * If we reallocate the color-pair array, we have to adjust the fast-index.
+ */
+NCURSES_EXPORT(void)
+_nc_copy_pairs(SCREEN *sp, colorpair_t * target, colorpair_t * source, int length)
+{
+    int n;
+    for (n = 0; n < length; ++n) {
+       void *find = tfind(source + n, &sp->_ordered_pairs, compare_data);
+       if (find != 0) {
+           tdelete(source + n, &sp->_ordered_pairs, compare_data);
+           tsearch(target + n, &sp->_ordered_pairs, compare_data);
+       }
+    }
+}
+
 NCURSES_EXPORT(int)
 NCURSES_SP_NAME(alloc_pair) (NCURSES_SP_DCLx int fg, int bg)
 {
     int pair;
 
     T((T_CALLED("alloc_pair(%d,%d)"), fg, bg));
-    if ((pair = _nc_find_color_pair(SP_PARM, fg, bg)) < 0) {
+    if (SP_PARM == 0) {
+       pair = -1;
+    } else if ((pair = _nc_find_color_pair(SP_PARM, fg, bg)) < 0) {
        /*
         * Check if all of the slots have been used.  If not, find one and
         * use that.
@@ -234,13 +268,21 @@ NCURSES_SP_NAME(alloc_pair) (NCURSES_SP_DCLx int fg, int bg)
             * The linear search is done to allow mixing calls to init_pair()
             * and alloc_pair().  The former can make gaps...
             */
-           for (pair = hint + 1; pair < SP_PARM->_pair_limit; pair++) {
+           for (pair = hint + 1; pair < SP_PARM->_pair_alloc; pair++) {
                if (SP_PARM->_color_pairs[pair].mode == cpFREE) {
                    T(("found gap %d", pair));
                    found = TRUE;
                    break;
                }
            }
+           if (!found && (SP_PARM->_pair_alloc < SP_PARM->_pair_limit)) {
+               pair = SP_PARM->_pair_alloc;
+               if (_nc_reserve_pairs(sp, pair) == 0) {
+                   pair = -1;
+               } else {
+                   found = TRUE;
+               }
+           }
            if (!found) {
                for (pair = 1; pair <= hint; pair++) {
                    if (SP_PARM->_color_pairs[pair].mode == cpFREE) {
@@ -261,11 +303,8 @@ NCURSES_SP_NAME(alloc_pair) (NCURSES_SP_DCLx int fg, int bg)
            T(("reusing %d", pair));
        }
 
-       if (pair > 0 && pair <= MAX_XCURSES_PAIR) {
-           IGNORE_RC(init_pair((short)pair, (short)fg, (short)bg));
-       } else {
+       if (_nc_init_pair(SP_PARM, pair, fg, bg) == ERR)
            pair = ERR;
-       }
     }
     returnCode(pair);
 }
@@ -285,7 +324,7 @@ NCURSES_SP_NAME(free_pair) (NCURSES_SP_DCLx int pair)
 {
     int result = ERR;
     T((T_CALLED("free_pair(%d)"), pair));
-    if (ValidPair(SP_PARM, pair)) {
+    if (ValidPair(SP_PARM, pair) && pair < SP_PARM->_pair_alloc) {
        colorpair_t *cp = &(SP_PARM->_color_pairs[pair]);
        if (pair != 0) {
            _nc_change_pair(SP_PARM, pair);
@@ -336,5 +375,5 @@ void _nc_new_pair(void);
 void
 _nc_new_pair(void)
 {
-};
+}
 #endif /* USE_NEW_PAIR */