summaryrefslogtreecommitdiffhomepage
path: root/mixlib/mix_predicate_list.c
blob: 42f6ea613322eb1ef6c3d335df59a95bbb6dc0d4 (plain)
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
/* -*-c-*- -------------- mix_predicate_list.c :
 * Implementation of the functions declared in mix_predicate_list.h
 * ------------------------------------------------------------------
 *  Last change: Time-stamp: "01/07/18 23:02:26 jao"
 * ------------------------------------------------------------------
 * Copyright (C) 2001 Free Software Foundation, Inc.
 *  
 * 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 "mix_predicate_list.h"

/* the predicate list type */
struct mix_predicate_list_t 
{
  GSList *predicates;
  const mix_vm_t *vm;
  const mix_predicate_t *last;
};

/* a list of predicates */
mix_predicate_list_t *
mix_predicate_list_new (const mix_vm_t *vm)
{
  mix_predicate_list_t *result;
  g_return_val_if_fail (vm != NULL, NULL);
  result = g_new (mix_predicate_list_t, 1);
  result->predicates = NULL;
  result->vm = vm;
  result->last = NULL;
  return result;
}

void
mix_predicate_list_delete (mix_predicate_list_t *list)
{
  g_return_if_fail (list != NULL);
  g_slist_free (list->predicates);
  g_free (list);
}

/* evaluate the predicate list */
gboolean
mix_predicate_list_eval (mix_predicate_list_t *list)
{
  GSList *node;
  
  g_return_val_if_fail (list != NULL, FALSE);
  node = list->predicates;
  while (node) {
    mix_predicate_t *pred = (mix_predicate_t *)(node->data);
    if (mix_predicate_eval (pred, list->vm))
      {
	list->last = pred;
	return TRUE;
      }
    node = node->next;
  }
  list->last = NULL;
  return FALSE;
}

/* add/remove predicates to the list */
void
mix_predicate_list_add (mix_predicate_list_t *list, mix_predicate_t *predicate)
{
  g_return_if_fail (list != NULL);
  g_return_if_fail (predicate != NULL);
  if (!g_slist_find (list->predicates, predicate))
    list->predicates = g_slist_append (list->predicates, (gpointer)predicate);
  (void)mix_predicate_eval (predicate, list->vm);
}

gboolean
mix_predicate_list_remove (mix_predicate_list_t *list,
			   mix_predicate_t *predicate)
{
  g_return_val_if_fail (list != NULL, FALSE);
  g_return_val_if_fail (predicate != NULL, FALSE);
  if (g_slist_find (list->predicates, predicate))
    {
      list->predicates = g_slist_remove (list->predicates, predicate);
      return TRUE;
    }
  else
    return FALSE;
}

void
mix_predicate_list_clear (mix_predicate_list_t *list)
{
  g_return_if_fail (list != NULL);
  g_slist_free (list->predicates);
  list->predicates = NULL;
  list->last = NULL;
}

const mix_predicate_t *
mix_predicate_list_last_true_eval (const mix_predicate_list_t *list)
{
  g_return_val_if_fail (list != NULL, NULL);
  return list->last;
}