/
SC_DirUtils.cpp
604 lines (498 loc) · 12.8 KB
/
SC_DirUtils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
/*
* Copyright (c) 2005 Tim Walters. All rights reserved.
* Created by Tim Walters on 10/19/05.
*
* 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
*
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdexcept>
#ifdef _WIN32
# include <direct.h>
# include "SC_Win32Utils.h"
#else
# include <unistd.h>
# include <dirent.h>
# include <glob.h>
# include <sys/param.h>
# include <sys/stat.h>
# include <sys/types.h>
#endif
#include "SC_DirUtils.h"
#if defined(__APPLE__) || defined(SC_IPHONE)
#ifndef _SC_StandAloneInfo_
# include "SC_StandAloneInfo_Darwin.h"
#endif
# include <CoreFoundation/CFString.h>
# include <CoreFoundation/CFBundle.h>
#ifndef SC_IPHONE
# include <CoreServices/CoreServices.h>
#endif
#endif
const char * gIdeName = "none";
// Add a component to a path.
void sc_AppendToPath(char *path, size_t max_size, const char *component)
{
size_t currentLength = strlen(path);
if (currentLength >= max_size-1)
return;
path[currentLength] = SC_PATH_DELIMITER[0];
path[currentLength+1] = 0;
++currentLength;
char * tail = path + currentLength;
size_t remain = max_size - currentLength;
strncat(tail, component, remain);
}
char *sc_StandardizePath(const char *path, char *newpath2)
{
char newpath1[MAXPATHLEN];
newpath1[0] = '\0';
newpath2[0] = '\0';
size_t pathLen = strlen(path);
if ((pathLen >= 2) && (path[0] == '~') && ((path[1] == '/') || (path[1] == '\\'))) {
char home[PATH_MAX];
sc_GetUserHomeDirectory(home, PATH_MAX);
if (home != 0) {
if ((pathLen - 1 + strlen(home)) >= MAXPATHLEN) {
return 0;
}
strcpy(newpath1, home);
strcat(newpath1, path + 1);
} else {
if (pathLen >= MAXPATHLEN) {
return 0;
}
strcpy(newpath1, path);
newpath1[0] = '.';
}
} else {
if (pathLen >= MAXPATHLEN) {
return 0;
}
strcpy(newpath1, path);
}
bool isAlias = false;
if(sc_ResolveIfAlias(newpath1, newpath2, isAlias, PATH_MAX)!=0) {
strcpy(newpath2, newpath1);
}
return newpath2;
}
// Returns TRUE iff dirname is an existing directory.
bool sc_DirectoryExists(const char *dirname)
{
#if defined(_WIN32)
DWORD attr = GetFileAttributes(dirname);
return ((attr != INVALID_FILE_ATTRIBUTES) &&
(attr & FILE_ATTRIBUTE_DIRECTORY));
#else
struct stat buf;
return ((stat(dirname, &buf) == 0) &&
S_ISDIR(buf.st_mode));
#endif
}
bool sc_IsSymlink(const char* path)
{
#if defined(_WIN32)
// FIXME
return false;
#else
struct stat buf;
return ((stat(path, &buf) == 0) &&
S_ISLNK(buf.st_mode));
#endif
}
bool sc_IsNonHostPlatformDir(const char *name)
{
#if defined(SC_IPHONE)
const char a[] = "linux", b[] = "windows", c[]="osx";
#elif defined(__APPLE__)
const char a[] = "linux", b[] = "windows", c[]="iphone";
#elif defined(__linux__)
const char a[] = "osx", b[] = "windows", c[]="iphone";
#elif defined(__FreeBSD__)
const char a[] = "osx", b[] = "windows", c[]="iphone";
#elif defined(_WIN32)
const char a[] = "osx", b[] = "linux", c[]="iphone";
#endif
return ((strcmp(name, a) == 0) ||
(strcmp(name, b) == 0) ||
(strcmp(name, c) == 0));
}
// Returns TRUE iff 'name' is special directory '.' or '..'
inline static bool sc_IsSpecialDirectory(const char* name)
{
return (strcmp(name, ".") == 0) || (strcmp(name, "..") == 0);
}
// Returns TRUE iff 'name' is to be ignored during compilation.
bool sc_SkipDirectory(const char *name)
{
return (stringCaseCompare(name, "help") ||
stringCaseCompare(name, "ignore") ||
(strcmp(name, ".svn") == 0) ||
(strcmp(name, ".git") == 0) ||
(strcmp(name, "_darcs") == 0) ||
((strncmp(name, "scide_", 6) == 0) && (strcmp(name+6, gIdeName) != 0)) ||
sc_IsNonHostPlatformDir(name));
}
int sc_ResolveIfAlias(const char *path, char *returnPath, bool &isAlias, int length)
{
isAlias = false;
#if defined(__APPLE__) && !defined(SC_IPHONE)
FSRef dirRef;
OSStatus osStatusErr = FSPathMakeRef ((const UInt8 *) path, &dirRef, NULL);
if ( !osStatusErr ) {
Boolean isFolder;
Boolean wasAliased;
OSErr err = FSResolveAliasFile (&dirRef, true, &isFolder, &wasAliased);
if (err)
{
return -1;
}
isAlias = wasAliased;
if (wasAliased)
{
UInt8 resolvedPath[PATH_MAX];
osStatusErr = FSRefMakePath (&dirRef, resolvedPath, length);
if (osStatusErr)
{
return -1;
}
strncpy(returnPath, (char *) resolvedPath, length);
return 0;
}
}
#endif
strcpy(returnPath, path);
return 0;
}
// Support for Bundles
#if defined(__APPLE__) && !defined(SC_IPHONE) // running on OS X
// Support for stand-alone applications
bool sc_IsStandAlone()
{
return SC_StandAloneInfo::IsStandAlone();
}
void sc_GetResourceDirectory(char* pathBuf, int length)
{
SC_StandAloneInfo::GetResourceDir(pathBuf, length);
}
void sc_AppendBundleName(char *str, int size)
{
CFBundleRef mainBundle;
mainBundle = CFBundleGetMainBundle();
if(mainBundle){
CFDictionaryRef dictRef = CFBundleGetInfoDictionary(mainBundle);
CFStringRef strRef;
strRef = (CFStringRef)CFDictionaryGetValue(dictRef, CFSTR("CFBundleName"));
if(strRef){
const char *bundleName = CFStringGetCStringPtr(strRef, CFStringGetSystemEncoding());
if(bundleName) {
sc_AppendToPath(str, size, bundleName);
return;
}
}
}
sc_AppendToPath(str, size, "SuperCollider");
}
#elif defined(SC_IPHONE)
bool sc_IsStandAlone()
{
return false;
}
void sc_GetResourceDirectory(char* pathBuf, int length)
{
sc_GetUserAppSupportDirectory(pathBuf, length);
}
#elif defined(__unix__)
bool sc_IsStandAlone()
{
return false;
}
void sc_GetResourceDirectory(char* pathBuf, int length)
{
#ifdef SC_DATA_DIR
strncpy(pathBuf, SC_DATA_DIR, length);
#else
strncpy(pathBuf, "/usr/share/SuperCollider", length);
#endif
}
#else
bool sc_IsStandAlone()
{
return false;
}
static void sc_GetResourceDirectoryFromAppDirectory(char* pathBuf, int length)
{
char * result = getcwd(pathBuf, length);
if (result != pathBuf)
throw std::runtime_error("cannot get current working directory");
}
void sc_GetResourceDirectory(char* pathBuf, int length)
{
return sc_GetResourceDirectoryFromAppDirectory(pathBuf, length);
}
#endif
// Support for Extensions
// Get the user home directory.
void sc_GetUserHomeDirectory(char *str, int size)
{
#ifndef _WIN32
const char *home = getenv("HOME");
if(home!=NULL){
strncpy(str, home, size);
}else{
// cwd is not the user home directory; but this is better than leaving mem uninitialised.
strcpy(str, "./");
}
#else
win32_GetHomeFolder(str,size);
#endif
}
// Get the System level data directory.
void sc_GetSystemAppSupportDirectory(char *str, int size)
{
strncpy(str,
#if defined(SC_DATA_DIR)
SC_DATA_DIR,
#elif defined(SC_IPHONE)
"/",
#elif defined(__APPLE__)
"/Library/Application Support",
#elif defined(_WIN32)
( getenv("SC_SYSAPPSUP_PATH")==NULL ) ? "C:\\SuperCollider" : getenv("SC_SYSAPPSUP_PATH"),
#else
"/usr/local/share/SuperCollider",
#endif
size);
#if defined(__APPLE__)
// Get the main bundle name for the app from the enclosed Info.plist
sc_AppendBundleName(str, size);
#endif
}
// Get the User level data directory.
void sc_GetUserAppSupportDirectory(char *str, int size)
{
// XDG support according to http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
const char * xdg_data_home = getenv("XDG_DATA_HOME");
if (xdg_data_home) {
strncpy(str, xdg_data_home, size);
sc_AppendToPath(str, size, "SuperCollider");
return;
}
sc_GetUserHomeDirectory(str, size);
#if defined(SC_IPHONE)
sc_AppendToPath(str, size, "Documents");
#elif defined(__APPLE__)
// Get the main bundle name for the app
sc_AppendToPath(str, size, "Library/Application Support");
sc_AppendBundleName(str, size);
#elif defined(_WIN32)
sc_AppendToPath(str, size, "SuperCollider");
#else
sc_AppendToPath(str, size, ".local/share/SuperCollider");
#endif
}
// Get the System level 'Extensions' directory.
void sc_GetSystemExtensionDirectory(char *str, int size)
{
sc_GetSystemAppSupportDirectory(str, size);
sc_AppendToPath(str, size, "Extensions");
}
// Get the System level 'Extensions' directory.
void sc_GetUserExtensionDirectory(char *str, int size)
{
sc_GetUserAppSupportDirectory(str, size);
sc_AppendToPath(str, size, "Extensions");
}
// Get the directory for the configuration files.
void sc_GetUserConfigDirectory(char *str, int size)
{
// XDG support according to http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
const char * xdg_config_home = getenv("XDG_CONFIG_HOME");
if (xdg_config_home) {
strncpy(str, xdg_config_home, size);
sc_AppendToPath(str, size, "SuperCollider");
return;
}
#if defined(__linux__) || defined(__freebsd__)
sc_GetUserHomeDirectory(str, size);
sc_AppendToPath(str, size, ".config/SuperCollider");
#else
sc_GetUserAppSupportDirectory(str, size);
#endif
}
// Directory access
struct SC_DirHandle
{
#ifdef _WIN32
HANDLE mHandle;
WIN32_FIND_DATA mEntry;
bool mAtEnd;
#else
DIR* mHandle;
struct dirent* mEntry;
#endif
};
SC_DirHandle* sc_OpenDir(const char* dirname)
{
SC_DirHandle* dir = new SC_DirHandle;
memset(dir, 0, sizeof(SC_DirHandle));
#ifdef _WIN32
char allInDir[PATH_MAX];
snprintf(allInDir, PATH_MAX, "%s\\*.*", dirname);
dir->mHandle = ::FindFirstFile(allInDir, &dir->mEntry);
if (dir->mHandle == INVALID_HANDLE_VALUE) {
delete dir;
return 0;
}
dir->mAtEnd = false;
#else
dir->mHandle = opendir(dirname);
if (!dir->mHandle) {
delete dir;
return 0;
}
#endif
return dir;
}
void sc_CloseDir(SC_DirHandle* dir)
{
#ifdef _WIN32
::FindClose(dir->mHandle);
#else
closedir(dir->mHandle);
#endif
delete dir;
}
bool sc_ReadDir(SC_DirHandle* dir, const char* dirname, char* path, bool& skipEntry)
{
#ifdef _WIN32
bool success = true;
if (dir->mAtEnd)
return false;
const char* entry = dir->mEntry.cFileName;
if (sc_IsSpecialDirectory(entry) || (skipEntry && sc_SkipDirectory(entry))) {
skipEntry = true;
success = true;
} else {
skipEntry = false;
success = true;
}
char entrypathname[PATH_MAX];
strncpy(entrypathname, dirname, PATH_MAX);
sc_AppendToPath(entrypathname, PATH_MAX, dir->mEntry.cFileName);
bool isAlias = false;
sc_ResolveIfAlias(entrypathname, path, isAlias, PATH_MAX);
if (!::FindNextFile(dir->mHandle, &dir->mEntry)) {
dir->mAtEnd = true;
}
return true;
#else
if (!dir->mHandle)
return false;
dir->mEntry = readdir(dir->mHandle);
if (!dir->mEntry)
return false;
const char* entry = dir->mEntry->d_name;
if (sc_IsSpecialDirectory(entry) || (skipEntry && sc_SkipDirectory(entry))) {
skipEntry = true;
return true;
} else {
skipEntry = false;
}
// construct path from dir entry
char entrypathname[PATH_MAX];
strncpy(entrypathname, dirname, PATH_MAX);
sc_AppendToPath(entrypathname, PATH_MAX, dir->mEntry->d_name);
// resolve path
bool isAlias = false;
if (sc_ResolveIfAlias(entrypathname, path, isAlias, strlen(entrypathname))<0)
{
skipEntry = true;
}
return true;
#endif
}
// Globbing
struct SC_GlobHandle
{
#ifdef _WIN32
HANDLE mHandle;
char mFolder[PATH_MAX];
WIN32_FIND_DATA mEntry;
char mEntryPath[PATH_MAX];
bool mAtEnd;
#else
glob_t mHandle;
size_t mEntry;
#endif
};
SC_GlobHandle* sc_Glob(const char* pattern)
{
SC_GlobHandle* glob = new SC_GlobHandle;
#ifdef _WIN32
char patternWin[1024];
strncpy(patternWin, pattern, 1024);
patternWin[1023] = 0;
win32_ReplaceCharInString(patternWin, 1024, '/', '\\');
win32_ExtractContainingFolder(glob->mFolder, patternWin, PATH_MAX);
glob->mHandle = ::FindFirstFile(patternWin, &glob->mEntry);
if (glob->mHandle == INVALID_HANDLE_VALUE) {
delete glob;
return 0;
}
glob->mAtEnd = false;
#else
int flags = GLOB_MARK | GLOB_TILDE;
#ifdef __APPLE__
flags |= GLOB_QUOTE;
#endif
int err = ::glob(pattern, flags, NULL, &glob->mHandle);
if (err < 0) {
delete glob;
return 0;
}
glob->mEntry = 0;
#endif
return glob;
}
void sc_GlobFree(SC_GlobHandle* glob)
{
#ifdef _WIN32
::FindClose(glob->mHandle);
#else
globfree(&glob->mHandle);
#endif
delete glob;
}
const char* sc_GlobNext(SC_GlobHandle* glob)
{
#ifdef _WIN32
if (glob->mAtEnd)
return 0;
strncpy(glob->mEntryPath, glob->mFolder, PATH_MAX);
sc_AppendToPath(glob->mEntryPath, PATH_MAX, glob->mEntry.cFileName);
if (!::FindNextFile(glob->mHandle, &glob->mEntry))
glob->mAtEnd = true;
return glob->mEntryPath;
#else
if (glob->mEntry >= glob->mHandle.gl_pathc)
return 0;
return glob->mHandle.gl_pathv[glob->mEntry++];
#endif
}