diff options
author | Campbell Barton <ideasman42@gmail.com> | 2011-05-02 17:35:04 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2011-05-02 17:35:04 +0400 |
commit | 08d8914b3d0c0028c0f204d499ddfe46a76da113 (patch) | |
tree | 39749ad52c1f15627bd061b2fa92bd780dbd4ee0 /source/blender/blenlib | |
parent | 6baa456dfdb293f87b7ab4efdf856e7516b4d942 (diff) |
reverse string lookup listbase function BLI_findstring counterparts, added BLI_rfindstring, BLI_rfindstring_ptr, these search from the end of the listbase (like pythons rfind).
Diffstat (limited to 'source/blender/blenlib')
-rw-r--r-- | source/blender/blenlib/BLI_listbase.h | 9 | ||||
-rw-r--r-- | source/blender/blenlib/intern/listbase.c | 49 |
2 files changed, 49 insertions, 9 deletions
diff --git a/source/blender/blenlib/BLI_listbase.h b/source/blender/blenlib/BLI_listbase.h index 73af9a3738c..90556ea4b05 100644 --- a/source/blender/blenlib/BLI_listbase.h +++ b/source/blender/blenlib/BLI_listbase.h @@ -45,9 +45,16 @@ extern "C" { void BLI_insertlink(struct ListBase *listbase, void *vprevlink, void *vnewlink); void *BLI_findlink(const struct ListBase *listbase, int number); int BLI_findindex(const struct ListBase *listbase, void *vlink); +int BLI_findstringindex(const struct ListBase *listbase, const char *id, const int offset); + +/* find forwards */ void *BLI_findstring(const struct ListBase *listbase, const char *id, const int offset); void *BLI_findstring_ptr(const struct ListBase *listbase, const char *id, const int offset); -int BLI_findstringindex(const struct ListBase *listbase, const char *id, const int offset); + +/* find backwards */ +void *BLI_rfindstring(const struct ListBase *listbase, const char *id, const int offset); +void *BLI_rfindstring_ptr(const struct ListBase *listbase, const char *id, const int offset); + void BLI_freelistN(struct ListBase *listbase); void BLI_addtail(struct ListBase *listbase, void *vlink); void BLI_remlink(struct ListBase *listbase, void *vlink); diff --git a/source/blender/blenlib/intern/listbase.c b/source/blender/blenlib/intern/listbase.c index 15bdb4a446f..05f71e0c01f 100644 --- a/source/blender/blenlib/intern/listbase.c +++ b/source/blender/blenlib/intern/listbase.c @@ -366,14 +366,30 @@ void *BLI_findstring(const ListBase *listbase, const char *id, const int offset) if (listbase == NULL) return NULL; - link= listbase->first; - while (link) { + for (link= listbase->first; link; link= link->next) { id_iter= ((const char *)link) + offset; - if(id[0] == id_iter[0] && strcmp(id, id_iter)==0) + if (id[0] == id_iter[0] && strcmp(id, id_iter)==0) { return link; + } + } - link= link->next; + return NULL; +} +/* same as above but find reverse */ +void *BLI_rfindstring(const ListBase *listbase, const char *id, const int offset) +{ + Link *link= NULL; + const char *id_iter; + + if (listbase == NULL) return NULL; + + for (link= listbase->last; link; link= link->prev) { + id_iter= ((const char *)link) + offset; + + if (id[0] == id_iter[0] && strcmp(id, id_iter)==0) { + return link; + } } return NULL; @@ -386,15 +402,32 @@ void *BLI_findstring_ptr(const ListBase *listbase, const char *id, const int off if (listbase == NULL) return NULL; - link= listbase->first; - while (link) { + for (link= listbase->first; link; link= link->next) { /* exact copy of BLI_findstring(), except for this line */ id_iter= *((const char **)(((const char *)link) + offset)); - if(id[0] == id_iter[0] && strcmp(id, id_iter)==0) + if (id[0] == id_iter[0] && strcmp(id, id_iter)==0) { return link; + } + } - link= link->next; + return NULL; +} +/* same as above but find reverse */ +void *BLI_rfindstring_ptr(const ListBase *listbase, const char *id, const int offset) +{ + Link *link= NULL; + const char *id_iter; + + if (listbase == NULL) return NULL; + + for (link= listbase->last; link; link= link->prev) { + /* exact copy of BLI_rfindstring(), except for this line */ + id_iter= *((const char **)(((const char *)link) + offset)); + + if (id[0] == id_iter[0] && strcmp(id, id_iter)==0) { + return link; + } } return NULL; |