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