Postfix3.3.1
name_code.c
[詳解]
1 /*++
2 /* NAME
3 /* name_code 3
4 /* SUMMARY
5 /* name to number table mapping
6 /* SYNOPSIS
7 /* #include <name_code.h>
8 /*
9 /* typedef struct {
10 /* .in +4
11 /* const char *name;
12 /* int code;
13 /* .in -4
14 /* } NAME_CODE;
15 /*
16 /* int name_code(table, flags, name)
17 /* const NAME_CODE *table;
18 /* int flags;
19 /* const char *name;
20 /*
21 /* const char *str_name_code(table, code)
22 /* const NAME_CODE *table;
23 /* int code;
24 /* DESCRIPTION
25 /* This module does simple name<->number mapping. The process
26 /* is controlled by a table of (name, code) values.
27 /* The table is terminated with a null pointer and a code that
28 /* corresponds to "name not found".
29 /*
30 /* name_code() looks up the code that corresponds with the name.
31 /* The lookup is case insensitive. The flags argument specifies
32 /* zero or more of the following:
33 /* .IP NAME_CODE_FLAG_STRICT_CASE
34 /* String lookups are case sensitive.
35 /* .PP
36 /* For convenience the constant NAME_CODE_FLAG_NONE requests
37 /* no special processing.
38 /*
39 /* str_name_code() translates a number to its equivalent string.
40 /* DIAGNOSTICS
41 /* When the search fails, the result is the "name not found" code
42 /* or the null pointer, respectively.
43 /* LICENSE
44 /* .ad
45 /* .fi
46 /* The Secure Mailer license must be distributed with this software.
47 /* AUTHOR(S)
48 /* Wietse Venema
49 /* IBM T.J. Watson Research
50 /* P.O. Box 704
51 /* Yorktown Heights, NY 10598, USA
52 /*--*/
53 
54 /* System library. */
55 
56 #include <sys_defs.h>
57 #include <string.h>
58 
59 /* Utility library. */
60 
61 #include <name_code.h>
62 
63 /* name_code - look up code by name */
64 
65 int name_code(const NAME_CODE *table, int flags, const char *name)
66 {
67  const NAME_CODE *np;
68  int (*lookup) (const char *, const char *);
69 
70  if (flags & NAME_CODE_FLAG_STRICT_CASE)
71  lookup = strcmp;
72  else
73  lookup = strcasecmp;
74 
75  for (np = table; np->name; np++)
76  if (lookup(name, np->name) == 0)
77  break;
78  return (np->code);
79 }
80 
81 /* str_name_code - look up name by code */
82 
83 const char *str_name_code(const NAME_CODE *table, int code)
84 {
85  const NAME_CODE *np;
86 
87  for (np = table; np->name; np++)
88  if (code == np->code)
89  break;
90  return (np->name);
91 }
int code
Definition: name_code.h:19
#define NAME_CODE_FLAG_STRICT_CASE
Definition: name_code.h:23
const char * str_name_code(const NAME_CODE *table, int code)
Definition: name_code.c:83
const char * name
Definition: name_code.h:18
int name_code(const NAME_CODE *table, int flags, const char *name)
Definition: name_code.c:65
int int
Definition: smtpd_proxy.h:21
int strcasecmp(const char *s1, const char *s2)
Definition: strcasecmp.c:41