Commit | Line | Data |
---|---|---|
460b9539 | 1 | /* |
2 | * This file is part of DisOrder | |
65bb0fff | 3 | * Copyright (C) 2006, 2007 Richard Kettlewell |
460b9539 | 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 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but | |
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
18 | * USA | |
19 | */ | |
65bb0fff RK |
20 | /** @file lib/selection.c |
21 | * @brief Select management for Disobedience | |
22 | */ | |
460b9539 | 23 | |
24 | #include <config.h> | |
25 | #include "types.h" | |
26 | ||
27 | #include "mem.h" | |
28 | #include "hash.h" | |
29 | #include "selection.h" | |
30 | ||
65bb0fff RK |
31 | /** @brief Create a new selection manager |
32 | * @return Pointer to @ref hash used to manage the selection | |
33 | */ | |
460b9539 | 34 | hash *selection_new(void) { |
35 | return hash_new(sizeof (int)); | |
36 | } | |
37 | ||
65bb0fff RK |
38 | /** @brief Add or remove a key in a selection |
39 | * @param h Hash representing selection | |
40 | * @param key Key to insert | |
41 | * @param selected non-0 if key is selected, 0 if it is not | |
42 | * | |
43 | * @p key is copied so the pointer need not remain valid. Newly selected keys | |
44 | * are not marked as live. | |
45 | */ | |
460b9539 | 46 | void selection_set(hash *h, const char *key, int selected) { |
4a99f7ba RK |
47 | if(selected) { |
48 | int *const liveness = xmalloc_noptr(sizeof (int)); | |
49 | *liveness = 0; | |
50 | hash_add(h, key, liveness, HASH_INSERT_OR_REPLACE); | |
51 | } else | |
460b9539 | 52 | hash_remove(h, key); |
53 | } | |
54 | ||
65bb0fff RK |
55 | /** @brief Test whether a key is set in a selection |
56 | * @param h Hash representing selection | |
57 | * @param key Key to check | |
58 | * @return non-0 if key is present, 0 if it is not | |
59 | */ | |
460b9539 | 60 | int selection_selected(hash *h, const char *key) { |
61 | return hash_find(h, key) != 0; | |
62 | } | |
63 | ||
65bb0fff RK |
64 | /** @brief Invert a key's selection status |
65 | * @param h Hash representing selection | |
66 | * @param key Key to flip | |
67 | * | |
68 | * If the key is selected as a result it is not marked as live. | |
69 | */ | |
460b9539 | 70 | void selection_flip(hash *h, const char *key) { |
71 | selection_set(h, key, !selection_selected(h, key)); | |
72 | } | |
73 | ||
65bb0fff RK |
74 | /** @brief Mark a selection key as live |
75 | * @param h Hash representing selection | |
76 | * @param key Key to mark as live | |
77 | * | |
78 | * Live keys will survive a call to selection_cleanup(). @p need not be in the | |
79 | * selection (if it is not then the call will be ignored). | |
80 | */ | |
460b9539 | 81 | void selection_live(hash *h, const char *key) { |
82 | int *ptr = hash_find(h, key); | |
83 | ||
84 | if(ptr) | |
85 | *ptr = 1; | |
86 | } | |
87 | ||
88 | static int selection_cleanup_callback(const char *key, | |
89 | void *value, | |
90 | void *v) { | |
91 | if(*(int *)value) | |
92 | *(int *)value = 0; | |
93 | else | |
94 | hash_remove((hash *)v, key); | |
95 | return 0; | |
96 | } | |
97 | ||
65bb0fff RK |
98 | /** @brief Delete all non-live keys from a selection |
99 | * @param h Hash representing selection | |
100 | * | |
4a99f7ba | 101 | * After cleanup, no keys are marked as live. |
65bb0fff | 102 | */ |
460b9539 | 103 | void selection_cleanup(hash *h) { |
104 | hash_foreach(h, selection_cleanup_callback, h); | |
105 | } | |
106 | ||
107 | static int selection_empty_callback(const char *key, | |
108 | void attribute((unused)) *value, | |
109 | void *v) { | |
110 | hash_remove((hash *)v, key); | |
111 | return 0; | |
112 | } | |
113 | ||
65bb0fff RK |
114 | /** @brief Remove all keys from a selection |
115 | * @param h Hash representing selection | |
116 | */ | |
460b9539 | 117 | void selection_empty(hash *h) { |
118 | hash_foreach(h, selection_empty_callback, h); | |
119 | } | |
120 | ||
121 | /* | |
122 | Local Variables: | |
123 | c-basic-offset:2 | |
124 | comment-column:40 | |
125 | fill-column:79 | |
126 | indent-tabs-mode:nil | |
127 | End: | |
128 | */ |