OSDN Git Service

first
[psychlops/cpp.git] / psychlops / platform / win32 / psychlops_io_file_Win32.cpp
1 /*
2  *  psychlops_io_file_Win32.cpp
3  *  Psychlops Standard Library (Win32)
4  *
5  *  Last Modified 2006/01/05 by Kenchi HOSOKAWA
6  *  (C) 2006 Kenchi HOSOKAWA, Kazushi MARUYA, Takao SATO
7  */
8
9 #include <string>\r
10 #include <windows.h>
11
12 #include "../../core/ApplicationInterfaces/psychlops_app.h"
13 #include "../../core/devices/psychlops_io_file.h"
14
15
16 namespace Psychlops {
17
18         const char File::PATH_DEVIDER = '\\';\r
19
20         void File::initialize() {\r
21                 unsigned int loc;
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
35                 delete [] apppath;\r
36 \r
37                 BYTE * documentspath = new BYTE[1024];\r
38                 for(int i=0; i<1024; i++) documentspath[i] = 0;\r
39                 HKEY phk;\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
44                 RegCloseKey(phk);\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
48 \r
49                 initialize_appname();\r
50         }
51
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
59                         //}\r
60                         appname_initialized = true;\r
61                 }
62         }
63
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
68                 }\r
69                 return std::string(current_directory_path);\r
70         }\r
71 \r
72         void File::setCurrentDirectory(const std::string &path, bool force_mkdir) {
73                 if(SetCurrentDirectory(path.c_str())==0) {\r
74                         if(force_mkdir) {
75                                 CreateDirectory(path.c_str(), NULL);
76                                 SetCurrentDirectory(path.c_str());\r
77                         } else {\r
78                                 throw Exception(typeid(File), "", "Failed to set current directory to requested path.");\r
79                         }
80                 }
81         }
82
83         void File::setCurrentDirectoryDefault() {\r
84                 setCurrentDirectory(STANDARD_PATH[PATH_APP].path, false);\r
85         }\r
86 \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
90                 for(;;) {\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
94                         restart = found;\r
95                 }\r
96                 return decoded_path;\r
97         }\r
98
99 }       /*      <- namespace Psycholops         */