1*e789a0dbScloder /* $OpenBSD: constants.c,v 1.10 2005/04/08 22:32:09 cloder Exp $ */
2eb840acdSniklas /* $EOM: constants.c,v 1.7 1999/04/02 00:57:31 niklas Exp $ */
32040585eSniklas
42040585eSniklas /*
5eb840acdSniklas * Copyright (c) 1998, 1999 Niklas Hallqvist. All rights reserved.
62040585eSniklas *
72040585eSniklas * Redistribution and use in source and binary forms, with or without
82040585eSniklas * modification, are permitted provided that the following conditions
92040585eSniklas * are met:
102040585eSniklas * 1. Redistributions of source code must retain the above copyright
112040585eSniklas * notice, this list of conditions and the following disclaimer.
122040585eSniklas * 2. Redistributions in binary form must reproduce the above copyright
132040585eSniklas * notice, this list of conditions and the following disclaimer in the
142040585eSniklas * documentation and/or other materials provided with the distribution.
152040585eSniklas *
162040585eSniklas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
172040585eSniklas * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
182040585eSniklas * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
192040585eSniklas * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
202040585eSniklas * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
212040585eSniklas * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
222040585eSniklas * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
232040585eSniklas * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
242040585eSniklas * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
252040585eSniklas * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
262040585eSniklas */
272040585eSniklas
282040585eSniklas /*
292040585eSniklas * This code was written under funding by Ericsson Radio Systems.
302040585eSniklas */
312040585eSniklas
322040585eSniklas #include <stdio.h>
332040585eSniklas #include <string.h>
342040585eSniklas
352040585eSniklas #include "constants.h"
362040585eSniklas
372040585eSniklas int
constant_value(struct constant_map * map,char * name)382040585eSniklas constant_value(struct constant_map *map, char *name)
392040585eSniklas {
402040585eSniklas struct constant_map *entry = map;
412040585eSniklas
422040585eSniklas for (entry = map; entry->name; entry++)
432040585eSniklas if (strcasecmp(entry->name, name) == 0)
442040585eSniklas return entry->value;
452040585eSniklas return 0;
462040585eSniklas }
472040585eSniklas
482040585eSniklas char *
constant_lookup(struct constant_map * map,int value)492040585eSniklas constant_lookup(struct constant_map *map, int value)
502040585eSniklas {
512040585eSniklas struct constant_map *entry = map;
522040585eSniklas
532040585eSniklas for (entry = map; entry->name; entry++)
542040585eSniklas if (entry->value == value)
552040585eSniklas return entry->name;
562040585eSniklas return 0;
572040585eSniklas }
582040585eSniklas
595ce8a46dSniklas struct constant_map *
constant_link_lookup(struct constant_map * map,int value)605ce8a46dSniklas constant_link_lookup(struct constant_map *map, int value)
615ce8a46dSniklas {
625ce8a46dSniklas struct constant_map *entry = map;
635ce8a46dSniklas
645ce8a46dSniklas for (entry = map; entry->name; entry++)
655ce8a46dSniklas if (entry->value == value)
665ce8a46dSniklas return entry->link;
675ce8a46dSniklas return 0;
685ce8a46dSniklas }
695ce8a46dSniklas
702040585eSniklas char *
constant_name(struct constant_map * map,int value)712040585eSniklas constant_name(struct constant_map *map, int value)
722040585eSniklas {
732040585eSniklas static char tmp[32];/* XXX Ugly, I know. */
742040585eSniklas char *retval = constant_lookup(map, value);
752040585eSniklas
76fb9475d6Sderaadt if (!retval) {
77f799be47Sho snprintf(tmp, sizeof tmp, "<Unknown %d>", value);
782040585eSniklas return tmp;
792040585eSniklas }
802040585eSniklas return retval;
812040585eSniklas }
822040585eSniklas
832040585eSniklas char *
constant_name_maps(struct constant_map ** maps,int value)842040585eSniklas constant_name_maps(struct constant_map **maps, int value)
852040585eSniklas {
862040585eSniklas static char tmp[32];/* XXX Ugly, I know. */
872040585eSniklas char *retval;
882040585eSniklas struct constant_map **map;
892040585eSniklas
90fb9475d6Sderaadt for (map = maps; *map; map++) {
912040585eSniklas retval = constant_lookup(*map, value);
922040585eSniklas if (retval)
932040585eSniklas return retval;
942040585eSniklas }
95f799be47Sho snprintf(tmp, sizeof tmp, "<Unknown %d>", value);
962040585eSniklas return tmp;
972040585eSniklas }
98