1/* $NetBSD$ */ 2 3/*++ 4/* NAME 5/* name_code 3 6/* SUMMARY 7/* name to number table mapping 8/* SYNOPSIS 9/* #include <name_code.h> 10/* 11/* typedef struct { 12/* .in +4 13/* const char *name; 14/* int code; 15/* .in -4 16/* } NAME_CODE; 17/* 18/* int name_code(table, flags, name) 19/* const NAME_CODE *table; 20/* int flags; 21/* const char *name; 22/* 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". 31/* 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. 40/* 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/*--*/ 55 56/* System library. */ 57 58#include <sys_defs.h> 59#include <string.h> 60 61#ifdef STRCASECMP_IN_STRINGS_H 62#include <strings.h> 63#endif 64 65/* Utility library. */ 66 67#include <name_code.h> 68 69/* name_code - look up code by name */ 70 71int name_code(const NAME_CODE *table, int flags, const char *name) 72{ 73 const NAME_CODE *np; 74 int (*lookup) (const char *, const char *); 75 76 if (flags & NAME_CODE_FLAG_STRICT_CASE) 77 lookup = strcmp; 78 else 79 lookup = strcasecmp; 80 81 for (np = table; np->name; np++) 82 if (lookup(name, np->name) == 0) 83 break; 84 return (np->code); 85} 86 87/* str_name_code - look up name by code */ 88 89const char *str_name_code(const NAME_CODE *table, int code) 90{ 91 const NAME_CODE *np; 92 93 for (np = table; np->name; np++) 94 if (code == np->code) 95 break; 96 return (np->name); 97} 98