NeoMutt  2020-11-20
Teaching an old dog new tricks
DOXYGEN
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. More...
 
int mutt_map_get_value_n (const char *name, size_t len, const struct Mapping *map)
 Lookup the constant for a string. More...
 
int mutt_map_get_value (const char *name, const struct Mapping *map)
 Lookup the constant for a string. More...
 

Detailed Description

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

Authors
  • Michael R. Elkins
  • Richard Russon

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
Definition: mapping.h:33
+ 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 }
int value
Definition: mapping.h:34
bool mutt_istrn_equal(const char *a, const char *b, size_t l)
Check for equality of two strings ignoring case (to a maximum), safely.
Definition: string.c:621
const char * name
Definition: mapping.h:33
+ 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:631
+ Here is the call graph for this function:
+ Here is the caller graph for this function: