1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef INCLUDED_DESKTOP_WIN32_SOURCE_EXTENDLOADERENVIRONMENT_HXX
29 #define INCLUDED_DESKTOP_WIN32_SOURCE_EXTENDLOADERENVIRONMENT_HXX
30 
31 #include "sal/config.h"
32 
33 #include <cstddef>
34 
35 #include <tchar.h>
36 
37 #define MY_LENGTH(s) (sizeof (s) / sizeof *(s) - 1)
38 #define MY_STRING(s) (s), MY_LENGTH(s)
39 
40 namespace desktop_win32 {
41 
42 inline WCHAR * commandLineAppend(
43     WCHAR * buffer, WCHAR const * text, std::size_t length)
44 {
45     wcsncpy(buffer, text, length + 1); // trailing null
46     return buffer + length;
47 }
48 
49 inline WCHAR * commandLineAppend(WCHAR * buffer, WCHAR const * text) {
50     return commandLineAppend(buffer, text, wcslen(text));
51 }
52 
53 inline WCHAR * commandLineAppendEncoded(WCHAR * buffer, WCHAR const * text) {
54     std::size_t n = 0;
55     for (;;) {
56         WCHAR c = *text++;
57         if (c == L'\0') {
58             break;
59         } else if (c == L'$') {
60             buffer = commandLineAppend(buffer, MY_STRING(L"\\$"));
61             n = 0;
62         } else if (c == L'\\') {
63             buffer = commandLineAppend(buffer, MY_STRING(L"\\\\"));
64             n += 2;
65         } else {
66             *buffer++ = c;
67             n = 0;
68         }
69     }
70     // The command line will continue with a double quote, so double any
71     // preceding backslashes as required by Windows:
72     for (std::size_t i = 0; i < n; ++i) {
73         *buffer++ = L'\\';
74     }
75     *buffer = L'\0';
76     return buffer;
77 }
78 
79 // Set the PATH environment variable in the current (loader) process, so that a
80 // following CreateProcess has the necessary environment:
81 //
82 // @param binPath
83 // Must point to an array of size at least MAX_PATH.  Is filled with the null
84 // terminated full path to the "bin" file corresponding to the current
85 // executable.
86 //
87 // @param iniDirectory
88 // Must point to an array of size at least MAX_PATH.  Is filled with the null
89 // terminated full directory path (ending in "\") to the "ini" file
90 // corresponding to the current executable.
91 void extendLoaderEnvironment(WCHAR * binPath, WCHAR * iniDirectory);
92 
93 }
94 
95 #endif
96