diff options
author | Miika Hamalainen <blender@miikah.org> | 2011-10-22 20:43:23 +0400 |
---|---|---|
committer | Miika Hamalainen <blender@miikah.org> | 2011-10-22 20:43:23 +0400 |
commit | d4541fd891a246f851f647c3259fc26648fc926d (patch) | |
tree | 5397c3a85424b8c08854fa4f9711db4344a790b7 /source/blender/blenlib | |
parent | 30cba27987362054d16b10e73ddf2601af93be68 (diff) | |
parent | 952560dc028516d60a5318eadbcbd5f2c0668335 (diff) |
Merge with trunk r41197
Diffstat (limited to 'source/blender/blenlib')
23 files changed, 807 insertions, 360 deletions
diff --git a/source/blender/blenlib/BLI_blenlib.h b/source/blender/blenlib/BLI_blenlib.h index ba5d04f3021..356709d5ccd 100644 --- a/source/blender/blenlib/BLI_blenlib.h +++ b/source/blender/blenlib/BLI_blenlib.h @@ -64,8 +64,6 @@ struct ListBase; #include <stdlib.h> -extern char btempdir[]; /* creator.c temp dir used instead of U.tempdir, set with BLI_where_is_temp( btempdir, 1 ); */ - #ifdef __cplusplus extern "C" { #endif @@ -74,9 +72,9 @@ extern "C" { #include "BLI_string.h" -#include "BLI_path_util.h" +#include "BLI_string_utf8.h" -#include "BLI_storage.h" +#include "BLI_path_util.h" #include "BLI_fileops.h" @@ -84,13 +82,6 @@ extern "C" { #include "BLI_noise.h" -/** - * @param strct The structure of interest - * @param member The name of a member field of @a strct - * @retval The offset in bytes of @a member within @a strct - */ -#define BLI_STRUCT_OFFSET(strct, member) ((int)(intptr_t) &((strct*) 0)->member) - #ifdef __cplusplus } #endif diff --git a/source/blender/blenlib/BLI_edgehash.h b/source/blender/blenlib/BLI_edgehash.h index fba13035f02..23ffdbae59b 100644 --- a/source/blender/blenlib/BLI_edgehash.h +++ b/source/blender/blenlib/BLI_edgehash.h @@ -30,7 +30,7 @@ #ifndef BLI_EDGEHASH_H #define BLI_EDGEHASH_H -/** \file BLI_storage.h +/** \file BLI_edgehash.h * \ingroup bli * \author Daniel Dunbar * \brief A general unordered 2-int pair hash table ADT. diff --git a/source/blender/blenlib/BLI_fileops.h b/source/blender/blenlib/BLI_fileops.h index 21d28ca3185..ebe231e2973 100644 --- a/source/blender/blenlib/BLI_fileops.h +++ b/source/blender/blenlib/BLI_fileops.h @@ -29,10 +29,7 @@ /** \file BLI_fileops.h * \ingroup bli - * \author Daniel Dunbar - * \brief 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! + * \brief File and directory operations. * */ #ifndef BLI_FILEOPS_H @@ -42,26 +39,48 @@ extern "C" { #endif -void BLI_recurdir_fileops(const char *dirname); -int BLI_link(const char *file, const char *to); -int BLI_is_writable(const char *filename); +#include "BLI_fileops_types.h" -/** - * @attention Do not confuse with BLI_exist - */ -int BLI_exists(const char *file); -int BLI_copy_fileops(const char *file, const char *to); -int BLI_rename(const char *from, const char *to); -int BLI_gzip(const char *from, const char *to); -char *BLI_ungzip_to_mem(const char *from_file, int *size_r); -int BLI_delete(const char *file, int dir, int recursive); -int BLI_move(const char *file, const char *to); -int BLI_touch(const char *file); - -/* only for the sane unix world: direct calls to system functions :( */ -#ifndef WIN32 -void BLI_setCmdCallBack(int (*f)(char*)); -#endif +/* for size_t (needed on windows) */ +#include <stddef.h> + +/* Common */ + +int BLI_exists(const char *path); +int BLI_copy(const char *path, const char *to); +int BLI_rename(const char *from, const char *to); +int BLI_delete(const char *path, int dir, int recursive); +int BLI_move(const char *path, const char *to); +int BLI_create_symlink(const char *path, const char *to); + +/* Directories */ + +struct direntry; + +int BLI_is_dir(const char *path); +void BLI_dir_create_recursive(const char *dir); +double BLI_dir_free_space(const char *dir); +char *BLI_current_working_dir(char *dir, const int maxlen); + +unsigned int BLI_dir_contents(const char *dir, struct direntry **filelist); + +/* Files */ + +int BLI_file_is_writable(const char *file); +int BLI_file_touch(const char *file); + +int BLI_file_gzip(const char *from, const char *to); +char *BLI_file_ungzip_to_mem(const char *from_file, int *size_r); + +size_t BLI_file_descriptor_size(int file); +size_t BLI_file_size(const char *file); + + /* compare if one was last modified before the other */ +int BLI_file_older(const char *file1, const char *file2); + + /* read ascii file as lines, empty list if reading fails */ +struct LinkNode *BLI_file_read_as_lines(const char *file); +void BLI_file_free_lines(struct LinkNode *lines); #ifdef __cplusplus } diff --git a/source/blender/blenlib/BLI_storage_types.h b/source/blender/blenlib/BLI_fileops_types.h index 07c0ceffeb5..58dcf1a5441 100644 --- a/source/blender/blenlib/BLI_storage_types.h +++ b/source/blender/blenlib/BLI_fileops_types.h @@ -26,10 +26,11 @@ * * ***** END GPL LICENSE BLOCK ***** */ -#ifndef BLI_STORAGE_TYPES_H -#define BLI_STORAGE_TYPES_H -/** \file BLI_storage_types.h +#ifndef BLI_FILEOPS_TYPES_H +#define BLI_FILEOPS_TYPES_H + +/** \file BLI_fileops_types.h * \ingroup bli * \brief Some types for dealing with directories. */ @@ -75,5 +76,5 @@ struct dirlink char *name; }; -#endif /* BLI_STORAGE_TYPES_H */ +#endif /* BLI_FILEOPS_TYPES_H */ diff --git a/source/blender/blenlib/BLI_ghash.h b/source/blender/blenlib/BLI_ghash.h index e4afc6ad79b..e4ef834822f 100644 --- a/source/blender/blenlib/BLI_ghash.h +++ b/source/blender/blenlib/BLI_ghash.h @@ -39,13 +39,6 @@ extern "C" { #endif -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -#include "BLI_mempool.h" -#include "BLI_blenlib.h" - typedef unsigned int (*GHashHashFP) (const void *key); typedef int (*GHashCmpFP) (const void *a, const void *b); typedef void (*GHashKeyFreeFP) (void *key); diff --git a/source/blender/blenlib/BLI_mempool.h b/source/blender/blenlib/BLI_mempool.h index 4ea48929efb..56df71b5582 100644 --- a/source/blender/blenlib/BLI_mempool.h +++ b/source/blender/blenlib/BLI_mempool.h @@ -29,7 +29,7 @@ #ifndef BLI_MEMPOOL_H #define BLI_MEMPOOL_H -/** \file BLI_storage.h +/** \file BLI_mempool.h * \ingroup bli * \author Geoffrey Bantle * \brief Simple fast memory allocator. diff --git a/source/blender/blenlib/BLI_path_util.h b/source/blender/blenlib/BLI_path_util.h index 81fc8a50db6..82794e08fa0 100644 --- a/source/blender/blenlib/BLI_path_util.h +++ b/source/blender/blenlib/BLI_path_util.h @@ -65,10 +65,6 @@ char *BLI_get_folder_version(const int id, const int ver, const int do_check); #define BLENDER_SYSTEM_PLUGINS 54 #define BLENDER_SYSTEM_PYTHON 54 -#define BLENDER_TEMP 80 - -#define BLENDER_USERFOLDER(id) (id >= BLENDER_USER_CONFIG && id <= BLENDER_USER_PLUGINS) - /* for BLI_get_folder_version only */ #define BLENDER_RESOURCE_PATH_USER 0 #define BLENDER_RESOURCE_PATH_LOCAL 1 @@ -79,17 +75,6 @@ char *BLI_get_folder_version(const int id, const int ver, const int do_check); #define BLENDER_HISTORY_FILE "recent-files.txt" #ifdef WIN32 -#define BLENDER_USER_FORMAT "%s\\Blender Foundation\\Blender\\%s" -#define BLENDER_SYSTEM_FORMAT "%s\\Blender Foundation\\Blender\\%s" -#elif defined(__APPLE__) -#define BLENDER_USER_FORMAT "%s/Blender/%s" -#define BLENDER_SYSTEM_FORMAT "%s/Blender/%s" -#else -#define BLENDER_USER_FORMAT "%s/.blender/%s" -#define BLENDER_SYSTEM_FORMAT "%s/blender/%s" -#endif - -#ifdef WIN32 #define SEP '\\' #define ALTSEP '/' #else @@ -103,7 +88,9 @@ void BLI_setenv_if_new(const char *env, const char* val); 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(const char *name); -void BLI_split_dirfile(const char *string, char *dir, char *file); +void BLI_split_dirfile(const char *string, char *dir, char *file, const size_t dirlen, const size_t filelen); +void BLI_split_dir_part(const char *string, char *dir, const size_t dirlen); +void BLI_split_file_part(const char *string, char *file, const size_t filelen); void BLI_join_dirfile(char *string, const size_t maxlen, const char *dir, const char *file); char *BLI_path_basename(char *path); int BKE_rebase_path(char *abs, size_t abs_len, char *rel, size_t rel_len, const char *base_dir, const char *src_dir, const char *dest_dir); @@ -179,29 +166,20 @@ void BLI_path_rel(char *file, const char *relfile); */ 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 size_t maxlen, const char *name); - - /** - * 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, const size_t maxlen, int usertemp); - + /* Initialize path to program executable */ +void BLI_init_program_path(const char *argv0); + /* Initialize path to temporary directory. + * NOTE: On Window userdir will be set to the temporary directory! */ +void BLI_init_temporary_dir(char *userdir); + + /* Path to executable */ +const char *BLI_program_path(void); + /* Path to directory of executable */ +const char *BLI_program_dir(void); + /* Path to temporary directory (with trailing slash) */ +const char *BLI_temporary_dir(void); + /* Path to the system temporary directory (with trailing slash) */ +void BLI_system_temporary_dir(char *dir); #ifdef WITH_ICONV void BLI_string_to_utf8(char *original, char *utf_8, const char *code); diff --git a/source/blender/blenlib/BLI_scanfill.h b/source/blender/blenlib/BLI_scanfill.h index c5acf7b7f70..3cc6ebcc7da 100644 --- a/source/blender/blenlib/BLI_scanfill.h +++ b/source/blender/blenlib/BLI_scanfill.h @@ -30,7 +30,7 @@ #ifndef BLI_SCANFILL_H #define BLI_SCANFILL_H -/** \file BLI_storage.h +/** \file BLI_scanfill.h * \ingroup bli * \since March 2001 * \author nzc diff --git a/source/blender/blenlib/BLI_storage.h b/source/blender/blenlib/BLI_storage.h deleted file mode 100644 index 017f9877baf..00000000000 --- a/source/blender/blenlib/BLI_storage.h +++ /dev/null @@ -1,82 +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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 - -/** \file BLI_storage.h - * \ingroup bli - */ - -#ifdef WIN32 -/* for size_t, only needed on win32 for some reason */ -#include <stddef.h> -#endif - -struct direntry; - - -void BLI_adddirstrings(void); -void BLI_builddir(const char *dirname, const char *relname); -int BLI_compare(struct direntry *entry1, struct direntry *entry2); - -size_t BLI_filesize(int file); -size_t BLI_filepathsize(const char *path); -double BLI_diskfree(const char *dir); -char *BLI_getwdN(char *dir, const int maxncpy); - -unsigned int BLI_getdir(const char *dirname, struct direntry **filelist); -/** - * @attention Do not confuse with BLI_exists - */ -int BLI_exist(const 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. - */ - -int BLI_is_dir(const char *file); - -struct LinkNode *BLI_read_file_as_lines(const char *name); - - /** - * Free the list returned by BLI_read_file_as_lines. - */ -void BLI_free_file_lines(struct LinkNode *lines); - - /* Compare if one was last modified before the other */ -int BLI_file_older(const char *file1, const char *file2); - -#endif /* BLI_STORAGE_H */ - diff --git a/source/blender/blenlib/BLI_string.h b/source/blender/blenlib/BLI_string.h index c53ce9dced5..958f240e3a8 100644 --- a/source/blender/blenlib/BLI_string.h +++ b/source/blender/blenlib/BLI_string.h @@ -25,9 +25,7 @@ * Contributor(s): none yet. * * ***** END GPL LICENSE BLOCK ***** - * - * $Id$ -*/ + */ #ifndef BLI_STRING_H #define BLI_STRING_H @@ -142,12 +140,6 @@ void BLI_timestr(double _time, char *str); /* time var is global */ void BLI_ascii_strtolower(char *str, int len); void BLI_ascii_strtoupper(char *str, int len); - -/* string_utf8.c - may move these into their own header some day - campbell */ -char *BLI_strncpy_utf8(char *dst, const char *src, size_t maxncpy); -int BLI_utf8_invalid_byte(const char *str, int length); -int BLI_utf8_invalid_strip(char *str, int length); - #ifdef __cplusplus } #endif diff --git a/source/blender/blenlib/BLI_string_utf8.h b/source/blender/blenlib/BLI_string_utf8.h new file mode 100644 index 00000000000..765ae93828e --- /dev/null +++ b/source/blender/blenlib/BLI_string_utf8.h @@ -0,0 +1,63 @@ +/* + * $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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * Contributor(s): none yet. + * + * ***** END GPL LICENSE BLOCK ***** + */ + +#ifndef BLI_STRING_UTF8_H +#define BLI_STRING_UTF8_H + +/** \file BLI_string_utf8.h + * \ingroup bli + */ + +#ifdef __cplusplus +extern "C" { +#endif + +char *BLI_strncpy_utf8(char *dst, const char *src, size_t maxncpy); +int BLI_utf8_invalid_byte(const char *str, int length); +int BLI_utf8_invalid_strip(char *str, int length); + + /* copied from glib */ +unsigned int BLI_str_utf8_as_unicode(const char *p); +unsigned int BLI_str_utf8_as_unicode_and_size(const char *p, size_t *index); +unsigned int BLI_str_utf8_as_unicode_step(const char *p, size_t *index); +size_t BLI_str_utf8_from_unicode(unsigned int c, char *outbuf); + +char *BLI_str_find_prev_char_utf8(const char *str, const char *p); +char *BLI_str_find_next_char_utf8(const char *p, const char *end); +char *BLI_str_prev_char_utf8(const char *p); + + /* wchar_t functions, copied from blenders own font.c originally */ +size_t BLI_wstrlen_utf8(const wchar_t *src); +size_t BLI_strlen_utf8(const char *strc); +size_t BLI_strncpy_wchar_as_utf8(char *dst, const wchar_t *src, const size_t maxcpy); +size_t BLI_strncpy_wchar_from_utf8(wchar_t *dst, const char *src, const size_t maxcpy); + +#define BLI_UTF8_MAX 6 +#define BLI_UTF8_ERR ((unsigned int)-1) + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/source/blender/blenlib/BLI_utildefines.h b/source/blender/blenlib/BLI_utildefines.h index 456ee72c4e0..c1eed40de74 100644 --- a/source/blender/blenlib/BLI_utildefines.h +++ b/source/blender/blenlib/BLI_utildefines.h @@ -67,63 +67,113 @@ #define MAX3(x,y,z) MAX2( MAX2((x),(y)) , (z) ) #define MAX4(x,y,z,a) MAX2( MAX2((x),(y)) , MAX2((z),(a)) ) -#define INIT_MINMAX(min, max) { (min)[0]= (min)[1]= (min)[2]= 1.0e30f; (max)[0]= (max)[1]= (max)[2]= -1.0e30f; } - -#define INIT_MINMAX2(min, max) { (min)[0]= (min)[1]= 1.0e30f; (max)[0]= (max)[1]= -1.0e30f; } - -#define DO_MIN(vec, min) { if( (min)[0]>(vec)[0] ) (min)[0]= (vec)[0]; \ - if( (min)[1]>(vec)[1] ) (min)[1]= (vec)[1]; \ - if( (min)[2]>(vec)[2] ) (min)[2]= (vec)[2]; } \ - -#define DO_MAX(vec, max) { if( (max)[0]<(vec)[0] ) (max)[0]= (vec)[0]; \ - if( (max)[1]<(vec)[1] ) (max)[1]= (vec)[1]; \ - if( (max)[2]<(vec)[2] ) (max)[2]= (vec)[2]; } \ - -#define DO_MINMAX(vec, min, max) { if( (min)[0]>(vec)[0] ) (min)[0]= (vec)[0]; \ - if( (min)[1]>(vec)[1] ) (min)[1]= (vec)[1]; \ - if( (min)[2]>(vec)[2] ) (min)[2]= (vec)[2]; \ - if( (max)[0]<(vec)[0] ) (max)[0]= (vec)[0]; \ - if( (max)[1]<(vec)[1] ) (max)[1]= (vec)[1]; \ - if( (max)[2]<(vec)[2] ) (max)[2]= (vec)[2]; } \ - -#define DO_MINMAX2(vec, min, max) { if( (min)[0]>(vec)[0] ) (min)[0]= (vec)[0]; \ - if( (min)[1]>(vec)[1] ) (min)[1]= (vec)[1]; \ - if( (max)[0]<(vec)[0] ) (max)[0]= (vec)[0]; \ - if( (max)[1]<(vec)[1] ) (max)[1]= (vec)[1]; } +#define INIT_MINMAX(min, max) { \ + (min)[0]= (min)[1]= (min)[2]= 1.0e30f; \ + (max)[0]= (max)[1]= (max)[2]= -1.0e30f; \ + } +#define INIT_MINMAX2(min, max) { \ + (min)[0]= (min)[1]= 1.0e30f; \ + (max)[0]= (max)[1]= -1.0e30f; \ + } +#define DO_MIN(vec, min) { \ + if( (min)[0]>(vec)[0] ) (min)[0]= (vec)[0]; \ + if( (min)[1]>(vec)[1] ) (min)[1]= (vec)[1]; \ + if( (min)[2]>(vec)[2] ) (min)[2]= (vec)[2]; \ + } +#define DO_MAX(vec, max) { \ + if( (max)[0]<(vec)[0] ) (max)[0]= (vec)[0]; \ + if( (max)[1]<(vec)[1] ) (max)[1]= (vec)[1]; \ + if( (max)[2]<(vec)[2] ) (max)[2]= (vec)[2]; \ + } +#define DO_MINMAX(vec, min, max) { \ + if( (min)[0]>(vec)[0] ) (min)[0]= (vec)[0]; \ + if( (min)[1]>(vec)[1] ) (min)[1]= (vec)[1]; \ + if( (min)[2]>(vec)[2] ) (min)[2]= (vec)[2]; \ + if( (max)[0]<(vec)[0] ) (max)[0]= (vec)[0]; \ + if( (max)[1]<(vec)[1] ) (max)[1]= (vec)[1]; \ + if( (max)[2]<(vec)[2] ) (max)[2]= (vec)[2]; \ + } +#define DO_MINMAX2(vec, min, max) { \ + if( (min)[0]>(vec)[0] ) (min)[0]= (vec)[0]; \ + if( (min)[1]>(vec)[1] ) (min)[1]= (vec)[1]; \ + if( (max)[0]<(vec)[0] ) (max)[0]= (vec)[0]; \ + if( (max)[1]<(vec)[1] ) (max)[1]= (vec)[1]; \ + } /* some math and copy defines */ #ifndef SWAP -# define SWAP(type, a, b) { type sw_ap; sw_ap=(a); (a)=(b); (b)=sw_ap; } +# define SWAP(type, a, b) { type sw_ap; sw_ap=(a); (a)=(b); (b)=sw_ap; } #endif -#define ABS(a) ( (a)<0 ? (-(a)) : (a) ) - -#define AVG2(x, y) ( 0.5 * ((x) + (y)) ) +#define ABS(a) ( (a)<0 ? (-(a)) : (a) ) #define FTOCHAR(val) ((val)<=0.0f)? 0 : (((val)>(1.0f-0.5f/255.0f))? 255 : (char)((255.0f*(val))+0.5f)) #define FTOUSHORT(val) ((val >= 1.0f-0.5f/65535)? 65535: (val <= 0.0f)? 0: (unsigned short)(val*65535.0f + 0.5f)) -#define F3TOCHAR3(v2,v1) (v1)[0]=FTOCHAR((v2[0])); (v1)[1]=FTOCHAR((v2[1])); (v1)[2]=FTOCHAR((v2[2])) -#define F3TOCHAR4(v2,v1) { (v1)[0]=FTOCHAR((v2[0])); (v1)[1]=FTOCHAR((v2[1])); (v1)[2]=FTOCHAR((v2[2])); \ - (v1)[3] = 255; } -#define F4TOCHAR4(v2,v1) { (v1)[0]=FTOCHAR((v2[0])); (v1)[1]=FTOCHAR((v2[1])); (v1)[2]=FTOCHAR((v2[2])); \ - (v1)[3]=FTOCHAR((v2[3])); } - - -#define VECCOPY(v1,v2) {*(v1)= *(v2); *(v1+1)= *(v2+1); *(v1+2)= *(v2+2);} -#define VECCOPY2D(v1,v2) {*(v1)= *(v2); *(v1+1)= *(v2+1);} -#define QUATCOPY(v1,v2) {*(v1)= *(v2); *(v1+1)= *(v2+1); *(v1+2)= *(v2+2); *(v1+3)= *(v2+3);} -#define LONGCOPY(a, b, c) {int lcpc=c, *lcpa=(int *)a, *lcpb=(int *)b; while(lcpc-->0) *(lcpa++)= *(lcpb++);} - - -#define VECADD(v1,v2,v3) {*(v1)= *(v2) + *(v3); *(v1+1)= *(v2+1) + *(v3+1); *(v1+2)= *(v2+2) + *(v3+2);} -#define VECSUB(v1,v2,v3) {*(v1)= *(v2) - *(v3); *(v1+1)= *(v2+1) - *(v3+1); *(v1+2)= *(v2+2) - *(v3+2);} -#define VECSUB2D(v1,v2,v3) {*(v1)= *(v2) - *(v3); *(v1+1)= *(v2+1) - *(v3+1);} -#define VECADDFAC(v1,v2,v3,fac) {*(v1)= *(v2) + *(v3)*(fac); *(v1+1)= *(v2+1) + *(v3+1)*(fac); *(v1+2)= *(v2+2) + *(v3+2)*(fac);} -#define VECSUBFAC(v1,v2,v3,fac) {*(v1)= *(v2) - *(v3)*(fac); *(v1+1)= *(v2+1) - *(v3+1)*(fac); *(v1+2)= *(v2+2) - *(v3+2)*(fac);} -#define QUATADDFAC(v1,v2,v3,fac) {*(v1)= *(v2) + *(v3)*(fac); *(v1+1)= *(v2+1) + *(v3+1)*(fac); *(v1+2)= *(v2+2) + *(v3+2)*(fac); *(v1+3)= *(v2+3) + *(v3+3)*(fac);} - -#define INPR(v1, v2) ( (v1)[0]*(v2)[0] + (v1)[1]*(v2)[1] + (v1)[2]*(v2)[2] ) +#define F3TOCHAR3(v2, v1) { \ + (v1)[0]= FTOCHAR((v2[0])); \ + (v1)[1]= FTOCHAR((v2[1])); \ + (v1)[2]= FTOCHAR((v2[2])); \ + } +#define F3TOCHAR4(v2, v1) { \ + (v1)[0]= FTOCHAR((v2[0])); \ + (v1)[1]= FTOCHAR((v2[1])); \ + (v1)[2]= FTOCHAR((v2[2])); \ + (v1)[3]= 255; \ + } +#define F4TOCHAR4(v2, v1) { \ + (v1)[0]= FTOCHAR((v2[0])); \ + (v1)[1]= FTOCHAR((v2[1])); \ + (v1)[2]= FTOCHAR((v2[2])); \ + (v1)[3]= FTOCHAR((v2[3])); \ + } +#define VECCOPY(v1, v2) { \ + *(v1)= *(v2); \ + *(v1+1)= *(v2+1); \ + *(v1+2)= *(v2+2); \ + } +#define VECCOPY2D(v1, v2) { \ + *(v1)= *(v2); \ + *(v1+1)= *(v2+1); \ + } +#define QUATCOPY(v1, v2) { \ + *(v1)= *(v2); \ + *(v1+1)= *(v2+1); \ + *(v1+2)= *(v2+2); \ + *(v1+3)= *(v2+3); \ + } +#define VECADD(v1,v2,v3) { \ + *(v1)= *(v2) + *(v3); \ + *(v1+1)= *(v2+1) + *(v3+1); \ + *(v1+2)= *(v2+2) + *(v3+2); \ + } +#define VECSUB(v1,v2,v3) { \ + *(v1)= *(v2) - *(v3); \ + *(v1+1)= *(v2+1) - *(v3+1); \ + *(v1+2)= *(v2+2) - *(v3+2); \ + } +#define VECSUB2D(v1,v2,v3) { \ + *(v1)= *(v2) - *(v3); \ + *(v1+1)= *(v2+1) - *(v3+1); \ + } +#define VECADDFAC(v1,v2,v3,fac) { \ + *(v1)= *(v2) + *(v3)*(fac); \ + *(v1+1)= *(v2+1) + *(v3+1)*(fac); \ + *(v1+2)= *(v2+2) + *(v3+2)*(fac); \ + } +#define VECSUBFAC(v1,v2,v3,fac) { \ + *(v1)= *(v2) - *(v3)*(fac); \ + *(v1+1)= *(v2+1) - *(v3+1)*(fac); \ + *(v1+2)= *(v2+2) - *(v3+2)*(fac); \ + } +#define QUATADDFAC(v1,v2,v3,fac) { \ + *(v1)= *(v2) + *(v3)*(fac); \ + *(v1+1)= *(v2+1) + *(v3+1)*(fac); \ + *(v1+2)= *(v2+2) + *(v3+2)*(fac); \ + *(v1+3)= *(v2+3) + *(v3+3)*(fac); \ + } + +#define INPR(v1, v2) ( (v1)[0]*(v2)[0] + (v1)[1]*(v2)[1] + (v1)[2]*(v2)[2] ) /* some misc stuff.... */ #define CLAMP(a, b, c) if((a)<(b)) (a)=(b); else if((a)>(c)) (a)=(c) @@ -138,28 +188,36 @@ #define IN_RANGE_INCL(a, b, c) ((b < c)? ((b<=a && a<=c)? 1:0) : ((c<=a && a<=b)? 1:0)) /* array helpers */ -#define ARRAY_LAST_ITEM(arr_start, arr_dtype, elem_size, tot) (arr_dtype *)((char*)arr_start + (elem_size*(tot - 1))) -#define ARRAY_HAS_ITEM(item, arr_start, arr_dtype, elem_size, tot) ((item >= arr_start) && (item <= ARRAY_LAST_ITEM(arr_start, arr_dtype, elem_size, tot))) +#define ARRAY_LAST_ITEM(arr_start, arr_dtype, elem_size, tot) \ + (arr_dtype *)((char*)arr_start + (elem_size*(tot - 1))) + +#define ARRAY_HAS_ITEM(item, arr_start, arr_dtype, elem_size, tot) ( \ + (item >= arr_start) && \ + (item <= ARRAY_LAST_ITEM(arr_start, arr_dtype, elem_size, tot)) \ + ) /* This one rotates the bytes in an int64, int (32) and short (16) */ -#define SWITCH_INT64(a) { \ - char s_i, *p_i; \ - p_i= (char *)&(a); \ - s_i=p_i[0]; p_i[0]=p_i[7]; p_i[7]=s_i; \ - s_i=p_i[1]; p_i[1]=p_i[6]; p_i[6]=s_i; \ - s_i=p_i[2]; p_i[2]=p_i[5]; p_i[5]=s_i; \ - s_i=p_i[3]; p_i[3]=p_i[4]; p_i[4]=s_i; } - - #define SWITCH_INT(a) { \ - char s_i, *p_i; \ - p_i= (char *)&(a); \ - s_i=p_i[0]; p_i[0]=p_i[3]; p_i[3]=s_i; \ - s_i=p_i[1]; p_i[1]=p_i[2]; p_i[2]=s_i; } - -#define SWITCH_SHORT(a) { \ - char s_i, *p_i; \ - p_i= (char *)&(a); \ - s_i=p_i[0]; p_i[0]=p_i[1]; p_i[1]=s_i; } +#define SWITCH_INT64(a) { \ + char s_i, *p_i; \ + p_i= (char *)&(a); \ + s_i= p_i[0]; p_i[0]= p_i[7]; p_i[7]= s_i; \ + s_i= p_i[1]; p_i[1]= p_i[6]; p_i[6]= s_i; \ + s_i= p_i[2]; p_i[2]= p_i[5]; p_i[5]= s_i; \ + s_i= p_i[3]; p_i[3]= p_i[4]; p_i[4]= s_i; \ + } + +#define SWITCH_INT(a) { \ + char s_i, *p_i; \ + p_i= (char *)&(a); \ + s_i= p_i[0]; p_i[0]= p_i[3]; p_i[3]= s_i; \ + s_i= p_i[1]; p_i[1]= p_i[2]; p_i[2]= s_i; \ + } + +#define SWITCH_SHORT(a) { \ + char s_i, *p_i; \ + p_i= (char *)&(a); \ + s_i=p_i[0]; p_i[0]=p_i[1]; p_i[1]=s_i; \ + } /* Warning-free macros for storing ints in pointers. Use these _only_ @@ -221,25 +279,25 @@ # else # define _dummy_abort() (void)0 # endif -# ifdef __GNUC__ /* just want to check if __func__ is available */ -# define BLI_assert(a) \ -do { \ - if (!(a)) { \ - fprintf(stderr, \ - "BLI_assert failed: %s, %s(), %d at \'%s\'\n", \ - __FILE__, __func__, __LINE__, STRINGIFY(a)); \ - _dummy_abort(); \ - } \ +# if defined(__GNUC__) || defined(_MSC_VER) /* check __func__ is available */ +# define BLI_assert(a) \ +do { \ + if (!(a)) { \ + fprintf(stderr, \ + "BLI_assert failed: %s, %s(), %d at \'%s\'\n", \ + __FILE__, __func__, __LINE__, STRINGIFY(a)); \ + _dummy_abort(); \ + } \ } while (0) # else # define BLI_assert(a) \ -do { \ - if (0 == (a)) { \ - fprintf(stderr, \ - "BLI_assert failed: %s, %d at \'%s\'\n", \ - __FILE__, __LINE__, STRINGIFY(a)); \ - _dummy_abort(); \ - } \ +do { \ + if (0 == (a)) { \ + fprintf(stderr, \ + "BLI_assert failed: %s, %d at \'%s\'\n", \ + __FILE__, __LINE__, STRINGIFY(a)); \ + _dummy_abort(); \ + } \ } while (0) # endif #else diff --git a/source/blender/blenlib/CMakeLists.txt b/source/blender/blenlib/CMakeLists.txt index aa822731474..49d849bdf14 100644 --- a/source/blender/blenlib/CMakeLists.txt +++ b/source/blender/blenlib/CMakeLists.txt @@ -99,6 +99,7 @@ set(SRC BLI_edgehash.h BLI_editVert.h BLI_fileops.h + BLI_fileops_types.h BLI_fnmatch.h BLI_ghash.h BLI_graph.h @@ -125,9 +126,8 @@ set(SRC BLI_rand.h BLI_rect.h BLI_scanfill.h - BLI_storage.h - BLI_storage_types.h BLI_string.h + BLI_string_utf8.h BLI_threads.h BLI_utildefines.h BLI_uvproject.h diff --git a/source/blender/blenlib/PIL_time.h b/source/blender/blenlib/PIL_time.h index 82869035d50..36ea43bef2d 100644 --- a/source/blender/blenlib/PIL_time.h +++ b/source/blender/blenlib/PIL_time.h @@ -1,7 +1,5 @@ -/* - * @file PIL_time.h - * - * Platform independant time functions. +/* + * Platform independent time functions. * $Id$ * * ***** BEGIN GPL LICENSE BLOCK ***** @@ -51,7 +49,7 @@ extern double PIL_check_seconds_timer (void); /** - * Platform-independant sleep function. + * Platform-independent sleep function. * @param ms Number of milliseconds to sleep */ void PIL_sleep_ms (int ms); diff --git a/source/blender/blenlib/intern/BLI_args.c b/source/blender/blenlib/intern/BLI_args.c index cf3605a80ff..00903aa42cd 100644 --- a/source/blender/blenlib/intern/BLI_args.c +++ b/source/blender/blenlib/intern/BLI_args.c @@ -35,12 +35,13 @@ #include <ctype.h> /* for tolower */ +#include <string.h> #include "MEM_guardedalloc.h" +#include "BLI_utildefines.h" #include "BLI_listbase.h" #include "BLI_string.h" -#include "BLI_utildefines.h" #include "BLI_args.h" #include "BLI_ghash.h" diff --git a/source/blender/blenlib/intern/BLI_ghash.c b/source/blender/blenlib/intern/BLI_ghash.c index 03e3b7ab560..e34abaf15f9 100644 --- a/source/blender/blenlib/intern/BLI_ghash.c +++ b/source/blender/blenlib/intern/BLI_ghash.c @@ -32,11 +32,20 @@ * \ingroup bli */ +#include <string.h> +#include <stdlib.h> + #include "MEM_guardedalloc.h" + + +// #include "BLI_blenlib.h" + +#include "BLI_mempool.h" #include "BLI_utildefines.h" #include "BLI_ghash.h" + #include "BLO_sys_types.h" // for intptr_t support /***/ diff --git a/source/blender/blenlib/intern/bpath.c b/source/blender/blenlib/intern/bpath.c index 485b8137a02..293f824c5fd 100644 --- a/source/blender/blenlib/intern/bpath.c +++ b/source/blender/blenlib/intern/bpath.c @@ -400,7 +400,7 @@ static void seq_setpath(struct BPathIterator *bpi, const char *path) if (SEQ_HAS_PATH(seq)) { if (ELEM3(seq->type, SEQ_IMAGE, SEQ_MOVIE, SEQ_SOUND)) { - BLI_split_dirfile(path, seq->strip->dir, seq->strip->stripdata->name); + BLI_split_dirfile(path, seq->strip->dir, seq->strip->stripdata->name, sizeof(seq->strip->dir), sizeof(seq->strip->stripdata->name)); } else { /* simple case */ @@ -903,7 +903,7 @@ void findMissingFiles(Main *bmain, const char *str) //XXX waitcursor( 1 ); - BLI_split_dirfile(str, dirname, NULL); + BLI_split_dir_part(str, dirname, sizeof(dirname)); BLI_bpathIterator_init(&bpi, bmain, bmain->name, 0); diff --git a/source/blender/blenlib/intern/fileops.c b/source/blender/blenlib/intern/fileops.c index 9ccd7fbe121..fef102d8267 100644 --- a/source/blender/blenlib/intern/fileops.c +++ b/source/blender/blenlib/intern/fileops.c @@ -65,7 +65,8 @@ return -1 if zlib fails, -2 if the originating file does not exist note: will remove the "from" file */ -int BLI_gzip(const char *from, const char *to) { +int BLI_file_gzip(const char *from, const char *to) +{ char buffer[10240]; int file; int readsize = 0; @@ -109,7 +110,7 @@ int BLI_gzip(const char *from, const char *to) { /* gzip the file in from_file and write it to memery to_mem, at most size bytes. return the unziped size */ -char *BLI_ungzip_to_mem(const char *from_file, int *size_r) +char *BLI_file_ungzip_to_mem(const char *from_file, int *size_r) { gzFile gzfile; int readsize, size, alloc_size=0; @@ -150,7 +151,7 @@ char *BLI_ungzip_to_mem(const char *from_file, int *size_r) /* return 1 when file can be written */ -int BLI_is_writable(const char *filename) +int BLI_file_is_writable(const char *filename) { int file; @@ -178,7 +179,7 @@ int BLI_is_writable(const char *filename) } } -int BLI_touch(const char *file) +int BLI_file_touch(const char *file) { FILE *f = fopen(file,"r+b"); if (f != NULL) { @@ -195,15 +196,12 @@ int BLI_touch(const char *file) return 0; } -int BLI_exists(const char *file) { - return BLI_exist(file); -} - #ifdef WIN32 static char str[MAXPATHLEN+12]; -int BLI_delete(const char *file, int dir, int recursive) { +int BLI_delete(const char *file, int dir, int recursive) +{ int err; if (recursive) { @@ -220,14 +218,15 @@ int BLI_delete(const char *file, int dir, int recursive) { return err; } -int BLI_move(const char *file, const char *to) { +int BLI_move(const char *file, const char *to) +{ int err; // windows doesn't support moveing to a directory // it has to be 'mv filename filename' and not // 'mv filename destdir' - strcpy(str, to); + BLI_strncpy(str, to, sizeof(str)); // points 'to' to a directory ? if (BLI_last_slash(str) == (str + strlen(str) - 1)) { if (BLI_last_slash(file) != NULL) { @@ -245,14 +244,15 @@ int BLI_move(const char *file, const char *to) { } -int BLI_copy_fileops(const char *file, const char *to) { +int BLI_copy(const char *file, const char *to) +{ int err; // windows doesn't support copying to a directory // it has to be 'cp filename filename' and not // 'cp filename destdir' - strcpy(str, to); + BLI_strncpy(str, to, sizeof(str)); // points 'to' to a directory ? if (BLI_last_slash(str) == (str + strlen(str) - 1)) { if (BLI_last_slash(file) != NULL) { @@ -270,14 +270,16 @@ int BLI_copy_fileops(const char *file, const char *to) { return err; } -int BLI_link(const char *file, const char *to) { +int BLI_create_symlink(const char *file, const char *to) +{ callLocalErrorCallBack("Linking files is unsupported on Windows"); (void)file; (void)to; return 1; } -void BLI_recurdir_fileops(const char *dirname) { +void BLI_dir_create_recursive(const char *dirname) +{ char *lslash; char tmp[MAXPATHLEN]; @@ -286,7 +288,7 @@ void BLI_recurdir_fileops(const char *dirname) { // blah1/blah2/ (with slash) after creating // blah1/blah2 (without slash) - strcpy(tmp, dirname); + BLI_strncpy(tmp, dirname, sizeof(tmp)); lslash= BLI_last_slash(tmp); if (lslash == tmp + strlen(tmp) - 1) { @@ -299,7 +301,7 @@ void BLI_recurdir_fileops(const char *dirname) { if (lslash) { /* Split about the last slash and recurse */ *lslash = 0; - BLI_recurdir_fileops(tmp); + BLI_dir_create_recursive(tmp); } if(dirname[0]) /* patch, this recursive loop tries to create a nameless directory */ @@ -307,7 +309,8 @@ void BLI_recurdir_fileops(const char *dirname) { callLocalErrorCallBack("Unable to create directory\n"); } -int BLI_rename(const char *from, const char *to) { +int BLI_rename(const char *from, const char *to) +{ if (!BLI_exists(from)) return 0; /* make sure the filenames are different (case insensitive) before removing */ @@ -347,43 +350,48 @@ int BLI_delete(const char *file, int dir, int recursive) return -1; } -int BLI_move(const char *file, const char *to) { +int BLI_move(const char *file, const char *to) +{ BLI_snprintf(str, sizeof(str), "/bin/mv -f \"%s\" \"%s\"", file, to); return system(str); } -int BLI_copy_fileops(const char *file, const char *to) { +int BLI_copy(const char *file, const char *to) +{ BLI_snprintf(str, sizeof(str), "/bin/cp -rf \"%s\" \"%s\"", file, to); return system(str); } -int BLI_link(const char *file, const char *to) { +int BLI_create_symlink(const char *file, const char *to) +{ BLI_snprintf(str, sizeof(str), "/bin/ln -f \"%s\" \"%s\"", file, to); return system(str); } -void BLI_recurdir_fileops(const char *dirname) { +void BLI_dir_create_recursive(const char *dirname) +{ char *lslash; char tmp[MAXPATHLEN]; if (BLI_exists(dirname)) return; - strcpy(tmp, dirname); + BLI_strncpy(tmp, dirname, sizeof(tmp)); lslash= BLI_last_slash(tmp); if (lslash) { /* Split about the last slash and recurse */ *lslash = 0; - BLI_recurdir_fileops(tmp); + BLI_dir_create_recursive(tmp); } mkdir(dirname, 0777); } -int BLI_rename(const char *from, const char *to) { +int BLI_rename(const char *from, const char *to) +{ if (!BLI_exists(from)) return 0; if (BLI_exists(to)) if(BLI_delete(to, 0, 0)) return 1; @@ -392,3 +400,4 @@ int BLI_rename(const char *from, const char *to) { } #endif + diff --git a/source/blender/blenlib/intern/freetypefont.c b/source/blender/blenlib/intern/freetypefont.c index 0fe028eb3c0..7e5d03423e5 100644 --- a/source/blender/blenlib/intern/freetypefont.c +++ b/source/blender/blenlib/intern/freetypefont.c @@ -364,7 +364,7 @@ static VFontData *objfnt_to_ftvfontdata(PackedFile * pf) // get the name fontname = FT_Get_Postscript_Name(face); - strcpy(vfd->name, (fontname == NULL) ? "" : fontname); + BLI_strncpy(vfd->name, (fontname == NULL) ? "" : fontname, sizeof(vfd->name)); // Extract the first 256 character from TTF lcode= charcode= FT_Get_First_Char(face, &glyph_index); diff --git a/source/blender/blenlib/intern/path_util.c b/source/blender/blenlib/intern/path_util.c index ab7d082c432..d9176c5f162 100644 --- a/source/blender/blenlib/intern/path_util.c +++ b/source/blender/blenlib/intern/path_util.c @@ -41,13 +41,11 @@ #include "MEM_guardedalloc.h" -#include "DNA_userdef_types.h" +#include "DNA_listBase.h" #include "BLI_fileops.h" #include "BLI_path_util.h" #include "BLI_string.h" -#include "BLI_storage.h" -#include "BLI_storage_types.h" #include "BLI_utildefines.h" #include "BKE_utildefines.h" @@ -84,10 +82,24 @@ #endif /* WIN32 */ +/* standard paths */ +#ifdef WIN32 +#define BLENDER_USER_FORMAT "%s\\Blender Foundation\\Blender\\%s" +#define BLENDER_SYSTEM_FORMAT "%s\\Blender Foundation\\Blender\\%s" +#elif defined(__APPLE__) +#define BLENDER_USER_FORMAT "%s/Blender/%s" +#define BLENDER_SYSTEM_FORMAT "%s/Blender/%s" +#else +#define BLENDER_USER_FORMAT "%s/.blender/%s" +#define BLENDER_SYSTEM_FORMAT "%s/blender/%s" +#endif + /* local */ #define UNIQUE_NAME_MAX 128 -extern char bprogname[]; +static char bprogname[FILE_MAX]; /* path to program executable */ +static char bprogdir[FILE_MAX]; /* path in which executable is located */ +static char btempdir[FILE_MAX]; /* temporary directory */ static int add_win32_extension(char *name); static char *blender_version_decimal(const int ver); @@ -726,7 +738,7 @@ int BLI_path_cwd(char *path) if (wasrelative==1) { char cwd[FILE_MAXDIR + FILE_MAXFILE]= ""; - BLI_getwdN(cwd, sizeof(cwd)); /* incase the full path to the blend isnt used */ + BLI_current_working_dir(cwd, sizeof(cwd)); /* incase the full path to the blend isnt used */ if (cwd[0] == '\0') { printf( "Could not get the current working directory - $PWD for an unknown reason."); @@ -875,7 +887,6 @@ static int test_env_path(char *path, const char *envvar) static int get_path_local(char *targetpath, const char *folder_name, const char *subfolder_name, const int ver) { - char bprogdir[FILE_MAX]; char relfolder[FILE_MAX]; #ifdef PATH_DEBUG2 @@ -892,10 +903,7 @@ static int get_path_local(char *targetpath, const char *folder_name, const char else { relfolder[0]= '\0'; } - - /* use argv[0] (bprogname) to get the path to the executable */ - BLI_split_dirfile(bprogname, bprogdir, NULL); - + /* try EXECUTABLE_DIR/2.5x/folder_name - new default directory for local blender installed files */ if(test_path(targetpath, bprogdir, blender_version_decimal(ver), relfolder)) return 1; @@ -963,10 +971,6 @@ static int get_path_system(char *targetpath, const char *folder_name, const char * these are only used when running blender from source */ char cwd[FILE_MAX]; char relfolder[FILE_MAX]; - char bprogdir[FILE_MAX]; - - /* use argv[0] (bprogname) to get the path to the executable */ - BLI_split_dirfile(bprogname, bprogdir, NULL); if(folder_name) { if (subfolder_name) { @@ -980,7 +984,7 @@ static int get_path_system(char *targetpath, const char *folder_name, const char } /* try CWD/release/folder_name */ - if(BLI_getwdN(cwd, sizeof(cwd))) { + if(BLI_current_working_dir(cwd, sizeof(cwd))) { if(test_path(targetpath, cwd, "release", relfolder)) { return 1; } @@ -1111,7 +1115,7 @@ char *BLI_get_folder_create(int folder_id, const char *subfolder) if (!path) { path = BLI_get_user_folder_notest(folder_id, subfolder); - if (path) BLI_recurdir_fileops(path); + if (path) BLI_dir_create_recursive(path); } return path; @@ -1244,7 +1248,7 @@ void BLI_make_existing_file(const char *name) /* test exist */ if (BLI_exists(di) == 0) { - BLI_recurdir_fileops(di); + BLI_dir_create_recursive(di); } } @@ -1411,24 +1415,35 @@ int BLI_replace_extension(char *path, size_t maxlen, const char *ext) * - dosnt use CWD, or deal with relative paths. * - Only fill's in *dir and *file when they are non NULL * */ -void BLI_split_dirfile(const char *string, char *dir, char *file) +void BLI_split_dirfile(const char *string, char *dir, char *file, const size_t dirlen, const size_t filelen) { char *lslash_str = BLI_last_slash(string); - int lslash= lslash_str ? (int)(lslash_str - string) + 1 : 0; + size_t lslash= lslash_str ? (size_t)(lslash_str - string) + 1 : 0; if (dir) { if (lslash) { - BLI_strncpy( dir, string, lslash + 1); /* +1 to include the slash and the last char */ - } else { + BLI_strncpy( dir, string, MIN2(dirlen, lslash + 1)); /* +1 to include the slash and the last char */ + } + else { dir[0] = '\0'; } } if (file) { - strcpy( file, string+lslash); + BLI_strncpy(file, string+lslash, filelen); } } +void BLI_split_dir_part(const char *string, char *dir, const size_t dirlen) +{ + BLI_split_dirfile(string, dir, NULL, dirlen, 0); +} + +void BLI_split_file_part(const char *string, char *file, const size_t filelen) +{ + BLI_split_dirfile(string, NULL, file, 0, filelen); +} + /* simple appending of filename to dir, does not check for valid path! */ void BLI_join_dirfile(char *dst, const size_t maxlen, const char *dir, const char *file) { @@ -1515,7 +1530,7 @@ int BKE_rebase_path(char *abs, size_t abs_len, char *rel, size_t rel_len, const if (rel) rel[0]= 0; - BLI_split_dirfile(base_dir, blend_dir, NULL); + BLI_split_dir_part(base_dir, blend_dir, sizeof(blend_dir)); if (src_dir[0]=='\0') return 0; @@ -1526,7 +1541,7 @@ int BKE_rebase_path(char *abs, size_t abs_len, char *rel, size_t rel_len, const BLI_path_abs(path, base_dir); /* get the directory part */ - BLI_split_dirfile(path, dir, base); + BLI_split_dirfile(path, dir, base, sizeof(dir), sizeof(base)); len= strlen(blend_dir); @@ -1630,7 +1645,7 @@ static int add_win32_extension(char *name) int retval = 0; int type; - type = BLI_exist(name); + type = BLI_exists(name); if ((type == 0) || S_ISDIR(type)) { #ifdef _WIN32 char filename[FILE_MAXDIR+FILE_MAXFILE]; @@ -1650,7 +1665,7 @@ static int add_win32_extension(char *name) strcat(filename, extensions); } - type = BLI_exist(filename); + type = BLI_exists(filename); if (type && (! S_ISDIR(type))) { retval = 1; strcpy(name, filename); @@ -1666,8 +1681,19 @@ static int add_win32_extension(char *name) return (retval); } -/* filename must be FILE_MAX length minimum */ -void BLI_where_am_i(char *fullname, const size_t maxlen, const char *name) +/* +* 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 +* (must be FILE_MAX minimum) +* @param name The name of the executable (usually argv[0]) to be checked +*/ +static void bli_where_am_i(char *fullname, const size_t maxlen, const char *name) { char filename[FILE_MAXDIR+FILE_MAXFILE]; const char *path = NULL, *temp; @@ -1680,7 +1706,7 @@ void BLI_where_am_i(char *fullname, const size_t maxlen, const char *name) #ifdef WITH_BINRELOC - /* linux uses binreloc since argv[0] is not relyable, call br_init( NULL ) first */ + /* linux uses binreloc since argv[0] is not reliable, call br_init( NULL ) first */ path = br_find_exe( NULL ); if (path) { BLI_strncpy(fullname, path, maxlen); @@ -1704,7 +1730,7 @@ void BLI_where_am_i(char *fullname, const size_t maxlen, const char *name) BLI_strncpy(fullname, name, maxlen); if (name[0] == '.') { char wdir[FILE_MAX]= ""; - BLI_getwdN(wdir, sizeof(wdir)); /* backup cwd to restore after */ + BLI_current_working_dir(wdir, sizeof(wdir)); /* backup cwd to restore after */ // not needed but avoids annoying /./ in name if(name[1]==SEP) @@ -1747,12 +1773,37 @@ void BLI_where_am_i(char *fullname, const size_t maxlen, const char *name) } } -void BLI_where_is_temp(char *fullname, const size_t maxlen, int usertemp) +void BLI_init_program_path(const char *argv0) +{ + bli_where_am_i(bprogname, sizeof(bprogname), argv0); + BLI_split_dir_part(bprogname, bprogdir, sizeof(bprogdir)); +} + +const char *BLI_program_path(void) +{ + return bprogname; +} + +const char *BLI_program_dir(void) +{ + return bprogdir; +} + +/** +* 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 +* @param userdir Directory specified in user preferences +*/ +void BLI_where_is_temp(char *fullname, const size_t maxlen, char *userdir) { fullname[0] = '\0'; - if (usertemp && BLI_is_dir(U.tempdir)) { - BLI_strncpy(fullname, U.tempdir, maxlen); + if (userdir && BLI_is_dir(userdir)) { + BLI_strncpy(fullname, userdir, maxlen); } @@ -1786,13 +1837,28 @@ void BLI_where_is_temp(char *fullname, const size_t maxlen, int usertemp) /* add a trailing slash if needed */ BLI_add_slash(fullname); #ifdef WIN32 - if(U.tempdir != fullname) { - BLI_strncpy(U.tempdir, fullname, maxlen); /* also set user pref to show %TEMP%. /tmp/ is just plain confusing for Windows users. */ + if(userdir != fullname) { + BLI_strncpy(userdir, fullname, maxlen); /* also set user pref to show %TEMP%. /tmp/ is just plain confusing for Windows users. */ } #endif } } +void BLI_init_temporary_dir(char *userdir) +{ + BLI_where_is_temp(btempdir, FILE_MAX, userdir); +} + +const char *BLI_temporary_dir(void) +{ + return btempdir; +} + +void BLI_system_temporary_dir(char *dir) +{ + BLI_where_is_temp(dir, FILE_MAX, NULL); +} + #ifdef WITH_ICONV void BLI_string_to_utf8(char *original, char *utf_8, const char *code) diff --git a/source/blender/blenlib/intern/storage.c b/source/blender/blenlib/intern/storage.c index e336b914ffa..b3caeb71822 100644 --- a/source/blender/blenlib/intern/storage.c +++ b/source/blender/blenlib/intern/storage.c @@ -88,10 +88,9 @@ #include "DNA_listBase.h" +#include "BLI_fileops.h" #include "BLI_listbase.h" #include "BLI_linklist.h" -#include "BLI_storage.h" -#include "BLI_storage_types.h" #include "BLI_string.h" #include "BKE_utildefines.h" @@ -104,7 +103,7 @@ static struct ListBase dirbase_={NULL, NULL}; static struct ListBase *dirbase = &dirbase_; /* can return NULL when the size is not big enough */ -char *BLI_getwdN(char *dir, const int maxncpy) +char *BLI_current_working_dir(char *dir, const int maxncpy) { const char *pwd= getenv("PWD"); if (pwd){ @@ -116,7 +115,7 @@ char *BLI_getwdN(char *dir, const int maxncpy) } -int BLI_compare(struct direntry *entry1, struct direntry *entry2) +static int bli_compare(struct direntry *entry1, struct direntry *entry2) { /* type is equal to stat.st_mode */ @@ -143,7 +142,7 @@ int BLI_compare(struct direntry *entry1, struct direntry *entry2) } -double BLI_diskfree(const char *dir) +double BLI_dir_free_space(const char *dir) { #ifdef WIN32 DWORD sectorspc, bytesps, freec, clusters; @@ -198,7 +197,7 @@ double BLI_diskfree(const char *dir) #endif } -void BLI_builddir(const char *dirname, const char *relname) +static void bli_builddir(const char *dirname, const char *relname) { struct dirent *fname; struct dirlink *dlink; @@ -273,7 +272,7 @@ void BLI_builddir(const char *dirname, const char *relname) } BLI_freelist(dirbase); - if (files) qsort(files, actnum, sizeof(struct direntry), (int (*)(const void *,const void*))BLI_compare); + if (files) qsort(files, actnum, sizeof(struct direntry), (int (*)(const void *,const void*))bli_compare); } else { printf("%s empty directory\n",dirname); } @@ -284,7 +283,7 @@ void BLI_builddir(const char *dirname, const char *relname) } } -void BLI_adddirstrings(void) +static void bli_adddirstrings(void) { char datum[100]; char buf[512]; @@ -304,15 +303,15 @@ void BLI_adddirstrings(void) for(num=0, file= files; num<actnum; num++, file++){ #ifdef WIN32 mode = 0; - strcpy(file->mode1, types[0]); - strcpy(file->mode2, types[0]); - strcpy(file->mode3, types[0]); + BLI_strncpy(file->mode1, types[0], sizeof(file->mode1)); + BLI_strncpy(file->mode2, types[0], sizeof(file->mode2)); + BLI_strncpy(file->mode3, types[0], sizeof(file->mode3)); #else mode = file->s.st_mode; - strcpy(file->mode1, types[(mode & 0700) >> 6]); - strcpy(file->mode2, types[(mode & 0070) >> 3]); - strcpy(file->mode3, types[(mode & 0007)]); + BLI_strncpy(file->mode1, types[(mode & 0700) >> 6], sizeof(file->mode1)); + BLI_strncpy(file->mode2, types[(mode & 0070) >> 3], sizeof(file->mode2)); + BLI_strncpy(file->mode3, types[(mode & 0007)], sizeof(file->mode3)); if (((mode & S_ISGID) == S_ISGID) && (file->mode2[2]=='-'))file->mode2[2]='l'; @@ -392,7 +391,7 @@ void BLI_adddirstrings(void) } } -unsigned int BLI_getdir(const char *dirname, struct direntry **filelist) +unsigned int BLI_dir_contents(const char *dirname, struct direntry **filelist) { // reset global variables // memory stored in files is free()'d in @@ -401,8 +400,8 @@ unsigned int BLI_getdir(const char *dirname, struct direntry **filelist) actnum = totnum = 0; files = NULL; - BLI_builddir(dirname,""); - BLI_adddirstrings(); + bli_builddir(dirname,""); + bli_adddirstrings(); if (files) { *(filelist) = files; @@ -416,7 +415,7 @@ unsigned int BLI_getdir(const char *dirname, struct direntry **filelist) } -size_t BLI_filesize(int file) +size_t BLI_file_descriptor_size(int file) { struct stat buf; @@ -425,20 +424,20 @@ size_t BLI_filesize(int file) return (buf.st_size); } -size_t BLI_filepathsize(const char *path) +size_t BLI_file_size(const char *path) { int size, file = open(path, O_BINARY|O_RDONLY); if (file == -1) return -1; - size = BLI_filesize(file); + size = BLI_file_descriptor_size(file); close(file); return size; } -int BLI_exist(const char *name) +int BLI_exists(const char *name) { #if defined(WIN32) && !defined(__MINGW32__) struct _stat64i32 st; @@ -471,10 +470,10 @@ int BLI_exist(const char *name) /* would be better in fileops.c except that it needs stat.h so add here */ int BLI_is_dir(const char *file) { - return S_ISDIR(BLI_exist(file)); + return S_ISDIR(BLI_exists(file)); } -LinkNode *BLI_read_file_as_lines(const char *name) +LinkNode *BLI_file_read_as_lines(const char *name) { FILE *fp= fopen(name, "r"); LinkNode *lines= NULL; @@ -515,7 +514,7 @@ LinkNode *BLI_read_file_as_lines(const char *name) return lines; } -void BLI_free_file_lines(LinkNode *lines) +void BLI_file_free_lines(LinkNode *lines) { BLI_linklist_free(lines, (void(*)(void*)) MEM_freeN); } diff --git a/source/blender/blenlib/intern/string_utf8.c b/source/blender/blenlib/intern/string_utf8.c index 961a41690f7..b1ad04eb70d 100644 --- a/source/blender/blenlib/intern/string_utf8.c +++ b/source/blender/blenlib/intern/string_utf8.c @@ -18,6 +18,8 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * The Original Code is Copyright (C) 2011 Blender Foundation. + * Code from gutf8.c Copyright (C) 1999 Tom Tromey + * Copyright (C) 2000 Red Hat, Inc. * All rights reserved. * * Contributor(s): Campbell Barton. @@ -31,8 +33,10 @@ */ #include <string.h> +#include <wchar.h> +#include <wctype.h> -#include "BLI_string.h" +#include "BLI_string_utf8.h" /* from libswish3, originally called u8_isvalid(), * modified to return the index of the bad character (byte index not utf). @@ -183,3 +187,352 @@ char *BLI_strncpy_utf8(char *dst, const char *src, size_t maxncpy) return dst_r; } + +/* --------------------------------------------------------------------------*/ +/* wchar_t / utf8 functions */ + +size_t BLI_strncpy_wchar_as_utf8(char *dst, const wchar_t *src, const size_t maxcpy) +{ + size_t len = 0; + while(*src && len < maxcpy) { /* XXX can still run over the buffer because utf8 size isnt known :| */ + len += BLI_str_utf8_from_unicode(*src++, dst+len); + } + + dst[len]= '\0'; + + return len; +} + +/* wchar len in utf8 */ +size_t BLI_wstrlen_utf8(const wchar_t *src) +{ + size_t len = 0; + + while(*src) { + len += BLI_str_utf8_from_unicode(*src++, NULL); + } + + return len; +} + +// utf8slen +size_t BLI_strlen_utf8(const char *strc) +{ + int len=0; + + while(*strc) { + if ((*strc & 0xe0) == 0xc0) { + if((strc[1] & 0x80) && (strc[1] & 0x40) == 0x00) + strc++; + } else if ((*strc & 0xf0) == 0xe0) { + if((strc[1] & strc[2] & 0x80) && ((strc[1] | strc[2]) & 0x40) == 0x00) + strc += 2; + } else if ((*strc & 0xf8) == 0xf0) { + if((strc[1] & strc[2] & strc[3] & 0x80) && ((strc[1] | strc[2] | strc[3]) & 0x40) == 0x00) + strc += 3; + } + + strc++; + len++; + } + + return len; +} + +size_t BLI_strncpy_wchar_from_utf8(wchar_t *dst_w, const char *src_c, const size_t maxcpy) +{ + int len=0; + + if(dst_w==NULL || src_c==NULL) return(0); + + while(*src_c && len < maxcpy) { + size_t step= 0; + unsigned int unicode= BLI_str_utf8_as_unicode_and_size(src_c, &step); + if (unicode != BLI_UTF8_ERR) { + *dst_w= (wchar_t)unicode; + src_c += step; + } + else { + *dst_w = '?'; + src_c= BLI_str_find_next_char_utf8(src_c, NULL); + } + dst_w++; + len++; + } + return len; +} + +/* end wchar_t / utf8 functions */ +/* --------------------------------------------------------------------------*/ + +/* copied from glib's gutf8.c */ + +/* note, glib uses unsigned int for unicode, best we do the same, + * though we dont typedef it - campbell */ + +#define UTF8_COMPUTE(Char, Mask, Len) \ + if (Char < 128) { \ + Len = 1; \ + Mask = 0x7f; \ + } \ + else if ((Char & 0xe0) == 0xc0) { \ + Len = 2; \ + Mask = 0x1f; \ + } \ + else if ((Char & 0xf0) == 0xe0) { \ + Len = 3; \ + Mask = 0x0f; \ + } \ + else if ((Char & 0xf8) == 0xf0) { \ + Len = 4; \ + Mask = 0x07; \ + } \ + else if ((Char & 0xfc) == 0xf8) { \ + Len = 5; \ + Mask = 0x03; \ + } \ + else if ((Char & 0xfe) == 0xfc) { \ + Len = 6; \ + Mask = 0x01; \ + } \ + else { \ + Len = -1; \ + } + +/* same as glib define but added an 'Err' arg */ +#define UTF8_GET(Result, Chars, Count, Mask, Len, Err) \ + (Result) = (Chars)[0] & (Mask); \ + for ((Count) = 1; (Count) < (Len); ++(Count)) { \ + if (((Chars)[(Count)] & 0xc0) != 0x80) { \ + (Result) = Err; \ + break; \ + } \ + (Result) <<= 6; \ + (Result) |= ((Chars)[(Count)] & 0x3f); \ + } + + +/* was g_utf8_get_char */ +/** + * BLI_str_utf8_as_unicode: + * @p: a pointer to Unicode character encoded as UTF-8 + * + * Converts a sequence of bytes encoded as UTF-8 to a Unicode character. + * If @p does not point to a valid UTF-8 encoded character, results are + * undefined. If you are not sure that the bytes are complete + * valid Unicode characters, you should use g_utf8_get_char_validated() + * instead. + * + * Return value: the resulting character + **/ +unsigned int BLI_str_utf8_as_unicode(const char *p) +{ + int i, mask = 0, len; + unsigned int result; + unsigned char c = (unsigned char) *p; + + UTF8_COMPUTE (c, mask, len); + if (len == -1) + return BLI_UTF8_ERR; + UTF8_GET (result, p, i, mask, len, BLI_UTF8_ERR); + + return result; +} + +/* varient that increments the length */ +unsigned int BLI_str_utf8_as_unicode_and_size(const char *p, size_t *index) +{ + int i, mask = 0, len; + unsigned int result; + unsigned char c = (unsigned char) *p; + + UTF8_COMPUTE (c, mask, len); + if (len == -1) + return BLI_UTF8_ERR; + UTF8_GET (result, p, i, mask, len, BLI_UTF8_ERR); + *index += len; + return result; +} + +/* another varient that steps over the index, + * note, currently this also falls back to latin1 for text drawing. */ +unsigned int BLI_str_utf8_as_unicode_step(const char *p, size_t *index) +{ + int i, mask = 0, len; + unsigned int result; + unsigned char c; + + p += *index; + c= (unsigned char) *p; + + UTF8_COMPUTE (c, mask, len); + if (len == -1) { + /* when called with NULL end, result will never be NULL, + * checks for a NULL character */ + char *p_next= BLI_str_find_next_char_utf8(p, NULL); + /* will never return the same pointer unless '\0', + * eternal loop is prevented */ + *index += (size_t)(p_next - p); + return BLI_UTF8_ERR; + } + + /* this is tricky since there are a few ways we can bail out of bad unicode + * values, 3 possible solutions. */ +#if 0 + UTF8_GET (result, p, i, mask, len, BLI_UTF8_ERR); +#elif 1 + /* WARNING: this is NOT part of glib, or supported by similar functions. + * this is added for text drawing because some filepaths can have latin1 + * characters */ + UTF8_GET (result, p, i, mask, len, BLI_UTF8_ERR); + if(result == BLI_UTF8_ERR) { + len= 1; + result= *p; + } + /* end warning! */ +#else + /* without a fallback like '?', text drawing will stop on this value */ + UTF8_GET (result, p, i, mask, len, '?'); +#endif + + *index += len; + return result; +} + +/* was g_unichar_to_utf8 */ +/** + * BLI_str_utf8_from_unicode: + * @c: a Unicode character code + * @outbuf: output buffer, must have at least 6 bytes of space. + * If %NULL, the length will be computed and returned + * and nothing will be written to @outbuf. + * + * Converts a single character to UTF-8. + * + * Return value: number of bytes written + **/ +size_t BLI_str_utf8_from_unicode(unsigned int c, char *outbuf) +{ + /* If this gets modified, also update the copy in g_string_insert_unichar() */ + unsigned int len = 0; + int first; + int i; + + if (c < 0x80) { + first = 0; + len = 1; + } + else if (c < 0x800) { + first = 0xc0; + len = 2; + } + else if (c < 0x10000) { + first = 0xe0; + len = 3; + } + else if (c < 0x200000) { + first = 0xf0; + len = 4; + } + else if (c < 0x4000000) { + first = 0xf8; + len = 5; + } + else { + first = 0xfc; + len = 6; + } + + if (outbuf) { + for (i = len - 1; i > 0; --i) { + outbuf[i] = (c & 0x3f) | 0x80; + c >>= 6; + } + outbuf[0] = c | first; + } + + return len; +} + +/* was g_utf8_find_prev_char */ +/** + * BLI_str_find_prev_char_utf8: + * @str: pointer to the beginning of a UTF-8 encoded string + * @p: pointer to some position within @str + * + * Given a position @p with a UTF-8 encoded string @str, find the start + * of the previous UTF-8 character starting before @p. Returns %NULL if no + * UTF-8 characters are present in @str before @p. + * + * @p does not have to be at the beginning of a UTF-8 character. No check + * is made to see if the character found is actually valid other than + * it starts with an appropriate byte. + * + * Return value: a pointer to the found character or %NULL. + **/ +char * BLI_str_find_prev_char_utf8(const char *str, const char *p) +{ + for (--p; p >= str; --p) { + if ((*p & 0xc0) != 0x80) { + return (char *)p; + } + } + return NULL; +} + +/* was g_utf8_find_next_char */ +/** + * BLI_str_find_next_char_utf8: + * @p: a pointer to a position within a UTF-8 encoded string + * @end: a pointer to the byte following the end of the string, + * or %NULL to indicate that the string is nul-terminated. + * + * Finds the start of the next UTF-8 character in the string after @p. + * + * @p does not have to be at the beginning of a UTF-8 character. No check + * is made to see if the character found is actually valid other than + * it starts with an appropriate byte. + * + * Return value: a pointer to the found character or %NULL + **/ +char *BLI_str_find_next_char_utf8(const char *p, const char *end) +{ + if (*p) { + if (end) { + for (++p; p < end && (*p & 0xc0) == 0x80; ++p) { + /* do nothing */ + } + } + else { + for (++p; (*p & 0xc0) == 0x80; ++p) { + /* do nothing */ + } + } + } + return (p == end) ? NULL : (char *)p; +} + +/* was g_utf8_prev_char */ +/** + * BLI_str_prev_char_utf8: + * @p: a pointer to a position within a UTF-8 encoded string + * + * Finds the previous UTF-8 character in the string before @p. + * + * @p does not have to be at the beginning of a UTF-8 character. No check + * is made to see if the character found is actually valid other than + * it starts with an appropriate byte. If @p might be the first + * character of the string, you must use g_utf8_find_prev_char() instead. + * + * Return value: a pointer to the found character. + **/ +char *BLI_str_prev_char_utf8(const char *p) +{ + while (1) { + p--; + if ((*p & 0xc0) != 0x80) { + return (char *)p; + } + } +} +/* end glib copy */ diff --git a/source/blender/blenlib/intern/winstuff.c b/source/blender/blenlib/intern/winstuff.c index 3b14abb0bee..21ad0ff7253 100644 --- a/source/blender/blenlib/intern/winstuff.c +++ b/source/blender/blenlib/intern/winstuff.c @@ -53,11 +53,10 @@ int BLI_getInstallationDir( char * str ) { char dir[FILE_MAXDIR]; - char file[FILE_MAXFILE]; int a; GetModuleFileName(NULL,str,FILE_MAXDIR+FILE_MAXFILE); - BLI_split_dirfile(str,dir,file); /* shouldn't be relative */ + BLI_split_dir_part(str, dir, sizeof(dir)); /* shouldn't be relative */ a = strlen(dir); if(dir[a-1] == '\\') dir[a-1]=0; |