Skip to content

Commit

Permalink
Implemented system default config file locations. Had to pull in the …
Browse files Browse the repository at this point in the history
…CoreFoundation framework on mac so we can access the app bundle, and fixed the path separator for windows to make ExtractDirectory work
  • Loading branch information
Joe Sadusk committed May 31, 2012
1 parent 2f5ce36 commit adf561c
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 3 deletions.
110 changes: 109 additions & 1 deletion src/mgl/abstractable.cc
Expand Up @@ -16,10 +16,19 @@ using namespace std;

#include "log.h"

#ifdef __APPLE__
#include <CFBundle.h>
#endif

#ifdef WIN32
#include <Windows.h>
#include <Shlobj.h>
#else
#include <sys/types.h>
#include <pwd.h>
#endif

#include <fstream>

std::ostream &MyComputer::log()
{
Expand Down Expand Up @@ -79,7 +88,11 @@ string FileSystemAbstractor::pathJoin(string path, string filename) const

char FileSystemAbstractor::getPathSeparatorCharacter() const
{
return '/'; // Linux & Mac, works on Windows most times
#ifdef WIN32
return '\\';
#else
return '/'; // Linux & Mac, works on Windows most times
#endif
}

string FileSystemAbstractor::ExtractDirectory(const char *directoryPath) const
Expand Down Expand Up @@ -111,6 +124,101 @@ string FileSystemAbstractor::removeExtension(const char *filename) const
+ filenameStr.substr(0, filenameStr.find_last_of('.'));
}

bool FileSystemAbstractor::fileReadable(const char *filename) const {
ifstream testfile(filename, ifstream::in);
bool readable = !testfile.fail();
testfile.close();

return readable;
}

string FileSystemAbstractor::getConfigFile(const char *filename) const {
string found = getUserConfigFile(filename);
if (found.length() > 0 && fileReadable(found.c_str()))
return found;

found = getSystemConfigFile(filename);
if (found.length() > 0 && fileReadable(found.c_str()))
return found;

return string();
}

string FileSystemAbstractor::getSystemConfigFile(const char *filename) const {
#ifdef WIN32
char app_path[1024];

GetModuleFileNameA(0, app_path, sizeof(app_path) - 1);

// Extract directory
std::string app_dir = ExtractDirectory(app_path);
return pathJoin(app_dir, filename);

#elif defined __APPLE__
CFBundleRef mainBundle;

mainBundle = CFBundleGetMainBundle();
if(!mainBundle)
return string();

CFURLRef resourceURL;
resourceURL =
CFBundleCopyResourceURL(mainBundle,
CFStringCreateWithCString(NULL,
filename,
kCFStringEncodingASCII),

NULL,
NULL);

if (!resourceURL)
return string();

char fileurl[1024];
if(!CFURLGetFileSystemRepresentation(resourceURL,
true,
(UInt8*)
fileurl,
1024)) {
return string();
p }

return string(fileurl);
#else //linux

return pathJoin(string("/etc/xdg/makerbot/"), filename);
#endif
}

string FileSystemAbstractor::getUserConfigFile(const char *filename) const {
#ifdef WIN32
char app_data[1024];
if(SUCCEEDED(SHGetFolderPathA(NULL, CSIDL_LOCAL_APPDATA, NULL, 0,
app_data))) {
return pathJoin(pathJoin(string(app_data), "Makerbot"),
filename);
}
#else //linux or apple
char pwbuff[1024];
struct passwd pw;
struct passwd *tempptr;
if (getpwuid_r(getuid(), &pw, pwbuff, 1024, &tempptr) == 0) {

#ifdef __APPLE__
return pathJoin(pathJoin(string(pw.pw_dir),
"Library/Preferences/Makerbot/Miracle-Grue"),
filename);
#else //linux
return pathJoin(pathJoin(string(pw.pw_dir), ".config/makerbot"),
filename);
#endif //linux
}

#endif //win32

return string();
}

ProgressLog::ProgressLog(unsigned int count)
:ProgressBar(count,"")
{
Expand Down
5 changes: 5 additions & 0 deletions src/mgl/abstractable.h
Expand Up @@ -63,7 +63,12 @@ class FileSystemAbstractor
::std::string pathJoin(std::string path, std::string filename) const;

int guarenteeDirectoryExists(const char* dirPath);
bool fileReadable(const char *filename) const;

::std::string getConfigFile(const char *filename) const;
private:
::std::string getUserConfigFile(const char *filename) const;
::std::string getSystemConfigFile(const char *filename) const;
};


Expand Down
9 changes: 8 additions & 1 deletion src/mgl/configuration.cc
Expand Up @@ -18,12 +18,19 @@
#include "configuration.h"
#include "gcoder.h"
#include "pather.h"
#include "abstractable.h"

using namespace mgl;
using namespace std;

string Configuration::defaultFilename() {
return string("miracle.config");
MyComputer computer;
string file = computer.fileSystem.getConfigFile("miracle.config");

if (file.length() > 0)
return file;
else
return string("miracle.config");
}


Expand Down
4 changes: 3 additions & 1 deletion src/mgl/mgl.pro.inc
Expand Up @@ -5,7 +5,9 @@
#
#-------------------------------------------------


mac {
INCLUDEPATH += /System/Library/Frameworks/CoreFoundation.framework/Versions/Current/Headers
}

QMAKE_CXXFLAGS += -fopenmp

Expand Down

0 comments on commit adf561c

Please sign in to comment.