1 | /*
|
---|
2 | * synergy -- mouse and keyboard sharing utility
|
---|
3 | * Copyright (C) 2002 Chris Schoeneman
|
---|
4 | *
|
---|
5 | * This package is free software; you can redistribute it and/or
|
---|
6 | * modify it under the terms of the GNU General Public License
|
---|
7 | * found in the file COPYING that should have accompanied this file.
|
---|
8 | *
|
---|
9 | * This package is distributed in the hope that it will be useful,
|
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
12 | * GNU General Public License for more details.
|
---|
13 | */
|
---|
14 |
|
---|
15 | #include "CArchFileWindows.h"
|
---|
16 | #include <windows.h>
|
---|
17 | #include <shlobj.h>
|
---|
18 | #include <tchar.h>
|
---|
19 | #include <string.h>
|
---|
20 |
|
---|
21 | //
|
---|
22 | // CArchFileWindows
|
---|
23 | //
|
---|
24 |
|
---|
25 | CArchFileWindows::CArchFileWindows()
|
---|
26 | {
|
---|
27 | // do nothing
|
---|
28 | }
|
---|
29 |
|
---|
30 | CArchFileWindows::~CArchFileWindows()
|
---|
31 | {
|
---|
32 | // do nothing
|
---|
33 | }
|
---|
34 |
|
---|
35 | const char*
|
---|
36 | CArchFileWindows::getBasename(const char* pathname)
|
---|
37 | {
|
---|
38 | if (pathname == NULL) {
|
---|
39 | return NULL;
|
---|
40 | }
|
---|
41 |
|
---|
42 | // check for last slash
|
---|
43 | const char* basename = strrchr(pathname, '/');
|
---|
44 | if (basename != NULL) {
|
---|
45 | ++basename;
|
---|
46 | }
|
---|
47 | else {
|
---|
48 | basename = pathname;
|
---|
49 | }
|
---|
50 |
|
---|
51 | // check for last backslash
|
---|
52 | const char* basename2 = strrchr(pathname, '\\');
|
---|
53 | if (basename2 != NULL && basename2 > basename) {
|
---|
54 | basename = basename2 + 1;
|
---|
55 | }
|
---|
56 |
|
---|
57 | return basename;
|
---|
58 | }
|
---|
59 |
|
---|
60 | std::string
|
---|
61 | CArchFileWindows::getUserDirectory()
|
---|
62 | {
|
---|
63 | // try %HOMEPATH%
|
---|
64 | TCHAR dir[MAX_PATH];
|
---|
65 | DWORD size = sizeof(dir) / sizeof(TCHAR);
|
---|
66 | DWORD result = GetEnvironmentVariable(_T("HOMEPATH"), dir, size);
|
---|
67 | if (result != 0 && result <= size) {
|
---|
68 | // sanity check -- if dir doesn't appear to start with a
|
---|
69 | // drive letter and isn't a UNC name then don't use it
|
---|
70 | // FIXME -- allow UNC names
|
---|
71 | if (dir[0] != '\0' && (dir[1] == ':' ||
|
---|
72 | ((dir[0] == '\\' || dir[0] == '/') &&
|
---|
73 | (dir[1] == '\\' || dir[1] == '/')))) {
|
---|
74 | return dir;
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | // get the location of the personal files. that's as close to
|
---|
79 | // a home directory as we're likely to find.
|
---|
80 | ITEMIDLIST* idl;
|
---|
81 | if (SUCCEEDED(SHGetSpecialFolderLocation(NULL, CSIDL_PERSONAL, &idl))) {
|
---|
82 | TCHAR* path = NULL;
|
---|
83 | if (SHGetPathFromIDList(idl, dir)) {
|
---|
84 | DWORD attr = GetFileAttributes(dir);
|
---|
85 | if (attr != 0xffffffff && (attr & FILE_ATTRIBUTE_DIRECTORY) != 0)
|
---|
86 | path = dir;
|
---|
87 | }
|
---|
88 |
|
---|
89 | IMalloc* shalloc;
|
---|
90 | if (SUCCEEDED(SHGetMalloc(&shalloc))) {
|
---|
91 | shalloc->Free(idl);
|
---|
92 | shalloc->Release();
|
---|
93 | }
|
---|
94 |
|
---|
95 | if (path != NULL) {
|
---|
96 | return path;
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 | // use root of C drive as a default
|
---|
101 | return "C:";
|
---|
102 | }
|
---|
103 |
|
---|
104 | std::string
|
---|
105 | CArchFileWindows::getSystemDirectory()
|
---|
106 | {
|
---|
107 | // get windows directory
|
---|
108 | char dir[MAX_PATH];
|
---|
109 | if (GetWindowsDirectory(dir, sizeof(dir)) != 0) {
|
---|
110 | return dir;
|
---|
111 | }
|
---|
112 | else {
|
---|
113 | // can't get it. use C:\ as a default.
|
---|
114 | return "C:";
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | std::string
|
---|
119 | CArchFileWindows::concatPath(const std::string& prefix,
|
---|
120 | const std::string& suffix)
|
---|
121 | {
|
---|
122 | std::string path;
|
---|
123 | path.reserve(prefix.size() + 1 + suffix.size());
|
---|
124 | path += prefix;
|
---|
125 | if (path.size() == 0 ||
|
---|
126 | (path[path.size() - 1] != '\\' &&
|
---|
127 | path[path.size() - 1] != '/')) {
|
---|
128 | path += '\\';
|
---|
129 | }
|
---|
130 | path += suffix;
|
---|
131 | return path;
|
---|
132 | }
|
---|