/src/neomutt/mutt/mapping.c
Line | Count | Source (jump to first uncovered line) |
1 | | /** |
2 | | * @file |
3 | | * Store links between a user-readable string and a constant |
4 | | * |
5 | | * @authors |
6 | | * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org> |
7 | | * Copyright (C) 2017 Richard Russon <rich@flatcap.org> |
8 | | * |
9 | | * @copyright |
10 | | * This program is free software: you can redistribute it and/or modify it under |
11 | | * the terms of the GNU General Public License as published by the Free Software |
12 | | * Foundation, either version 2 of the License, or (at your option) any later |
13 | | * version. |
14 | | * |
15 | | * This program is distributed in the hope that it will be useful, but WITHOUT |
16 | | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
17 | | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
18 | | * details. |
19 | | * |
20 | | * You should have received a copy of the GNU General Public License along with |
21 | | * this program. If not, see <http://www.gnu.org/licenses/>. |
22 | | */ |
23 | | |
24 | | /** |
25 | | * @page mutt_mapping Map between a string and a constant |
26 | | * |
27 | | * Map a string to a constant and vice versa. |
28 | | */ |
29 | | |
30 | | #include "config.h" |
31 | | #include <stddef.h> |
32 | | #include "mapping.h" |
33 | | #include "string2.h" |
34 | | |
35 | | /** |
36 | | * mutt_map_get_name - Lookup a string for a constant |
37 | | * @param val ID to locate in map |
38 | | * @param map NULL-terminated map of strings and constants |
39 | | * @retval ptr String matching ID |
40 | | * @retval NULL Error, or ID not found |
41 | | */ |
42 | | const char *mutt_map_get_name(int val, const struct Mapping *map) |
43 | 0 | { |
44 | 0 | if (!map) |
45 | 0 | return NULL; |
46 | | |
47 | 0 | for (size_t i = 0; map[i].name; i++) |
48 | 0 | if (map[i].value == val) |
49 | 0 | return map[i].name; |
50 | | |
51 | 0 | return NULL; |
52 | 0 | } |
53 | | |
54 | | /** |
55 | | * mutt_map_get_value_n - Lookup the constant for a string |
56 | | * @param name String to locate in map |
57 | | * @param len Length of the name string (need not be null terminated) |
58 | | * @param map NULL-terminated map of strings and constants |
59 | | * @retval num ID matching string |
60 | | * @retval -1 Error, or string not found |
61 | | */ |
62 | | int mutt_map_get_value_n(const char *name, size_t len, const struct Mapping *map) |
63 | 1.17k | { |
64 | 1.17k | if (!name || (len == 0) || !map) |
65 | 0 | return -1; |
66 | | |
67 | 12.0k | for (size_t i = 0; map[i].name; i++) |
68 | 11.6k | { |
69 | 11.6k | if (mutt_istrn_equal(map[i].name, name, len) && (map[i].name[len] == '\0')) |
70 | 810 | { |
71 | 810 | return map[i].value; |
72 | 810 | } |
73 | 11.6k | } |
74 | | |
75 | 367 | return -1; |
76 | 1.17k | } |
77 | | |
78 | | /** |
79 | | * mutt_map_get_value - Lookup the constant for a string |
80 | | * @param name String to locate in map |
81 | | * @param map NULL-terminated map of strings and constants |
82 | | * @retval num ID matching string |
83 | | * @retval -1 Error, or string not found |
84 | | */ |
85 | | int mutt_map_get_value(const char *name, const struct Mapping *map) |
86 | 0 | { |
87 | 0 | return mutt_map_get_value_n(name, mutt_str_len(name), map); |
88 | 0 | } |