chiark / gitweb /
Add many comments to server/play.c, in advance of possible
[disorder] / lib / selection.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder
65bb0fff 3 * Copyright (C) 2006, 2007 Richard Kettlewell
460b9539 4 *
e7eb3a27 5 * This program is free software: you can redistribute it and/or modify
460b9539 6 * it under the terms of the GNU General Public License as published by
e7eb3a27 7 * the Free Software Foundation, either version 3 of the License, or
460b9539 8 * (at your option) any later version.
e7eb3a27
RK
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 *
460b9539 15 * You should have received a copy of the GNU General Public License
e7eb3a27 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
460b9539 17 */
65bb0fff
RK
18/** @file lib/selection.c
19 * @brief Select management for Disobedience
20 */
460b9539 21
05b75f8d 22#include "common.h"
460b9539 23
24#include "mem.h"
25#include "hash.h"
26#include "selection.h"
27
65bb0fff
RK
28/** @brief Create a new selection manager
29 * @return Pointer to @ref hash used to manage the selection
30 */
460b9539 31hash *selection_new(void) {
32 return hash_new(sizeof (int));
33}
34
65bb0fff
RK
35/** @brief Add or remove a key in a selection
36 * @param h Hash representing selection
37 * @param key Key to insert
38 * @param selected non-0 if key is selected, 0 if it is not
39 *
40 * @p key is copied so the pointer need not remain valid. Newly selected keys
41 * are not marked as live.
42 */
460b9539 43void selection_set(hash *h, const char *key, int selected) {
4a99f7ba
RK
44 if(selected) {
45 int *const liveness = xmalloc_noptr(sizeof (int));
46 *liveness = 0;
47 hash_add(h, key, liveness, HASH_INSERT_OR_REPLACE);
48 } else
460b9539 49 hash_remove(h, key);
50}
51
65bb0fff
RK
52/** @brief Test whether a key is set in a selection
53 * @param h Hash representing selection
54 * @param key Key to check
55 * @return non-0 if key is present, 0 if it is not
56 */
460b9539 57int selection_selected(hash *h, const char *key) {
58 return hash_find(h, key) != 0;
59}
60
65bb0fff
RK
61/** @brief Invert a key's selection status
62 * @param h Hash representing selection
63 * @param key Key to flip
64 *
65 * If the key is selected as a result it is not marked as live.
66 */
460b9539 67void selection_flip(hash *h, const char *key) {
68 selection_set(h, key, !selection_selected(h, key));
69}
70
65bb0fff
RK
71/** @brief Mark a selection key as live
72 * @param h Hash representing selection
73 * @param key Key to mark as live
74 *
75 * Live keys will survive a call to selection_cleanup(). @p need not be in the
76 * selection (if it is not then the call will be ignored).
77 */
460b9539 78void selection_live(hash *h, const char *key) {
79 int *ptr = hash_find(h, key);
80
81 if(ptr)
82 *ptr = 1;
83}
84
85static int selection_cleanup_callback(const char *key,
86 void *value,
87 void *v) {
88 if(*(int *)value)
89 *(int *)value = 0;
90 else
91 hash_remove((hash *)v, key);
92 return 0;
93}
94
65bb0fff
RK
95/** @brief Delete all non-live keys from a selection
96 * @param h Hash representing selection
97 *
4a99f7ba 98 * After cleanup, no keys are marked as live.
65bb0fff 99 */
460b9539 100void selection_cleanup(hash *h) {
101 hash_foreach(h, selection_cleanup_callback, h);
102}
103
104static int selection_empty_callback(const char *key,
105 void attribute((unused)) *value,
106 void *v) {
107 hash_remove((hash *)v, key);
108 return 0;
109}
110
65bb0fff
RK
111/** @brief Remove all keys from a selection
112 * @param h Hash representing selection
113 */
460b9539 114void selection_empty(hash *h) {
115 hash_foreach(h, selection_empty_callback, h);
116}
117
118/*
119Local Variables:
120c-basic-offset:2
121comment-column:40
122fill-column:79
123indent-tabs-mode:nil
124End:
125*/