Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a function to find a device which starts with a given name #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions dtree.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,24 @@ struct dtree_dev_t *dtree_byname(const char *name)
return curr;
}

struct dtree_dev_t *dtree_byname_prefix(const char *name_prefix)
{
struct dtree_dev_t *curr = NULL;
int string_length = strlen(name_prefix);

if(name_prefix == NULL || string_length == 0)
return NULL;

while((curr = dtree_next()) != NULL) {
if(!strncmp(name_prefix, curr->name, string_length))
break;

dtree_dev_free(curr);
}

return curr;
}

static
int is_compatible(const struct dtree_dev_t *dev, const char *compat)
{
Expand Down
14 changes: 14 additions & 0 deletions dtree.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,20 @@ struct dtree_dev_t *dtree_next(void);
*/
struct dtree_dev_t *dtree_byname(const char *name);

/**
* Look up for device which starts with the name prefix.
* Returns the first occurence of device with the given
* name prefix.
* The entry should be free'd by dtree_dev_free().
*
* Uses shared internal iterator.
* To search from beginning call dtree_reset().
*
* Returns NULL when not found or on error.
* On error sets error state.
*/
struct dtree_dev_t *dtree_byname_prefix(const char *name_prefix);

/**
* Looks up for device compatible with the given type.
* The entry should be free'd by dtree_dev_free().
Expand Down