From ebb6628a0143a91189cc209487e6588ab3e9fa99 Mon Sep 17 00:00:00 2001 From: Andrea Weikert Date: Sat, 20 Dec 2008 10:02:00 +0000 Subject: 2.5 BLI_blenlib cleanup DONE: * moved almost all declarations from BLI_blenlib.h into their own proper header files. * BLI_blenlib.h still includes all the declarations for convenience and to avoid changes in existing code * split util.c into several files, where it wasn't done already * DynamicList -> dynamiclist, * ListBase -> listbase, * String utility functions -> string.c * removed a few unused macros and functions, if they're needed back, they're still in svn ;) TODO: * btempdir global * further cleanup in the code of the different modules (especially util.c) --- source/blender/blenlib/BLI_blenlib.h | 344 +------------ source/blender/blenlib/BLI_dynamiclist.h | 19 +- source/blender/blenlib/BLI_fileops.h | 66 +++ source/blender/blenlib/BLI_listbase.h | 64 +++ source/blender/blenlib/BLI_noise.h | 66 +++ source/blender/blenlib/BLI_rect.h | 65 +++ source/blender/blenlib/BLI_scanfill.h | 81 +++ source/blender/blenlib/BLI_storage.h | 72 +++ source/blender/blenlib/BLI_string.h | 100 ++++ source/blender/blenlib/BLI_util.h | 136 +++++ source/blender/blenlib/BLI_winstuff.h | 4 + source/blender/blenlib/intern/BLI_fileops.h | 47 -- source/blender/blenlib/intern/BLI_scanfill.h | 38 -- source/blender/blenlib/intern/BLI_storage.h | 45 -- source/blender/blenlib/intern/BLI_util.h | 42 -- source/blender/blenlib/intern/dynamiclist.c | 265 ++++++++++ source/blender/blenlib/intern/dynamiclist.h | 55 ++ source/blender/blenlib/intern/listbase.c | 363 +++++++++++++ source/blender/blenlib/intern/string.c | 163 ++++++ source/blender/blenlib/intern/threads.c | 2 + source/blender/blenlib/intern/util.c | 728 +-------------------------- 21 files changed, 1547 insertions(+), 1218 deletions(-) create mode 100644 source/blender/blenlib/BLI_fileops.h create mode 100644 source/blender/blenlib/BLI_listbase.h create mode 100644 source/blender/blenlib/BLI_noise.h create mode 100644 source/blender/blenlib/BLI_rect.h create mode 100644 source/blender/blenlib/BLI_scanfill.h create mode 100644 source/blender/blenlib/BLI_storage.h create mode 100644 source/blender/blenlib/BLI_string.h create mode 100644 source/blender/blenlib/BLI_util.h delete mode 100644 source/blender/blenlib/intern/BLI_fileops.h delete mode 100644 source/blender/blenlib/intern/BLI_scanfill.h delete mode 100644 source/blender/blenlib/intern/BLI_storage.h delete mode 100644 source/blender/blenlib/intern/BLI_util.h create mode 100644 source/blender/blenlib/intern/dynamiclist.c create mode 100644 source/blender/blenlib/intern/dynamiclist.h create mode 100644 source/blender/blenlib/intern/listbase.c create mode 100644 source/blender/blenlib/intern/string.c (limited to 'source/blender/blenlib') diff --git a/source/blender/blenlib/BLI_blenlib.h b/source/blender/blenlib/BLI_blenlib.h index f65aaa72a75..cb5f68ed1e0 100644 --- a/source/blender/blenlib/BLI_blenlib.h +++ b/source/blender/blenlib/BLI_blenlib.h @@ -62,357 +62,33 @@ #ifndef BLI_BLENLIB_H #define BLI_BLENLIB_H -/* braindamage for the masses... needed - because fillfacebase and fillvertbase are used outside */ -#include "DNA_listBase.h" +struct ListBase; #include -extern ListBase fillfacebase; -extern ListBase fillvertbase; -/** - * @attention Defined in scanfill.c - */ -extern ListBase filledgebase; extern char btempdir[]; /* creator.c temp dir used instead of U.tempdir, set with BLI_where_is_temp( btempdir, 1 ); */ -struct chardesc; -struct direntry; -struct rctf; -struct rcti; -struct EditVert; -struct PackedFile; -struct LinkNode; -struct DynamicList; - #ifdef __cplusplus extern "C" { #endif -/* BLI_util.h */ -char *BLI_gethome(void); -void BLI_make_file_string(const char *relabase, char *string, const char *dir, const char *file); -void BLI_make_exist(char *dir); -void BLI_make_existing_file(char *name); -void BLI_split_dirfile(char *string, char *dir, char *file); -void BLI_split_dirfile_basic(const char *string, char *dir, char *file); -void BLI_join_dirfile(char *string, const char *dir, const char *file); -int BLI_testextensie(const char *str, const char *ext); -void addlisttolist(ListBase *list1, ListBase *list2); -void BLI_insertlink(struct ListBase *listbase, void *vprevlink, void *vnewlink); -void *BLI_findlink(struct ListBase *listbase, int number); -int BLI_findindex(struct ListBase *listbase, void *vlink); -void BLI_freelistN(struct ListBase *listbase); -void BLI_addtail(struct ListBase *listbase, void *vlink); -void BLI_remlink(struct ListBase *listbase, void *vlink); -void BLI_uniquename(struct ListBase *list, void *vlink, char defname[], short name_offs, short len); -void BLI_newname(char * name, int add); -int BLI_stringdec(char *string, char *kop, char *start, unsigned short *numlen); -void BLI_stringenc(char *string, char *kop, char *start, unsigned short numlen, int pic); -void BLI_addhead(struct ListBase *listbase, void *vlink); -void BLI_insertlinkbefore(struct ListBase *listbase, void *vnextlink, void *vnewlink); -void BLI_insertlinkafter(struct ListBase *listbase, void *vprevlink, void *vnewlink); -void BLI_sortlist(struct ListBase *listbase, int (*cmp)(void *, void *)); -void BLI_freelist(struct ListBase *listbase); -int BLI_countlist(struct ListBase *listbase); -void BLI_freelinkN(ListBase *listbase, void *vlink); -void BLI_duplicatelist(ListBase *list1, ListBase *list2); /* copy from 2 to 1 */ - - -void BLI_splitdirstring(char *di,char *fi); - -struct DynamicList *BLI_dlist_from_listbase(struct ListBase *lb); -struct ListBase *BLI_listbase_from_dlist(struct DynamicList *dlist, struct ListBase *lb); -void * BLI_dlist_find_link(struct DynamicList *dlist, unsigned int index); -unsigned int BLI_count_items(struct DynamicList *dlist); -void BLI_dlist_free_item(struct DynamicList *dlist, unsigned int index); -void BLI_dlist_rem_item(struct DynamicList *dlist, unsigned int index); -void * BLI_dlist_add_item_index(struct DynamicList *dlist, void *item, unsigned int index); -void BLI_dlist_destroy(struct DynamicList *dlist); -void BLI_dlist_init(struct DynamicList *dlist); -void BLI_dlist_reinit(struct DynamicList *dlist); - - /** - * dir can be any input, like from buttons, and this function - * converts it to a regular full path. - * Also removes garbage from directory paths, like /../ or double slashes etc - */ -void BLI_cleanup_file(const char *relabase, char *dir); -void BLI_cleanup_dir(const char *relabase, char *dir); /* same as above but adds a trailing slash */ +#include "BLI_listbase.h" -/* go back one directory */ -int BLI_parent_dir(char *path); +#include "BLI_dynamiclist.h" - /** - * Blender's path code replacement function. - * Bases @a path strings leading with "//" by the - * directory @a basepath, and replaces instances of - * '#' with the @a framenum. Results are written - * back into @a path. - * - * @a path The path to convert - * @a basepath The directory to base relative paths with. - * @a framenum The framenumber to replace the frame code with. - * @retval Returns true if the path was relative (started with "//"). - */ -int BLI_convertstringcode(char *path, const char *basepath); -int BLI_convertstringframe(char *path, int frame); -int BLI_convertstringcwd(char *path); +#include "BLI_string.h" -void BLI_makestringcode(const char *relfile, char *file); +#include "BLI_util.h" - /** - * Change every @a from in @a string into @a to. The - * result will be in @a string - * - * @a string The string to work on - * @a from The character to replace - * @a to The character to replace with - */ -void BLI_char_switch(char *string, char from, char to); +#include "BLI_storage.h" - /** - * Makes sure @a path has platform-specific slashes. - * - * @a path The path to 'clean' - */ -void BLI_clean(char *path); - /** - * Duplicates the cstring @a str into a newly mallocN'd - * string and returns it. - * - * @param str The string to be duplicated - * @retval Returns the duplicated string - */ -char *BLI_strdup(const char *str); +#include "BLI_fileops.h" - /** - * Duplicates the first @a len bytes of cstring @a str - * into a newly mallocN'd string and returns it. @a str - * is assumed to be at least len bytes long. - * - * @param str The string to be duplicated - * @param len The number of bytes to duplicate - * @retval Returns the duplicated string - */ -char *BLI_strdupn(const char *str, int len); - - /** - * Like strncpy but ensures dst is always - * '\0' terminated. - * - * @param dst Destination for copy - * @param src Source string to copy - * @param maxncpy Maximum number of characters to copy (generally - * the size of dst) - * @retval Returns dst - */ -char *BLI_strncpy(char *dst, const char *src, int maxncpy); - - /* - * Replacement for snprintf - */ -int BLI_snprintf(char *buffer, size_t count, const char *format, ...); - - /** - * Compare two strings - * - * @retval True if the strings are equal, false otherwise. - */ -int BLI_streq(char *a, char *b); - - /** - * Compare two strings without regard to case. - * - * @retval True if the strings are equal, false otherwise. - */ -int BLI_strcaseeq(char *a, char *b); - -/* in util.c */ -#ifdef WITH_ICONV -void BLI_string_to_utf8(char *original, char *utf_8, const char *code); -#endif - - /** - * Read a file as ASCII lines. An empty list is - * returned if the file cannot be opened or read. - * - * @attention The returned list should be free'd with - * BLI_free_file_lines. - * - * @param name The name of the file to read. - * @retval A list of strings representing the file lines. - */ -struct LinkNode *BLI_read_file_as_lines(char *name); - - /** - * Free the list returned by BLI_read_file_as_lines. - */ -void BLI_free_file_lines(struct LinkNode *lines); - - /** - * Checks if name is a fully qualified filename to an executable. - * If not it searches $PATH for the file. On Windows it also - * adds the correct extension (.com .exe etc) from - * $PATHEXT if necessary. Also on Windows it translates - * the name to its 8.3 version to prevent problems with - * spaces and stuff. Final result is returned in fullname. - * - * @param fullname The full path and full name of the executable - * @param name The name of the executable (usually argv[0]) to be checked - */ -void BLI_where_am_i(char *fullname, const char *name); - -char *get_install_dir(void); - /** - * Gets the temp directory when blender first runs. - * If the default path is not found, use try $TEMP - * - * Also make sure the temp dir has a trailing slash - * - * @param fullname The full path to the temp directory - */ -void BLI_where_is_temp(char *fullname, int usertemp); - - - /** - * determines the full path to the application bundle on OS X - * - * @return path to application bundle - */ -#ifdef __APPLE__ -char* BLI_getbundle(void); -#endif - -#ifdef WIN32 -int BLI_getInstallationDir(char *str); -#endif - -/* BLI_storage.h */ -int BLI_filesize(int file); -int BLI_filepathsize(const char *path); -double BLI_diskfree(char *dir); -char *BLI_getwdN(char *dir); -void BLI_hide_dot_files(int set); -unsigned int BLI_getdir(char *dirname, struct direntry **filelist); - -/** - * @attention Do not confuse with BLI_exists - */ -int BLI_exist(char *name); +#include "BLI_rect.h" -/* BLI_fileops.h */ -void BLI_recurdir_fileops(char *dirname); -int BLI_link(char *file, char *to); -int BLI_is_writable(char *filename); +#include "BLI_scanfill.h" -/** - * @attention Do not confuse with BLI_exist - */ -int BLI_exists(char *file); -int BLI_copy_fileops(char *file, char *to); -int BLI_rename(char *from, char *to); -int BLI_gzip(char *from, char *to); -int BLI_delete(char *file, int dir, int recursive); -int BLI_move(char *file, char *to); -int BLI_touch(const char *file); -char *BLI_last_slash(const char *string); -void BLI_add_slash(char *string); -void BLI_del_slash(char *string); - -/* BLI_rct.c */ -/** - * Determine if a rect is empty. An empty - * rect is one with a zero (or negative) - * width or height. - * - * @return True if @a rect is empty. - */ -int BLI_rcti_is_empty(struct rcti *rect); -void BLI_init_rctf(struct rctf *rect, float xmin, float xmax, float ymin, float ymax); -void BLI_init_rcti(struct rcti *rect, int xmin, int xmax, int ymin, int ymax); -void BLI_translate_rctf(struct rctf *rect, float x, float y); -void BLI_translate_rcti(struct rcti *rect, int x, int y); -int BLI_in_rcti(struct rcti *rect, int x, int y); -int BLI_in_rctf(struct rctf *rect, float x, float y); -int BLI_isect_rctf(struct rctf *src1, struct rctf *src2, struct rctf *dest); -int BLI_isect_rcti(struct rcti *src1, struct rcti *src2, struct rcti *dest); -void BLI_union_rctf(struct rctf *rcta, struct rctf *rctb); - -/* scanfill.c: used in displist only... */ -struct EditVert *BLI_addfillvert(float *vec); -struct EditEdge *BLI_addfilledge(struct EditVert *v1, struct EditVert *v2); -int BLI_edgefill(int mode, int mat_nr); -void BLI_end_edgefill(void); - -/* noise.h: */ -float BLI_hnoise(float noisesize, float x, float y, float z); -float BLI_hnoisep(float noisesize, float x, float y, float z); -float BLI_turbulence(float noisesize, float x, float y, float z, int nr); -float BLI_turbulence1(float noisesize, float x, float y, float z, int nr); -/* newnoise: generic noise & turbulence functions to replace the above BLI_hnoise/p & BLI_turbulence/1. - * This is done so different noise basis functions can be used */ -float BLI_gNoise(float noisesize, float x, float y, float z, int hard, int noisebasis); -float BLI_gTurbulence(float noisesize, float x, float y, float z, int oct, int hard, int noisebasis); -/* newnoise: musgrave functions */ -float mg_fBm(float x, float y, float z, float H, float lacunarity, float octaves, int noisebasis); -float mg_MultiFractal(float x, float y, float z, float H, float lacunarity, float octaves, int noisebasis); -float mg_VLNoise(float x, float y, float z, float distortion, int nbas1, int nbas2); -float mg_HeteroTerrain(float x, float y, float z, float H, float lacunarity, float octaves, float offset, int noisebasis); -float mg_HybridMultiFractal(float x, float y, float z, float H, float lacunarity, float octaves, float offset, float gain, int noisebasis); -float mg_RidgedMultiFractal(float x, float y, float z, float H, float lacunarity, float octaves, float offset, float gain, int noisebasis); -/* newnoise: voronoi */ -void voronoi(float x, float y, float z, float* da, float* pa, float me, int dtype); -/* newnoise: cellNoise & cellNoiseV (for vector/point/color) */ -float cellNoise(float x, float y, float z); -void cellNoiseV(float x, float y, float z, float *ca); - -/* These callbacks are needed to make the lib finction properly */ - -/** - * Set a function taking a char* as argument to flag errors. If the - * callback is not set, the error is discarded. - * @param f The function to use as callback - * @attention used in creator.c - */ -void BLI_setErrorCallBack(void (*f)(char*)); - -/** - * Set a function to be able to interrupt the execution of processing - * in this module. If the function returns true, the execution will - * terminate gracefully. If the callback is not set, interruption is - * not possible. - * @param f The function to use as callback - * @attention used in creator.c - */ -void BLI_setInterruptCallBack(int (*f)(void)); - -char *BLI_strcasestr(const char *s, const char *find); -int BLI_strcasecmp(const char *s1, const char *s2); -int BLI_strncasecmp(const char *s1, const char *s2, int n); -void BLI_timestr(double _time, char *str); /* time var is global */ - -/** - * Trick to address 32 GB with an int (only for malloced pointers) - */ -int BLI_int_from_pointer(void *poin); -void *BLI_pointer_from_int(int val); - - -#define PRNTSUB(type,arg) printf(#arg ": %" #type " ", arg) - -#ifndef PRINT -#define PRINT(t,v) {PRNTSUB(t,v); printf("\n");} -#define PRINT2(t1,v1,t2,v2) {PRNTSUB(t1,v1); PRNTSUB(t2,v2); printf("\n");} -#define PRINT3(t1,v1,t2,v2,t3,v3) {PRNTSUB(t1,v1); PRNTSUB(t2,v2); PRNTSUB(t3,v3); printf("\n");} -#define PRINT4(t1,v1,t2,v2,t3,v3,t4,v4) {PRNTSUB(t1,v1); PRNTSUB(t2,v2); PRNTSUB(t3,v3); PRNTSUB(t4,v4); printf("\n");} -#endif - -/** - * @param array The array in question - * @retval The number of elements in the array. - */ -#define BLI_ARRAY_NELEMS(array) (sizeof((array))/sizeof((array)[0])) +#include "BLI_noise.h" /** * @param strct The structure of interest diff --git a/source/blender/blenlib/BLI_dynamiclist.h b/source/blender/blenlib/BLI_dynamiclist.h index e8c93fbcf23..b496d633b31 100644 --- a/source/blender/blenlib/BLI_dynamiclist.h +++ b/source/blender/blenlib/BLI_dynamiclist.h @@ -26,12 +26,10 @@ * ***** END GPL LICENSE BLOCK ***** */ -#ifndef B_DYNAMIC_LIST_H -#define B_DYNAMIC_LIST_H +#ifndef BLI_DYNAMIC_LIST_H +#define BLI_DYNAMIC_LIST_H -#define PAGE_SIZE 4 - -struct ListBase; +#include "DNA_listBase.h" /* * Access array using realloc @@ -52,4 +50,15 @@ typedef struct DynamicList { struct ListBase lb; /* two way linked dynamic list */ } DynamicList; +struct DynamicList *BLI_dlist_from_listbase(struct ListBase *lb); +struct ListBase *BLI_listbase_from_dlist(struct DynamicList *dlist, struct ListBase *lb); +void * BLI_dlist_find_link(struct DynamicList *dlist, unsigned int index); +unsigned int BLI_count_items(struct DynamicList *dlist); +void BLI_dlist_free_item(struct DynamicList *dlist, unsigned int index); +void BLI_dlist_rem_item(struct DynamicList *dlist, unsigned int index); +void * BLI_dlist_add_item_index(struct DynamicList *dlist, void *item, unsigned int index); +void BLI_dlist_destroy(struct DynamicList *dlist); +void BLI_dlist_init(struct DynamicList *dlist); +void BLI_dlist_reinit(struct DynamicList *dlist); + #endif diff --git a/source/blender/blenlib/BLI_fileops.h b/source/blender/blenlib/BLI_fileops.h new file mode 100644 index 00000000000..146ca16606e --- /dev/null +++ b/source/blender/blenlib/BLI_fileops.h @@ -0,0 +1,66 @@ +/** + * 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 + + + +void BLI_recurdir_fileops(char *dirname); +int BLI_link(char *file, char *to); +int BLI_is_writable(char *filename); + +/** + * @attention Do not confuse with BLI_exist + */ +int BLI_exists(char *file); +int BLI_copy_fileops(char *file, char *to); +int BLI_rename(char *from, char *to); +int BLI_gzip(char *from, char *to); +int BLI_delete(char *file, int dir, int recursive); +int BLI_move(char *file, char *to); +int BLI_touch(const char *file); +char *BLI_last_slash(const char *string); +void BLI_add_slash(char *string); +void BLI_del_slash(char *string); +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/BLI_listbase.h b/source/blender/blenlib/BLI_listbase.h new file mode 100644 index 00000000000..703689e76a6 --- /dev/null +++ b/source/blender/blenlib/BLI_listbase.h @@ -0,0 +1,64 @@ +/* + * $Id: BLI_blenlib.h 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 ***** + * + * $Id: $ +*/ + +#ifndef BLI_LISTBASE_H +#define BLI_LISTBASE_H + +//#include "DNA_listbase.h" +struct ListBase; + +#ifdef __cplusplus +extern "C" { +#endif + +void addlisttolist(struct ListBase *list1, struct ListBase *list2); +void BLI_insertlink(struct ListBase *listbase, void *vprevlink, void *vnewlink); +void *BLI_findlink(struct ListBase *listbase, int number); +int BLI_findindex(struct ListBase *listbase, void *vlink); +void BLI_freelistN(struct ListBase *listbase); +void BLI_addtail(struct ListBase *listbase, void *vlink); +void BLI_remlink(struct ListBase *listbase, void *vlink); +void BLI_uniquename(struct ListBase *list, void *vlink, char defname[], short name_offs, short len); + +void BLI_addhead(struct ListBase *listbase, void *vlink); +void BLI_insertlinkbefore(struct ListBase *listbase, void *vnextlink, void *vnewlink); +void BLI_insertlinkafter(struct ListBase *listbase, void *vprevlink, void *vnewlink); +void BLI_sortlist(struct ListBase *listbase, int (*cmp)(void *, void *)); +void BLI_freelist(struct ListBase *listbase); +int BLI_countlist(struct ListBase *listbase); +void BLI_freelinkN(struct ListBase *listbase, void *vlink); +void BLI_duplicatelist(struct ListBase *list1, struct ListBase *list2); /* copy from 2 to 1 */ + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/source/blender/blenlib/BLI_noise.h b/source/blender/blenlib/BLI_noise.h new file mode 100644 index 00000000000..f419746bc13 --- /dev/null +++ b/source/blender/blenlib/BLI_noise.h @@ -0,0 +1,66 @@ +/* + * $Id: BLI_blenlib.h 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 ***** + * + * $Id: $ +*/ + +#ifndef BLI_NOISE_H +#define BLI_NOISE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/* noise.h: */ +float BLI_hnoise(float noisesize, float x, float y, float z); +float BLI_hnoisep(float noisesize, float x, float y, float z); +float BLI_turbulence(float noisesize, float x, float y, float z, int nr); +float BLI_turbulence1(float noisesize, float x, float y, float z, int nr); +/* newnoise: generic noise & turbulence functions to replace the above BLI_hnoise/p & BLI_turbulence/1. + * This is done so different noise basis functions can be used */ +float BLI_gNoise(float noisesize, float x, float y, float z, int hard, int noisebasis); +float BLI_gTurbulence(float noisesize, float x, float y, float z, int oct, int hard, int noisebasis); +/* newnoise: musgrave functions */ +float mg_fBm(float x, float y, float z, float H, float lacunarity, float octaves, int noisebasis); +float mg_MultiFractal(float x, float y, float z, float H, float lacunarity, float octaves, int noisebasis); +float mg_VLNoise(float x, float y, float z, float distortion, int nbas1, int nbas2); +float mg_HeteroTerrain(float x, float y, float z, float H, float lacunarity, float octaves, float offset, int noisebasis); +float mg_HybridMultiFractal(float x, float y, float z, float H, float lacunarity, float octaves, float offset, float gain, int noisebasis); +float mg_RidgedMultiFractal(float x, float y, float z, float H, float lacunarity, float octaves, float offset, float gain, int noisebasis); +/* newnoise: voronoi */ +void voronoi(float x, float y, float z, float* da, float* pa, float me, int dtype); +/* newnoise: cellNoise & cellNoiseV (for vector/point/color) */ +float cellNoise(float x, float y, float z); +void cellNoiseV(float x, float y, float z, float *ca); + + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/source/blender/blenlib/BLI_rect.h b/source/blender/blenlib/BLI_rect.h new file mode 100644 index 00000000000..ce8012963b0 --- /dev/null +++ b/source/blender/blenlib/BLI_rect.h @@ -0,0 +1,65 @@ +/* + * $Id: BLI_blenlib.h 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 ***** + * + * $Id: $ +*/ + +#ifndef BLI_RECT_H +#define BLI_RECT_H + +struct rctf; +struct rcti; + +#ifdef __cplusplus +extern "C" { +#endif + +/* BLI_rct.c */ +/** + * Determine if a rect is empty. An empty + * rect is one with a zero (or negative) + * width or height. + * + * @return True if @a rect is empty. + */ +int BLI_rcti_is_empty(struct rcti *rect); +void BLI_init_rctf(struct rctf *rect, float xmin, float xmax, float ymin, float ymax); +void BLI_init_rcti(struct rcti *rect, int xmin, int xmax, int ymin, int ymax); +void BLI_translate_rctf(struct rctf *rect, float x, float y); +void BLI_translate_rcti(struct rcti *rect, int x, int y); +int BLI_in_rcti(struct rcti *rect, int x, int y); +int BLI_in_rctf(struct rctf *rect, float x, float y); +int BLI_isect_rctf(struct rctf *src1, struct rctf *src2, struct rctf *dest); +int BLI_isect_rcti(struct rcti *src1, struct rcti *src2, struct rcti *dest); +void BLI_union_rctf(struct rctf *rcta, struct rctf *rctb); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/source/blender/blenlib/BLI_scanfill.h b/source/blender/blenlib/BLI_scanfill.h new file mode 100644 index 00000000000..1541b01da62 --- /dev/null +++ b/source/blender/blenlib/BLI_scanfill.h @@ -0,0 +1,81 @@ +/** + * 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 + +/** + * @attention Defined in scanfill.c + */ +extern struct ListBase fillvertbase; +extern struct ListBase filledgebase; +extern struct ListBase fillfacebase; + +struct EditVert; + +#ifdef __cplusplus +extern "C" { +#endif + +/* scanfill.c: used in displist only... */ +struct EditVert *BLI_addfillvert(float *vec); +struct EditEdge *BLI_addfilledge(struct EditVert *v1, struct EditVert *v2); +int BLI_edgefill(int mode, int mat_nr); +void BLI_end_edgefill(void); + +/* These callbacks are needed to make the lib finction properly */ + +/** + * Set a function taking a char* as argument to flag errors. If the + * callback is not set, the error is discarded. + * @param f The function to use as callback + * @attention used in creator.c + */ +void BLI_setErrorCallBack(void (*f)(char*)); + +/** + * Set a function to be able to interrupt the execution of processing + * in this module. If the function returns true, the execution will + * terminate gracefully. If the callback is not set, interruption is + * not possible. + * @param f The function to use as callback + * @attention used in creator.c + */ +void BLI_setInterruptCallBack(int (*f)(void)); + +#ifdef __cplusplus +} +#endif + +#endif + diff --git a/source/blender/blenlib/BLI_storage.h b/source/blender/blenlib/BLI_storage.h new file mode 100644 index 00000000000..d7bf3bd13f8 --- /dev/null +++ b/source/blender/blenlib/BLI_storage.h @@ -0,0 +1,72 @@ +/* $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 + +struct direntry; + +void BLI_adddirstrings(void); +void BLI_builddir(char *dirname, char *relname); +int BLI_compare(struct direntry *entry1, struct direntry *entry2); + +int BLI_filesize(int file); +int BLI_filepathsize(const char *path); +double BLI_diskfree(char *dir); +char *BLI_getwdN(char *dir); +void BLI_hide_dot_files(int set); +unsigned int BLI_getdir(char *dirname, struct direntry **filelist); +/** + * @attention Do not confuse with BLI_exists + */ +int BLI_exist(char *name); +/** + * Read a file as ASCII lines. An empty list is + * returned if the file cannot be opened or read. + * + * @attention The returned list should be free'd with + * BLI_free_file_lines. + * + * @param name The name of the file to read. + * @retval A list of strings representing the file lines. + */ +struct LinkNode *BLI_read_file_as_lines(char *name); + + /** + * Free the list returned by BLI_read_file_as_lines. + */ +void BLI_free_file_lines(struct LinkNode *lines); + +#endif /* BLI_STORAGE_H */ + diff --git a/source/blender/blenlib/BLI_string.h b/source/blender/blenlib/BLI_string.h new file mode 100644 index 00000000000..d9d030432d3 --- /dev/null +++ b/source/blender/blenlib/BLI_string.h @@ -0,0 +1,100 @@ +/* + * $Id: BLI_blenlib.h 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 ***** + * + * $Id: $ +*/ + +#ifndef BLI_STRING_H +#define BLI_STRING_H + +#ifdef __cplusplus +extern "C" { +#endif + + /** + * Duplicates the cstring @a str into a newly mallocN'd + * string and returns it. + * + * @param str The string to be duplicated + * @retval Returns the duplicated string + */ +char *BLI_strdup(const char *str); + + /** + * Duplicates the first @a len bytes of cstring @a str + * into a newly mallocN'd string and returns it. @a str + * is assumed to be at least len bytes long. + * + * @param str The string to be duplicated + * @param len The number of bytes to duplicate + * @retval Returns the duplicated string + */ +char *BLI_strdupn(const char *str, int len); + + /** + * Like strncpy but ensures dst is always + * '\0' terminated. + * + * @param dst Destination for copy + * @param src Source string to copy + * @param maxncpy Maximum number of characters to copy (generally + * the size of dst) + * @retval Returns dst + */ +char *BLI_strncpy(char *dst, const char *src, int maxncpy); + + /* + * Replacement for snprintf + */ +int BLI_snprintf(char *buffer, size_t count, const char *format, ...); + + /** + * Compare two strings + * + * @retval True if the strings are equal, false otherwise. + */ +int BLI_streq(char *a, char *b); + + /** + * Compare two strings without regard to case. + * + * @retval True if the strings are equal, false otherwise. + */ +int BLI_strcaseeq(char *a, char *b); + +char *BLI_strcasestr(const char *s, const char *find); +int BLI_strcasecmp(const char *s1, const char *s2); +int BLI_strncasecmp(const char *s1, const char *s2, int n); + +void BLI_timestr(double _time, char *str); /* time var is global */ + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/source/blender/blenlib/BLI_util.h b/source/blender/blenlib/BLI_util.h new file mode 100644 index 00000000000..ca6dde8b3bd --- /dev/null +++ b/source/blender/blenlib/BLI_util.h @@ -0,0 +1,136 @@ +/** + * 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 + +/* XXX doesn't seem to be used, marded for removal +#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; +struct direntry; + +char *BLI_gethome(void); +void BLI_make_file_string(const char *relabase, char *string, const char *dir, const char *file); +void BLI_make_exist(char *dir); +void BLI_make_existing_file(char *name); +void BLI_split_dirfile(char *string, char *dir, char *file); +void BLI_split_dirfile_basic(const char *string, char *dir, char *file); +void BLI_join_dirfile(char *string, const char *dir, const char *file); +int BLI_testextensie(const char *str, const char *ext); +void BLI_uniquename(struct ListBase *list, void *vlink, char defname[], short name_offs, short len); +void BLI_newname(char * name, int add); +int BLI_stringdec(char *string, char *kop, char *start, unsigned short *numlen); +void BLI_stringenc(char *string, char *kop, char *start, unsigned short numlen, int pic); +void BLI_splitdirstring(char *di,char *fi); + +/** + * dir can be any input, like from buttons, and this function + * converts it to a regular full path. + * Also removes garbage from directory paths, like /../ or double slashes etc + */ +void BLI_cleanup_file(const char *relabase, char *dir); +void BLI_cleanup_dir(const char *relabase, char *dir); /* same as above but adds a trailing slash */ + +/* go back one directory */ +int BLI_parent_dir(char *path); + + /** + * Blender's path code replacement function. + * Bases @a path strings leading with "//" by the + * directory @a basepath, and replaces instances of + * '#' with the @a framenum. Results are written + * back into @a path. + * + * @a path The path to convert + * @a basepath The directory to base relative paths with. + * @a framenum The framenumber to replace the frame code with. + * @retval Returns true if the path was relative (started with "//"). + */ +int BLI_convertstringcode(char *path, const char *basepath); +int BLI_convertstringframe(char *path, int frame); +int BLI_convertstringcwd(char *path); + +void BLI_makestringcode(const char *relfile, char *file); + + /** + * Change every @a from in @a string into @a to. The + * result will be in @a string + * + * @a string The string to work on + * @a from The character to replace + * @a to The character to replace with + */ +void BLI_char_switch(char *string, char from, char to); + +/** + * Checks if name is a fully qualified filename to an executable. + * If not it searches $PATH for the file. On Windows it also + * adds the correct extension (.com .exe etc) from + * $PATHEXT if necessary. Also on Windows it translates + * the name to its 8.3 version to prevent problems with + * spaces and stuff. Final result is returned in fullname. + * + * @param fullname The full path and full name of the executable + * @param name The name of the executable (usually argv[0]) to be checked + */ +void BLI_where_am_i(char *fullname, const char *name); + +char *get_install_dir(void); + /** + * Gets the temp directory when blender first runs. + * If the default path is not found, use try $TEMP + * + * Also make sure the temp dir has a trailing slash + * + * @param fullname The full path to the temp directory + */ +void BLI_where_is_temp(char *fullname, int usertemp); + + + /** + * determines the full path to the application bundle on OS X + * + * @return path to application bundle + */ +#ifdef __APPLE__ +char* BLI_getbundle(void); +#endif + +#ifdef WITH_ICONV +void BLI_string_to_utf8(char *original, char *utf_8, const char *code); +#endif + +#endif + diff --git a/source/blender/blenlib/BLI_winstuff.h b/source/blender/blenlib/BLI_winstuff.h index 3bb63506c95..3e1b73e51be 100644 --- a/source/blender/blenlib/BLI_winstuff.h +++ b/source/blender/blenlib/BLI_winstuff.h @@ -120,6 +120,10 @@ int closedir (DIR *dp); void get_default_root(char *root); int check_file_chars(char *filename); +#ifdef WIN32 +int BLI_getInstallationDir(char *str); +#endif + #ifdef __cplusplus } #endif 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 +#include + + +#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 +#include +#include +#include + +#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 (c1c2) { + 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; ic2) { + 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 (c1c2) { - 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; ic2) { - 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 -- cgit v1.2.3