No empty .Rs/.Re
[netbsd-mini2440.git] / external / ibm-public / postfix / dist / src / util / name_code.c
blob94ecdae7361f059a4fda2255eeaa418f486e6c32
1 /* $NetBSD$ */
3 /*++
4 /* NAME
5 /* name_code 3
6 /* SUMMARY
7 /* name to number table mapping
8 /* SYNOPSIS
9 /* #include <name_code.h>
11 /* typedef struct {
12 /* .in +4
13 /* const char *name;
14 /* int code;
15 /* .in -4
16 /* } NAME_CODE;
18 /* int name_code(table, flags, name)
19 /* const NAME_CODE *table;
20 /* int flags;
21 /* const char *name;
23 /* const char *str_name_code(table, code)
24 /* const NAME_CODE *table;
25 /* int code;
26 /* DESCRIPTION
27 /* This module does simple name<->number mapping. The process
28 /* is controlled by a table of (name, code) values.
29 /* The table is terminated with a null pointer and a code that
30 /* corresponds to "name not found".
32 /* name_code() looks up the code that corresponds with the name.
33 /* The lookup is case insensitive. The flags argument specifies
34 /* zero or more of the following:
35 /* .IP NAME_CODE_FLAG_STRICT_CASE
36 /* String lookups are case sensitive.
37 /* .PP
38 /* For convenience the constant NAME_CODE_FLAG_NONE requests
39 /* no special processing.
41 /* str_name_code() translates a number to its equivalent string.
42 /* DIAGNOSTICS
43 /* When the search fails, the result is the "name not found" code
44 /* or the null pointer, respectively.
45 /* LICENSE
46 /* .ad
47 /* .fi
48 /* The Secure Mailer license must be distributed with this software.
49 /* AUTHOR(S)
50 /* Wietse Venema
51 /* IBM T.J. Watson Research
52 /* P.O. Box 704
53 /* Yorktown Heights, NY 10598, USA
54 /*--*/
56 /* System library. */
58 #include <sys_defs.h>
59 #include <string.h>
61 #ifdef STRCASECMP_IN_STRINGS_H
62 #include <strings.h>
63 #endif
65 /* Utility library. */
67 #include <name_code.h>
69 /* name_code - look up code by name */
71 int name_code(const NAME_CODE *table, int flags, const char *name)
73 const NAME_CODE *np;
74 int (*lookup) (const char *, const char *);
76 if (flags & NAME_CODE_FLAG_STRICT_CASE)
77 lookup = strcmp;
78 else
79 lookup = strcasecmp;
81 for (np = table; np->name; np++)
82 if (lookup(name, np->name) == 0)
83 break;
84 return (np->code);
87 /* str_name_code - look up name by code */
89 const char *str_name_code(const NAME_CODE *table, int code)
91 const NAME_CODE *np;
93 for (np = table; np->name; np++)
94 if (code == np->code)
95 break;
96 return (np->name);