Moved map functions from clixon_string to _map, new ptr2ptr map

This commit is contained in:
Olof hagsand 2024-08-10 13:33:08 +02:00
parent 781c9d9d27
commit 275cefbae7
47 changed files with 366 additions and 180 deletions

View file

@ -960,126 +960,6 @@ uri_str2cvec(char *string,
goto done;
}
/*! Map from int to string using str2int map
*
* @param[in] ms String, integer map
* @param[in] i Input integer
* @retval str String value
* @retval NULL Error, not found
* @note linear search
*/
const char *
clicon_int2str(const map_str2int *mstab,
int i)
{
const struct map_str2int *ms;
for (ms = &mstab[0]; ms->ms_str; ms++)
if (ms->ms_int == i)
return ms->ms_str;
return NULL;
}
/*! Map from string to int using str2int map
*
* @param[in] ms String, integer map
* @param[in] str Input string
* @retval int Value
* @retval -1 Error, not found
* @see clicon_str2int_search for optimized lookup, but strings must be sorted
*/
int
clicon_str2int(const map_str2int *mstab,
char *str)
{
const struct map_str2int *ms;
for (ms = &mstab[0]; ms->ms_str; ms++)
if (strcmp(ms->ms_str, str) == 0)
return ms->ms_int;
return -1;
}
/*! Map from string to int using binary (alphatical) search
*
* @param[in] ms String, integer map
* @param[in] str Input string
* @param[in] low Lower bound index
* @param[in] upper Upper bound index
* @param[in] len Length of array (max)
* @param[out] found Integer found (can also be negative)
* @retval 1 Found with "found" value set.
* @retval 0 Not found
* @note Assumes sorted strings, tree search
*/
static int
str2int_search1(const map_str2int *mstab,
char *str,
int low,
int upper,
int len,
int *found)
{
const struct map_str2int *ms;
int mid;
int cmp;
if (upper < low)
return 0; /* not found */
mid = (low + upper) / 2;
if (mid >= len) /* beyond range */
return 0; /* not found */
ms = &mstab[mid];
if ((cmp = strcmp(str, ms->ms_str)) == 0){
*found = ms->ms_int;
return 1; /* found */
}
else if (cmp < 0)
return str2int_search1(mstab, str, low, mid-1, len, found);
else
return str2int_search1(mstab, str, mid+1, upper, len, found);
}
/*! Map from string to int using str2int map
*
* @param[in] ms String, integer map
* @param[in] str Input string
* @retval int Value
* @retval -1 Error, not found
* @note Assumes sorted strings, tree search
* @note -1 can not be value
*/
int
clicon_str2int_search(const map_str2int *mstab,
char *str,
int len)
{
int found;
if (str2int_search1(mstab, str, 0, len, len, &found))
return found;
return -1; /* not found */
}
/*! Map from string to string using str2str map
*
* @param[in] mstab String, string map
* @param[in] str Input string
* @retval str Output string
* @retval NULL Error, not found
*/
char*
clicon_str2str(const map_str2str *mstab,
char *str)
{
const struct map_str2str *ms;
for (ms = &mstab[0]; ms->ms_s0; ms++)
if (strcmp(ms->ms_s0, str) == 0)
return ms->ms_s1;
return NULL;
}
/*! Split colon-separated node identifier into prefix and name
*
* @param[in] node-id