Skip to content

Commit

Permalink
Saving all changes so far
Browse files Browse the repository at this point in the history
  • Loading branch information
mossheim committed Jul 24, 2017
1 parent e8c400e commit c8d613a
Show file tree
Hide file tree
Showing 21 changed files with 478 additions and 331 deletions.
2 changes: 1 addition & 1 deletion SCDoc/SCDocPrim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "PyrPrimitive.h"
#include "PyrSymbol.h"
#include "SCBase.h"
#include "SC_DirUtils.h"
//#include "SC_DirUtils.h" // TODO_BRIAN: no longer needed, delete this

#include <stdlib.h>
#include <string.h>
Expand Down
162 changes: 156 additions & 6 deletions common/SC_DirUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,22 @@
#endif
#endif

const char * gIdeName = "none";
#include <boost/filesystem/path.hpp>

// TODO_BRIAN: create an enum for this
const char* gIdeName = "none";
const char* DOT_CONFIG = ".config";
const char* CWD = "./";
const char* SUPERCOLLIDER_DIR_NAME = "SuperCollider";
const char* DOCUMENTS_DIR_NAME = "Documents";
const char* LIBRARY_DIR_NAME = "Library";
const char* APPLICATION_SUPPORT_DIR_NAME = "Application Support";
const char* SHARE_DIR_NAME = "share";
const char* DOT_LOCAL = ".local";

// Add a component to a path.

#if 0
void sc_AppendToPath(char *path, size_t max_size, const char *component)
{
size_t currentLength = strlen(path);
Expand All @@ -72,8 +84,9 @@ void sc_AppendToPath(char *path, size_t max_size, const char *component)

strncat(tail, component, remain);
}
#endif


#if 0
char *sc_StandardizePath(const char *path, char *newpath2)
{
char newpath1[MAXPATHLEN];
Expand All @@ -83,6 +96,7 @@ char *sc_StandardizePath(const char *path, char *newpath2)

size_t pathLen = strlen(path);

// expand tilde
if ((pathLen >= 2) && (path[0] == '~') && ((path[1] == '/') || (path[1] == '\\'))) {
char home[PATH_MAX];
sc_GetUserHomeDirectory(home, PATH_MAX);
Expand Down Expand Up @@ -114,8 +128,31 @@ char *sc_StandardizePath(const char *path, char *newpath2)

return newpath2;
}
#endif

SC_DirUtils::Path SC_DirUtils::standardizePath(const Path& p)
{
Path ret;
Path::const_iterator piter = p.begin();

if (piter != p.end() && *piter == "~") {
ret = getUserHomeDirectory();
while (++piter != p.end())
ret /= *piter;
}

#if defined(__APPLE__) && !defined(SC_IPHONE)
bool ok;
Path resolved = resolveIfAlias(ret, ok);
if (ok)
return resolved;
#endif

return ret;
}


#if 0
// Returns TRUE iff dirname is an existing directory.

bool sc_DirectoryExists(const char *dirname)
Expand Down Expand Up @@ -143,6 +180,7 @@ bool sc_IsSymlink(const char* path)
S_ISLNK(buf.st_mode));
#endif
}
#endif // 0

bool sc_IsNonHostPlatformDir(const char *name)
{
Expand Down Expand Up @@ -183,7 +221,7 @@ bool sc_SkipDirectory(const char *name)
sc_IsNonHostPlatformDir(name));
}


#if 0
int sc_ResolveIfAlias(const char *path, char *returnPath, bool &isAlias, int length)
{
isAlias = false;
Expand Down Expand Up @@ -222,6 +260,46 @@ int sc_ResolveIfAlias(const char *path, char *returnPath, bool &isAlias, int len
strcpy(returnPath, path);
return 0;
}
#endif

#if defined(__APPLE__) && !defined(SC_IPHONE)
SC_DirUtils::Path SC_DirUtils::resolveIfAlias(const SC_DirUtils::Path& p, bool& ok)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *nsstringPath = [NSString stringWithCString: p.c_str() encoding: NSUTF8StringEncoding];
BOOL isDirectory;
// does the file exist? If not just copy and bail
if([[NSFileManager defaultManager] fileExistsAtPath: nsstringPath isDirectory: &isDirectory]) {
NSError *error;

NSData *bookmark = [NSURL bookmarkDataWithContentsOfURL: [NSURL fileURLWithPath:nsstringPath isDirectory: isDirectory] error: &error];

// is it an alias? If not just copy and bail
if(bookmark) {
NSError *resolvedURLError;
BOOL isStale;
NSURL *resolvedURL = [NSURL URLByResolvingBookmarkData: bookmark options: NSURLBookmarkResolutionWithoutUI relativeToURL: nil bookmarkDataIsStale: &isStale error: &resolvedURLError];
// does it actually lead to something?
if(isStale || (resolvedURL == nullptr) ) {
printf("Error: Target missing for alias at %s\n", p.c_str());
ok = false;
return Path();
}

NSString *resolvedString = [resolvedURL path];

const char *resolvedPath = [resolvedString cStringUsingEncoding: NSUTF8StringEncoding];
ok = true;
return Path(resolvedPath);
}
}

[pool release];

return p;
}

