Changeset main,2.1.2


Ignore:
Timestamp:
09/21/2007 06:47:41 AM (19 years ago)
Author:
David Owen <dsowen@…>
branch-nick:
tui
revision id:
dsowen@fugue88.ws-20070921064741-czpbr5h8c96s8e1u
Message:

Added key to menu handle to catch re-use of expired menus.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/menu2.c

    r2 r2.1.2  
    55
    66
    7 typedef int menu2_h;
    8 
     7#define _MAX_MENUS 16
    98#define _MAX_ITEMS 16
    109
     10#define _KEY_BITS 4
     11#define _IDX_MASK (~(0u) << _KEY_BITS)
     12#define _KEY_MASK (~_IDX_MASK)
     13
     14
     15
     16typedef unsigned menu2_h;
     17
    1118struct menu2_t {
     19        unsigned key;
     20
    1221        char *item_names[_MAX_ITEMS];
    1322        char *item_descriptions[_MAX_ITEMS];
     
    1827};
    1928
    20 static const int _MAX_MENUS = 16;
     29static struct menu2_t *_menus[_MAX_MENUS];
    2130
    2231
    23 
    24 static struct menu2_t *_menus[16];
    25 
    26 
    27 
    28 static int menu2_init(int i)
     32static menu2_h _menu2_init(unsigned i)
    2933{
    3034        _menus[i] = calloc(1, sizeof(struct menu2_t));
    3135        if(!_menus[i]) return 0;
    32         return i + 1;
     36
     37        do {
     38                _menus[i]->key = rand() & _KEY_MASK;
     39        } while(i == 0 && _menus[i]->key == 0);
     40
     41        return (i << _KEY_BITS) | _menus[i]->key;
     42}
     43
     44static struct menu2_t **_check_ptr(menu2_h menu)
     45{
     46        unsigned i, k;
     47        struct menu2_t **m;
     48
     49        if(!menu) return NULL;
     50
     51        i = menu >> _KEY_BITS;
     52        k = menu & _KEY_MASK;
     53
     54        if(i >= _MAX_MENUS) return NULL;
     55
     56        m = _menus + i;
     57
     58        if(!*m) return NULL;
     59        if((*m)->key != k) return NULL;
     60
     61        return m;
     62}
     63
     64static struct menu2_t *_check(menu2_h menu)
     65{
     66        struct menu2_t **m = _check_ptr(menu);
     67        if(!m) return NULL;
     68        return *m;
    3369}
    3470
     
    3874
    3975        for(i = 0; i < _MAX_MENUS; i++)
    40                 if(!_menus[i]) return menu2_init(i);
     76                if(!_menus[i]) return _menu2_init(i);
    4177
    4278        return 0;
    4379}
    4480
     81static int _filled(const char *str)
     82{
     83        return str && (strlen(str) > 0);
     84}
     85
    4586int menu2_add_item(menu2_h menu, const char *name, const char *description)
    4687{
    47         struct menu2_t *m = _menus[menu - 1];
     88        struct menu2_t *m = _check(menu);
    4889        int *i;
    4990
     
    5495
    5596        m->item_names[*i] = strdup(name);
    56         m->item_descriptions[*i] = strdup(description ? description : name);
     97        m->item_descriptions[*i] =
     98                strdup(_filled(description) ? description : name);
    5799        m->item_structs[*i] =
    58100                new_item(m->item_names[*i], m->item_descriptions[*i]);
     
    65107int menu2_destroy(menu2_h menu)
    66108{
    67         struct menu2_t *m = _menus[menu - 1];
     109        struct menu2_t **m = _check_ptr(menu);
    68110        int i;
    69111
    70112        if(!m) return 0;
    71113
    72         for(i = 0; i < m->item_count; i++) {
    73                 free_item(m->item_structs[i]);
    74                 free(m->item_descriptions[i]);
    75                 free(m->item_names[i]);
     114        for(i = 0; i < (*m)->item_count; i++) {
     115                free_item((*m)->item_structs[i]);
     116                free((*m)->item_descriptions[i]);
     117                free((*m)->item_names[i]);
    76118        }
    77119
    78         free(m);
    79         _menus[menu - 1] = NULL;
     120        free(*m);
     121        *m = NULL;
    80122
    81123        return 1;
     
    84126int menu2_post(menu2_h menu)
    85127{
    86         struct menu2_t *m = _menus[menu - 1];
     128        struct menu2_t *m = _check(menu);
    87129
    88130        if(!m) return 0;
     
    97139int menu2_drive(menu2_h menu, int c)
    98140{
    99         struct menu2_t *m = _menus[menu - 1];
     141        struct menu2_t *m = _check(menu);
    100142
    101143        if(!m) return 0;
     
    109151int menu2_unpost(menu2_h menu)
    110152{
    111         struct menu2_t *m = _menus[menu - 1];
     153        struct menu2_t *m = _check(menu);
    112154
    113155        if(!m) return 0;
Note: See TracChangeset for help on using the changeset viewer.