OSDN Git Service

first
[psychlops/cpp.git] / psychlops / platform / osx / psychlops_io_file_OSX.cpp
1 /*
2  *  psychlops_io_file_OSX.cpp
3  *  Psychlops Standard Library (MacOSX)
4  *
5  *  Last Modified 2006/01/05 by Kenchi HOSOKAWA
6  *  (C) 2006 Kenchi HOSOKAWA, Kazushi MARUYA, Takao SATO
7  */
8
9 #include <Carbon/Carbon.h>
10 #include <ApplicationServices/ApplicationServices.h>
11 #include <CoreServices/CoreServices.h>
12
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <unistd.h>
16 #include <time.h>
17
18 #include <string>
19
20 #include "../../core/ApplicationInterfaces/psychlops_app.h"
21 #include "../../core/devices/psychlops_io_file.h"
22
23
24 namespace Psychlops {
25
26         const char File::PATH_DEVIDER = '/';
27
28         void File::initialize() {
29                 const int BUFSIZE = 1024;
30                 char *buf = new char[BUFSIZE];
31                 CFStringRef apppath = CFURLCopyFileSystemPath(CFBundleCopyBundleURL(CFBundleGetMainBundle()), kCFURLPOSIXPathStyle);
32 //              CFStringRef apppath = CFURLCopyPath(CFBundleCopyBundleURL(CFBundleGetMainBundle()));
33                 CFStringGetCString(apppath, buf, BUFSIZE, kCFStringEncodingUTF8);
34                 STANDARD_PATH[PATH_APP].path = buf;
35                 STANDARD_PATH[PATH_RSC].path = STANDARD_PATH[PATH_APP].path + "/Contents/Resources/";
36                 STANDARD_PATH[PATH_APP].path += "/../";
37 //              char * home = getenv("USER");
38                 CFStringRef username = CSCopyUserName(true);
39                 STANDARD_PATH[PATH_HOME].path = "/Users/";
40                 CFStringGetCString(username, buf, BUFSIZE, kCFStringEncodingUTF8);
41                 STANDARD_PATH[PATH_HOME].path += buf;
42                 STANDARD_PATH[PATH_HOME].path += "/";
43                 STANDARD_PATH[PATH_DOCUMENTS_ROOT].path = STANDARD_PATH[PATH_HOME].path + "Documents/";
44
45                 initialize_appname();
46
47                 setCurrentDirectoryDefault();
48                 delete buf;
49         }
50
51         void File::initialize_appname() {
52                 if(!appname_initialized) {
53                         STANDARD_PATH[PATH_SETTING].path = STANDARD_PATH[PATH_HOME].path + "Library/" + AppInfo::appname + "/";
54                         STANDARD_PATH[PATH_DOCUMENTS].path = STANDARD_PATH[PATH_DOCUMENTS_ROOT].path + AppInfo::appname + "/";
55                         //if(AppInfo::appname=="Psychlops" && AppInfo::expname!="") {
56                         //      STANDARD_PATH[PATH_SETTING].path += AppInfo::expname + "/";
57                         //      STANDARD_PATH[PATH_DOCUMENTS].path += AppInfo::expname + "/";
58                         //}
59                         appname_initialized = true;
60                 }
61         }
62
63
64         std::string File::getCurrentDirectory() {
65                 char buf[1024];
66                 if(getcwd(buf, 1024) == 0) Exception("failed to get current directory name.");
67                 return std::string(buf);
68         }
69
70         void File::setCurrentDirectory(const std::string &path, bool force_mkdir) {
71                 if(chdir(path.c_str()) && force_mkdir) {
72                         mkdir(path.c_str(), 0700);
73                         chdir(path.c_str());
74                 }
75         }
76
77         void File::setCurrentDirectoryDefault() {
78                 setCurrentDirectory(STANDARD_PATH[PATH_DOCUMENTS].path, true);
79         }
80
81         std::string File::replacePathDevider(const std::string &path) {
82                 std::string decoded_path = path;
83                 unsigned int found, restart;
84                 for(;;) {
85                         found=decoded_path.find('\\',restart);
86                         if(found==std::string::npos) { break; }
87                         decoded_path.replace(found, 1, 1, PATH_DEVIDER);
88                         restart = found;
89                 }
90                 return decoded_path;
91         }
92
93
94 }       /*      <- namespace Psycholops         */