~ubuntu-branches/ubuntu/maverick/blender/maverick

« back to all changes in this revision

Viewing changes to source/blender/blenlib/intern/util.c

  • Committer: Bazaar Package Importer
  • Author(s): Khashayar Naderehvandi, Khashayar Naderehvandi, Alessio Treglia
  • Date: 2009-01-22 16:53:59 UTC
  • mfrom: (14.1.1 experimental)
  • Revision ID: james.westby@ubuntu.com-20090122165359-v0996tn7fbit64ni
Tags: 2.48a+dfsg-1ubuntu1
[ Khashayar Naderehvandi ]
* Merge from debian experimental (LP: #320045), Ubuntu remaining changes:
  - Add patch correcting header file locations.
  - Add libvorbis-dev and libgsm1-dev to Build-Depends.
  - Use avcodec_decode_audio2() in source/blender/src/hddaudio.c

[ Alessio Treglia ]
* Add missing previous changelog entries.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
 * various string, file, list operations.
4
4
 *
5
5
 *
6
 
 * $Id: util.c 14669 2008-05-04 09:41:15Z campbellbarton $
 
6
 * $Id: util.c 16943 2008-10-06 08:02:35Z campbellbarton $
7
7
 *
8
8
 * ***** BEGIN GPL LICENSE BLOCK *****
9
9
 *
865
865
void BLI_cleanup_dir(const char *relabase, char *dir)
866
866
{
867
867
        BLI_cleanup_file(relabase, dir);
868
 
#ifdef WIN32
869
 
        strcat(dir, "\\");
870
 
#else
871
 
        strcat(dir, "/");
872
 
#endif
 
868
        BLI_add_slash(dir);
 
869
 
873
870
}
874
871
 
875
872
void BLI_cleanup_file(const char *relabase, char *dir)
878
875
        char *start, *eind;
879
876
        if (relabase) {
880
877
                BLI_convertstringcode(dir, relabase);
 
878
        } else {
 
879
                if (dir[0]=='/' && dir[1]=='/') {
 
880
                        if (dir[2]== '\0') {
 
881
                                return; /* path is "//" - cant clean it */
 
882
                        }
 
883
                        dir = dir+2; /* skip the first // */
 
884
                }
881
885
        }
 
886
        
 
887
        /* Note
 
888
         *   memmove( start, eind, strlen(eind)+1 );
 
889
         * is the same as
 
890
         *   strcpy( start, eind ); 
 
891
         * except strcpy should not be used because there is overlap,
 
892
          * so use memmove's slightly more obscure syntax - Campbell
 
893
         */
 
894
        
882
895
#ifdef WIN32
883
 
        if(dir[0]=='.') {       /* happens for example in FILE_MAIN */
 
896
        
 
897
        /* Note, this should really be moved to the file selector,
 
898
         * since this function is used in many areas */
 
899
        if(strcmp(dir, ".")==0) {       /* happens for example in FILE_MAIN */
884
900
           get_default_root(dir);
885
901
           return;
886
902
        }       
892
908
                        if (dir[a] == '\\') break;
893
909
                        a--;
894
910
                }
895
 
                strcpy(dir+a,eind);
 
911
                if (a<0) {
 
912
                        break;
 
913
                } else {
 
914
                        memmove( dir+a, eind, strlen(eind)+1 );
 
915
                }
896
916
        }
897
917
 
898
918
        while ( (start = strstr(dir,"\\.\\")) ){
899
919
                eind = start + strlen("\\.\\") - 1;
900
 
                strcpy(start,eind);
 
920
                memmove( start, eind, strlen(eind)+1 );
901
921
        }
902
922
 
903
923
        while ( (start = strstr(dir,"\\\\" )) ){
904
924
                eind = start + strlen("\\\\") - 1;
905
 
                strcpy(start,eind);
 
925
                memmove( start, eind, strlen(eind)+1 );
906
926
        }
907
927
 
908
928
        if((a = strlen(dir))){                          /* remove the '\\' at the end */
925
945
                        if (dir[a] == '/') break;
926
946
                        a--;
927
947
                }
928
 
                strcpy(dir+a,eind);
 
948
                if (a<0) {
 
949
                        break;
 
950
                } else {
 
951
                        memmove( dir+a, eind, strlen(eind)+1 );
 
952
                }
929
953
        }
930
954
 
931
955
        while ( (start = strstr(dir,"/./")) ){
932
956
                eind = start + strlen("/./") - 1;
933
 
                strcpy(start,eind);
 
957
                memmove( start, eind, strlen(eind)+1 );
934
958
        }
935
959
 
