PageRenderTime 22ms CodeModel.GetById 12ms app.highlight 7ms RepoModel.GetById 2ms app.codeStats 0ms

/bncsutil/src/bncsutil/stack.c

http://ghostcb.googlecode.com/
C | 113 lines | 61 code | 21 blank | 31 comment | 11 complexity | 7788092ac4f60ca4e10ee3b98f063ea1 MD5 | raw file
  1/**
  2 * BNCSutil
  3 * Battle.Net Utility Library
  4 *
  5 * Copyright (C) 2004-2006 Eric Naeseth
  6 *
  7 * Stack
  8 * August 16, 2005
  9 *
 10 * This library is free software; you can redistribute it and/or
 11 * modify it under the terms of the GNU Lesser General Public
 12 * License as published by the Free Software Foundation; either
 13 * version 2.1 of the License, or (at your option) any later version.
 14 *
 15 * This library is distributed in the hope that it will be useful,
 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 18 * Lesser General Public License for more details.
 19 *
 20 * A copy of the GNU Lesser General Public License is included in the BNCSutil
 21 * distribution in the file COPYING.  If you did not receive this copy,
 22 * write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 23 * Boston, MA  02111-1307  USA
 24 */
 25
 26/*
 27 * Mule Server
 28 * Copyright (c) 2004-2006 Eric Naeseth.
 29 *
 30 * Stack
 31 * May 13, 2005
 32 */
 33
 34#include <bncsutil/stack.h>
 35#include <stdlib.h>
 36 
 37cm_stack_t cm_stack_create()
 38{
 39	cm_stack_t stack = (cm_stack_t) calloc(1, sizeof(struct cm_stack));
 40	if (!stack)
 41		return (cm_stack_t) 0;
 42	return stack;
 43}
 44
 45void cm_stack_destroy(cm_stack_t stack)
 46{
 47	cm_stack_node_t* node;
 48	cm_stack_node_t* next;
 49	
 50	if (!stack)
 51		return;
 52	
 53	node = stack->top;
 54	
 55	while (node) {
 56		next = node->next;
 57		free(node);
 58		node = next;
 59	}
 60	
 61	free(stack);
 62}
 63
 64void cm_stack_push(cm_stack_t stack, void* item)
 65{
 66	cm_stack_node_t* new_node;
 67	
 68	if (!stack || !item)
 69		return;
 70	
 71	new_node = (cm_stack_node_t*) malloc(sizeof(cm_stack_node_t));
 72	if (!new_node)
 73		return;
 74	new_node->next = stack->top;
 75	new_node->value = item;
 76	
 77	stack->size++;
 78	
 79	stack->top = new_node;
 80}
 81
 82void* cm_stack_pop(cm_stack_t stack)
 83{
 84	cm_stack_node_t* next;
 85	void* value;
 86	
 87	if (!stack || !stack->top)
 88		return (void*) 0;
 89	
 90	next = stack->top->next;
 91	value = stack->top->value;
 92	free(stack->top);
 93	
 94	stack->top = next;
 95	stack->size--;
 96	return value;
 97}
 98
 99void* cm_stack_peek(cm_stack_t stack)
100{
101	if (!stack || !stack->top)
102		return (void*) 0;
103	
104	return stack->top->value;
105}
106
107unsigned int cm_stack_size(cm_stack_t stack)
108{
109	if (!stack)
110		return 0;
111	
112	return stack->size;
113}