1*e89934bbSchristos /* $NetBSD: name_code.c,v 1.2 2017/02/14 01:16:49 christos Exp $ */
241fbaed0Stron
341fbaed0Stron /*++
441fbaed0Stron /* NAME
541fbaed0Stron /* name_code 3
641fbaed0Stron /* SUMMARY
741fbaed0Stron /* name to number table mapping
841fbaed0Stron /* SYNOPSIS
941fbaed0Stron /* #include <name_code.h>
1041fbaed0Stron /*
1141fbaed0Stron /* typedef struct {
1241fbaed0Stron /* .in +4
1341fbaed0Stron /* const char *name;
1441fbaed0Stron /* int code;
1541fbaed0Stron /* .in -4
1641fbaed0Stron /* } NAME_CODE;
1741fbaed0Stron /*
1841fbaed0Stron /* int name_code(table, flags, name)
1941fbaed0Stron /* const NAME_CODE *table;
2041fbaed0Stron /* int flags;
2141fbaed0Stron /* const char *name;
2241fbaed0Stron /*
2341fbaed0Stron /* const char *str_name_code(table, code)
2441fbaed0Stron /* const NAME_CODE *table;
2541fbaed0Stron /* int code;
2641fbaed0Stron /* DESCRIPTION
2741fbaed0Stron /* This module does simple name<->number mapping. The process
2841fbaed0Stron /* is controlled by a table of (name, code) values.
2941fbaed0Stron /* The table is terminated with a null pointer and a code that
3041fbaed0Stron /* corresponds to "name not found".
3141fbaed0Stron /*
3241fbaed0Stron /* name_code() looks up the code that corresponds with the name.
3341fbaed0Stron /* The lookup is case insensitive. The flags argument specifies
3441fbaed0Stron /* zero or more of the following:
3541fbaed0Stron /* .IP NAME_CODE_FLAG_STRICT_CASE
3641fbaed0Stron /* String lookups are case sensitive.
3741fbaed0Stron /* .PP
3841fbaed0Stron /* For convenience the constant NAME_CODE_FLAG_NONE requests
3941fbaed0Stron /* no special processing.
4041fbaed0Stron /*
4141fbaed0Stron /* str_name_code() translates a number to its equivalent string.
4241fbaed0Stron /* DIAGNOSTICS
4341fbaed0Stron /* When the search fails, the result is the "name not found" code
4441fbaed0Stron /* or the null pointer, respectively.
4541fbaed0Stron /* LICENSE
4641fbaed0Stron /* .ad
4741fbaed0Stron /* .fi
4841fbaed0Stron /* The Secure Mailer license must be distributed with this software.
4941fbaed0Stron /* AUTHOR(S)
5041fbaed0Stron /* Wietse Venema
5141fbaed0Stron /* IBM T.J. Watson Research
5241fbaed0Stron /* P.O. Box 704
5341fbaed0Stron /* Yorktown Heights, NY 10598, USA
5441fbaed0Stron /*--*/
5541fbaed0Stron
5641fbaed0Stron /* System library. */
5741fbaed0Stron
5841fbaed0Stron #include <sys_defs.h>
5941fbaed0Stron #include <string.h>
6041fbaed0Stron
6141fbaed0Stron /* Utility library. */
6241fbaed0Stron
6341fbaed0Stron #include <name_code.h>
6441fbaed0Stron
6541fbaed0Stron /* name_code - look up code by name */
6641fbaed0Stron
name_code(const NAME_CODE * table,int flags,const char * name)6741fbaed0Stron int name_code(const NAME_CODE *table, int flags, const char *name)
6841fbaed0Stron {
6941fbaed0Stron const NAME_CODE *np;
7041fbaed0Stron int (*lookup) (const char *, const char *);
7141fbaed0Stron
7241fbaed0Stron if (flags & NAME_CODE_FLAG_STRICT_CASE)
7341fbaed0Stron lookup = strcmp;
7441fbaed0Stron else
7541fbaed0Stron lookup = strcasecmp;
7641fbaed0Stron
7741fbaed0Stron for (np = table; np->name; np++)
7841fbaed0Stron if (lookup(name, np->name) == 0)
7941fbaed0Stron break;
8041fbaed0Stron return (np->code);
8141fbaed0Stron }
8241fbaed0Stron
8341fbaed0Stron /* str_name_code - look up name by code */
8441fbaed0Stron
str_name_code(const NAME_CODE * table,int code)8541fbaed0Stron const char *str_name_code(const NAME_CODE *table, int code)
8641fbaed0Stron {
8741fbaed0Stron const NAME_CODE *np;
8841fbaed0Stron
8941fbaed0Stron for (np = table; np->name; np++)
9041fbaed0Stron if (code == np->code)
9141fbaed0Stron break;
9241fbaed0Stron return (np->name);
9341fbaed0Stron }
94