936
960
        while ( (start = strstr(dir,"//" )) ){
937
961
                eind = start + strlen("//") - 1;
938
 
                strcpy(start,eind);
 
962
                memmove( start, eind, strlen(eind)+1 );
939
963
        }
940
964
 
941
965
        if( (a = strlen(dir)) ){                                /* remove all '/' at the end */
1037
1061
        }
1038
1062
}
1039
1063
 
 
1064
int BLI_parent_dir(char *path)
 
1065
{
 
1066
#ifdef WIN32
 
1067
        static char *parent_dir="..\\";
 
1068
#else
 
1069
        static char *parent_dir="../";
 
1070
#endif
 
1071
        char tmp[FILE_MAXDIR+FILE_MAXFILE+4];
 
1072
        BLI_strncpy(tmp, path, sizeof(tmp));
 
1073
        BLI_add_slash(tmp);
 
1074
        strcat(tmp, parent_dir);
 
1075
        BLI_cleanup_dir(NULL, tmp);
 
1076
        
 
1077
        if (!BLI_testextensie(tmp, parent_dir)) {
 
1078
                BLI_strncpy(path, tmp, sizeof(tmp));    
 
1079
                return 1;
 
1080
        } else {
 
1081
                return 0;
 
1082
        }
 
1083
}
1040
1084
 
