2 * psychlops_io_file_Win32.cpp
3 * Psychlops Standard Library (Win32)
5 * Last Modified 2006/01/05 by Kenchi HOSOKAWA
6 * (C) 2006 Kenchi HOSOKAWA, Kazushi MARUYA, Takao SATO
12 #include "../../core/ApplicationInterfaces/psychlops_app.h"
13 #include "../../core/devices/psychlops_io_file.h"
18 const char File::PATH_DEVIDER = '\\';
\r
20 void File::initialize() {
\r
22 char * apppath = new char[1024];
23 for(int i=0; i<1024; i++) apppath[i] = 0;
24 GetModuleFileName(NULL, apppath, 1024);
\r
25 STANDARD_PATH[PATH_APP].path = apppath;
\r
26 loc = STANDARD_PATH[PATH_APP].path.find_last_of('\\');
\r
27 STANDARD_PATH[PATH_APP].path.erase(loc+1);
\r
28 STANDARD_PATH[PATH_RSC].path = STANDARD_PATH[PATH_APP].path + "Resources\\";
\r
29 GetEnvironmentVariable("USERPROFILE", apppath, 1024);
30 STANDARD_PATH[PATH_HOME].path = apppath;
\r
31 STANDARD_PATH[PATH_HOME].path += "\\";
\r
32 GetEnvironmentVariable("APPDATA", apppath, 1024);
\r
33 STANDARD_PATH[PATH_SETTING].path = apppath;
\r
34 STANDARD_PATH[PATH_SETTING].path += "\\";
\r
37 BYTE * documentspath = new BYTE[1024];
\r
38 for(int i=0; i<1024; i++) documentspath[i] = 0;
\r
40 unsigned long lpReserved = 0, lpcbData = 1024;
\r
41 LONG err = RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders", 0, KEY_QUERY_VALUE, &phk);
\r
42 if(err!=0) Exception(typeid(File), "", "Failed to get user home path.");;
\r
43 RegQueryValueEx(phk, "Personal", NULL, NULL, documentspath, &lpcbData);
\r
45 STANDARD_PATH[PATH_DOCUMENTS_ROOT].path.assign(reinterpret_cast<char *>(documentspath));
\r
46 STANDARD_PATH[PATH_DOCUMENTS_ROOT].path += "\\";
\r
47 delete [] documentspath;
\r
49 initialize_appname();
\r
52 void File::initialize_appname() {
\r
53 if(!appname_initialized) {
54 STANDARD_PATH[PATH_SETTING].path += AppInfo::appname + '\\';
\r
55 STANDARD_PATH[PATH_DOCUMENTS].path = STANDARD_PATH[PATH_DOCUMENTS_ROOT].path + AppInfo::appname + '\\';
56 //if(AppInfo::appname=="Psychlops" && AppInfo::expname!="") {
\r
57 // STANDARD_PATH[PATH_SETTING].path += AppInfo::expname + '\\';
\r
58 // STANDARD_PATH[PATH_DOCUMENTS].path += AppInfo::expname + '\\';
\r
60 appname_initialized = true;
\r
64 std::string File::getCurrentDirectory() {
\r
65 TCHAR current_directory_path[1024];
\r
66 if(GetCurrentDirectory(1024, current_directory_path)==0) {
\r
67 throw Exception(typeid(File), "", "Failed to get current directory.");
\r
69 return std::string(current_directory_path);
\r
72 void File::setCurrentDirectory(const std::string &path, bool force_mkdir) {
73 if(SetCurrentDirectory(path.c_str())==0) {
\r
75 CreateDirectory(path.c_str(), NULL);
76 SetCurrentDirectory(path.c_str());
\r
78 throw Exception(typeid(File), "", "Failed to set current directory to requested path.");
\r
83 void File::setCurrentDirectoryDefault() {
\r
84 setCurrentDirectory(STANDARD_PATH[PATH_APP].path, false);
\r
87 std::string File::replacePathDevider(const std::string &path) {
\r
88 std::string decoded_path = path;
\r
89 unsigned int found, restart=0;
\r
91 found=decoded_path.find('/',restart);
\r
92 if(found==std::string::npos) { break; }
\r
93 decoded_path.replace(found, 1, 1, PATH_DEVIDER);
\r
96 return decoded_path;
\r
99 } /* <- namespace Psycholops */