Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add generalized string array support for new code. Signed-off-by: Mark Rustad <mark.d.rustad@intel.com> Tested-by: Ross Brattain <ross.b.brattain@intel.com> Signed-off-by: Robert Love <robert.w.love@intel.com>
- Loading branch information
Showing
1 changed file
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright (c) 2011 Intel Corporation. All rights reserved. | ||
* | ||
* This program is free software; you can redistribute it and/or modify it | ||
* under the terms and conditions of the GNU General Public License, | ||
* version 2, as published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope 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 | ||
* this program; if not, write to the Free Software Foundation, Inc., | ||
* 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. | ||
* | ||
* Maintained at www.Open-FCoE.org | ||
*/ | ||
|
||
#ifndef _STRARR_H_ | ||
#define _STRARR_H_ | ||
|
||
typedef const char * const _strarr[]; | ||
|
||
struct _str_arr_desc { | ||
const unsigned limit; | ||
const _strarr * const str_arr; | ||
const char * const unknown_str; | ||
const char * const out_of_range_str; | ||
}; | ||
|
||
#define GEN_STR_ARR(sc, sz, nm, unk, oor, ...) \ | ||
static const char * const _##nm##_strs[sz] = { __VA_ARGS__ }; \ | ||
sc const struct _str_arr_desc nm = { \ | ||
.limit = sizeof(_##nm##_strs) / sizeof(_##nm##_strs[0]),\ | ||
.str_arr = &_##nm##_strs, \ | ||
.unknown_str = unk, \ | ||
.out_of_range_str = oor, \ | ||
} | ||
|
||
#define EXT_STR_ARR(nm, unk, oor, ...) \ | ||
GEN_STR_ARR(, , nm, unk, oor, __VA_ARGS__) | ||
#define EXT_STR_ARR_SZ(nm, sz, unk, oor, ...) \ | ||
GEN_STR_ARR(, sz, nm, unk, oor, __VA_ARGS__) | ||
#define STR_ARR(nm, unk, oor, ...) \ | ||
GEN_STR_ARR(static, , nm, unk, oor, __VA_ARGS__) | ||
#define STR_ARR_SZ(nm, sz, unk, oor, ...) \ | ||
GEN_STR_ARR(static, sz, nm, unk, oor, __VA_ARGS__) | ||
|
||
static inline const char * | ||
getstr(const struct _str_arr_desc *desc, unsigned ix) | ||
{ | ||
const char *str; | ||
|
||
if (ix >= desc->limit) | ||
return desc->out_of_range_str; | ||
str = (*desc->str_arr)[ix]; | ||
if (!str) | ||
return desc->unknown_str; | ||
|
||
return str; | ||
} | ||
|
||
#endif /* _STRARR_H_ */ |