Improve definitions of parse_ulong and parse_ullong:

- Use macro to eliminate duplicate body definitions.
- Rename okay as is_okay; add parenthesis to help "clarify" logic.


git-svn-id: svn://svn.berlios.de/openocd/trunk@2253 b42882b7-edfa-0310-969c-e2dbd0fdcd60
__archive__
zwelch 2009-06-17 00:29:56 +00:00
parent c928fe0fa0
commit 8b16068941
1 changed files with 11 additions and 14 deletions

View File

@ -863,17 +863,14 @@ long jim_global_long(const char *variable)
return 0; return 0;
} }
int parse_ullong(const char *str, unsigned long long *ul) #define DEFINE_PARSE_NUM_TYPE(name, type, func, max) \
{ int parse##name(const char *str, type *ul) \
char *end; { \
*ul = strtoull(str, &end, 0); char *end; \
bool okay = *str && !*end && ULLONG_MAX != *ul; *ul = func(str, &end, 0); \
return okay ? ERROR_OK : ERROR_COMMAND_SYNTAX_ERROR; bool is_okay = *str && !*end && (max != *ul); \
} return is_okay ? ERROR_OK : ERROR_COMMAND_SYNTAX_ERROR; \
int parse_ulong(const char *str, unsigned long *ul) }
{ DEFINE_PARSE_NUM_TYPE(_ulong, unsigned long , strtoul, ULONG_MAX)
char *end; DEFINE_PARSE_NUM_TYPE(_ullong, unsigned long long, strtoull, ULLONG_MAX)
*ul = strtoul(str, &end, 0);
bool okay = *str && !*end && ULONG_MAX != *ul;
return okay ? ERROR_OK : ERROR_COMMAND_SYNTAX_ERROR;
}