Main Page | Modules | Data Structures | Directories | File List | Data Fields | Globals

# stack.c

Go to the documentation of this file.

    00001 /* $Progeny$ */
    00002
    00014 /*
    00015  * AUTHOR: Josh Bressers <bressers@progeny.com>
    00016  *
    00017  * Copyright 2002 Progeny Linux Systems, Inc.
    00018  *
    00019  * Permission is hereby granted, free of charge, to any person obtaining a
    00020  * copy of this software and associated documentation files (the "Software"),
    00021  * to deal in the Software without restriction, including without limitation
    00022  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
    00023  * and/or sell copies of the Software, and to permit persons to whom the
    00024  * Software is furnished to do so, subject to the following conditions:
    00025  *
    00026  * The above copyright notice and this permission notice shall be included in
    00027  * all copies or substantial portions of the Software.
    00028  *
    00029  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    00030  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    00031  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
    00032  * THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    00033  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    00034  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
    00035  * DEALINGS IN THE SOFTWARE.
    00036  */
    00037
    00038 #include <stdio.h>
    00039 #include <assert.h>
    00040 #include <stdbool.h>
    00041 #include <stdlib.h>
    00042
    00043 #include <discover/utils.h>
    00044 #include <discover/stack.h>
    00045
    00046
    00048 discover_xml_stack * discover_xml_stack_new()
    00049 {
    00050     discover_xml_stack *stack;
    00051     stack = (discover_xml_stack *) _discover_xmalloc(sizeof(discover_xml_stack));
    00052     assert(stack != NULL);
    00053
    00054     stack->prev = NULL;
    00055     stack->data = NULL;
    00056     stack->depth = 0;
    00057
    00058     return stack;
    00059 }
    00060
    00062 void discover_xml_stack_destroy(discover_xml_stack *stack)
    00063 {
    00064     free(stack);
    00065 }
    00066
    00068 void
    00069 discover_xml_stack_push(discover_xml_stack **stack, void *data)
    00070 {
    00071     discover_xml_stack *new_stack;
    00072
    00073     new_stack = discover_xml_stack_new();
    00074
    00075
    00076     new_stack->prev = *stack;
    00077     new_stack->data = data;
    00078     new_stack->depth = (*stack)->depth + 1;
    00079     *stack = new_stack;
    00080 }
    00081
    00083 void * discover_xml_stack_pop(discover_xml_stack **stack)
    00084 {
    00085     void *ctx;
    00086     discover_xml_stack *oldstack;
    00087
    00088     if((*stack)->prev == 0) {
    00089         return NULL;
    00090     }
    00091
    00092     oldstack = *stack;
    00093     ctx = (*stack)->data;
    00094
    00095     *stack = (*stack)->prev;
    00096     (*stack)->depth = oldstack->depth - 1;
    00097     discover_xml_stack_destroy(oldstack);
    00098
    00099     return ctx;
    00100 }
    00101
    00103 void * discover_xml_stack_get(discover_xml_stack *stack)
    00104 {
    00105         return stack->data;
    00106 }
    00107
    00109 void * discover_xml_stack_getbynum(discover_xml_stack *stack, int i)
    00110 {
    00111     discover_xml_stack *S;
    00112
    00113     S = stack;
    00114     while(S->depth < i) {
    00115         S = stack->prev;
    00116     }
    00117     return S->data;
    00118 }
    00119
    00120 /*
    00121  * Local variables:
    00122  * c-file-style: "progeny"
    00123  * indent-tabs-mode: nil
    00124  * End:
    00125  */
    00126 /* vim: set cin fo=tcroq sw=4 et sts=4 tw=75: */