~ubuntu-branches/ubuntu/trusty/postfix/trusty-proposed

« back to all changes in this revision

Viewing changes to src/util/name_code.c

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2005-02-27 09:33:07 UTC
  • Revision ID: james.westby@ubuntu.com-20050227093307-cn789t27ibnlh6tf
Tags: upstream-2.1.5
ImportĀ upstreamĀ versionĀ 2.1.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
/*      NAME_CODE *table;
 
18
/*      int     flags;
 
19
/*      const char *name;
 
20
/*
 
21
/*      const char *str_name_code(table, code)
 
22
/*      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 equivalend 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
#ifdef STRCASECMP_IN_STRINGS_H
 
60
#include <strings.h>
 
61
#endif
 
62
 
 
63
/* Utility library. */
 
64
 
 
65
#include <name_code.h>
 
66
 
 
67
/* name_code - look up code by name */
 
68
 
 
69
int     name_code(NAME_CODE *table, int flags, const char *name)
 
70
{
 
71
    NAME_CODE *np;
 
72
    int     (*lookup) (const char *, const char *);
 
73
 
 
74
    if (flags & NAME_CODE_FLAG_STRICT_CASE)
 
75
        lookup = strcmp;
 
76
    else
 
77
        lookup = strcasecmp;
 
78
 
 
79
    for (np = table; np->name; np++)
 
80
        if (lookup(name, np->name) == 0)
 
81
            break;
 
82
    return (np->code);
 
83
}
 
84
 
 
85
/* str_name_code - look up name by code */
 
86
 
 
87
const char *str_name_code(NAME_CODE *table, int code)
 
88
{
 
89
    NAME_CODE *np;
 
90
 
 
91
    for (np = table; np->name; np++)
 
92
        if (code == np->code)
 
93
            break;
 
94
    return (np->name);
 
95
}