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 "suricata-common.h"
25 
26 #include "util-enum.h"
27 #include "util-debug.h"
28 
29 /**
30  * \brief Maps a string name to an enum value from the supplied table. Please
31  * specify the last element of any map table with a {NULL, -1}. If
32  * missing, you will be welcomed with a segfault :)
33  *
34  * \param enum_name Character string that has to be mapped to an enum value
35  * from the table
36  * \param table Enum-Char table, from which the mapping is retrieved
37  *
38  * \retval result The enum_value for the enum_name string or -1 on failure
39  */
40 int SCMapEnumNameToValue(const char *enum_name, SCEnumCharMap *table)
41 {
42  int result = -1;
43 
44  if (enum_name == NULL || table == NULL) {
45  SCLogDebug("Invalid argument(s) passed into SCMapEnumNameToValue");
46  return -1;
47  }
48 
49  for (; table->enum_name != NULL; table++) {
50  if (strcasecmp(table->enum_name, enum_name) == 0) {
51  result = table->enum_value;
52  break;
53  }
54  }
55 
56  return result;
57 }
58 
59 /**
60  * \brief Maps an enum value to a string name, from the supplied table
61  *
62  * \param enum_value Enum_value that has to be mapped to a string_value
63  * from the table
64  * \param table Enum-Char table, from which the mapping is retrieved
65  *
66  * \retval result The enum_name for the enum_value supplied or NULL on failure
67  */
68 const char * SCMapEnumValueToName(int enum_value, SCEnumCharMap *table)
69 {
70  if (table == NULL) {
71  SCLogDebug("Invalid argument(s) passed into SCMapEnumValueToName");
72  return NULL;
73  }
74 
75  for (; table->enum_name != NULL; table++) {
76  if (table->enum_value == enum_value) {
77  return table->enum_name;
78  }
79  }
80 
81  SCLogDebug("A enum by the value %d doesn't exist in this table", enum_value);
82 
83  return NULL;
84 }
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
SCEnumCharMap_::enum_value
int enum_value
Definition: util-enum.h:29
util-debug.h
SCMapEnumValueToName
const char * SCMapEnumValueToName(int enum_value, SCEnumCharMap *table)
Maps an enum value to a string name, from the supplied table.
Definition: util-enum.c:68
SCMapEnumNameToValue
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:40
suricata-common.h
SCEnumCharMap_
Definition: util-enum.h:27
SCEnumCharMap_::enum_name
const char * enum_name
Definition: util-enum.h:28
util-enum.h