diff options
author | Bastien Montagne <montagne29@wanadoo.fr> | 2017-11-16 14:49:31 +0300 |
---|---|---|
committer | Bastien Montagne <montagne29@wanadoo.fr> | 2017-11-16 14:49:31 +0300 |
commit | d697e3d46ea9829c150d94411312e4cac434e4c0 (patch) | |
tree | 4c4430848cc86abffecf405fcdb50f4f41705115 /source/blender/blenlib/intern/listbase.c | |
parent | 470b4cb62f543263f0087a1e9d39b4b125752d23 (diff) |
BLI listbase: add bytes finding helpers.
Quite similar to string ones actually, except more generic. Used in
id_override_static branch currently.
Diffstat (limited to 'source/blender/blenlib/intern/listbase.c')
-rw-r--r-- | source/blender/blenlib/intern/listbase.c | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/source/blender/blenlib/intern/listbase.c b/source/blender/blenlib/intern/listbase.c index 0a6d575c7d6..d2cf0cf49a2 100644 --- a/source/blender/blenlib/intern/listbase.c +++ b/source/blender/blenlib/intern/listbase.c @@ -692,6 +692,46 @@ void *BLI_rfindptr(const ListBase *listbase, const void *ptr, const int offset) } /** + * Finds the first element of listbase which contains the specified bytes + * at the specified offset, returning NULL if not found. + */ +void *BLI_listbase_bytes_find(const ListBase *listbase, const void *bytes, const size_t bytes_size, const int offset) +{ + Link *link = NULL; + const void *ptr_iter; + + for (link = listbase->first; link; link = link->next) { + ptr_iter = (const void *)(((const char *)link) + offset); + + if (memcmp(bytes, ptr_iter, bytes_size) == 0) { + return link; + } + } + + return NULL; +} +/* same as above but find reverse */ +/** + * Finds the last element of listbase which contains the specified bytes + * at the specified offset, returning NULL if not found. + */ +void *BLI_listbase_bytes_rfind(const ListBase *listbase, const void *bytes, const size_t bytes_size, const int offset) +{ + Link *link = NULL; + const void *ptr_iter; + + for (link = listbase->last; link; link = link->prev) { + ptr_iter = (const void *)(((const char *)link) + offset); + + if (memcmp(bytes, ptr_iter, bytes_size) == 0) { + return link; + } + } + + return NULL; +} + +/** * Returns the 0-based index of the first element of listbase which contains the specified * null-terminated string at the specified offset, or -1 if not found. */ |