summaryrefslogtreecommitdiffstats
path: root/lib/name_map.c
blob: d3ea2af77f9d558ff21b123109724c5f5d17214f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* Map value to name -- functions
 * Copyright (C) 2009 Chris Hall (GMCH), Highwayman
 *
 * This file is part of GNU Zebra.
 *
 * GNU Zebra is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation; either version 2, or (at your
 * option) any later version.
 *
 * GNU Zebra is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GNU Zebra; see the file COPYING.  If not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <misc.h>

#include "name_map.h"

/*==============================================================================
 * Direct Map
 */

/*------------------------------------------------------------------------------
 * Map given value to name, using a map_direct_t.
 */
extern name_str_t
map_direct(const map_direct_t map, int val)
{
  const char* name ;
  int         index ;

  name_str_t QFB_QFS(st, qfs) ;

  name = NULL ;
  if ((val >= map->min_val) && ((index = val - map->min_val) < map->count))
    name = map->body[index] ;

  if      (name != NULL)
    qfs_append(qfs, name) ;
  else if (map->deflt != NULL)
    qfs_printf(qfs, map->deflt, val) ;

  qfs_term(qfs) ;

  return st ;
} ;

/*------------------------------------------------------------------------------
 * Tiny test of map_direct()
 */
#if 0

#include "stdio.h"

extern void test_map_direct(void) ;

const char* test_map_body[] =
{
  [ 1]  = "one",
  [10]  = "ten",

  [ 2]  = "two",
  [ 7]  = "seven",
} ;

const map_direct_t test_map = map_direct_s(test_map_body, "DEFAULT[%d]") ;

enum { mm = -3 } ;

const char* test_map_body_m[] =
{
  [ -2 - mm]  = "m2",
  [ -1 - mm]  = "m1",

  [  3 - mm]  = "three",
  [  5 - mm]  = "five",
} ;

const map_direct_t test_map_m = map_direct_min_s(test_map_body_m, NULL, mm) ;

extern void
test_map_direct(void)
{
  int i ;

  for (i = -2 ; i <= 12 ; ++i)
    fprintf(stdout, "Val=%3d  name='%s'\n", i, map_direct(test_map, i).str) ;

  for (i = -5 ; i <= 9 ; ++i)
    fprintf(stdout, "Val=%3d  name='%s'\n", i, map_direct(test_map_m, i).str) ;
} ;

#endif