#endif

// Support for Bundles

Expand All @@ -234,12 +312,14 @@ bool sc_IsStandAlone()
return SC_StandAloneInfo::IsStandAlone();
}

void sc_GetResourceDirectory(char* pathBuf, int length)
boost::filesystem::path sc_GetResourceDirectory()
{
SC_StandAloneInfo::GetResourceDir(pathBuf, length);
char pathBuf[PATH_MAX];
SC_StandAloneInfo::GetResourceDir(pathBuf, PATH_MAX);
return boost::filesystem::path(pathBuf);
}


#if 0
void sc_AppendBundleName(char *str, int size)
{
CFBundleRef mainBundle;
Expand All @@ -258,6 +338,25 @@ void sc_AppendBundleName(char *str, int size)
}
sc_AppendToPath(str, size, "SuperCollider");
}
#endif

const char* SC_DirUtils::getBundleName()
{
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) {
return bundleName;
}
}
}
return SUPERCOLLIDER_DIR_NAME;
}

#elif defined(SC_IPHONE)

Expand Down Expand Up @@ -328,6 +427,7 @@ void sc_GetResourceDirectory(char* pathBuf, int length)

// Get the user home directory.

// TODO_BRIAN: move this into separate implementation files
void sc_GetUserHomeDirectory(char *str, int size)
{
#ifndef _WIN32
Expand All @@ -343,6 +443,20 @@ void sc_GetUserHomeDirectory(char *str, int size)
#endif
}

const SC_DirUtils::Path SC_DirUtils::getUserHomeDirectory()
{
#ifdef _WIN32
char *str;
return SC_DirUtils::Path(win32_GetKnownFolderPath(CSIDL_PROFILE, str, PATH_MAX));
#else
const char *home = getenv("HOME");
if (home)
return SC_DirUtils::Path(home);
else // should throw an error here
return SC_DirUtils::Path(CWD);
#endif // _WIN32
}


// Get the System level data directory.

Expand Down Expand Up @@ -406,6 +520,29 @@ void sc_GetUserAppSupportDirectory(char *str, int size)
#endif
}

// TODO_BRIAN: move this out :(
const SC_DirUtils::Path SC_DirUtils::getUserAppSupportDirectory()
{
// TODO_BRIAN: XDG isn't really a thing on windows... is it?
const char *xdgDataHome = getenv("XDG_DATA_HOME");
if (xdgDataHome)
return Path(xdgDataHome) / SUPERCOLLIDER_DIR_NAME;

#ifdef _WIN32
char *str;
win32_GetKnownFolderPath(CSIDL_LOCAL_APPDATA, str, MAX_PATH);
return SC_DirUtils::Path(str) / SUPERCOLLIDER_DIR_NAME;
#else
# ifdef SC_IPHONE
return getUserHomeDirectory() / DOCUMENTS_DIR_NAME;
# elif defined(__APPLE__)
return getUserHomeDirectory() / LIBRARY_DIR_NAME / APPLICATION_SUPPORT_DIR_NAME / getBundleName();
# else
return getUserHomeDirectory() / DOT_LOCAL / SHARE_DIR_NAME / SUPERCOLLIDER_DIR_NAME;
# endif // SC_IPHONE / __APPLE__
#endif // _WIN32
}


// Get the System level 'Extensions' directory.

Expand Down Expand Up @@ -443,6 +580,19 @@ void sc_GetUserConfigDirectory(char *str, int size)
#endif
}

const SC_DirUtils::Path SC_DirUtils::getUserConfigDirectory()
{
const char * xdgConfigHome = getenv("XDG_CONFIG_HOME");
if (xdgConfigHome) {
return Path(xdgConfigHome) / SUPERCOLLIDER_DIR_NAME;
}
#if defined(__linux__) || defined(__freebsd__)
return getUserHomeDirectory() / DOT_CONFIG / SUPERCOLLIDER_DIR_NAME;
#else
return getUserAppSupportDirectory();
#endif // __linux__ || __freebsd__
}


// Directory access

Expand Down
Loading

0 comments on commit c8d613a

Please sign in to comment.