File: itk_util.c

package info (click to toggle)
itk3 3.3-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 1,216 kB
  • ctags: 253
  • sloc: ansic: 3,670; tcl: 417; sh: 302; makefile: 116
file content (200 lines) | stat: -rw-r--r-- 5,818 bytes parent folder | download | duplicates (13)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
 * ------------------------------------------------------------------------
 *      PACKAGE:  [incr Tk]
 *  DESCRIPTION:  Building mega-widgets with [incr Tcl]
 *
 *  [incr Tk] provides a framework for building composite "mega-widgets"
 *  using [incr Tcl] classes.  It defines a set of base classes that are
 *  specialized to create all other widgets.
 *
 *  This part defines some utility procedures that are useful for
 *  [incr Tk].
 *
 * ========================================================================
 *  AUTHOR:  Michael J. McLennan
 *           Bell Labs Innovations for Lucent Technologies
 *           mmclennan@lucent.com
 *           http://www.tcltk.com/itcl
 *
 *     RCS:  $Id: itk_util.c,v 1.1 1998/07/27 18:45:25 stanton Exp $
 * ========================================================================
 *           Copyright (c) 1993-1998  Lucent Technologies, Inc.
 * ------------------------------------------------------------------------
 * See the file "license.terms" for information on usage and redistribution
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 */
#include "itk.h"


/*
 * ------------------------------------------------------------------------
 *  Itk_OptListInit()
 *
 *  Initializes an ordered option list, allocating a certain amount of
 *  memory for an initial option list.
 * ------------------------------------------------------------------------
 */
void
Itk_OptListInit(olist, options)
    ItkOptList *olist;       /* list to be initialized */
    Tcl_HashTable *options;  /* table containing the real option entries */
{
    olist->options = options;
    olist->len = 0;
    olist->max = 10;
    olist->list = (Tcl_HashEntry**)ckalloc(
        (unsigned)(olist->max*sizeof(Tcl_HashEntry*))
    );
}


/*
 * ------------------------------------------------------------------------
 *  Itk_OptListFree()
 *
 *  Frees an ordered option list created by Itk_OptListInit().
 *  This only frees the memory associated with the list, not the
 *  list itself.
 * ------------------------------------------------------------------------
 */
void
Itk_OptListFree(olist)
    ItkOptList *olist;     /* list to be freed */
{
    ckfree((char*)olist->list);
    olist->len = olist->max = 0;
}


/*
 * ------------------------------------------------------------------------
 *  Itk_OptListAdd()
 *
 *  Adds the hash table entry for an option like '-background' to an
 *  ordered list of options.  The list is kept in alphabetical order,
 *  so that it can be searched quickly and printed out in order.
 * ------------------------------------------------------------------------
 */
void
Itk_OptListAdd(olist, entry)
    ItkOptList *olist;     /* ordered list */
    Tcl_HashEntry *entry;  /* entry to be added to the list */
{
    int i, first, last, cmp, pos, size;
    Tcl_HashEntry** newOrder;
    char *swname, *optname;

    /*
     *  Make sure that the option list is big enough.  Resize
     *  if needed.
     */
    if (olist->len >= olist->max) {
        size = olist->max*sizeof(Tcl_HashEntry*);
        newOrder = (Tcl_HashEntry**)ckalloc((unsigned)2*size);
        memcpy((VOID*)newOrder, (VOID*)olist->list, (size_t)size);
        ckfree((char*)olist->list);

        olist->list = newOrder;
        olist->max *= 2;
    }

    /*
     *  Perform a binary search to find the option switch quickly.
     */
    first = 0;
    last  = olist->len-1;
    swname = Tcl_GetHashKey(olist->options, entry) + 1;

    while (last >= first) {
        pos = (first+last)/2;
        optname = Tcl_GetHashKey(olist->options, olist->list[pos]) + 1;
        if (*swname == *optname) {
            cmp = strcmp(swname, optname);
            if (cmp == 0) {
                break;    /* found it! */
            }
        }
        else if (*swname < *optname) {
            cmp = -1;
        }
        else {
            cmp = 1;
        }

        if (cmp > 0)
            first = pos+1;
        else
            last = pos-1;
    }

    /*
     *  If a matching entry was not found, then insert one.
     */
    if (last < first) {
        pos = first;

        for (i=olist->len; i > pos; i--) {
            olist->list[i] = olist->list[i-1];
        }
        olist->list[pos] = entry;
        olist->len++;
    }
}


/*
 * ------------------------------------------------------------------------
 *  Itk_OptListRemove()
 *
 *  Removes a hash table entry from an ordered list of options.
 *  This negates the action of Itk_OptionListAdd(), and is usually
 *  called when an option is completely removed from a mega-widget.
 *  This should be called before the entry is removed from the
 *  real option table.
 * ------------------------------------------------------------------------
 */
void
Itk_OptListRemove(olist, entry)
    ItkOptList *olist;     /* ordered list */
    Tcl_HashEntry *entry;  /* entry to be removed from the list */
{
    int pos = 0;
    int i, first, last, cmp;
    char *swname, *optname;

    first = 0;
    last  = olist->len-1;
    swname = Tcl_GetHashKey(olist->options, entry) + 1;

    while (last >= first) {
        pos = (first+last)/2;
        optname = Tcl_GetHashKey(olist->options, olist->list[pos]) + 1;
        if (*swname == *optname) {
            cmp = strcmp(swname, optname);
            if (cmp == 0) {
                break;    /* found it! */
            }
        }
        else if (*swname < *optname) {
            cmp = -1;
        }
        else {
            cmp = 1;
        }

        if (cmp > 0)
            first = pos+1;
        else
            last = pos-1;
    }

    /*
     *  If a matching entry was found, then remove it.
     */
    if (last >= first) {
        olist->len--;
        for (i=pos; i < olist->len; i++) {
            olist->list[i] = olist->list[i+1];
        }
    }
}