* File name: m_stack.h
* Author: Seree Rakwong
* Date: 16-SEP-2013
*
*/
#ifndef _MSTACK_H_
#define _MSTACK_H_
#include "m_list.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef void* mstack_t;
typedef void* mstack_item_t;
long mstack_init();
void mstack_release();
mstack_t mstack_create(int items);
void mstack_destroy(mstack_t stack);
int mstack_is_full(mstack_t stack);
int mstack_is_empty(mstack_t stack);
void mstack_pop(mstack_t stack);
int mstack_push(mstack_t stack, const void *data, size_t size);
size_t mstack_get_top(mstack_t stack, void *data, size_t size);
#ifdef __cplusplus
}
#endif
#endif /* _MSTACK_H_ */
/*
* File name: m_stack.c
* Author: Seree Rakwong
* Date: 16-SEP-2013
*
*/
#include "m_stack.h"
#define MSTACK_SIGNATURE 0x5354434b
#ifdef __cplusplus
extern "C" {
#endif
typedef struct mstack_s
{
mlist_t list;
unsigned long items;
unsigned long signature;
} mstack_s, *mstack_sp;
/*
* #################### BOF helper functions ####################
*/
mstack_sp mstack_verify_stack(mstack_t stack);
mstack_sp mstack_verify_stack(mstack_t stack)
{
mstack_sp pstack = (mstack_sp)stack;
if (!pstack || pstack->signature != MSTACK_SIGNATURE)
return 0;
return pstack;
}
/*
#################### EOF helper functions ####################
*/
long mstack_init()
{
return mlist_init();
}
void mstack_release()
{
mlist_release();
}
mstack_t mstack_create(int items)
{
mstack_sp pstack = (mstack_sp)malloc(sizeof(mstack_s));
if (!pstack)
return 0;
if (items <= 0)
items = 1024;
pstack->list = mlist_create();
pstack->items = items;
pstack->signature = MSTACK_SIGNATURE;
return (mstack_t)pstack;
}
void mstack_destroy(mstack_t stack)
{
mstack_sp pstack = mstack_verify_stack(stack);
if (!pstack)
return;
mlist_destroy(pstack->list);
free(pstack);
pstack = 0;
}
int mstack_is_full(mstack_t stack)
{
mstack_sp pstack = mstack_verify_stack(stack);
if (!pstack)
return 1;
return (pstack->items == mlist_get_count(pstack->list) ? 1 : 0);
}
int mstack_is_empty(mstack_t stack)
{
mstack_sp pstack = mstack_verify_stack(stack);
if (!pstack)
return 1;
return (0 == mlist_get_count(pstack->list) ? 1 : 0);
}
void mstack_pop(mstack_t stack)
{
mstack_sp pstack = mstack_verify_stack(stack);
if (!pstack)
return;
if (mstack_is_empty(stack) == 1)
return;
mlist_remove_first(pstack->list);
}
int mstack_push(mstack_t stack, const void *data, size_t size)
{
mstack_sp pstack = mstack_verify_stack(stack);
if (!pstack)
return 0;
if (mstack_is_full(stack) == 1)
return 0;
mlist_insert_first(pstack->list, data, size);
return (mlist_get_count(pstack->list));
}
size_t mstack_get_top(mstack_t stack, void *data, size_t size)
{
mstack_sp pstack = mstack_verify_stack(stack);
if (!pstack)
return 0;
if (mstack_is_empty(stack) == 1)
return 0;
return mlist_get_item(mlist_get_begin(pstack->list), data, size);
}
#ifdef __cplusplus
}
#endif
No comments:
Post a Comment