~siretart/ubuntu/utopic/blender/libav10

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Matteo F. Vescovi
  • Date: 2012-07-23 08:54:18 UTC
  • mfrom: (14.2.16 sid)
  • mto: (14.2.19 sid)
  • mto: This revision was merged to the branch mainline in revision 42.
  • Revision ID: package-import@ubuntu.com-20120723085418-9foz30v6afaf5ffs
Tags: 2.63a-2
* debian/: Cycles support added (Closes: #658075)
  For now, this top feature has been enabled only
  on [any-amd64 any-i386] architectures because
  of OpenImageIO failing on all others
* debian/: scripts installation path changed
  from /usr/lib to /usr/share:
  + debian/patches/: patchset re-worked for path changing
  + debian/control: "Breaks" field added on yafaray-exporter

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
2
 
 * $Id: storage.c 30553 2010-07-20 19:26:53Z elubie $
3
 
 *
 
1
/*
4
2
 * ***** BEGIN GPL LICENSE BLOCK *****
5
3
 *
6
4
 * This program is free software; you can redistribute it and/or
29
27
 * Some really low-level file thingies.
30
28
 */
31
29
 
32
 
/* needed for mingw & _stat64i32 */
33
 
#ifdef FREE_WINDOWS
34
 
# define __MSVCRT_VERSION__ 0x0800
35
 
#endif
 
30
/** \file blender/blenlib/intern/storage.c
 
31
 *  \ingroup bli
 
32
 */
 
33
 
36
34
 
37
35
#include <sys/types.h>
38
36
#include <stdio.h>
45
43
#include <time.h>
46
44
#include <sys/stat.h>
47
45
 
48
 
#if defined (__sun__) || defined (__sun) || defined (__sgi) || defined (__NetBSD__)
 
46
#if defined (__sun__) || defined (__sun) || defined (__NetBSD__)
49
47
#include <sys/statvfs.h> /* Other modern unix os's should probably use this also */
50
48
#elif !defined(__FreeBSD__) && !defined(linux) && (defined(__sparc) || defined(__sparc__))
51
49
#include <sys/statfs.h>
70
68
#include <fcntl.h>
71
69
#include <string.h>                     /* strcpy etc.. */
72
70
 
73
 
#ifndef WIN32
74
 
#include <sys/ioctl.h>
75
 
#include <unistd.h>                     /*  */
76
 
#include <pwd.h>
77
 
#endif
78
 
 
79
 
#if !defined(__FreeBSD__) && !defined(__APPLE__)
80
 
#include <malloc.h>
81
 
#endif
82
 
 
83
71
#ifdef WIN32
84
 
#include <io.h>
85
 
#include <direct.h>
86
 
#include "BLI_winstuff.h"
 
72
#  include <io.h>
 
73
#  include <direct.h>
 
74
#  include "BLI_winstuff.h"
 
75
#  include "utfconv.h"
 
76
#else
 
77
#  include <sys/ioctl.h>
 
78
#  include <unistd.h>
 
79
#  include <pwd.h>
87
80
#endif
88
81
 
89
 
 
90
82
/* lib includes */
91
83
#include "MEM_guardedalloc.h"
92
84
 
94
86
 
95
87
#include "BLI_listbase.h"
96
88
#include "BLI_linklist.h"
97
 
#include "BLI_storage_types.h"
 
89
#include "BLI_fileops.h"
 
90
 
 
91
#include "BLI_fileops_types.h"
98
92
#include "BLI_string.h"
 
93
#include "BLI_fileops.h"
 
94
 
99
95
#include "BKE_utildefines.h"
100
96
 
101
97
/* vars: */
102
98
static int totnum,actnum;
103
99
static struct direntry *files;
104
100
 
105
 
static struct ListBase dirbase_={
106
 
        0,0};
 
101
static struct ListBase dirbase_={NULL, NULL};
107
102
static struct ListBase *dirbase = &dirbase_;
108
103
 
109
 
 
110
 
char *BLI_getwdN(char *dir)
 
104
/* can return NULL when the size is not big enough */
 
105
char *BLI_current_working_dir(char *dir, const int maxncpy)
111
106
{
112
 
        char *pwd;
113
 
 
114
 
        if (dir) {
115
 
                pwd = getenv("PWD");
116
 
                if (pwd){
117
 
                        strcpy(dir, pwd);
118
 
                        return(dir);
119
 
                }
120
 
                /* 160 is FILE_MAXDIR in filesel.c */
121
 
                return( getcwd(dir, 160) );
 
107
        const char *pwd= getenv("PWD");
 
108
        if (pwd) {
 
109
                BLI_strncpy(dir, pwd, maxncpy);
 
110
                return dir;
122
111
        }
123
 
        return(0);
 
112
 
 
113
        return getcwd(dir, maxncpy);
124
114
}
125
115
 
126
116
 
127
 
int BLI_compare(struct direntry *entry1, struct direntry *entry2)
 
117
static int bli_compare(struct direntry *entry1, struct direntry *entry2)
128
118
{
129
119
        /* type is equal to stat.st_mode */
130
120
 
131
 
        if (S_ISDIR(entry1->type)){
 
121
        if (S_ISDIR(entry1->type)) {
132
122
                if (S_ISDIR(entry2->type)==0) return (-1);
133
 
        } else{
 
123
        }
 
124
        else {
134
125
                if (S_ISDIR(entry2->type)) return (1);
135
126
        }
136
 
        if (S_ISREG(entry1->type)){
 
127
        if (S_ISREG(entry1->type)) {
137
128
                if (S_ISREG(entry2->type)==0) return (-1);
138
 
        } else{
 
129
        }
 
130
        else {
139
131
                if (S_ISREG(entry2->type)) return (1);
140
132
        }
141
133
        if ((entry1->type & S_IFMT) < (entry2->type & S_IFMT)) return (-1);
142
134
        if ((entry1->type & S_IFMT) > (entry2->type & S_IFMT)) return (1);
143
135
        
144
136
        /* make sure "." and ".." are always first */
145
 
        if( strcmp(entry1->relname, ".")==0 ) return (-1);
146
 
        if( strcmp(entry2->relname, ".")==0 ) return (1);
147
 
        if( strcmp(entry1->relname, "..")==0 ) return (-1);
148
 
        if( strcmp(entry2->relname, "..")==0 ) return (1);
 
137
        if ( strcmp(entry1->relname, ".")==0 ) return (-1);
 
138
        if ( strcmp(entry2->relname, ".")==0 ) return (1);
 
139
        if ( strcmp(entry1->relname, "..")==0 ) return (-1);
 
140
        if ( strcmp(entry2->relname, "..")==0 ) return (1);
149
141
 
150
142
        return (BLI_natstrcmp(entry1->relname,entry2->relname));
151
143
}
152
144
 
153
145
 
154
 
double BLI_diskfree(char *dir)
 
146
double BLI_dir_free_space(const char *dir)
155
147
{
156
148
#ifdef WIN32
157
149
        DWORD sectorspc, bytesps, freec, clusters;
161
153
        if (dir[0]=='/' || dir[0]=='\\') {
162
154
                tmp[0]='\\';
163
155
                tmp[1]=0;
164
 
        } else if (dir[1]==':') {
 
156
        }
 
157
        else if (dir[1]==':') {
165
158
                tmp[0]=dir[0];
166
159
                tmp[1]=':';
167
160
                tmp[2]='\\';
173
166
        return (double) (freec*bytesps*sectorspc);
174
167
#else
175
168
 
176
 
#if defined (__sun__) || defined (__sun) || defined (__sgi) || defined (__NetBSD__)
 
169
#if defined (__sun__) || defined (__sun) || defined (__NetBSD__)
177
170
        struct statvfs disk;
178
171
#else
179
172
        struct statfs disk;
186
179
        
187
180
        strcpy(name,dir);
188
181
 
189
 
        if(len){
 
182
        if (len) {
190
183
                slash = strrchr(name,'/');
191
184
                if (slash) slash[1] = 0;
192
 
        } else strcpy(name,"/");
 
185
        }
 
186
        else strcpy(name,"/");
193
187
 
194
188
#if defined (__FreeBSD__) || defined (linux) || defined (__OpenBSD__) || defined (__APPLE__) || defined(__GNU__) || defined(__GLIBC__)
195
189
        if (statfs(name, &disk)) return(-1);
196
190
#endif
197
191
 
198
 
#if defined (__sun__) || defined (__sun) || defined (__sgi) || defined (__NetBSD__)
 
192
#if defined (__sun__) || defined (__sun) || defined (__NetBSD__)
199
193
        if (statvfs(name, &disk)) return(-1);   
200
194
#elif !defined(__FreeBSD__) && !defined(linux) && (defined(__sparc) || defined(__sparc__))
201
195
        /* WARNING - This may not be supported by geeneric unix os's - Campbell */
206
200
#endif
207
201
}
208
202
 
209
 
void BLI_builddir(char *dirname, char *relname)
 
203
static void bli_builddir(const char *dirname, const char *relname)
210
204
{
211
205
        struct dirent *fname;
212
206
        struct dirlink *dlink;
213
 
        int rellen, newnum = 0, len;
 
207
        int rellen, newnum = 0;
214
208
        char buf[256];
215
209
        DIR *dir;
216
210
 
217
 
        strcpy(buf,relname);
 
211
        BLI_strncpy(buf, relname, sizeof(buf));
218
212
        rellen=strlen(relname);
219
213
 
220
 
        if (rellen){
 
214
        if (rellen) {
221
215
                buf[rellen]='/';
222
216
                rellen++;
223
217
        }
224
 
 
225
 
        if (chdir(dirname) == -1){
226
 
                perror(dirname);
227
 
                return;
228
 
        }
229
 
 
230
 
        if ( (dir = (DIR *)opendir(".")) ){
 
218
#ifndef WIN32
 
219
        if (chdir(dirname) == -1) {
 
220
                perror(dirname);
 
221
                return;
 
222
        }
 
223
#else
 
224
        UTF16_ENCODE(dirname);
 
225
        if (!SetCurrentDirectoryW(dirname_16)) {
 
226
                perror(dirname);
 
227
                free(dirname_16);
 
228
                return;
 
229
        }
 
230
        UTF16_UN_ENCODE(dirname);
 
231
 
 
232
#endif
 
233
        if ((dir = (DIR *)opendir("."))) {
231
234
                while ((fname = (struct dirent*) readdir(dir)) != NULL) {
232
 
                        len= strlen(fname->d_name);
233
 
                        
234
235
                        dlink = (struct dirlink *)malloc(sizeof(struct dirlink));
235
 
                        if (dlink){
236
 
                                strcpy(buf+rellen,fname->d_name);
 
236
                        if (dlink) {
 
237
                                BLI_strncpy(buf + rellen ,fname->d_name, sizeof(buf) - rellen);
237
238
                                dlink->name = BLI_strdup(buf);
238
239
                                BLI_addhead(dirbase,dlink);
239
240
                                newnum++;
240
241
                        }
241
242
                }
242
243
                
243
 
                if (newnum){
244
 
 
245
 
                        if (files) files=(struct direntry *)realloc(files,(totnum+newnum) * sizeof(struct direntry));
246
 
                        else files=(struct direntry *)malloc(newnum * sizeof(struct direntry));
247
 
 
248
 
                        if (files){
 
244
                if (newnum) {
 
245
 
 
246
                        if (files) {
 
247
                                void *tmp = realloc(files, (totnum+newnum) * sizeof(struct direntry));
 
248
                                if (tmp) {
 
249
                                        files = (struct direntry *)tmp;
 
250
                                }
 
251
                                else { /* realloc fail */
 
252
                                        free(files);
 
253
                                        files = NULL;
 
254
                                }
 
255
                        }
 
256
                        
 
257
                        if (files==NULL)
 
258
                                files=(struct direntry *)malloc(newnum * sizeof(struct direntry));
 
259
 
 
260
                        if (files) {
249
261
                                dlink = (struct dirlink *) dirbase->first;
250
 
                                while(dlink){
 
262
                                while (dlink) {
251
263
                                        memset(&files[actnum], 0 , sizeof(struct direntry));
252
264
                                        files[actnum].relname = dlink->name;
253
265
                                        files[actnum].path = BLI_strdupcat(dirname, dlink->name);
254
266
// use 64 bit file size, only needed for WIN32 and WIN64. 
255
 
// Excluding other than current MSVC compiler until able to test.
 
267
// Excluding other than current MSVC compiler until able to test
 
268
#ifdef WIN32
 
269
                                        {wchar_t * name_16 = alloc_utf16_from_8(dlink->name,0);
256
270
#if (defined(WIN32) || defined(WIN64)) && (_MSC_VER>=1500)
257
 
                                        _stat64(dlink->name,&files[actnum].s);
 
271
                                        _wstat64(name_16,&files[actnum].s);
 
272
#elif defined(__MINGW32__)
 
273
                                        _stati64(dlink->name,&files[actnum].s);
 
274
#endif
 
275
                                        free(name_16);};
 
276
 
258
277
#else
259
278
                                        stat(dlink->name,&files[actnum].s);
260
279
#endif
264
283
                                        actnum++;
265
284
                                        dlink = dlink->next;
266
285
                                }
267
 
                        } else{
 
286
                        }
 
287
                        else {
268
288
                                printf("Couldn't get memory for dir\n");
269
289
                                exit(1);
270
290
                        }
271
291
 
272
292
                        BLI_freelist(dirbase);
273
 
                        if (files) qsort(files, actnum, sizeof(struct direntry), (int (*)(const void *,const void*))BLI_compare);
274
 
                } else {
 
293
                        if (files) qsort(files, actnum, sizeof(struct direntry), (int (*)(const void *,const void*))bli_compare);
 
294
                }
 
295
                else {
275
296
                        printf("%s empty directory\n",dirname);
276
297
                }
277
298
 
278
299
                closedir(dir);
279
 
        } else {
 
300
        }
 
301
        else {
280
302
                printf("%s non-existant directory\n",dirname);
281
303
        }
282
304
}
283
305
 
284
 
void BLI_adddirstrings()
 
306
static void bli_adddirstrings(void)
285
307
{
286
308
        char datum[100];
287
309
        char buf[512];
288
310
        char size[250];
289
 
        static char * types[8] = {"---", "--x", "-w-", "-wx", "r--", "r-x", "rw-", "rwx"};
 
311
        static const char * types[8] = {"---", "--x", "-w-", "-wx", "r--", "r-x", "rw-", "rwx"};
290
312
        int num, mode;
291
313
#ifdef WIN32
292
314
        __int64 st_size;
298
320
        struct tm *tm;
299
321
        time_t zero= 0;
300
322
        
301
 
        for(num=0, file= files; num<actnum; num++, file++){
 
323
        for (num=0, file= files; num<actnum; num++, file++) {
302
324
#ifdef WIN32
303
325
                mode = 0;
304
 
                strcpy(file->mode1, types[0]);
305
 
                strcpy(file->mode2, types[0]);
306
 
                strcpy(file->mode3, types[0]);
 
326
                BLI_strncpy(file->mode1, types[0], sizeof(file->mode1));
 
327
                BLI_strncpy(file->mode2, types[0], sizeof(file->mode2));
 
328
                BLI_strncpy(file->mode3, types[0], sizeof(file->mode3));
307
329
#else
308
330
                mode = file->s.st_mode;
309
331
 
310
 
                strcpy(file->mode1, types[(mode & 0700) >> 6]);
311
 
                strcpy(file->mode2, types[(mode & 0070) >> 3]);
312
 
                strcpy(file->mode3, types[(mode & 0007)]);
 
332
                BLI_strncpy(file->mode1, types[(mode & 0700) >> 6], sizeof(file->mode1));
 
333
                BLI_strncpy(file->mode2, types[(mode & 0070) >> 3], sizeof(file->mode2));
 
334
                BLI_strncpy(file->mode3, types[(mode & 0007)], sizeof(file->mode3));
313
335
                
314
336
                if (((mode & S_ISGID) == S_ISGID) && (file->mode2[2]=='-'))file->mode2[2]='l';
315
337
 
316
 
                if (mode & (S_ISUID | S_ISGID)){
 
338
                if (mode & (S_ISUID | S_ISGID)) {
317
339
                        if (file->mode1[2]=='x') file->mode1[2]='s';
318
340
                        else file->mode1[2]='S';
319
341
 
320
342
                        if (file->mode2[2]=='x')file->mode2[2]='s';
321
343
                }
322
344
 
323
 
                if (mode & S_ISVTX){
 
345
                if (mode & S_ISVTX) {
324
346
                        if (file->mode3[2] == 'x') file->mode3[2] = 't';
325
347
                        else file->mode3[2] = 'T';
326
348
                }
334
356
                        pwuser = getpwuid(file->s.st_uid);
335
357
                        if ( pwuser ) {
336
358
                                BLI_strncpy(file->owner, pwuser->pw_name, sizeof(file->owner));
337
 
                        } else {
338
 
                                snprintf(file->owner, sizeof(file->owner), "%d", file->s.st_uid);
 
359
                        }
 
360
                        else {
 
361
                                BLI_snprintf(file->owner, sizeof(file->owner), "%d", file->s.st_uid);
339
362
                        }
340
363
                }
341
364
#endif
342
365
 
343
366
                tm= localtime(&file->s.st_mtime);
344
367
                // prevent impossible dates in windows
345
 
                if(tm==NULL) tm= localtime(&zero);
346
 
                strftime(file->time, 8, "%H:%M", tm);
347
 
                strftime(file->date, 16, "%d-%b-%y", tm);
 
368
                if (tm==NULL) tm= localtime(&zero);
 
369
                strftime(file->time, sizeof(file->time), "%H:%M", tm);
 
370
                strftime(file->date, sizeof(file->date), "%d-%b-%y", tm);
348
371
 
349
372
                /*
350
373
                 * Seems st_size is signed 32-bit value in *nix and Windows.  This
354
377
                st_size= file->s.st_size;
355
378
 
356
379
                if (st_size > 1024*1024*1024) {
357
 
                        sprintf(file->size, "%.2f GB", ((double)st_size)/(1024*1024*1024));     
 
380
                        BLI_snprintf(file->size, sizeof(file->size), "%.2f GB", ((double)st_size)/(1024*1024*1024));
358
381
                }
359
382
                else if (st_size > 1024*1024) {
360
 
                        sprintf(file->size, "%.1f MB", ((double)st_size)/(1024*1024));
 
383
                        BLI_snprintf(file->size, sizeof(file->size), "%.1f MB", ((double)st_size)/(1024*1024));
361
384
                }
362
385
                else if (st_size > 1024) {
363
 
                        sprintf(file->size, "%d KB", (int)(st_size/1024));
 
386
                        BLI_snprintf(file->size, sizeof(file->size), "%d KB", (int)(st_size/1024));
364
387
                }
365
388
                else {
366
 
                        sprintf(file->size, "%d B", (int)st_size);
 
389
                        BLI_snprintf(file->size, sizeof(file->size), "%d B", (int)st_size);
367
390
                }
368
391
 
369
 
                strftime(datum, 32, "%d-%b-%y %H:%M", tm);
 
392
                strftime(datum, 32, "%d-%b-%y %H:%M", tm); /* XXX, is this used? - campbell */
370
393
 
371
394
                if (st_size < 1000) {
372
 
                        sprintf(size, "%10d", (int) st_size);
373
 
                } else if (st_size < 1000 * 1000) {
374
 
                        sprintf(size, "%6d %03d", (int) (st_size / 1000), (int) (st_size % 1000));
375
 
                } else if (st_size < 100 * 1000 * 1000) {
376
 
                        sprintf(size, "%2d %03d %03d", (int) (st_size / (1000 * 1000)), (int) ((st_size / 1000) % 1000), (int) ( st_size % 1000));
377
 
                } else {
378
 
                        sprintf(size, "> %4.1f M", (double) (st_size / (1024.0 * 1024.0)));
379
 
                        sprintf(size, "%10d", (int) st_size);
380
 
                }
381
 
 
382
 
                sprintf(buf,"%s %s %s %7s %s %s %10s %s", file->mode1, file->mode2, file->mode3, file->owner, file->date, file->time, size,
383
 
                        file->relname);
384
 
 
385
 
                file->string=MEM_mallocN(strlen(buf)+1, "filestring");
386
 
                if (file->string){
387
 
                        strcpy(file->string,buf);
388
 
                }
 
395
                        BLI_snprintf(size, sizeof(size), "%10d",
 
396
                                     (int) st_size);
 
397
                }
 
398
                else if (st_size < 1000 * 1000) {
 
399
                        BLI_snprintf(size, sizeof(size), "%6d %03d",
 
400
                                     (int) (st_size / 1000), (int) (st_size % 1000));
 
401
                }
 
402
                else if (st_size < 100 * 1000 * 1000) {
 
403
                        BLI_snprintf(size, sizeof(size), "%2d %03d %03d",
 
404
                                     (int) (st_size / (1000 * 1000)), (int) ((st_size / 1000) % 1000), (int) ( st_size % 1000));
 
405
                }
 
406
                else {
 
407
                        /* XXX, whats going on here?. 2x calls - campbell */
 
408
                        BLI_snprintf(size, sizeof(size), "> %4.1f M", (double) (st_size / (1024.0 * 1024.0)));
 
409
                        BLI_snprintf(size, sizeof(size), "%10d", (int) st_size);
 
410
                }
 
411
 
 
412
                BLI_snprintf(buf, sizeof(buf), "%s %s %s %7s %s %s %10s %s",
 
413
                             file->mode1, file->mode2, file->mode3, file->owner,
 
414
                             file->date, file->time, size, file->relname);
 
415
 
 
416
                file->string = BLI_strdup(buf);
389
417
        }
390
418
}
391
419
 
392
 
unsigned int BLI_getdir(char *dirname,  struct direntry **filelist)
 
420
unsigned int BLI_dir_contents(const char *dirname,  struct direntry **filelist)
393
421
{
394
422
        // reset global variables
395
423
        // memory stored in files is free()'d in
396
424
        // filesel.c:freefilelist()
397
425
 
398
426
        actnum = totnum = 0;
399
 
        files = 0;
 
427
        files = NULL;
400
428
 
401
 
        BLI_builddir(dirname,"");
402
 
        BLI_adddirstrings();
 
429
        bli_builddir(dirname,"");
 
430
        bli_adddirstrings();
403
431
 
404
432
        if (files) {
405
433
                *(filelist) = files;
406
 
        } else {
 
434
        }
 
435
        else {
407
436
                // keep blender happy. Blender stores this in a variable
408
437
                // where 0 has special meaning.....
409
438
                *(filelist) = files = malloc(sizeof(struct direntry));
413
442
}
414
443
 
415
444
 
416
 
int BLI_filesize(int file)
 
445
size_t BLI_file_descriptor_size(int file)
417
446
{
418
447
        struct stat buf;
419
448
 
420
449
        if (file <= 0) return (-1);
421
 
        fstat(file, &buf);
 
450
        fstat(file, &buf);//CHANGE
422
451
        return (buf.st_size);
423
452
}
424
453
 
425
 
int BLI_filepathsize(const char *path)
 
454
size_t BLI_file_size(const char *path)
426
455
{
427
 
        int size, file = open(path, O_BINARY|O_RDONLY);
 
456
        int size, file = BLI_open(path, O_BINARY|O_RDONLY, 0);
428
457
        
429
 
        if (file < 0)
 
458
        if (file == -1)
430
459
                return -1;
431
460
        
432
 
        size = BLI_filesize(file);
 
461
        size = BLI_file_descriptor_size(file);
433
462
        close(file);
434
463
        return size;
435
464
}
436
465
 
437
466
 
438
 
int BLI_exist(char *name)
 
467
int BLI_exists(const char *name)
439
468
{
440
 
#if defined(WIN32) && !defined(__MINGW32__)
 
469
#if defined(WIN32) 
 
470
#ifndef __MINGW32__
441
471
        struct _stat64i32 st;
442
 
        /*  in Windows stat doesn't recognize dir ending on a slash 
443
 
                To not break code where the ending slash is expected we
444
 
                don't mess with the argument name directly here - elubie */
445
 
        char tmp[FILE_MAXDIR+FILE_MAXFILE];
 
472
#else
 
473
        struct _stati64 st;
 
474
#endif
 
475
        /* in Windows stat doesn't recognize dir ending on a slash
 
476
         * To not break code where the ending slash is expected we
 
477
         * don't mess with the argument name directly here - elubie */
 
478
        wchar_t * tmp_16 = alloc_utf16_from_8(name, 0);
446
479
        int len, res;
447
 
        BLI_strncpy(tmp, name, FILE_MAXDIR+FILE_MAXFILE);
448
 
        len = strlen(tmp);
449
 
        if (len > 3 && ( tmp[len-1]=='\\' || tmp[len-1]=='/') ) tmp[len-1] = '\0';
450
 
        res = _stat(tmp, &st);
 
480
        len = wcslen(tmp_16);
 
481
        if (len > 3 && ( tmp_16[len-1]==L'\\' || tmp_16[len-1]==L'/') ) tmp_16[len-1] = '\0';
 
482
#ifndef __MINGW32__
 
483
        res = _wstat(tmp_16, &st);
 
484
#else
 
485
        res = _wstati64(tmp_16, &st);
 
486
#endif
 
487
        free(tmp_16);
451
488
        if (res == -1) return(0);
452
 
#elif defined(WIN32) && defined(__MINGW32__)
453
 
        struct stat st;
454
 
        char tmp[FILE_MAXDIR+FILE_MAXFILE];
455
 
        int len, res;
456
 
        BLI_strncpy(tmp, name, FILE_MAXDIR+FILE_MAXFILE);
457
 
        len = strlen(tmp);
458
 
        if (len > 3 && ( tmp[len-1]=='\\' || tmp[len-1]=='/') ) tmp[len-1] = '\0';
459
 
        res = stat(tmp, &st);
460
 
        if (res) return(0);
461
489
#else
462
490
        struct stat st;
463
491
        if (stat(name,&st)) return(0);  
465
493
        return(st.st_mode);
466
494
}
467
495
 
 
496
 
 
497
#ifdef WIN32
 
498
int BLI_stat(const char *path, struct stat *buffer)
 
499
{
 
500
        int r;
 
501
        UTF16_ENCODE(path);
 
502
        r=_wstat(path_16,buffer);
 
503
        UTF16_UN_ENCODE(path);
 
504
        return r;
 
505
}
 
506
#else
 
507
int BLI_stat(const char *path, struct stat *buffer)
 
508
{
 
509
        return stat(path, buffer);
 
510
}
 
511
#endif
 
512
 
468
513
/* would be better in fileops.c except that it needs stat.h so add here */
469
 
int BLI_is_dir(char *file) {
470
 
        return S_ISDIR(BLI_exist(file));
471
 
}
472
 
 
473
 
LinkNode *BLI_read_file_as_lines(char *name)
474
 
{
475
 
        FILE *fp= fopen(name, "r");
 
514
int BLI_is_dir(const char *file)
 
515
{
 
516
        return S_ISDIR(BLI_exists(file));
 
517
}
 
518
 
 
519
int BLI_is_file(const char *path)
 
520
{
 
521
        int mode= BLI_exists(path);
 
522
        return (mode && !S_ISDIR(mode));
 
523
}
 
524
 
 
525
LinkNode *BLI_file_read_as_lines(const char *name)
 
526
{
 
527
        FILE *fp= BLI_fopen(name, "r");
476
528
        LinkNode *lines= NULL;
477
529
        char *buf;
478
 
        int size;
 
530
        size_t size;
479
531
 
480
532
        if (!fp) return NULL;
481
533
                
482
534
        fseek(fp, 0, SEEK_END);
483
 
        size= ftell(fp);
 
535
        size= (size_t)ftell(fp);
484
536
        fseek(fp, 0, SEEK_SET);
485
537
 
486
538
        buf= MEM_mallocN(size, "file_as_lines");
487
539
        if (buf) {
488
 
                int i, last= 0;
 
540
                size_t i, last= 0;
489
541
                
490
542
                        /* 
491
543
                         * size = because on win32 reading
511
563
        return lines;
512
564
}
513
565
 
514
 
void BLI_free_file_lines(LinkNode *lines)
 
566
void BLI_file_free_lines(LinkNode *lines)
515
567
{
516
568
        BLI_linklist_free(lines, (void(*)(void*)) MEM_freeN);
517
569
}
518
570
 
 
571
/** is file1 older then file2 */
519
572
int BLI_file_older(const char *file1, const char *file2)
520
573
{
 
574
#ifdef WIN32
 
575
        struct _stat st1, st2;
 
576
 
 
577
        UTF16_ENCODE(file1);
 
578
        UTF16_ENCODE(file2);
 
579
        
 
580
        if (_wstat(file1_16, &st1)) return 0;
 
581
        if (_wstat(file2_16, &st2)) return 0;
 
582
 
 
583
        UTF16_UN_ENCODE(file2);
 
584
        UTF16_UN_ENCODE(file1);
 
585
#else
521
586
        struct stat st1, st2;
522
587
 
523
 
        if(stat(file1, &st1)) return 0;
524
 
        if(stat(file2, &st2)) return 0;
525
 
 
 
588
        if (stat(file1, &st1)) return 0;
 
589
        if (stat(file2, &st2)) return 0;
 
590
#endif
526
591
        return (st1.st_mtime < st2.st_mtime);
527
592
}
528
593