NeoMutt  2024-11-14-34-g5aaf0d
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
mapping.c File Reference

Store links between a user-readable string and a constant. More...

#include "config.h"
#include <stddef.h>
#include "mapping.h"
#include "string2.h"
+ Include dependency graph for mapping.c:

Go to the source code of this file.

Functions

const char * mutt_map_get_name (int val, const struct Mapping *map)
 Lookup a string for a constant.
 
int mutt_map_get_value_n (const char *name, size_t len, const struct Mapping *map)
 Lookup the constant for a string.
 
int mutt_map_get_value (const char *name, const struct Mapping *map)
 Lookup the constant for a string.
 

Detailed Description

Store links between a user-readable string and a constant.

Authors
  • Richard Russon
  • Pietro Cerutti

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, see http://www.gnu.org/licenses/.

Definition in file mapping.c.

Function Documentation

◆ mutt_map_get_name()

const char * mutt_map_get_name ( int  val,
const struct Mapping map 
)

Lookup a string for a constant.

Parameters
valID to locate in map
mapNULL-terminated map of strings and constants
Return values
ptrString matching ID
NULLError, or ID not found

Definition at line 42 of file mapping.c.

43{
44 if (!map)
45 return NULL;
46
47 for (size_t i = 0; map[i].name; i++)
48 if (map[i].value == val)
49 return map[i].name;
50
51 return NULL;
52}
const char * name
String value.
Definition: mapping.h:34
+ Here is the caller graph for this function:

◆ mutt_map_get_value_n()

int mutt_map_get_value_n ( const char *  name,
size_t  len,
const struct Mapping map 
)

Lookup the constant for a string.

Parameters
nameString to locate in map
lenLength of the name string (need not be null terminated)
mapNULL-terminated map of strings and constants
Return values
numID matching string
-1Error, or string not found

Definition at line 62 of file mapping.c.

63{
64 if (!name || (len == 0) || !map)
65 return -1;
66
67 for (size_t i = 0; map[i].name; i++)
68 {
69 if (mutt_istrn_equal(map[i].name, name, len) && (map[i].name[len] == '\0'))
70 {
71 return map[i].value;
72 }
73 }
74
75 return -1;
76}
bool mutt_istrn_equal(const char *a, const char *b, size_t num)
Check for equality of two strings ignoring case (to a maximum), safely.
Definition: string.c:453
int value
Integer value.
Definition: mapping.h:35
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_map_get_value()

int mutt_map_get_value ( const char *  name,
const struct Mapping map 
)

Lookup the constant for a string.

Parameters
nameString to locate in map
mapNULL-terminated map of strings and constants
Return values
numID matching string
-1Error, or string not found

Definition at line 85 of file mapping.c.

86{
87 return mutt_map_get_value_n(name, mutt_str_len(name), map);
88}
int mutt_map_get_value_n(const char *name, size_t len, const struct Mapping *map)
Lookup the constant for a string.
Definition: mapping.c:62
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:496
+ Here is the call graph for this function:
+ Here is the caller graph for this function: