diff options
Diffstat (limited to 'source/blender/blenlib/intern')
-rw-r--r-- | source/blender/blenlib/intern/BLI_fileops.h | 47 | ||||
-rw-r--r-- | source/blender/blenlib/intern/BLI_scanfill.h | 38 | ||||
-rw-r--r-- | source/blender/blenlib/intern/BLI_storage.h | 45 | ||||
-rw-r--r-- | source/blender/blenlib/intern/BLI_util.h | 42 | ||||
-rw-r--r-- | source/blender/blenlib/intern/dynamiclist.c | 265 | ||||
-rw-r--r-- | source/blender/blenlib/intern/dynamiclist.h | 55 | ||||
-rw-r--r-- | source/blender/blenlib/intern/listbase.c | 363 | ||||
-rw-r--r-- | source/blender/blenlib/intern/string.c | 163 | ||||
-rw-r--r-- | source/blender/blenlib/intern/threads.c | 2 | ||||
-rw-r--r-- | source/blender/blenlib/intern/util.c | 728 |
10 files changed, 869 insertions, 879 deletions
diff --git a/source/blender/blenlib/intern/BLI_fileops.h b/source/blender/blenlib/intern/BLI_fileops.h deleted file mode 100644 index bc42030e475..00000000000 --- a/source/blender/blenlib/intern/BLI_fileops.h +++ /dev/null @@ -1,47 +0,0 @@ -/** - * blenlib/BLI_listBase.h mar 2001 Nzc - * - * $Id$ - * - * ***** BEGIN GPL LICENSE BLOCK ***** - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * - * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. - * All rights reserved. - * - * The Original Code is: all of this file. - * - * Contributor(s): none yet. - * - * ***** END GPL LICENSE BLOCK ***** - * - * More low-level fileops from Daniel Dunbar. Two functions were also - * defined in storage.c. These are the old fop_ prefixes. There is - * definitely some redundancy here! - * */ - -#ifndef BLI_FILEOPS_H -#define BLI_FILEOPS_H - -char *first_slash(char *string); - -/* only for the sane unix world: direct calls to system functions :( */ -#ifndef WIN32 -void BLI_setCmdCallBack(int (*f)(char*)); -#endif - -#endif - diff --git a/source/blender/blenlib/intern/BLI_scanfill.h b/source/blender/blenlib/intern/BLI_scanfill.h deleted file mode 100644 index 43431dd5087..00000000000 --- a/source/blender/blenlib/intern/BLI_scanfill.h +++ /dev/null @@ -1,38 +0,0 @@ -/** - * blenlib/BLI_scanfill.h mar 2001 Nzc - * - * Filling meshes. - * - * $Id$ - * - * ***** BEGIN GPL LICENSE BLOCK ***** - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * - * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. - * All rights reserved. - * - * The Original Code is: all of this file. - * - * Contributor(s): none yet. - * - * ***** END GPL LICENSE BLOCK ***** - */ - -#ifndef BLI_SCANFILL_H -#define BLI_SCANFILL_H - -#endif - diff --git a/source/blender/blenlib/intern/BLI_storage.h b/source/blender/blenlib/intern/BLI_storage.h deleted file mode 100644 index 3e0dec367da..00000000000 --- a/source/blender/blenlib/intern/BLI_storage.h +++ /dev/null @@ -1,45 +0,0 @@ -/* $Id$ - * - * ***** BEGIN GPL LICENSE BLOCK ***** - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * - * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. - * All rights reserved. - * - * The Original Code is: all of this file. - * - * Contributor(s): none yet. - * - * ***** END GPL LICENSE BLOCK ***** - */ -#ifndef BLI_STORAGE_H -#define BLI_STORAGE_H - -#ifndef __APPLE__ -#ifndef WIN32 -#define _LARGEFILE_SOURCE 1 -#define _FILE_OFFSET_BITS 64 -#endif -#endif - -#include "BLI_storage_types.h" - -void BLI_adddirstrings(void); -void BLI_builddir(char *dirname, char *relname); -int BLI_compare(struct direntry *entry1, struct direntry *entry2); - -#endif /* BLI_STORAGE_H */ - diff --git a/source/blender/blenlib/intern/BLI_util.h b/source/blender/blenlib/intern/BLI_util.h deleted file mode 100644 index c37a7fd803b..00000000000 --- a/source/blender/blenlib/intern/BLI_util.h +++ /dev/null @@ -1,42 +0,0 @@ -/** - * blenlib/BLI_storage_types.h - * - * Some types for dealing with directories - * - * $Id$ - * - * ***** BEGIN GPL LICENSE BLOCK ***** - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * - * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. - * All rights reserved. - * - * The Original Code is: all of this file. - * - * Contributor(s): none yet. - * - * ***** END GPL LICENSE BLOCK ***** - */ -#ifndef BLI_UTIL_H -#define BLI_UTIL_H - -#define mallocstructN(x,y,name) (x*)MEM_mallocN((y)* sizeof(x),name) -#define callocstructN(x,y,name) (x*)MEM_callocN((y)* sizeof(x),name) - -struct ListBase; - -#endif - diff --git a/source/blender/blenlib/intern/dynamiclist.c b/source/blender/blenlib/intern/dynamiclist.c new file mode 100644 index 00000000000..fbb87124bba --- /dev/null +++ b/source/blender/blenlib/intern/dynamiclist.c @@ -0,0 +1,265 @@ +/* util.c + * + * various string, file, list operations. + * + * + * $Id: util.c 17433 2008-11-12 21:16:53Z blendix $ + * + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL LICENSE BLOCK ***** + * + */ + +#include "MEM_guardedalloc.h" + +#include "DNA_listBase.h" + +#include "BLI_listbase.h" +#include "BLI_dynamiclist.h" + +#define PAGE_SIZE 4 + +/*=====================================================================================*/ +/* Methods for access array (realloc) */ +/*=====================================================================================*/ + +/* remove item with index */ +static void rem_array_item(struct DynamicArray *da, unsigned int index) +{ + da->items[index]=NULL; + da->count--; + if(index==da->last_item_index){ + while((!da->items[da->last_item_index]) && (da->last_item_index>0)){ + da->last_item_index--; + } + } +} + +/* add array (if needed, then realloc) */ +static void add_array_item(struct DynamicArray *da, void *item, unsigned int index) +{ + /* realloc of access array */ + if(da->max_item_index < index){ + unsigned int i, max = da->max_item_index; + void **nitems; + + do { + da->max_item_index += PAGE_SIZE; /* OS can allocate only PAGE_SIZE Bytes */ + } while(da->max_item_index<=index); + + nitems = (void**)MEM_mallocN(sizeof(void*)*(da->max_item_index+1), "dlist access array"); + for(i=0;i<=max;i++) + nitems[i] = da->items[i]; + + /* set rest pointers to the NULL */ + for(i=max+1; i<=da->max_item_index; i++) + nitems[i]=NULL; + + MEM_freeN(da->items); /* free old access array */ + da->items = nitems; + } + + da->items[index] = item; + da->count++; + if(index > da->last_item_index) da->last_item_index = index; +} + +/* free access array */ +static void destroy_array(DynamicArray *da) +{ + da->count=0; + da->last_item_index=0; + da->max_item_index=0; + MEM_freeN(da->items); + da->items = NULL; +} + +/* initialize dynamic array */ +static void init_array(DynamicArray *da) +{ + unsigned int i; + + da->count=0; + da->last_item_index=0; + da->max_item_index = PAGE_SIZE-1; + da->items = (void*)MEM_mallocN(sizeof(void*)*(da->max_item_index+1), "dlist access array"); + for(i=0; i<=da->max_item_index; i++) da->items[i]=NULL; +} + +/* reinitialize dynamic array */ +static void reinit_array(DynamicArray *da) +{ + destroy_array(da); + init_array(da); +} + +/*=====================================================================================*/ +/* Methods for two way dynamic list with access array */ +/*=====================================================================================*/ + +/* create new two way dynamic list with access array from two way dynamic list + * it doesn't copy any items to new array or something like this It is strongly + * recomended to use BLI_dlist_ methods for adding/removing items from dynamic list + * unless you can end with inconsistence system !!! */ +DynamicList *BLI_dlist_from_listbase(ListBase *lb) +{ + DynamicList *dlist; + Link *item; + int i=0, count; + + if(!lb) return NULL; + + count = BLI_countlist(lb); + + dlist = MEM_mallocN(sizeof(DynamicList), "temp dynamic list"); + /* ListBase stuff */ + dlist->lb.first = lb->first; + dlist->lb.last = lb->last; + /* access array stuff */ + dlist->da.count=count; + dlist->da.max_item_index = count-1; + dlist->da.last_item_index = count -1; + dlist->da.items = (void*)MEM_mallocN(sizeof(void*)*count, "temp dlist access array"); + + item = (Link*)lb->first; + while(item){ + dlist->da.items[i] = (void*)item; + item = item->next; + i++; + } + + /* to prevent you of using original ListBase :-) */ + lb->first = lb->last = NULL; + + return dlist; +} + +/* take out ListBase from DynamicList and destroy all temporary structures of DynamicList */ +ListBase *BLI_listbase_from_dlist(DynamicList *dlist, ListBase *lb) +{ + if(!dlist) return NULL; + + if(!lb) lb = (ListBase*)MEM_mallocN(sizeof(ListBase), "ListBase"); + + lb->first = dlist->lb.first; + lb->last = dlist->lb.last; + + /* free all items of access array */ + MEM_freeN(dlist->da.items); + /* free DynamicList*/ + MEM_freeN(dlist); + + return lb; +} + +/* return pointer at item from th dynamic list with access array */ +void *BLI_dlist_find_link(DynamicList *dlist, unsigned int index) +{ + if(!dlist || !dlist->da.items) return NULL; + + if((index <= dlist->da.last_item_index) && (index >= 0) && (dlist->da.count>0)){ + return dlist->da.items[index]; + } + else { + return NULL; + } +} + +/* return count of items in the dynamic list with access array */ +unsigned int BLI_count_items(DynamicList *dlist) +{ + if(!dlist) return 0; + + return dlist->da.count; +} + +/* free item from the dynamic list with access array */ +void BLI_dlist_free_item(DynamicList *dlist, unsigned int index) +{ + if(!dlist || !dlist->da.items) return; + + if((index <= dlist->da.last_item_index) && (dlist->da.items[index])){ + BLI_freelinkN(&(dlist->lb), dlist->da.items[index]); + rem_array_item(&(dlist->da), index); + } +} + +/* remove item from the dynamic list with access array */ +void BLI_dlist_rem_item(DynamicList *dlist, unsigned int index) +{ + if(!dlist || !dlist->da.items) return; + + if((index <= dlist->da.last_item_index) && (dlist->da.items[index])){ + BLI_remlink(&(dlist->lb), dlist->da.items[index]); + rem_array_item(&(dlist->da), index); + } +} + +/* add item to the dynamic list with access array (index) */ +void* BLI_dlist_add_item_index(DynamicList *dlist, void *item, unsigned int index) +{ + if(!dlist || !dlist->da.items) return NULL; + + if((index <= dlist->da.max_item_index) && (dlist->da.items[index])) { + /* you can't place item at used index */ + return NULL; + } + else { + add_array_item(&(dlist->da), item, index); + BLI_addtail(&(dlist->lb), item); + return item; + } +} + +/* destroy dynamic list with access array */ +void BLI_dlist_destroy(DynamicList *dlist) +{ + if(!dlist) return; + + BLI_freelistN(&(dlist->lb)); + destroy_array(&(dlist->da)); +} + +/* initialize dynamic list with access array */ +void BLI_dlist_init(DynamicList *dlist) +{ + if(!dlist) return; + + dlist->lb.first = NULL; + dlist->lb.last = NULL; + + init_array(&(dlist->da)); +} + +/* reinitialize dynamic list with acces array */ +void BLI_dlist_reinit(DynamicList *dlist) +{ + if(!dlist) return; + + BLI_freelistN(&(dlist->lb)); + reinit_array(&(dlist->da)); +} + +/*=====================================================================================*/ diff --git a/source/blender/blenlib/intern/dynamiclist.h b/source/blender/blenlib/intern/dynamiclist.h new file mode 100644 index 00000000000..aba3eda0696 --- /dev/null +++ b/source/blender/blenlib/intern/dynamiclist.h @@ -0,0 +1,55 @@ +/** + * $Id: BLI_dynamiclist.h 13161 2008-01-07 19:13:47Z hos $ + * + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * Contributor(s): Jiri Hnidek. + * + * Documentation of Two way dynamic list with access array can be found at: + * + * http://wiki.blender.org/bin/view.pl/Blenderwiki/DynamicListWithAccessArray + * + * ***** END GPL LICENSE BLOCK ***** + */ + +#ifndef B_DYNAMIC_LIST_H +#define B_DYNAMIC_LIST_H + +#define PAGE_SIZE 4 + +struct ListBase; + +/* + * Access array using realloc + */ +typedef struct DynamicArray{ + unsigned int count; /* count of items in list */ + unsigned int max_item_index; /* max available index */ + unsigned int last_item_index; /* max used index */ + void **items; /* dynamicaly allocated array of pointers + pointing at items in list */ +} DynamicArray; + +/* + * Two way dynamic list with access array + */ +typedef struct DynamicList { + struct DynamicArray da; /* access array */ + struct ListBase lb; /* two way linked dynamic list */ +} DynamicList; + +#endif diff --git a/source/blender/blenlib/intern/listbase.c b/source/blender/blenlib/intern/listbase.c new file mode 100644 index 00000000000..774e72091c0 --- /dev/null +++ b/source/blender/blenlib/intern/listbase.c @@ -0,0 +1,363 @@ +/* util.c + * + * various string, file, list operations. + * + * + * $Id: util.c 17433 2008-11-12 21:16:53Z blendix $ + * + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL LICENSE BLOCK ***** + * + */ + +#include <string.h> +#include <stdlib.h> + + +#include "MEM_guardedalloc.h" + +#include "DNA_listBase.h" + +#include "BLI_listbase.h" + + +static int add_win32_extension(char *name); + +/* implementation */ + +/* Ripped this from blender.c */ +void addlisttolist(ListBase *list1, ListBase *list2) +{ + if (list2->first==0) return; + + if (list1->first==0) { + list1->first= list2->first; + list1->last= list2->last; + } + else { + ((Link *)list1->last)->next= list2->first; + ((Link *)list2->first)->prev= list1->last; + list1->last= list2->last; + } + list2->first= list2->last= 0; +} + +void BLI_addhead(ListBase *listbase, void *vlink) +{ + Link *link= vlink; + + if (link == NULL) return; + if (listbase == NULL) return; + + link->next = listbase->first; + link->prev = NULL; + + if (listbase->first) ((Link *)listbase->first)->prev = link; + if (listbase->last == NULL) listbase->last = link; + listbase->first = link; +} + + +void BLI_addtail(ListBase *listbase, void *vlink) +{ + Link *link= vlink; + + if (link == NULL) return; + if (listbase == NULL) return; + + link->next = NULL; + link->prev = listbase->last; + + if (listbase->last) ((Link *)listbase->last)->next = link; + if (listbase->first == 0) listbase->first = link; + listbase->last = link; +} + + +void BLI_remlink(ListBase *listbase, void *vlink) +{ + Link *link= vlink; + + if (link == NULL) return; + if (listbase == NULL) return; + + if (link->next) link->next->prev = link->prev; + if (link->prev) link->prev->next = link->next; + + if (listbase->last == link) listbase->last = link->prev; + if (listbase->first == link) listbase->first = link->next; +} + + +void BLI_freelinkN(ListBase *listbase, void *vlink) +{ + Link *link= vlink; + + if (link == NULL) return; + if (listbase == NULL) return; + + BLI_remlink(listbase,link); + MEM_freeN(link); +} + + +void BLI_insertlink(ListBase *listbase, void *vprevlink, void *vnewlink) +{ + Link *prevlink= vprevlink; + Link *newlink= vnewlink; + + /* newlink comes after prevlink */ + if (newlink == NULL) return; + if (listbase == NULL) return; + + /* empty list */ + if (listbase->first == NULL) { + + listbase->first= newlink; + listbase->last= newlink; + return; + } + + /* insert before first element */ + if (prevlink == NULL) { + newlink->next= listbase->first; + newlink->prev= 0; + newlink->next->prev= newlink; + listbase->first= newlink; + return; + } + + /* at end of list */ + if (listbase->last== prevlink) + listbase->last = newlink; + + newlink->next= prevlink->next; + prevlink->next= newlink; + if (newlink->next) newlink->next->prev= newlink; + newlink->prev= prevlink; +} + +/* This uses insertion sort, so NOT ok for large list */ +void BLI_sortlist(ListBase *listbase, int (*cmp)(void *, void *)) +{ + Link *current = NULL; + Link *previous = NULL; + Link *next = NULL; + + if (cmp == NULL) return; + if (listbase == NULL) return; + + if (listbase->first != listbase->last) + { + for( previous = listbase->first, current = previous->next; current; current = next ) + { + next = current->next; + previous = current->prev; + + BLI_remlink(listbase, current); + + while(previous && cmp(previous, current) == 1) + { + previous = previous->prev; + } + + BLI_insertlinkafter(listbase, previous, current); + } + } +} + +void BLI_insertlinkafter(ListBase *listbase, void *vprevlink, void *vnewlink) +{ + Link *prevlink= vprevlink; + Link *newlink= vnewlink; + + /* newlink before nextlink */ + if (newlink == NULL) return; + if (listbase == NULL) return; + + /* empty list */ + if (listbase->first == NULL) { + listbase->first= newlink; + listbase->last= newlink; + return; + } + + /* insert at head of list */ + if (prevlink == NULL) { + newlink->prev = NULL; + newlink->next = listbase->first; + ((Link *)listbase->first)->prev = newlink; + listbase->first = newlink; + return; + } + + /* at end of list */ + if (listbase->last == prevlink) + listbase->last = newlink; + + newlink->next = prevlink->next; + newlink->prev = prevlink; + prevlink->next = newlink; + if (newlink->next) newlink->next->prev = newlink; +} + +void BLI_insertlinkbefore(ListBase *listbase, void *vnextlink, void *vnewlink) +{ + Link *nextlink= vnextlink; + Link *newlink= vnewlink; + + /* newlink before nextlink */ + if (newlink == NULL) return; + if (listbase == NULL) return; + + /* empty list */ + if (listbase->first == NULL) { + listbase->first= newlink; + listbase->last= newlink; + return; + } + + /* insert at end of list */ + if (nextlink == NULL) { + newlink->prev= listbase->last; + newlink->next= 0; + ((Link *)listbase->last)->next= newlink; + listbase->last= newlink; + return; + } + + /* at beginning of list */ + if (listbase->first== nextlink) + listbase->first = newlink; + + newlink->next= nextlink; + newlink->prev= nextlink->prev; + nextlink->prev= newlink; + if (newlink->prev) newlink->prev->next= newlink; +} + + +void BLI_freelist(ListBase *listbase) +{ + Link *link, *next; + + if (listbase == NULL) + return; + + link= listbase->first; + while (link) { + next= link->next; + free(link); + link= next; + } + + listbase->first= NULL; + listbase->last= NULL; +} + +void BLI_freelistN(ListBase *listbase) +{ + Link *link, *next; + + if (listbase == NULL) return; + + link= listbase->first; + while (link) { + next= link->next; + MEM_freeN(link); + link= next; + } + + listbase->first= NULL; + listbase->last= NULL; +} + + +int BLI_countlist(ListBase *listbase) +{ + Link *link; + int count = 0; + + if (listbase) { + link = listbase->first; + while (link) { + count++; + link= link->next; + } + } + return count; +} + +void *BLI_findlink(ListBase *listbase, int number) +{ + Link *link = NULL; + + if (number >= 0) { + link = listbase->first; + while (link != NULL && number != 0) { + number--; + link = link->next; + } + } + + return link; +} + +int BLI_findindex(ListBase *listbase, void *vlink) +{ + Link *link= NULL; + int number= 0; + + if (listbase == NULL) return -1; + if (vlink == NULL) return -1; + + link= listbase->first; + while (link) { + if (link == vlink) + return number; + + number++; + link= link->next; + } + + return -1; +} + +void BLI_duplicatelist(ListBase *list1, ListBase *list2) /* copy from 2 to 1 */ +{ + struct Link *link1, *link2; + + list1->first= list1->last= 0; + + link2= list2->first; + while(link2) { + + link1= MEM_dupallocN(link2); + BLI_addtail(list1, link1); + + link2= link2->next; + } +} + diff --git a/source/blender/blenlib/intern/string.c b/source/blender/blenlib/intern/string.c new file mode 100644 index 00000000000..2fefe1d9b45 --- /dev/null +++ b/source/blender/blenlib/intern/string.c @@ -0,0 +1,163 @@ +/* util.c + * + * various string, file, list operations. + * + * + * $Id: util.c 17433 2008-11-12 21:16:53Z blendix $ + * + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL LICENSE BLOCK ***** + * + */ + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <stdarg.h> + +#include "MEM_guardedalloc.h" + +char *BLI_strdupn(const char *str, int len) { + char *n= MEM_mallocN(len+1, "strdup"); + memcpy(n, str, len); + n[len]= '\0'; + + return n; +} +char *BLI_strdup(const char *str) { + return BLI_strdupn(str, strlen(str)); +} + +char *BLI_strncpy(char *dst, const char *src, int maxncpy) { + int srclen= strlen(src); + int cpylen= (srclen>(maxncpy-1))?(maxncpy-1):srclen; + + memcpy(dst, src, cpylen); + dst[cpylen]= '\0'; + + return dst; +} + +int BLI_snprintf(char *buffer, size_t count, const char *format, ...) +{ + int n; + va_list arg; + + va_start(arg, format); + n = vsnprintf(buffer, count, format, arg); + + if (n != -1 && n < count) { + buffer[n] = '\0'; + } else { + buffer[count-1] = '\0'; + } + + va_end(arg); + return n; +} + +int BLI_streq(char *a, char *b) { + return (strcmp(a, b)==0); +} +int BLI_strcaseeq(char *a, char *b) { + return (BLI_strcasecmp(a, b)==0); +} + +/* strcasestr not available in MSVC */ +char *BLI_strcasestr(const char *s, const char *find) +{ + register char c, sc; + register size_t len; + + if ((c = *find++) != 0) { + c= tolower(c); + len = strlen(find); + do { + do { + if ((sc = *s++) == 0) + return (NULL); + sc= tolower(sc); + } while (sc != c); + } while (BLI_strncasecmp(s, find, len) != 0); + s--; + } + return ((char *) s); +} + + +int BLI_strcasecmp(const char *s1, const char *s2) { + int i; + + for (i=0; ; i++) { + char c1 = tolower(s1[i]); + char c2 = tolower(s2[i]); + + if (c1<c2) { + return -1; + } else if (c1>c2) { + return 1; + } else if (c1==0) { + break; + } + } + + return 0; +} + +int BLI_strncasecmp(const char *s1, const char *s2, int n) { + int i; + + for (i=0; i<n; i++) { + char c1 = tolower(s1[i]); + char c2 = tolower(s2[i]); + + if (c1<c2) { + return -1; + } else if (c1>c2) { + return 1; + } else if (c1==0) { + break; + } + } + + return 0; +} + +void BLI_timestr(double _time, char *str) +{ + /* format 00:00:00.00 (hr:min:sec) string has to be 12 long */ + int hr= ( (int) _time) / (60*60); + int min= (((int) _time) / 60 ) % 60; + int sec= ( (int) (_time)) % 60; + int hun= ( (int) (_time * 100.0)) % 100; + + if (hr) { + sprintf(str, "%.2d:%.2d:%.2d.%.2d",hr,min,sec,hun); + } else { + sprintf(str, "%.2d:%.2d.%.2d",min,sec,hun); + } + + str[11]=0; +}
\ No newline at end of file diff --git a/source/blender/blenlib/intern/threads.c b/source/blender/blenlib/intern/threads.c index 07c02b8024f..884724270ae 100644 --- a/source/blender/blenlib/intern/threads.c +++ b/source/blender/blenlib/intern/threads.c @@ -35,6 +35,8 @@ #include "MEM_guardedalloc.h" +#include "DNA_listBase.h" + #include "BLI_blenlib.h" #include "BLI_threads.h" diff --git a/source/blender/blenlib/intern/util.c b/source/blender/blenlib/intern/util.c index d3cfdf6417c..f363e9c4cc5 100644 --- a/source/blender/blenlib/intern/util.c +++ b/source/blender/blenlib/intern/util.c @@ -102,23 +102,6 @@ static int add_win32_extension(char *name); /* implementation */ -/* Ripped this from blender.c */ -void addlisttolist(ListBase *list1, ListBase *list2) -{ - if (list2->first==0) return; - - if (list1->first==0) { - list1->first= list2->first; - list1->last= list2->last; - } - else { - ((Link *)list1->last)->next= list2->first; - ((Link *)list2->first)->prev= list1->last; - list1->last= list2->last; - } - list2->first= list2->last= 0; -} - int BLI_stringdec(char *string, char *kop, char *start, unsigned short *numlen) { unsigned short len, len2, nums = 0, nume = 0; @@ -301,574 +284,6 @@ void BLI_uniquename(ListBase *list, void *vlink, char defname[], short name_offs } } - -void BLI_addhead(ListBase *listbase, void *vlink) -{ - Link *link= vlink; - - if (link == NULL) return; - if (listbase == NULL) return; - - link->next = listbase->first; - link->prev = NULL; - - if (listbase->first) ((Link *)listbase->first)->prev = link; - if (listbase->last == NULL) listbase->last = link; - listbase->first = link; -} - - -void BLI_addtail(ListBase *listbase, void *vlink) -{ - Link *link= vlink; - - if (link == NULL) return; - if (listbase == NULL) return; - - link->next = NULL; - link->prev = listbase->last; - - if (listbase->last) ((Link *)listbase->last)->next = link; - if (listbase->first == 0) listbase->first = link; - listbase->last = link; -} - - -void BLI_remlink(ListBase *listbase, void *vlink) -{ - Link *link= vlink; - - if (link == NULL) return; - if (listbase == NULL) return; - - if (link->next) link->next->prev = link->prev; - if (link->prev) link->prev->next = link->next; - - if (listbase->last == link) listbase->last = link->prev; - if (listbase->first == link) listbase->first = link->next; -} - - -void BLI_freelinkN(ListBase *listbase, void *vlink) -{ - Link *link= vlink; - - if (link == NULL) return; - if (listbase == NULL) return; - - BLI_remlink(listbase,link); - MEM_freeN(link); -} - - -void BLI_insertlink(ListBase *listbase, void *vprevlink, void *vnewlink) -{ - Link *prevlink= vprevlink; - Link *newlink= vnewlink; - - /* newlink comes after prevlink */ - if (newlink == NULL) return; - if (listbase == NULL) return; - - /* empty list */ - if (listbase->first == NULL) { - - listbase->first= newlink; - listbase->last= newlink; - return; - } - - /* insert before first element */ - if (prevlink == NULL) { - newlink->next= listbase->first; - newlink->prev= 0; - newlink->next->prev= newlink; - listbase->first= newlink; - return; - } - - /* at end of list */ - if (listbase->last== prevlink) - listbase->last = newlink; - - newlink->next= prevlink->next; - prevlink->next= newlink; - if (newlink->next) newlink->next->prev= newlink; - newlink->prev= prevlink; -} - -/* This uses insertion sort, so NOT ok for large list */ -void BLI_sortlist(ListBase *listbase, int (*cmp)(void *, void *)) -{ - Link *current = NULL; - Link *previous = NULL; - Link *next = NULL; - - if (cmp == NULL) return; - if (listbase == NULL) return; - - if (listbase->first != listbase->last) - { - for( previous = listbase->first, current = previous->next; current; current = next ) - { - next = current->next; - previous = current->prev; - - BLI_remlink(listbase, current); - - while(previous && cmp(previous, current) == 1) - { - previous = previous->prev; - } - - BLI_insertlinkafter(listbase, previous, current); - } - } -} - -void BLI_insertlinkafter(ListBase *listbase, void *vprevlink, void *vnewlink) -{ - Link *prevlink= vprevlink; - Link *newlink= vnewlink; - - /* newlink before nextlink */ - if (newlink == NULL) return; - if (listbase == NULL) return; - - /* empty list */ - if (listbase->first == NULL) { - listbase->first= newlink; - listbase->last= newlink; - return; - } - - /* insert at head of list */ - if (prevlink == NULL) { - newlink->prev = NULL; - newlink->next = listbase->first; - ((Link *)listbase->first)->prev = newlink; - listbase->first = newlink; - return; - } - - /* at end of list */ - if (listbase->last == prevlink) - listbase->last = newlink; - - newlink->next = prevlink->next; - newlink->prev = prevlink; - prevlink->next = newlink; - if (newlink->next) newlink->next->prev = newlink; -} - -void BLI_insertlinkbefore(ListBase *listbase, void *vnextlink, void *vnewlink) -{ - Link *nextlink= vnextlink; - Link *newlink= vnewlink; - - /* newlink before nextlink */ - if (newlink == NULL) return; - if (listbase == NULL) return; - - /* empty list */ - if (listbase->first == NULL) { - listbase->first= newlink; - listbase->last= newlink; - return; - } - - /* insert at end of list */ - if (nextlink == NULL) { - newlink->prev= listbase->last; - newlink->next= 0; - ((Link *)listbase->last)->next= newlink; - listbase->last= newlink; - return; - } - - /* at beginning of list */ - if (listbase->first== nextlink) - listbase->first = newlink; - - newlink->next= nextlink; - newlink->prev= nextlink->prev; - nextlink->prev= newlink; - if (newlink->prev) newlink->prev->next= newlink; -} - - -void BLI_freelist(ListBase *listbase) -{ - Link *link, *next; - - if (listbase == NULL) - return; - - link= listbase->first; - while (link) { - next= link->next; - free(link); - link= next; - } - - listbase->first= NULL; - listbase->last= NULL; -} - -void BLI_freelistN(ListBase *listbase) -{ - Link *link, *next; - - if (listbase == NULL) return; - - link= listbase->first; - while (link) { - next= link->next; - MEM_freeN(link); - link= next; - } - - listbase->first= NULL; - listbase->last= NULL; -} - - -int BLI_countlist(ListBase *listbase) -{ - Link *link; - int count = 0; - - if (listbase) { - link = listbase->first; - while (link) { - count++; - link= link->next; - } - } - return count; -} - -void *BLI_findlink(ListBase *listbase, int number) -{ - Link *link = NULL; - - if (number >= 0) { - link = listbase->first; - while (link != NULL && number != 0) { - number--; - link = link->next; - } - } - - return link; -} - -int BLI_findindex(ListBase *listbase, void *vlink) -{ - Link *link= NULL; - int number= 0; - - if (listbase == NULL) return -1; - if (vlink == NULL) return -1; - - link= listbase->first; - while (link) { - if (link == vlink) - return number; - - number++; - link= link->next; - } - - return -1; -} - -void BLI_duplicatelist(ListBase *list1, ListBase *list2) /* copy from 2 to 1 */ -{ - struct Link *link1, *link2; - - list1->first= list1->last= 0; - - link2= list2->first; - while(link2) { - - link1= MEM_dupallocN(link2); - BLI_addtail(list1, link1); - - link2= link2->next; - } -} - - -/*=====================================================================================*/ -/* Methods for access array (realloc) */ -/*=====================================================================================*/ - -/* remove item with index */ -static void rem_array_item(struct DynamicArray *da, unsigned int index) -{ - da->items[index]=NULL; - da->count--; - if(index==da->last_item_index){ - while((!da->items[da->last_item_index]) && (da->last_item_index>0)){ - da->last_item_index--; - } - } -} - -/* add array (if needed, then realloc) */ -static void add_array_item(struct DynamicArray *da, void *item, unsigned int index) -{ - /* realloc of access array */ - if(da->max_item_index < index){ - unsigned int i, max = da->max_item_index; - void **nitems; - - do { - da->max_item_index += PAGE_SIZE; /* OS can allocate only PAGE_SIZE Bytes */ - } while(da->max_item_index<=index); - - nitems = (void**)MEM_mallocN(sizeof(void*)*(da->max_item_index+1), "dlist access array"); - for(i=0;i<=max;i++) - nitems[i] = da->items[i]; - - /* set rest pointers to the NULL */ - for(i=max+1; i<=da->max_item_index; i++) - nitems[i]=NULL; - - MEM_freeN(da->items); /* free old access array */ - da->items = nitems; - } - - da->items[index] = item; - da->count++; - if(index > da->last_item_index) da->last_item_index = index; -} - -/* free access array */ -static void destroy_array(DynamicArray *da) -{ - da->count=0; - da->last_item_index=0; - da->max_item_index=0; - MEM_freeN(da->items); - da->items = NULL; -} - -/* initialize dynamic array */ -static void init_array(DynamicArray *da) -{ - unsigned int i; - - da->count=0; - da->last_item_index=0; - da->max_item_index = PAGE_SIZE-1; - da->items = (void*)MEM_mallocN(sizeof(void*)*(da->max_item_index+1), "dlist access array"); - for(i=0; i<=da->max_item_index; i++) da->items[i]=NULL; -} - -/* reinitialize dynamic array */ -static void reinit_array(DynamicArray *da) -{ - destroy_array(da); - init_array(da); -} - -/*=====================================================================================*/ -/* Methods for two way dynamic list with access array */ -/*=====================================================================================*/ - -/* create new two way dynamic list with access array from two way dynamic list - * it doesn't copy any items to new array or something like this It is strongly - * recomended to use BLI_dlist_ methods for adding/removing items from dynamic list - * unless you can end with inconsistence system !!! */ -DynamicList *BLI_dlist_from_listbase(ListBase *lb) -{ - DynamicList *dlist; - Link *item; - int i=0, count; - - if(!lb) return NULL; - - count = BLI_countlist(lb); - - dlist = MEM_mallocN(sizeof(DynamicList), "temp dynamic list"); - /* ListBase stuff */ - dlist->lb.first = lb->first; - dlist->lb.last = lb->last; - /* access array stuff */ - dlist->da.count=count; - dlist->da.max_item_index = count-1; - dlist->da.last_item_index = count -1; - dlist->da.items = (void*)MEM_mallocN(sizeof(void*)*count, "temp dlist access array"); - - item = (Link*)lb->first; - while(item){ - dlist->da.items[i] = (void*)item; - item = item->next; - i++; - } - - /* to prevent you of using original ListBase :-) */ - lb->first = lb->last = NULL; - - return dlist; -} - -/* take out ListBase from DynamicList and destroy all temporary structures of DynamicList */ -ListBase *BLI_listbase_from_dlist(DynamicList *dlist, ListBase *lb) -{ - if(!dlist) return NULL; - - if(!lb) lb = (ListBase*)MEM_mallocN(sizeof(ListBase), "ListBase"); - - lb->first = dlist->lb.first; - lb->last = dlist->lb.last; - - /* free all items of access array */ - MEM_freeN(dlist->da.items); - /* free DynamicList*/ - MEM_freeN(dlist); - - return lb; -} - -/* return pointer at item from th dynamic list with access array */ -void *BLI_dlist_find_link(DynamicList *dlist, unsigned int index) -{ - if(!dlist || !dlist->da.items) return NULL; - - if((index <= dlist->da.last_item_index) && (index >= 0) && (dlist->da.count>0)){ - return dlist->da.items[index]; - } - else { - return NULL; - } -} - -/* return count of items in the dynamic list with access array */ -unsigned int BLI_count_items(DynamicList *dlist) -{ - if(!dlist) return 0; - - return dlist->da.count; -} - -/* free item from the dynamic list with access array */ -void BLI_dlist_free_item(DynamicList *dlist, unsigned int index) -{ - if(!dlist || !dlist->da.items) return; - - if((index <= dlist->da.last_item_index) && (dlist->da.items[index])){ - BLI_freelinkN(&(dlist->lb), dlist->da.items[index]); - rem_array_item(&(dlist->da), index); - } -} - -/* remove item from the dynamic list with access array */ -void BLI_dlist_rem_item(DynamicList *dlist, unsigned int index) -{ - if(!dlist || !dlist->da.items) return; - - if((index <= dlist->da.last_item_index) && (dlist->da.items[index])){ - BLI_remlink(&(dlist->lb), dlist->da.items[index]); - rem_array_item(&(dlist->da), index); - } -} - -/* add item to the dynamic list with access array (index) */ -void* BLI_dlist_add_item_index(DynamicList *dlist, void *item, unsigned int index) -{ - if(!dlist || !dlist->da.items) return NULL; - - if((index <= dlist->da.max_item_index) && (dlist->da.items[index])) { - /* you can't place item at used index */ - return NULL; - } - else { - add_array_item(&(dlist->da), item, index); - BLI_addtail(&(dlist->lb), item); - return item; - } -} - -/* destroy dynamic list with access array */ -void BLI_dlist_destroy(DynamicList *dlist) -{ - if(!dlist) return; - - BLI_freelistN(&(dlist->lb)); - destroy_array(&(dlist->da)); -} - -/* initialize dynamic list with access array */ -void BLI_dlist_init(DynamicList *dlist) -{ - if(!dlist) return; - - dlist->lb.first = NULL; - dlist->lb.last = NULL; - - init_array(&(dlist->da)); -} - -/* reinitialize dynamic list with acces array */ -void BLI_dlist_reinit(DynamicList *dlist) -{ - if(!dlist) return; - - BLI_freelistN(&(dlist->lb)); - reinit_array(&(dlist->da)); -} - -/*=====================================================================================*/ - -char *BLI_strdupn(const char *str, int len) { - char *n= MEM_mallocN(len+1, "strdup"); - memcpy(n, str, len); - n[len]= '\0'; - - return n; -} -char *BLI_strdup(const char *str) { - return BLI_strdupn(str, strlen(str)); -} - -char *BLI_strncpy(char *dst, const char *src, int maxncpy) { - int srclen= strlen(src); - int cpylen= (srclen>(maxncpy-1))?(maxncpy-1):srclen; - - memcpy(dst, src, cpylen); - dst[cpylen]= '\0'; - - return dst; -} - -int BLI_snprintf(char *buffer, size_t count, const char *format, ...) -{ - int n; - va_list arg; - - va_start(arg, format); - n = vsnprintf(buffer, count, format, arg); - - if (n != -1 && n < count) { - buffer[n] = '\0'; - } else { - buffer[count-1] = '\0'; - } - - va_end(arg); - return n; -} - -int BLI_streq(char *a, char *b) { - return (strcmp(a, b)==0); -} -int BLI_strcaseeq(char *a, char *b) { - return (BLI_strcasecmp(a, b)==0); -} - /* ******************** string encoding ***************** */ /* This is quite an ugly function... its purpose is to @@ -1924,6 +1339,27 @@ void BLI_where_is_temp(char *fullname, int usertemp) } } +char *get_install_dir(void) { + extern char bprogname[]; + char *tmpname = BLI_strdup(bprogname); + char *cut; + +#ifdef __APPLE__ + cut = strstr(tmpname, ".app"); + if (cut) cut[0] = 0; +#endif + + cut = BLI_last_slash(tmpname); + + if (cut) { + cut[0] = 0; + return tmpname; + } else { + MEM_freeN(tmpname); + return NULL; + } +} + /* * returns absolute path to the app bundle * only useful on OS X @@ -1942,65 +1378,7 @@ char* BLI_getbundle(void) { } #endif -/* strcasestr not available in MSVC */ -char *BLI_strcasestr(const char *s, const char *find) -{ - register char c, sc; - register size_t len; - - if ((c = *find++) != 0) { - c= tolower(c); - len = strlen(find); - do { - do { - if ((sc = *s++) == 0) - return (NULL); - sc= tolower(sc); - } while (sc != c); - } while (BLI_strncasecmp(s, find, len) != 0); - s--; - } - return ((char *) s); -} - - -int BLI_strcasecmp(const char *s1, const char *s2) { - int i; - - for (i=0; ; i++) { - char c1 = tolower(s1[i]); - char c2 = tolower(s2[i]); - - if (c1<c2) { - return -1; - } else if (c1>c2) { - return 1; - } else if (c1==0) { - break; - } - } - - return 0; -} -int BLI_strncasecmp(const char *s1, const char *s2, int n) { - int i; - - for (i=0; i<n; i++) { - char c1 = tolower(s1[i]); - char c2 = tolower(s2[i]); - - if (c1<c2) { - return -1; - } else if (c1>c2) { - return 1; - } else if (c1==0) { - break; - } - } - - return 0; -} #ifdef WITH_ICONV @@ -2034,68 +1412,4 @@ void BLI_string_to_utf8(char *original, char *utf_8, const char *code) } #endif // WITH_ICONV -void BLI_timestr(double _time, char *str) -{ - /* format 00:00:00.00 (hr:min:sec) string has to be 12 long */ - int hr= ( (int) _time) / (60*60); - int min= (((int) _time) / 60 ) % 60; - int sec= ( (int) (_time)) % 60; - int hun= ( (int) (_time * 100.0)) % 100; - - if (hr) { - sprintf(str, "%.2d:%.2d:%.2d.%.2d",hr,min,sec,hun); - } else { - sprintf(str, "%.2d:%.2d.%.2d",min,sec,hun); - } - - str[11]=0; -} - -/* ************** 64 bits magic, trick to support up to 32 gig of address space *************** */ -/* only works for malloced pointers (8 aligned) */ - -#ifdef __LP64__ - -#if defined(WIN32) && !defined(FREE_WINDOWS) -#define PMASK 0x07FFFFFFFFi64 -#else -#define PMASK 0x07FFFFFFFFll -#endif - - -int BLI_int_from_pointer(void *poin) -{ - intptr_t lval= (intptr_t)poin; - - return (int)(lval>>3); -} - -void *BLI_pointer_from_int(int val) -{ - static int firsttime= 1; - static intptr_t basevalue= 0; - - if(firsttime) { - void *poin= malloc(10000); - basevalue= (intptr_t)poin; - basevalue &= ~PMASK; - printf("base: %d pointer %p\n", basevalue, poin); /* debug */ - firsttime= 0; - free(poin); - } - return (void *)(basevalue | (((intptr_t)val)<<3)); -} - -#else - -int BLI_int_from_pointer(void *poin) -{ - return (int)poin; -} -void *BLI_pointer_from_int(int val) -{ - return (void *)val; -} - -#endif |