Skip to content

Commit 128b8ea

Browse files
Piotr Gawlowiczcodebot
authored andcommitted
asn1,utils: add convert_enum_str function to be used in from_string in asn enums
1 parent 9030136 commit 128b8ea

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

include/srsran/asn1/asn1_utils.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,11 @@ void log_error_code(SRSASN_CODE code, const char* filename, int line);
122122
} while (0)
123123

124124
const char* convert_enum_idx(const char* array[], uint32_t nof_types, uint32_t enum_val, const char* enum_type);
125+
bool convert_enum_str(const char* array[],
126+
uint32_t nof_types,
127+
const char* str,
128+
uint32_t& enum_val,
129+
const char* enum_type);
125130
template <class ItemType>
126131
ItemType map_enum_number(ItemType* array, uint32_t nof_types, uint32_t enum_val, const char* enum_type);
127132

lib/asn1/asn1_utils.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,22 @@ const char* convert_enum_idx(const char* array[], uint32_t nof_types, uint32_t e
7171
return array[enum_val];
7272
}
7373

74+
bool convert_enum_str(const char* array[],
75+
uint32_t nof_types,
76+
const char* str,
77+
uint32_t& enum_val,
78+
const char* enum_type)
79+
{
80+
for (uint32_t i = 0; i < nof_types; ++i) {
81+
if (strcmp(str, array[i]) == 0) {
82+
enum_val = i;
83+
return true;
84+
}
85+
}
86+
log_error("The string '{}' is not a valid value for enum type {}.", str, enum_type);
87+
return false;
88+
}
89+
7490
template <class ItemType>
7591
ItemType map_enum_number(ItemType* array, uint32_t nof_types, uint32_t enum_val, const char* enum_type)
7692
{

0 commit comments

Comments
 (0)