3 * various string, file, list operations.
8 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version. The Blender
14 * Foundation also sells licenses for use in proprietary software under
15 * the Blender License. See http://www.blender.org/BL/ for information
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software Foundation,
25 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
28 * All rights reserved.
30 * The Original Code is: all of this file.
32 * Contributor(s): none yet.
34 * ***** END GPL/BL DUAL LICENSE BLOCK *****
44 #include "MEM_guardedalloc.h"
46 #include "BLI_blenlib.h"
47 #include "DNA_listBase.h"
48 #include "BLI_storage.h"
49 #include "BLI_storage_types.h"
64 #include "BLI_winstuff.h"
74 static int add_win32_extension(char *name);
78 /* Ripped this from blender.c
80 void addlisttolist(ListBase *list1, ListBase *list2)
83 if(list2->first==0) return;
86 list1->first= list2->first;
87 list1->last= list2->last;
90 ((struct Link *)list1->last)->next= list2->first;
91 ((struct Link *)list2->first)->prev= list1->last;
92 list1->last= list2->last;
94 list2->first= list2->last= 0;
97 int BLI_stringdec(char *string, char *kop, char *staart, unsigned short *numlen)
99 unsigned short len, len2, nums = 0, nume = 0;
102 len2 = len = strlen( string);
105 if (strncasecmp(string + len - 6, ".blend", 6) == 0) len -= 6;
106 else if (strncasecmp(string + len - 6, ".trace", 6) == 0) len -= 6;
111 /* handle .jf0 en .jf1 for jstreams */
112 if (strncasecmp(string + len - 4, ".jf", 3) == 0) len -= 4;
113 else if (strncasecmp(string + len - 4, ".tga", 4) == 0) len -= 4;
114 else if (strncasecmp(string + len - 4, ".jpg", 4) == 0) len -= 4;
115 else if (strncasecmp(string + len - 4, ".png", 4) == 0) len -= 4;
116 else if (strncasecmp(string + len - 4, ".txt", 4) == 0) len -= 4;
117 else if (strncasecmp(string + len - 4, ".cyc", 4) == 0) len -= 4;
118 else if (strncasecmp(string + len - 4, ".enh", 4) == 0) len -= 4;
119 else if (strncasecmp(string + len - 4, ".rgb", 4) == 0) len -= 4;
120 else if (strncasecmp(string + len - 4, ".psx", 4) == 0) len -= 4;
121 else if (strncasecmp(string + len - 4, ".ble", 4) == 0) len -= 4;
125 for (i = len - 1; i >= 0; i--){
126 if (string[i] == '/') break;
127 if (isdigit(string[i])) {
142 if (staart) strcpy(staart,&string[nume+1]);
147 if (numlen) *numlen = nume-nums+1;
148 return ((int)atoi(&(string[nums])));
150 if (staart) strcpy(staart, string + len);
152 strncpy(kop, string, len);
155 if (numlen) *numlen=0;
160 void BLI_stringenc(char *string, char *kop, char *staart, unsigned short numlen, int pic)
163 unsigned short len,i;
167 if (pic>0 || numlen==4) {
168 len= sprintf(numstr,"%d",pic);
170 for(i=len;i<numlen;i++){
173 strcat(string,numstr);
175 strcat(string,staart);
179 void BLI_newname(char * name, int add)
181 char head[128], tail[128];
183 unsigned short digits;
185 pic = BLI_stringdec(name, head, tail, &digits);
187 /* are we going from 100 -> 99 or from 10 -> 9 */
188 if (add < 0 && digits < 4 && digits > 0) {
191 for (i = digits; i > 1; i--) exp *= 10;
192 if (pic >= exp && (pic + add) < exp) digits--;
197 if(digits==4 && pic<0) pic= 0;
198 BLI_stringenc(name, head, tail, digits, pic);
202 void BLI_addhead(ListBase *listbase, void *vlink)
204 struct Link *link= vlink;
206 if (link == 0) return;
207 if (listbase == 0) return;
209 link->next = listbase->first;
212 if (listbase->first) ((struct Link *)listbase->first)->prev = link;
213 if (listbase->last == 0) listbase->last = link;
214 listbase->first = link;
218 void BLI_addtail(ListBase *listbase, void *vlink)
220 struct Link *link= vlink;
222 if (link == 0) return;
223 if (listbase == 0) return;
226 link->prev = listbase->last;
228 if (listbase->last) ((struct Link *)listbase->last)->next = link;
229 if (listbase->first == 0) listbase->first = link;
230 listbase->last = link;
234 void BLI_remlink(ListBase *listbase, void *vlink)
236 struct Link *link= vlink;
238 if (link == 0) return;
239 if (listbase == 0) return;
241 if (link->next) link->next->prev = link->prev;
242 if (link->prev) link->prev->next = link->next;
244 if (listbase->last == link) listbase->last = link->prev;
245 if (listbase->first == link) listbase->first = link->next;
249 void BLI_freelinkN(ListBase *listbase, void *vlink)
251 struct Link *link= vlink;
253 if (link == 0) return;
254 if (listbase == 0) return;
256 BLI_remlink(listbase,link);
261 void BLI_insertlink(ListBase *listbase, void *vprevlink, void *vnewlink)
263 struct Link *prevlink= vprevlink, *newlink= vnewlink;
265 /* newlink comes after prevlink */
267 if (newlink == 0) return;
268 if (listbase == 0) return;
270 if(listbase->first==0) { /* empty list */
271 listbase->first= newlink;
272 listbase->last= newlink;
275 if (prevlink== 0) { /* insert before first element */
276 newlink->next= listbase->first;
278 newlink->next->prev= newlink;
279 listbase->first= newlink;
283 if (listbase->last== prevlink) /* at end of list */
284 listbase->last = newlink;
286 newlink->next= prevlink->next;
287 prevlink->next= newlink;
288 if(newlink->next) newlink->next->prev= newlink;
289 newlink->prev= prevlink;
292 void BLI_insertlinkbefore(ListBase *listbase, void *vnextlink, void *vnewlink)
294 struct Link *nextlink= vnextlink, *newlink= vnewlink;
296 /* newlink before nextlink */
298 if (newlink == 0) return;
299 if (listbase == 0) return;
301 if(listbase->first==0) { /* empty list */
302 listbase->first= newlink;
303 listbase->last= newlink;
306 if (nextlink== 0) { /* insert at end of list */
307 newlink->prev= listbase->last;
309 ((struct Link *)listbase->last)->next= newlink;
310 listbase->last= newlink;
314 if (listbase->first== nextlink) /* at beginning of list */
315 listbase->first = newlink;
317 newlink->next= nextlink;
318 newlink->prev= nextlink->prev;
319 nextlink->prev= newlink;
320 if(newlink->prev) newlink->prev->next= newlink;
324 void BLI_freelist(ListBase *listbase)
326 struct Link *link,*next;
328 if (listbase == 0) return;
329 link= listbase->first;
339 void BLI_freelistN(ListBase *listbase)
341 struct Link *link,*next;
343 if (listbase == 0) return;
344 link= listbase->first;
355 int BLI_countlist(ListBase *listbase)
361 link = listbase->first;
370 void * BLI_findlink(ListBase *listbase, int number)
375 link = listbase->first;
376 while (link != NULL && number != 0) {
386 char *BLI_strdupn(char *str, int len) {
387 char *n= MEM_mallocN(len+1, "strdup");
393 char *BLI_strdup(char *str) {
394 return BLI_strdupn(str, strlen(str));
397 char *BLI_strncpy(char *dst, char *src, int maxncpy) {
398 int srclen= strlen(src);
399 int cpylen= (srclen>(maxncpy-1))?(maxncpy-1):srclen;
401 memcpy(dst, src, cpylen);
407 int BLI_streq(char *a, char *b) {
408 return (strcmp(a, b)==0);
410 int BLI_strcaseeq(char *a, char *b) {
411 return (strcasecmp(a, b)==0);
414 void BLI_makestringcode(char *fromfile, char *str)
416 char *slash, len, temp[512];
418 strcpy(temp, fromfile);
420 /* Find the last slash */
421 slash = (strrchr(temp, '/')>strrchr(temp, '\\'))
422 ? strrchr(temp, '/') : strrchr(temp, '\\');
427 if(strncmp(str, temp, len)==0) {
430 strcpy(temp+2, str+len);
437 int BLI_convertstringcode(char *path, char *basepath, int framenum)
439 int len, wasrelative= (strncmp(path, "//", 2)==0);
441 if (path[0] == '/' && path[1] == '/') {
442 char *filepart= BLI_strdup(path+2); /* skip code */
443 char *lslash= BLI_last_slash(basepath);
446 int baselen= (int) (lslash-basepath) + 1;
448 memcpy(path, basepath, baselen);
449 strcpy(path+baselen, filepart);
451 strcpy(path, filepart);
458 if(len && path[len-1]=='#') {
459 sprintf(path+len-1, "%04d", framenum);
465 void BLI_splitdirstring(char *di,char *fi)
467 char *lslash= BLI_last_slash(di);
470 strcpy(fi, lslash+1);
478 char *BLI_gethome(void) {
481 return "/boot/home/"; /* BeOS 4.5: doubleclick at icon doesnt give home env */
483 #elif !defined(WIN32)
484 return getenv("HOME");
488 static char dir[512];
490 ret = getenv("HOME");
492 if (BLI_exists(ret)) return ret;
495 // add user profile support for WIN 2K / NT
496 ret = getenv("USERPROFILE");
498 if (BLI_exists(ret)) { /* from fop, also below... */
499 sprintf(dir, "%s/Application Data/Not a Number/Blender", ret);
500 BLI_recurdir_fileops(dir);
501 if (BLI_exists(dir)) {
509 ret = getenv("WINDOWS");
511 if(BLI_exists(ret)) return ret;
514 ret = getenv("WINDIR");
516 if(BLI_exists(ret)) return ret;
523 static void char_switch(char *string, char from, char to)
525 while (*string != 0) {
526 if (*string == from) *string = to;
531 void BLI_make_exist(char *dir)
536 char_switch(dir, '/', '\\');
538 char_switch(dir, '\\', '/');
544 while(BLI_exists(dir) == 0){
546 while(dir[a] != '\\'){
550 if (a >= 0) dir[a+1] = 0;
557 while(BLI_exist(dir) == 0){
559 while(dir[a] != '/'){
563 if (a >= 0) dir[a+1] = 0;
572 void BLI_make_file_string(char *relabase, char *string, char *dir, char *file)
575 if (!string || !dir || !file) return; /* We don't want any NULLs */
577 string[0]= 0; /* ton */
579 /* Resolve relative references */
580 if (relabase && dir[0] == '/' && dir[1] == '/') {
583 /* Get the file name, chop everything past the last slash (ie. the filename) */
584 strcpy(string, relabase);
586 lslash= (strrchr(string, '/')>strrchr(string, '\\'))?strrchr(string, '/'):strrchr(string, '\\');
588 if(lslash) *(lslash+1)= 0;
590 dir+=2; /* Skip over the relative reference */
595 /* Make sure string ends in one (and only one) slash */
596 if (string[strlen(string)-1] != '/' && string[strlen(string)-1] != '\\')
599 while (*file && (*file == '/' || *file == '\\')) /* Trim slashes from the front of file */
602 strcat (string, file);
604 /* Push all slashes to the system preferred direction */
606 char_switch(string, '/', '\\');
608 char_switch(string, '\\', '/');
612 int BLI_testextensie(char *str, char *ext)
620 if(a==0 || b==0 || b>=a) {
622 } else if (strcasecmp(ext, str + a - b)) {
631 void BLI_split_dirfile(char *string, char *dir, char *file)
639 if (strlen(string)) {
640 if (string[0] == '/' || string[0] == '\\') {
642 } else if (string[1] == ':' && string[2] == '\\') {
653 if (S_ISDIR(BLI_exist(dir))) {
654 strcpy(file,string + strlen(dir));
656 if (strrchr(file,'\\')) strcpy(file,strrchr(file,'\\')+1);
658 if (a = strlen(dir)) {
659 if (dir[a-1] != '\\') strcat(dir,"\\");
664 while(a>0 && dir[a] != '\\') a--;
666 strcpy(file, string + strlen(dir));
675 if (strlen(string)) {
676 if (string[0] == '/') {
678 } else if (string[1] == ':' && string[2] == '\\') {
690 if (S_ISDIR(BLI_exist(dir))) {
691 strcpy(file,string + strlen(dir));
693 if (strrchr(file,'/')) strcpy(file,strrchr(file,'/')+1);
695 if ( (a = strlen(dir)) ) {
696 if (dir[a-1] != '/') strcat(dir,"/");
701 while(dir[a] != '/') a--;
703 strcpy(file, string + strlen(dir));
714 // copies from BKE_utildefines
716 #define FILE_MAXDIR 160
720 #define FILE_MAXFILE 80
723 static int add_win32_extension(char *name)
728 type = BLI_exist(name);
729 if ((type == 0) || S_ISDIR(type)) {
731 char filename[FILE_MAXDIR+FILE_MAXFILE];
732 char ext[FILE_MAXDIR+FILE_MAXFILE];
733 char *extensions = getenv("PATHEXT");
737 strcpy(filename, name);
738 temp = strstr(extensions, ";");
740 strncpy(ext, extensions, temp - extensions);
741 ext[temp - extensions] = 0;
742 extensions = temp + 1;
743 strcat(filename, ext);
745 strcat(filename, extensions);
748 type = BLI_exist(filename);
749 if (type && (! S_ISDIR(type))) {
751 strcpy(name, filename);
764 void BLI_where_am_i(char *fullname, char *name)
766 char filename[FILE_MAXDIR+FILE_MAXFILE];
770 char *seperator = ";";
773 char *seperator = ":";
777 if (name && fullname && strlen(name)) {
778 strcpy(fullname, name);
779 if (name[0] == '.') {
780 // relative path, prepend cwd
781 BLI_getwdN(fullname);
782 len = strlen(fullname);
783 if (len && fullname[len -1] != slash[0]) {
784 strcat(fullname, slash);
786 strcat(fullname, name);
787 add_win32_extension(fullname);
788 } else if (BLI_last_slash(name)) {
790 strcpy(fullname, name);
791 add_win32_extension(fullname);
793 // search for binary in $PATH
794 path = getenv("PATH");
797 temp = strstr(path, seperator);
799 strncpy(filename, path, temp - path);
800 filename[temp - path] = 0;
803 strncpy(filename, path, sizeof(filename));
805 len = strlen(filename);
806 if (len && filename[len - 1] != slash[0]) {
807 strcat(filename, slash);
809 strcat(filename, name);
810 if (add_win32_extension(filename)) {
811 strcpy(fullname, filename);
818 if (strcmp(name, fullname)) {
819 printf("guessing '%s' == '%s'\n", name, fullname);
824 // in windows change long filename to short filename because
825 // win2k doesn't know how to parse a commandline with lots of
826 // spaces and double-quotes. There's another solution to this
827 // with spawnv(P_WAIT, bprogname, argv) instead of system() but
828 // that's even uglier
829 GetShortPathName(fullname, fullname, FILE_MAXDIR+FILE_MAXFILE);
831 printf("Shortname = '%s'\n", fullname);