Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members

CArchFileWindows.cpp

00001 /*
00002  * synergy -- mouse and keyboard sharing utility
00003  * Copyright (C) 2002 Chris Schoeneman
00004  * 
00005  * This package is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * found in the file COPYING that should have accompanied this file.
00008  * 
00009  * This package is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
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 // CArchFileWindows
00023 //
00024 
00025 CArchFileWindows::CArchFileWindows()
00026 {
00027     // do nothing
00028 }
00029 
00030 CArchFileWindows::~CArchFileWindows()
00031 {
00032     // do nothing
00033 }
00034 
00035 const char*
00036 CArchFileWindows::getBasename(const char* pathname)
00037 {
00038     if (pathname == NULL) {
00039         return NULL;
00040     }
00041 
00042     // check for last slash
00043     const char* basename = strrchr(pathname, '/');
00044     if (basename != NULL) {
00045         ++basename;
00046     }
00047     else {
00048         basename = pathname;
00049     }
00050 
00051     // check for last backslash
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     // try %HOMEPATH%
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         // sanity check -- if dir doesn't appear to start with a
00069         // drive letter and isn't a UNC name then don't use it
00070         // FIXME -- allow UNC names
00071         if (dir[0] != '\0' && (dir[1] == ':' ||
00072             ((dir[0] == '\\' || dir[0] == '/') &&
00073             (dir[1] == '\\' || dir[1] == '/')))) {
00074             return dir;
00075         }
00076     }
00077 
00078     // get the location of the personal files.  that's as close to
00079     // a home directory as we're likely to find.
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     // use root of C drive as a default
00101     return "C:";
00102 }
00103 
00104 std::string
00105 CArchFileWindows::getSystemDirectory()
00106 {
00107     // get windows directory
00108     char dir[MAX_PATH];
00109     if (GetWindowsDirectory(dir, sizeof(dir)) != 0) {
00110         return dir;
00111     }
00112     else {
00113         // can't get it.  use C:\ as a default.
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 }

Generated on Fri Nov 6 00:21:13 2009 for synergy-plus by  doxygen 1.3.9.1