suricata
util-enum.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2010 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Anoop Saldanha <anoopsaldanha@gmail.com>
22  */
23 
24 #include <stdio.h>
25 #include <string.h>
26 
27 #include "util-enum.h"
28 #include "util-debug.h"
29 
30 /**
31  * \brief Maps a string name to an enum value from the supplied table. Please
32  * specify the last element of any map table with a {NULL, -1}. If
33  * missing, you will be welcomed with a segfault :)
34  *
35  * \param enum_name Character string that has to be mapped to an enum value
36  * from the table
37  * \param table Enum-Char table, from which the mapping is retrieved
38  *
39  * \retval result The enum_value for the enum_name string or -1 on failure
40  */
41 int SCMapEnumNameToValue(const char *enum_name, SCEnumCharMap *table)
42 {
43  int result = -1;
44 
45  if (enum_name == NULL || table == NULL) {
46  SCLogDebug("Invalid argument(s) passed into SCMapEnumNameToValue");
47  return -1;
48  }
49 
50  for (; table->enum_name != NULL; table++) {
51  if (strcasecmp(table->enum_name, enum_name) == 0) {
52  result = table->enum_value;
53  break;
54  }
55  }
56 
57  return result;
58 }
59 
60 /**
61  * \brief Maps an enum value to a string name, from the supplied table
62  *
63  * \param enum_value Enum_value that has to be mapped to a string_value
64  * from the table
65  * \param table Enum-Char table, from which the mapping is retrieved
66  *
67  * \retval result The enum_name for the enum_value supplied or NULL on failure
68  */
69 const char * SCMapEnumValueToName(int enum_value, SCEnumCharMap *table)
70 {
71  if (table == NULL) {
72  SCLogDebug("Invalid argument(s) passed into SCMapEnumValueToName");
73  return NULL;
74  }
75 
76  for (; table->enum_name != NULL; table++) {
77  if (table->enum_value == enum_value) {
78  return table->enum_name;
79  }
80  }
81 
82  SCLogDebug("A enum by the value %d doesn't exist in this table", enum_value);
83 
84  return NULL;
85 }
#define SCLogDebug(...)
Definition: util-debug.h:335
const char * SCMapEnumValueToName(int enum_value, SCEnumCharMap *table)
Maps an enum value to a string name, from the supplied table.
Definition: util-enum.c:69
int SCMapEnumNameToValue(const char *enum_name, SCEnumCharMap *table)
Maps a string name to an enum value from the supplied table. Please specify the last element of any m...
Definition: util-enum.c:41
const char * enum_name
Definition: util-enum.h:28