• Main Page
  • Classes
  • Files
  • File List

CArchFileWindows.cpp

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

Generated on Sat May 25 2013 00:00:03 for Synergy by  doxygen 1.7.1