chiark / gitweb /
Use tracksort_init() throughout the CGI.
[disorder] / lib / selection.c
CommitLineData
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
05b75f8d 24#include "common.h"
460b9539 25
26#include "mem.h"
27#include "hash.h"
28#include "selection.h"
29
65bb0fff
RK
30/** @brief Create a new selection manager
31 * @return Pointer to @ref hash used to manage the selection
32 */
460b9539 33hash *selection_new(void) {
34 return hash_new(sizeof (int));
35}
36
65bb0fff
RK
37/** @brief Add or remove a key in a selection
38 * @param h Hash representing selection
39 * @param key Key to insert
40 * @param selected non-0 if key is selected, 0 if it is not
41 *
42 * @p key is copied so the pointer need not remain valid. Newly selected keys
43 * are not marked as live.
44 */
460b9539 45void selection_set(hash *h, const char *key, int selected) {
4a99f7ba
RK
46 if(selected) {
47 int *const liveness = xmalloc_noptr(sizeof (int));
48 *liveness = 0;
49 hash_add(h, key, liveness, HASH_INSERT_OR_REPLACE);
50 } else
460b9539 51 hash_remove(h, key);
52}
53
65bb0fff
RK
54/** @brief Test whether a key is set in a selection
55 * @param h Hash representing selection
56 * @param key Key to check
57 * @return non-0 if key is present, 0 if it is not
58 */
460b9539 59int selection_selected(hash *h, const char *key) {
60 return hash_find(h, key) != 0;
61}
62
65bb0fff
RK
63/** @brief Invert a key's selection status
64 * @param h Hash representing selection
65 * @param key Key to flip
66 *
67 * If the key is selected as a result it is not marked as live.
68 */
460b9539 69void selection_flip(hash *h, const char *key) {
70 selection_set(h, key, !selection_selected(h, key));
71}
72
65bb0fff
RK
73/** @brief Mark a selection key as live
74 * @param h Hash representing selection
75 * @param key Key to mark as live
76 *
77 * Live keys will survive a call to selection_cleanup(). @p need not be in the
78 * selection (if it is not then the call will be ignored).
79 */
460b9539 80void selection_live(hash *h, const char *key) {
81 int *ptr = hash_find(h, key);
82
83 if(ptr)
84 *ptr = 1;
85}
86
87static int selection_cleanup_callback(const char *key,
88 void *value,
89 void *v) {
90 if(*(int *)value)
91 *(int *)value = 0;
92 else
93 hash_remove((hash *)v, key);
94 return 0;
95}
96
65bb0fff
RK
97/** @brief Delete all non-live keys from a selection
98 * @param h Hash representing selection
99 *
4a99f7ba 100 * After cleanup, no keys are marked as live.
65bb0fff 101 */
460b9539 102void selection_cleanup(hash *h) {
103 hash_foreach(h, selection_cleanup_callback, h);
104}
105
106static int selection_empty_callback(const char *key,
107 void attribute((unused)) *value,
108 void *v) {
109 hash_remove((hash *)v, key);
110 return 0;
111}
112
65bb0fff
RK
113/** @brief Remove all keys from a selection
114 * @param h Hash representing selection
115 */
460b9539 116void selection_empty(hash *h) {
117 hash_foreach(h, selection_empty_callback, h);
118}
119
120/*
121Local Variables:
122c-basic-offset:2
123comment-column:40
124fill-column:79
125indent-tabs-mode:nil
126End:
127*/