home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / file / managers / git-4.3 / git-4 / git-4.3.7 / src / xstack.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-08  |  2.8 KB  |  143 lines

  1. /* xstack.c -- simple stacks management file. */
  2.  
  3. /* Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
  4.  
  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 2, or (at your option)
  8.    any later version.
  9.  
  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU General Public License
  16.    along with this program; if not, write to the Free Software
  17.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. /* Written by Tudor Hulubei and Andrei Pitis.  */
  20.  
  21.  
  22. #ifdef HAVE_CONFIG_H
  23. #include <config.h>
  24. #endif
  25.  
  26. #include <stdio.h>
  27.  
  28. #ifdef HAVE_STDLIB_H
  29. #include <stdlib.h>
  30. #else /* !HAVE_STDLIB_H */
  31. #include "ansi_stdlib.h"
  32. #endif /* !HAVE_STDLIB_H */
  33.  
  34. #include <sys/types.h>
  35.  
  36. #include "xmalloc.h"
  37. #include "xstring.h"
  38. #include "xstack.h"
  39.  
  40.  
  41. /* Create a new stack.  */
  42.  
  43. xstack_t *
  44. xstack_init(esize)
  45.     int esize;
  46. {
  47.     xstack_t *s = (xstack_t *)xcalloc(1, sizeof(xstack_t));
  48.  
  49.     s->esize = esize;
  50.     return s;
  51. }
  52.  
  53.  
  54. /* Destroy a stack.  */
  55.  
  56. void
  57. xstack_end(stack)
  58.     xstack_t *stack;
  59. {
  60.     if (stack == NULL)
  61.         return;
  62.  
  63.     if (stack->data)
  64.         xfree(stack->data);
  65.  
  66.     xfree(stack);
  67. }
  68.  
  69.  
  70. /* Push one element to a stack.  */
  71.  
  72. void
  73. xstack_push(stack, data)
  74.     xstack_t *stack;
  75.     void *data;
  76. {
  77.     stack->data = (void *)xrealloc(stack->data, ++stack->point * stack->esize);
  78.  
  79.     memcpy(((char *)stack->data) + (stack->point - 1) * stack->esize,
  80.            data,
  81.            stack->esize);
  82. }
  83.  
  84.  
  85. /* Pop one element from a stack.  */
  86.  
  87. void *
  88. xstack_pop(stack, data)
  89.     xstack_t *stack;
  90.     void *data;
  91. {
  92.     if (stack->point == 0)
  93.         return NULL;
  94.  
  95.     memcpy(data,
  96.            ((char *)stack->data) + --stack->point * stack->esize,
  97.            stack->esize);
  98.  
  99.     stack->data = (void *)xrealloc(stack->data, stack->point * stack->esize);
  100.  
  101.     return data;
  102. }
  103.  
  104.  
  105. /* Preview the stack element located at 'offset' elements from the stack
  106.    point.  The stack point is located at offset = 0.  */
  107.  
  108. void *
  109. xstack_preview(stack, data, offset)
  110.     xstack_t *stack;
  111.     void *data;
  112.     int offset;
  113. {
  114.     if (stack->point == 0 || offset > stack->point)
  115.         return NULL;
  116.  
  117.     memcpy(data,
  118.            ((char *)stack->data) + (stack->point - offset) * stack->esize,
  119.            stack->esize);
  120.  
  121.     return data;
  122. }
  123.  
  124.  
  125. /* Truncate a stack to 'point' elements.  */
  126.  
  127. void
  128. xstack_truncate(stack, point)
  129.     xstack_t *stack;
  130.     int point;
  131. {
  132.     stack->point = point;
  133.     stack->data = (void *)xrealloc(stack->data, stack->point * stack->esize);
  134. }
  135.  
  136.  
  137. int
  138. xstack_point(stack)
  139.     xstack_t *stack;
  140. {
  141.     return stack->point;
  142. }
  143.