File: queue.c

package info (click to toggle)
gimp 2.2.13-1etch4
  • links: PTS
  • area: main
  • in suites: etch
  • size: 94,832 kB
  • ctags: 47,113
  • sloc: ansic: 524,858; xml: 36,798; lisp: 9,870; sh: 9,409; makefile: 7,923; python: 2,674; perl: 2,589; yacc: 520; lex: 334
file content (150 lines) | stat: -rw-r--r-- 3,006 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * The GIMP Help Browser
 * Copyright (C) 1999 Sven Neumann <sven@gimp.org>
 *                    Michael Natterer <mitschel@cs.tu-berlin.de>
 *
 * queue.c - a history queue
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include <glib.h>

#include "queue.h"

struct _Queue
{
  GList *queue;
  GList *current;
};

Queue *
queue_new (void)
{
  Queue *h;

  h = g_malloc (sizeof (Queue));
  
  h->queue = NULL;
  h->current = NULL;

  return (h);
}

void
queue_free (Queue *h)
{
  g_return_if_fail (h != NULL);

  /* needs to free data in list as well! */
  if (h->queue)
    {
      g_list_foreach (h->queue, (GFunc) g_free, NULL);
      g_list_free (h->queue);
    }

  g_free (h);
}

void
queue_move_prev (Queue *h)
{
  if (!h || !h->queue || (h->current == g_list_first (h->queue)))
    return;

  h->current = g_list_previous (h->current);
}

void
queue_move_next (Queue *h)
{
  if (!h || !h->queue || (h->current == g_list_last (h->queue)))
    return;

  h->current = g_list_next (h->current);
}

const gchar *
queue_prev (Queue *h)
{
  GList *p;

  if (!h || !h->queue || (h->current == g_list_first (h->queue)))
    return NULL;

  p = g_list_previous (h->current);

  return (const gchar *) p->data;
}

const gchar *
queue_next (Queue *h)
{
  GList *p;

  if (!h || !h->queue || (h->current == g_list_last(h->queue)))
    return NULL;

  p = g_list_next (h->current);

  return (const gchar *) p->data;
}

void 
queue_add (Queue       *h,
	   const gchar *ref)
{
  GList *trash = NULL;

  g_return_if_fail (h != NULL);
  g_return_if_fail (ref != NULL);

  if (h->current)
    {
      trash = h->current->next;
      h->current->next = NULL;
    }

  h->queue   = g_list_append (h->queue, g_strdup (ref));
  h->current = g_list_last (h->queue);

  if (trash)
    {
      g_list_foreach (trash, (GFunc) g_free, NULL);
      g_list_free (trash);
    }	
}

gboolean
queue_has_next (Queue *h)
{
  if (!h || !h->queue || (h->current == g_list_last (h->queue)))
    return FALSE;
  
  return (g_list_next (h->current) != NULL);
}

gboolean
queue_has_prev (Queue *h)
{
  if (!h || !h->queue || (h->current == g_list_first (h->queue)))
    return FALSE;
  
  return (g_list_previous (h->current) != NULL);
}