8 /* Callback functions prototypes */
9 static unsigned int state_hashf(const void *pkey
);
10 static int state_cmpf(const void *parg1
, const void *parg2
);
11 static void state_printf(const void *pkey
, const void *pdata
);
13 stret_t
state_init(state_t
**ppstate
, size_t size
, unsigned int factor
)
15 /* Allocate memory state's event table */
16 if ((*ppstate
= malloc(sizeof **ppstate
)) == NULL
)
19 if (((*ppstate
)->evttable
= malloc(sizeof *(*ppstate
)->evttable
)) == NULL
) {
24 /* Initialize hash table that stores the events the state can process */
25 if (htable_init((*ppstate
)->evttable
, size
, factor
,
26 state_hashf
, state_cmpf
, state_printf
) == HT_NOMEM
) {
27 free((*ppstate
)->evttable
);
32 /* Allocate memory for state's key */
33 if (((*ppstate
)->st_key
= malloc(sizeof *(*ppstate
)->st_key
)) == NULL
) {
34 free((*ppstate
)->evttable
);
42 stret_t
state_add_evt(state_t
*pstate
, unsigned int key
, const char *pdesc
,
43 void (*pactionf
)(void *pdata
), state_t
*pnewstate
)
48 /* Allocate memory for new key */
49 if ((pkey
= malloc(sizeof *pkey
)) == NULL
)
54 /* Allocate memory for new event */
55 if ((pevt
= malloc(sizeof *pevt
)) == NULL
) {
60 /* Fill in structure's members */
61 strncpy(pevt
->evt_desc
, pdesc
, MAX_EVT_DESC
);
62 pevt
->evt_actionf
= pactionf
;
63 pevt
->evt_newstate
= pnewstate
;
65 /* Insert event to hash table */
66 if (htable_insert(pstate
->evttable
, pkey
, pevt
) == HT_EXISTS
) {
75 stret_t
state_rem_evt(state_t
*pstate
, unsigned int key
)
77 if (htable_free_obj(pstate
->evttable
, &key
, HT_FREEKEY
| HT_FREEDATA
) == HT_NOTFOUND
)
83 unsigned int state_get_key(state_t
*pstate
)
85 return *pstate
->st_key
;
88 stret_t
state_free(state_t
*pstate
)
90 htable_free_all_obj(pstate
->evttable
, HT_FREEKEY
| HT_FREEDATA
);
91 htable_free(pstate
->evttable
);
92 free(pstate
->evttable
);
98 void state_print_evts(const state_t
*pstate
, FILE *fp
)
100 htable_print(pstate
->evttable
, fp
);
103 /* Callback funtions */
104 static unsigned int state_hashf(const void *pkey
)
106 return *(const unsigned int *) pkey
;
109 static int state_cmpf(const void *parg1
, const void *parg2
)
111 unsigned int a
= *(const unsigned int *) parg1
;
112 unsigned int b
= *(const unsigned int *) parg2
;
122 static void state_printf(const void *pkey
, const void *pdata
)
126 pevt
= (const event_t
*) pdata
;
127 printf("evtkey: %u\tdesc: %s\tnewstate: %u",
128 *(const unsigned int *) pkey
,
130 *(const unsigned int *) pevt
->evt_newstate
->st_key
);