summaryrefslogtreecommitdiff
path: root/libs/ardour/msvc/msvc_libardour.cc
blob: 2efcb4b937347503b0c663527d9e8afe421f8c3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
    Copyright (C) 2009 John Emmas

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#if (defined(PLATFORM_WINDOWS) && !defined(COMPILER_CYGWIN))
#include <shlobj.h>
#include <glibmm.h>
#ifdef COMPILER_MSVC
#pragma warning(disable:4996)
#endif
#else
#include <glib.h>
#endif

#include <string.h>
#include <stdlib.h>
#include <ardour/msvc_libardour.h>

namespace ARDOUR {

//***************************************************************
//
// placeholder_for_non_msvc_specific_function()
//
// Description
//
//	Returns:
//
//    On Success:
//
//    On Failure:
//
/* LIBARDOUR_API char* LIBARDOUR_APICALLTYPE
   placeholder_for_non_msvc_specific_function()
{
char *pRet = buffer;

	return (pRet);
}
*/

}  // namespace ARDOUR

#ifdef COMPILER_MSVC

#include <errno.h>

namespace ARDOUR {

//***************************************************************
//
//	symlink()
//
// Emulates POSIX symlink() but creates a Windows shortcut. To
// create a Windows shortcut the supplied shortcut name must end
// in ".lnk"
// Note that you can only create a shortcut in a folder for which
// you have appropriate access rights. Note also that the folder
// must already exist. If it doesn't exist or if you don't have
// sufficient access rights to it, symlink() will generate an
// error (in common with its POSIX counterpart).
//
//	Returns:
//
//    On Success: Zero
//    On Failure: -1 ('errno' will contain the specific error)
//
LIBARDOUR_API int LIBARDOUR_APICALLTYPE
symlink(const char *dest, const char *shortcut, const char *working_directory /*= NULL */)
{
IShellLinkA  *pISL = NULL;
IPersistFile *ppf  = NULL;
int           ret  = (-1);

	if ((NULL == dest) || (NULL == shortcut) || (strlen(shortcut) < 5) || (strlen(dest) == 0))
		_set_errno(EINVAL);
	else if ((strlen(shortcut) > _MAX_PATH) || (strlen(dest) > _MAX_PATH))
		_set_errno(ENAMETOOLONG);
	else if (Glib::file_test(shortcut, Glib::FILE_TEST_EXISTS))
		_set_errno(EEXIST);
	else
	{
		HRESULT hRet = 0;

		if (SUCCEEDED (hRet = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&pISL)))
		{
			if (SUCCEEDED (pISL->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf)))
			{
				char  sc_path_lower_case[_MAX_PATH];
				WCHAR shortcut_path[_MAX_PATH];

				// Fail if the path isn't a shortcut
				strcpy(sc_path_lower_case, shortcut);
				strlwr(sc_path_lower_case);
				const char *p = strlen(sc_path_lower_case) + sc_path_lower_case - 4;

				if (0 == strcmp(p, ".lnk"))
				{
					HRESULT hr;

					// We're apparently been given valid Windows shortcut name
					MultiByteToWideChar (CP_ACP, MB_PRECOMPOSED, shortcut, -1, shortcut_path, _MAX_PATH);

					// Create the shortcut
					if (FAILED (hr = ppf->Load(shortcut_path, STGM_CREATE|STGM_READWRITE|STGM_SHARE_EXCLUSIVE)))
						hr = ppf->Save(shortcut_path, TRUE);

					if (S_OK == hr)
					{
						// Set its target path
						if (S_OK == pISL->SetPath(dest))
						{
							// Set its working directory
							if (working_directory)
								p = working_directory;
							else
								p = "";

							if (S_OK == pISL->SetWorkingDirectory(p))
							{
								// Set its 'Show' command
								if (S_OK == pISL->SetShowCmd(SW_SHOWNORMAL))
								{
									// And finally, set its icon to the same file as the target.
									// For the time being, don't fail if the target has no icon.
					                if (Glib::file_test(dest, Glib::FILE_TEST_IS_DIR))
										pISL->SetIconLocation("%SystemRoot%\\system32\\shell32.dll", 1);
									else
										pISL->SetIconLocation(dest, 0);

									if (S_OK == ppf->Save(shortcut_path, FALSE))
									{
										Sleep(1500);

										ret = 0;
										// _set_errno(0);
									}
									else
										_set_errno(EACCES);
								}
								else
									_set_errno(EACCES);
							}
							else
								_set_errno(EACCES);
						}
						else
							_set_errno(EACCES);
					}
					else
						_set_errno(EBADF);
				}
				else
					_set_errno(EACCES);
			}
			else
				_set_errno(EBADF);
		}
		else
		{
			if (E_POINTER == hRet)
				_set_errno(EINVAL);
			else
				_set_errno(EIO);
		}
	}

	return (ret);
}


//***************************************************************
//
//	readlink()
//
// Emulates POSIX readlink() but using Windows shortcuts
// Doesn't (currently) resolve shortcuts to shortcuts. This would
// be quite simple to incorporate but we'd need to check for
// recursion (i.e. a shortcut that points to an earlier shortcut
// in the same chain).
//
//	Returns:
//
//    On Success: Zero
//    On Failure: -1 ('errno' will contain the specific error)
//
LIBARDOUR_API int LIBARDOUR_APICALLTYPE
readlink(const char *__restrict shortcut, char *__restrict buf, size_t bufsize)
{
IShellLinkA  *pISL = NULL;
IPersistFile *ppf  = NULL;
int           ret  = (-1);

	if ((NULL == shortcut) || (NULL == buf) || (strlen(shortcut) < 5) || (bufsize == 0))
		_set_errno(EINVAL);
	else if ((bufsize > _MAX_PATH) || (strlen(shortcut) > _MAX_PATH))
		_set_errno(ENAMETOOLONG);
	else
	{
		HRESULT hRet = 0;

		if (SUCCEEDED (hRet = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&pISL)))
		{
			if (SUCCEEDED (pISL->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf)))
			{
				char  target_path[_MAX_PATH];
				WCHAR shortcut_path[_MAX_PATH];

				// Fail if the path isn't a shortcut
				strcpy(target_path, shortcut); // Use 'target_path' temporarily
				strlwr(target_path);
				const char *p = strlen(target_path) + target_path - 4;

				if (0 == strcmp(p, ".lnk"))
				{
					// We're apparently pointing to a valid Windows shortcut
					MultiByteToWideChar (CP_ACP, MB_PRECOMPOSED, shortcut, -1, shortcut_path, _MAX_PATH);

					// Load the shortcut into our persistent file
					if (SUCCEEDED (ppf->Load(shortcut_path, 0)))
					{
						// Read the target information from the shortcut object
						if (S_OK == (pISL->GetPath (target_path, _MAX_PATH, NULL, SLGP_UNCPRIORITY)))
						{
							strncpy(buf, target_path, bufsize);
							ret = ((ret = strlen(buf)) > bufsize) ? bufsize : ret;
							// _set_errno(0);
						}
						else
							_set_errno(EACCES);
					}
					else
						_set_errno(EBADF);
				}
				else
					_set_errno(EINVAL);
			}
			else
				_set_errno(EBADF);
		}
		else
		{
			if (E_POINTER == hRet)
				_set_errno(EINVAL);
			else
				_set_errno(EIO);
		}

		if (ppf)
			ppf->Release();

		if (pISL)
			pISL->Release();
	}

	return (ret);
}

}  // namespace ARDOUR

#endif  // COMPILER_MSVC