1041
1085
int BLI_convertstringframe(char *path, int frame)
1042
1086
{
1108
1152
 
1109
1153
int BLI_convertstringcode(char *path, const char *basepath)
1110
1154
{
1111
 
        int wasrelative;
 
1155
        int wasrelative = (strncmp(path, "//", 2)==0);
1112
1156
        char tmp[FILE_MAX];
1113
1157
        char base[FILE_MAX];
 
1158
#ifdef WIN32
1114
1159
        char vol[3] = {'\0', '\0', '\0'};
1115
1160
 
1116
1161
        BLI_strncpy(vol, path, 3);
1117
 
        wasrelative= (strncmp(vol, "//", 2)==0);
1118
 
 
1119
 
#ifdef WIN32
1120
1162
        /* we are checking here if we have an absolute path that is not in the current
1121
1163
           blend file as a lib main - we are basically checking for the case that a 
1122
1164
           UNIX root '/' is passed.
1135
1177
        }
1136
1178
#else
1137
1179
        BLI_strncpy(tmp, path, FILE_MAX);
 
1180
        
 
1181
        /* Check for loading a windows path on a posix system
 
1182
         * in this case, there is no use in trying C:/ since it 
 
1183
         * will never exist on a unix os.
 
1184
         * 
 
1185
         * Add a / prefix and lowercase the driveletter, remove the :
 
1186
         * C:\foo.JPG -> /c/foo.JPG */
 
1187
        
 
1188
        if (isalpha(tmp[0]) && tmp[1] == ':' && (tmp[2]=='\\' || tmp[2]=='/') ) {
 
1189
                tmp[1] = tolower(tmp[0]); /* replace ':' with driveletter */
 
1190
                tmp[0] = '/'; 
 
1191
                /* '\' the slash will be converted later */
 
1192
        }
 
1193
        
1138
1194
#endif
1139
1195
 
1140
1196
        BLI_strncpy(base, basepath, FILE_MAX);
1150
1206
        BLI_char_switch(tmp, '\\', '/');
1151
1207
        BLI_char_switch(base, '\\', '/');       
1152
1208
 
1153
 
        if (tmp[0] == '/' && tmp[1] == '/') {
1154
 
                char *filepart= BLI_strdup(tmp+2); /* skip code */
 
1209
        /* Paths starting with // will get the blend file as their base,
 
1210
         * this isnt standard in any os but is uesed in blender all over the place */
 
1211
        if (wasrelative) {
1155
1212
                char *lslash= BLI_last_slash(base);
1156
 
 
1157
1213
                if (lslash) {
1158
1214
                        int baselen= (int) (lslash-base) + 1;
1159
 
 
 
1215
                        /* use path for for temp storage here, we copy back over it right away */
 
1216
                        BLI_strncpy(path, tmp+2, FILE_MAX);
 
1217
                        
1160
1218
                        memcpy(tmp, base, baselen);
1161
 
                        strcpy(tmp+baselen, filepart);
 
1219
                        strcpy(tmp+baselen, path);
 
1220
                        strcpy(path, tmp);
1162
1221
                } else {
1163
 
                        strcpy(tmp, filepart);
 
1222
                        strcpy(path, tmp+2);
1164
1223
                }
1165
 
                
1166
 
                MEM_freeN(filepart);
 
1224
        } else {
 
1225
                strcpy(path, tmp);
1167
1226
        }
1168
1227
        
1169
 
        strcpy(path, tmp);
 
1228
        if (path[0]!='\0') {
 
1229
                if ( path[strlen(path)-1]=='/') {
 
1230
                        BLI_cleanup_dir(NULL, path);
 
1231
                } else {
 
1232
                        BLI_cleanup_file(NULL, path);
 
1233
                }
 
1234
        }
1170
1235
        
1171
1236
#ifdef WIN32
1172
1237
        /* skip first two chars, which in case of
1181
1246
        return wasrelative;
1182
1247
}
1183
1248
 
 
1249
 
 
1250
/*
 
1251
 * Should only be done with command line paths.
 
1252
 * this is NOT somthing blenders internal paths support like the // prefix
 
1253
 */
 
1254
int BLI_convertstringcwd(char *path)
 
1255
{
 
1256
        int wasrelative = 1;
 
1257
        int filelen = strlen(path);
 
1258
        
 
1259
#ifdef WIN32
 
1260
        if (filelen >= 3 && path[1] == ':' && (path[2] == '\\' || path[2] == '/'))
 
1261
                wasrelative = 0;
 
1262
#else
 
1263
        if (filelen >= 2 && path[0] == '/')
 
1264
                wasrelative = 0;
 
1265
#endif
 
1266
        
 
1267
        if (wasrelative==1) {
 
1268
                char cwd[FILE_MAXDIR + FILE_MAXFILE];
 
1269
                BLI_getwdN(cwd); /* incase the full path to the blend isnt used */
 
1270
                
 
1271
                if (cwd[0] == '\0') {
 
1272
                        printf( "Could not get the current working directory - $PWD for an unknown reason.");
 
1273
                } else {
 
1274
                        /* uses the blend path relative to cwd important for loading relative linked files.
 
1275
                        *
 
1276
                        * cwd should contain c:\ etc on win32 so the relbase can be NULL
 
1277
                        * relbase being NULL also prevents // being misunderstood as relative to the current
 
1278
                        * blend file which isnt a feature we want to use in this case since were dealing
 
1279
                        * with a path from the command line, rather then from inside Blender */
 
1280
                        
 
1281
                        char origpath[FILE_MAXDIR + FILE_MAXFILE];
 
1282
                        BLI_strncpy(origpath, path, FILE_MAXDIR + FILE_MAXFILE);
 
1283
                        
 
1284
                        BLI_make_file_string(NULL, path, cwd, origpath); 
 
1285
                }
 
1286
        }
 
1287
        
 
1288
        return wasrelative;
 
1289
}
 
1290
 
 
1291
 
1184
1292
/* copy di to fi, filename only */
1185
1293
void BLI_splitdirstring(char *di, char *fi)
1186
1294
{
1692
1800
        path = br_find_exe( NULL );
1693
1801
        if (path) {
1694
1802
                strcpy(fullname, path);
 
1803
                free(path);
1695
1804
                return;
1696
1805
        }
1697
1806
#endif
1881
1990
#include "iconv.h"
1882
1991
#include "localcharset.h"
1883
1992
 
1884
 
void BLI_string_to_utf8(char *original, char *utf_8, char *code)
 
1993
void BLI_string_to_utf8(char *original, char *utf_8, const char *code)
1885
1994
{
1886
1995
        size_t inbytesleft=strlen(original);
1887
1996
        size_t outbytesleft=512;
1939
2048
 
1940
2049
int BLI_int_from_pointer(void *poin)
1941
2050
{
1942
 
        long lval= (long)poin;
 
2051
        intptr_t lval= (intptr_t)poin;
1943
2052
        
1944
2053
        return (int)(lval>>3);
1945
2054
}
1947
2056
void *BLI_pointer_from_int(int val)
1948
2057
{
1949
2058
        static int firsttime= 1;
1950
 
        static long basevalue= 0;
 
2059
        static intptr_t basevalue= 0;
1951
2060
        
1952
2061
        if(firsttime) {
1953
2062
                void *poin= malloc(10000);
1954
 
                basevalue= (long)poin;
 
2063
                basevalue= (intptr_t)poin;
1955
2064
                basevalue &= ~PMASK;
1956
2065
                printf("base: %d pointer %p\n", basevalue, poin); /* debug */
1957
2066
                firsttime= 0;
1958
2067
                free(poin);
1959
2068
        }
1960
 
        return (void *)(basevalue | (((long)val)<<3));
 
2069
        return (void *)(basevalue | (((intptr_t)val)<<3));
1961
2070
}
1962
2071
 
1963
2072
#else