Changeset main,2.1.2 for main
- Timestamp:
- 09/21/2007 06:47:41 AM (19 years ago)
- branch-nick:
- tui
- revision id:
- dsowen@fugue88.ws-20070921064741-czpbr5h8c96s8e1u
- File:
-
- 1 edited
-
main/menu2.c (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
-
main/menu2.c
r2 r2.1.2 5 5 6 6 7 typedef int menu2_h; 8 7 #define _MAX_MENUS 16 9 8 #define _MAX_ITEMS 16 10 9 10 #define _KEY_BITS 4 11 #define _IDX_MASK (~(0u) << _KEY_BITS) 12 #define _KEY_MASK (~_IDX_MASK) 13 14 15 16 typedef unsigned menu2_h; 17 11 18 struct menu2_t { 19 unsigned key; 20 12 21 char *item_names[_MAX_ITEMS]; 13 22 char *item_descriptions[_MAX_ITEMS]; … … 18 27 }; 19 28 20 static const int _MAX_MENUS = 16;29 static struct menu2_t *_menus[_MAX_MENUS]; 21 30 22 31 23 24 static struct menu2_t *_menus[16]; 25 26 27 28 static int menu2_init(int i) 32 static menu2_h _menu2_init(unsigned i) 29 33 { 30 34 _menus[i] = calloc(1, sizeof(struct menu2_t)); 31 35 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 44 static 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 64 static struct menu2_t *_check(menu2_h menu) 65 { 66 struct menu2_t **m = _check_ptr(menu); 67 if(!m) return NULL; 68 return *m; 33 69 } 34 70 … … 38 74 39 75 for(i = 0; i < _MAX_MENUS; i++) 40 if(!_menus[i]) return menu2_init(i);76 if(!_menus[i]) return _menu2_init(i); 41 77 42 78 return 0; 43 79 } 44 80 81 static int _filled(const char *str) 82 { 83 return str && (strlen(str) > 0); 84 } 85 45 86 int menu2_add_item(menu2_h menu, const char *name, const char *description) 46 87 { 47 struct menu2_t *m = _ menus[menu - 1];88 struct menu2_t *m = _check(menu); 48 89 int *i; 49 90 … … 54 95 55 96 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); 57 99 m->item_structs[*i] = 58 100 new_item(m->item_names[*i], m->item_descriptions[*i]); … … 65 107 int menu2_destroy(menu2_h menu) 66 108 { 67 struct menu2_t * m = _menus[menu - 1];109 struct menu2_t **m = _check_ptr(menu); 68 110 int i; 69 111 70 112 if(!m) return 0; 71 113 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]); 76 118 } 77 119 78 free( m);79 _menus[menu - 1]= NULL;120 free(*m); 121 *m = NULL; 80 122 81 123 return 1; … … 84 126 int menu2_post(menu2_h menu) 85 127 { 86 struct menu2_t *m = _ menus[menu - 1];128 struct menu2_t *m = _check(menu); 87 129 88 130 if(!m) return 0; … … 97 139 int menu2_drive(menu2_h menu, int c) 98 140 { 99 struct menu2_t *m = _ menus[menu - 1];141 struct menu2_t *m = _check(menu); 100 142 101 143 if(!m) return 0; … … 109 151 int menu2_unpost(menu2_h menu) 110 152 { 111 struct menu2_t *m = _ menus[menu - 1];153 struct menu2_t *m = _check(menu); 112 154 113 155 if(!m) return 0;
Note: See TracChangeset
for help on using the changeset viewer.
