00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "CArchFileWindows.h"
00020 #include <windows.h>
00021 #include <shlobj.h>
00022 #include <tchar.h>
00023 #include <string.h>
00024
00025
00026
00027
00028
00029 CArchFileWindows::CArchFileWindows()
00030 {
00031
00032 }
00033
00034 CArchFileWindows::~CArchFileWindows()
00035 {
00036
00037 }
00038
00039 const char*
00040 CArchFileWindows::getBasename(const char* pathname)
00041 {
00042 if (pathname == NULL) {
00043 return NULL;
00044 }
00045
00046
00047 const char* basename = strrchr(pathname, '/');
00048 if (basename != NULL) {
00049 ++basename;
00050 }
00051 else {
00052 basename = pathname;
00053 }
00054
00055
00056 const char* basename2 = strrchr(pathname, '\\');
00057 if (basename2 != NULL && basename2 > basename) {
00058 basename = basename2 + 1;
00059 }
00060
00061 return basename;
00062 }
00063
00064 std::string
00065 CArchFileWindows::getUserDirectory()
00066 {
00067
00068 TCHAR dir[MAX_PATH];
00069 DWORD size = sizeof(dir) / sizeof(TCHAR);
00070 DWORD result = GetEnvironmentVariable(_T("HOMEPATH"), dir, size);
00071 if (result != 0 && result <= size) {
00072
00073
00074
00075 if (dir[0] != '\0' && (dir[1] == ':' ||
00076 ((dir[0] == '\\' || dir[0] == '/') &&
00077 (dir[1] == '\\' || dir[1] == '/')))) {
00078 return dir;
00079 }
00080 }
00081
00082
00083
00084 ITEMIDLIST* idl;
00085 if (SUCCEEDED(SHGetSpecialFolderLocation(NULL, CSIDL_PERSONAL, &idl))) {
00086 TCHAR* path = NULL;
00087 if (SHGetPathFromIDList(idl, dir)) {
00088 DWORD attr = GetFileAttributes(dir);
00089 if (attr != 0xffffffff && (attr & FILE_ATTRIBUTE_DIRECTORY) != 0)
00090 path = dir;
00091 }
00092
00093 IMalloc* shalloc;
00094 if (SUCCEEDED(SHGetMalloc(&shalloc))) {
00095 shalloc->Free(idl);
00096 shalloc->Release();
00097 }
00098
00099 if (path != NULL) {
00100 return path;
00101 }
00102 }
00103
00104
00105 return "C:";
00106 }
00107
00108 std::string
00109 CArchFileWindows::getSystemDirectory()
00110 {
00111
00112 char dir[MAX_PATH];
00113 if (GetWindowsDirectory(dir, sizeof(dir)) != 0) {
00114 return dir;
00115 }
00116 else {
00117
00118 return "C:";
00119 }
00120 }
00121
00122 std::string
00123 CArchFileWindows::concatPath(const std::string& prefix,
00124 const std::string& suffix)
00125 {
00126 std::string path;
00127 path.reserve(prefix.size() + 1 + suffix.size());
00128 path += prefix;
00129 if (path.size() == 0 ||
00130 (path[path.size() - 1] != '\\' &&
00131 path[path.size() - 1] != '/')) {
00132 path += '\\';
00133 }
00134 path += suffix;
00135 return path;
00136 }