Skip to content

Commit

Permalink
Refactoring: moving find_string to libutil
Browse files Browse the repository at this point in the history
  • Loading branch information
Ravi Agrawal committed Jan 22, 2019
1 parent 726499e commit cac732b
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 69 deletions.
10 changes: 10 additions & 0 deletions src/include/libutil.h
Expand Up @@ -257,6 +257,16 @@ char **break_comma_list(char *list);
*/
char **break_delimited_str(char *list, char delim);

/*
* find index of str in strarr
*/
int find_string_idx(char **strarr, char *str);

/*
* is_string_in_arr - Does a string exist in the given array?
*/
int is_string_in_arr(char **strarr, char *str);

/*
* free_string_array - free an array of strings with NULL as sentinel
*/
Expand Down
50 changes: 50 additions & 0 deletions src/lib/Libutil/misc_utils.c
Expand Up @@ -1192,6 +1192,56 @@ break_comma_list(char *strlist)
return (break_delimited_str(strlist, ','));
}

/**
* @brief
* Does a string exist in the given array?
*
* @param[in] strarr - the string array to search, should be NULL terminated
* @param[in] str - the string to find
*
* @return int
* @retval 1 : if the string is found
* @retval 0 : the string is not found or on error
*
*/
int
is_string_in_arr(char **strarr, char *str)
{
int ind;

ind = find_string_idx(strarr, str);

if (ind >= 0)
return 1;

return 0;
}

/**
* @brief
* find index of str in strarr
*
* @param[in] strarr - the string array to search
* @param[in] str - the string to find
*
* @return int
* @retval index of string
* @retval -1 : if not found
*/
int
find_string_idx(char **strarr, char *str)
{
int i;
if (strarr == NULL || str == NULL)
return -1;

for (i = 0; strarr[i] != NULL && strcmp(strarr[i], str); i++)
;
if (strarr[i] == NULL)
return -1;

return i;
}

/**
* @brief
Expand Down
54 changes: 2 additions & 52 deletions src/scheduler/misc.c
Expand Up @@ -197,7 +197,7 @@ add_str_to_unique_array(char ***str_arr, char *str)
if (str_arr == NULL || str == NULL)
return -1;

ind = find_string_ind(*str_arr, str);
ind = find_string_idx(*str_arr, str);
if (ind >= 0) /* found it! */
return ind;

Expand Down Expand Up @@ -552,56 +552,6 @@ dup_string_array(char **ostrs)
return nstrs;
}

/**
* @brief
* find a string in a NULL terminated string array
*
* @param[in] strarr - the string array to search
* @param[in] str - the string to find
*
* @return int
* @retval 1 : if the string is found
* @retval 0 : the string is not found or on error
*
*/
int
find_string(char **strarr, char *str)
{
int ind;

ind = find_string_ind(strarr, str);

if (ind >= 0)
return 1;

return 0;
}

/**
* @brief
* find index of str in strarr
*
* @param[in] strarr - the string array to search
* @param[in] str - the string to find
*
* @return int
* @retval index of string
* @retval -1 : if not found
*/
int
find_string_ind(char **strarr, char *str)
{
int i;
if (strarr == NULL || str == NULL)
return -1;

for (i = 0; strarr[i] != NULL && strcmp(strarr[i], str); i++)
;
if (strarr[i] == NULL)
return -1;

return i;
}

/**
* @brief
Expand Down Expand Up @@ -650,7 +600,7 @@ enum match_string_array_ret match_string_array(char **strarr1, char **strarr2)
strarr2_len = count_array((void **)strarr2);

for (i = 0; strarr1[i] != NULL; i++) {
if (find_string(strarr2, strarr1[i]))
if (is_string_in_arr(strarr2, strarr1[i]))
match++;
}

Expand Down
10 changes: 0 additions & 10 deletions src/scheduler/misc.h
Expand Up @@ -129,16 +129,6 @@ char **dup_string_array(char **ostrs);
*/
unsigned string_array_verify(char **sa1, char **sa2);

/*
* find index of str in strarr
*/
int find_string_ind(char **strarr, char *str);

/*
* find_string - find a string in a NULL terminated string array
*/
int find_string(char **strarr, char *str);

/*
*
* match_string_array - match two NULL terminated string arrays
Expand Down
10 changes: 5 additions & 5 deletions src/scheduler/node_info.c
Expand Up @@ -4192,7 +4192,7 @@ parse_selspec(char *select_spec)
num_cpus += (num_chunks * req->amount);
}
if (!invalid && (req->type.is_boolean || conf.res_to_check == NULL ||
find_string(conf.res_to_check, kv[i].kv_keyw))) {
is_string_in_arr(conf.res_to_check, kv[i].kv_keyw))) {
if (!resdef_exists_in_array(spec->defs, req->def))
add_resdef_to_array(&(spec->defs), req->def);
if (req_head == NULL)
Expand Down Expand Up @@ -5183,7 +5183,7 @@ is_aoe_avail_on_vnode(node_info *ninfo, resource_resv *resresv)
return 0;

if ((resp = find_resource(ninfo->res, getallres(RES_AOE))) != NULL)
return find_string(resp->str_avail, resresv->aoename);
return is_string_in_arr(resp->str_avail, resresv->aoename);

return 0;
}
Expand Down Expand Up @@ -5222,7 +5222,7 @@ is_eoe_avail_on_vnode(node_info *ninfo, resource_resv *resresv)
return 0;

if ((resp = find_resource(ninfo->res, getallres(RES_EOE))) != NULL)
return find_string(resp->str_avail, resresv->eoename);
return is_string_in_arr(resp->str_avail, resresv->eoename);

return 0;
}
Expand Down Expand Up @@ -5459,7 +5459,7 @@ node_in_str(node_info *node, void *strarr)
if (node == NULL || strarr == NULL)
return 0;

if (find_string((char **)strarr, node->name))
if (is_string_in_arr((char **)strarr, node->name))
return 1;

return 0;
Expand Down Expand Up @@ -5860,7 +5860,7 @@ node_in_partition(node_info *ninfo, char **partitions)
if (ninfo->partition == NULL)
return 0;

if (find_string(partitions, ninfo->partition))
if (is_string_in_arr(partitions, ninfo->partition))
return 1;
else
return 0;
Expand Down
2 changes: 1 addition & 1 deletion src/scheduler/queue_info.c
Expand Up @@ -1070,7 +1070,7 @@ queue_in_partition(queue_info *qinfo, char **partitions)
if (qinfo->partition == NULL)
return 0;

if (find_string(partitions, qinfo->partition))
if (is_string_in_arr(partitions, qinfo->partition))
return 1;
else
return 0;
Expand Down
2 changes: 1 addition & 1 deletion src/scheduler/resv_info.c
Expand Up @@ -221,7 +221,7 @@ query_reservations(server_info *sinfo, struct batch_status *resvs)
ignore_resv = 1;
}
else if ((resresv->resv->resv_state == RESV_BEING_DELETED) && (resresv->resv->resv_nodes != NULL) &&
(!find_string(resresv->resv->resv_nodes[0]->resvs, resresv->name))) {
(!is_string_in_arr(resresv->resv->resv_nodes[0]->resvs, resresv->name))) {
schdlog(PBSEVENT_SCHED, PBS_EVENTCLASS_RESV, LOG_DEBUG,
resresv->name, "Reservation is being deleted and not present on node, ignoring this reservation");
ignore_resv = 1;
Expand Down

0 comments on commit cac732b

Please sign in to comment.