Revert r66580 and r66579.
[reactos.git] / reactos / base / shell / shell32 / wine / shellstring.c
1 /*
2 * Copyright 2000 Juergen Schmied
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library 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 GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17 */
18
19 #define WIN32_NO_STATUS
20 #define _INC_WINDOWS
21 #define NONAMELESSUNION
22 #define NONAMELESSSTRUCT
23
24 #include <windef.h>
25 #include <winbase.h>
26 #include <shlobj.h>
27 #include <shlwapi.h>
28 #include <wine/unicode.h>
29 #include <wine/debug.h>
30
31 #include "shell32_main.h"
32 #include "undocshell.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(shell);
35
36 /************************* STRRET functions ****************************/
37
38 static const char *debugstr_strret(STRRET *s)
39 {
40 switch (s->uType)
41 {
42 case STRRET_WSTR:
43 return "STRRET_WSTR";
44 case STRRET_CSTR:
45 return "STRRET_CSTR";
46 case STRRET_OFFSET:
47 return "STRRET_OFFSET";
48 default:
49 return "STRRET_???";
50 }
51 }
52
53 BOOL WINAPI StrRetToStrNA(LPSTR dest, DWORD len, LPSTRRET src, const ITEMIDLIST *pidl)
54 {
55 TRACE("dest=%p len=0x%x strret=%p(%s) pidl=%p\n", dest, len, src, debugstr_strret(src), pidl);
56
57 if (!dest)
58 return FALSE;
59
60 switch (src->uType)
61 {
62 case STRRET_WSTR:
63 WideCharToMultiByte(CP_ACP, 0, src->u.pOleStr, -1, dest, len, NULL, NULL);
64 CoTaskMemFree(src->u.pOleStr);
65 break;
66 case STRRET_CSTR:
67 lstrcpynA(dest, src->u.cStr, len);
68 break;
69 case STRRET_OFFSET:
70 lstrcpynA(dest, ((LPCSTR)&pidl->mkid)+src->u.uOffset, len);
71 break;
72 default:
73 FIXME("unknown type %u!\n", src->uType);
74 if (len)
75 *dest = '\0';
76 return FALSE;
77 }
78 TRACE("-- %s\n", debugstr_a(dest) );
79 return TRUE;
80 }
81
82 /************************************************************************/
83
84 BOOL WINAPI StrRetToStrNW(LPWSTR dest, DWORD len, LPSTRRET src, const ITEMIDLIST *pidl)
85 {
86 TRACE("dest=%p len=0x%x strret=%p(%s) pidl=%p\n", dest, len, src, debugstr_strret(src), pidl);
87
88 if (!dest)
89 return FALSE;
90
91 switch (src->uType)
92 {
93 case STRRET_WSTR:
94 lstrcpynW(dest, src->u.pOleStr, len);
95 CoTaskMemFree(src->u.pOleStr);
96 break;
97 case STRRET_CSTR:
98 if (!MultiByteToWideChar(CP_ACP, 0, src->u.cStr, -1, dest, len) && len)
99 dest[len-1] = 0;
100 break;
101 case STRRET_OFFSET:
102 if (!MultiByteToWideChar(CP_ACP, 0, ((LPCSTR)&pidl->mkid)+src->u.uOffset, -1, dest, len)
103 && len)
104 dest[len-1] = 0;
105 break;
106 default:
107 FIXME("unknown type %u!\n", src->uType);
108 if (len)
109 *dest = '\0';
110 return FALSE;
111 }
112 return TRUE;
113 }
114
115
116 /*************************************************************************
117 * StrRetToStrN [SHELL32.96]
118 *
119 * converts a STRRET to a normal string
120 *
121 * NOTES
122 * the pidl is for STRRET OFFSET
123 */
124 BOOL WINAPI StrRetToStrNAW(LPVOID dest, DWORD len, LPSTRRET src, const ITEMIDLIST *pidl)
125 {
126 if(SHELL_OsIsUnicode())
127 return StrRetToStrNW(dest, len, src, pidl);
128 else
129 return StrRetToStrNA(dest, len, src, pidl);
130 }
131
132 /************************* OLESTR functions ****************************/
133
134 /************************************************************************
135 * StrToOleStr [SHELL32.163]
136 *
137 */
138 static int StrToOleStrA (LPWSTR lpWideCharStr, LPCSTR lpMultiByteString)
139 {
140 TRACE("(%p, %p %s)\n",
141 lpWideCharStr, lpMultiByteString, debugstr_a(lpMultiByteString));
142
143 return MultiByteToWideChar(CP_ACP, 0, lpMultiByteString, -1, lpWideCharStr, MAX_PATH);
144
145 }
146 static int StrToOleStrW (LPWSTR lpWideCharStr, LPCWSTR lpWString)
147 {
148 TRACE("(%p, %p %s)\n",
149 lpWideCharStr, lpWString, debugstr_w(lpWString));
150
151 strcpyW (lpWideCharStr, lpWString );
152 return strlenW(lpWideCharStr);
153 }
154
155 BOOL WINAPI StrToOleStrAW (LPWSTR lpWideCharStr, LPCVOID lpString)
156 {
157 if (SHELL_OsIsUnicode())
158 return StrToOleStrW (lpWideCharStr, lpString);
159 return StrToOleStrA (lpWideCharStr, lpString);
160 }
161
162 /*************************************************************************
163 * StrToOleStrN [SHELL32.79]
164 * lpMulti, nMulti, nWide [IN]
165 * lpWide [OUT]
166 */
167 static BOOL StrToOleStrNA (LPWSTR lpWide, INT nWide, LPCSTR lpStrA, INT nStr)
168 {
169 TRACE("(%p, %x, %s, %x)\n", lpWide, nWide, debugstr_an(lpStrA,nStr), nStr);
170 return MultiByteToWideChar (CP_ACP, 0, lpStrA, nStr, lpWide, nWide);
171 }
172 static BOOL StrToOleStrNW (LPWSTR lpWide, INT nWide, LPCWSTR lpStrW, INT nStr)
173 {
174 TRACE("(%p, %x, %s, %x)\n", lpWide, nWide, debugstr_wn(lpStrW, nStr), nStr);
175
176 if (lstrcpynW (lpWide, lpStrW, nWide))
177 { return lstrlenW (lpWide);
178 }
179 return FALSE;
180 }
181
182 BOOL WINAPI StrToOleStrNAW (LPWSTR lpWide, INT nWide, LPCVOID lpStr, INT nStr)
183 {
184 if (SHELL_OsIsUnicode())
185 return StrToOleStrNW (lpWide, nWide, lpStr, nStr);
186 return StrToOleStrNA (lpWide, nWide, lpStr, nStr);
187 }
188
189 /*************************************************************************
190 * OleStrToStrN [SHELL32.78]
191 */
192 static BOOL OleStrToStrNA (LPSTR lpStr, INT nStr, LPCWSTR lpOle, INT nOle)
193 {
194 TRACE("(%p, %x, %s, %x)\n", lpStr, nStr, debugstr_wn(lpOle,nOle), nOle);
195 return WideCharToMultiByte (CP_ACP, 0, lpOle, nOle, lpStr, nStr, NULL, NULL);
196 }
197
198 static BOOL OleStrToStrNW (LPWSTR lpwStr, INT nwStr, LPCWSTR lpOle, INT nOle)
199 {
200 TRACE("(%p, %x, %s, %x)\n", lpwStr, nwStr, debugstr_wn(lpOle,nOle), nOle);
201
202 if (lstrcpynW ( lpwStr, lpOle, nwStr))
203 { return lstrlenW (lpwStr);
204 }
205 return FALSE;
206 }
207
208 BOOL WINAPI OleStrToStrNAW (LPVOID lpOut, INT nOut, LPCVOID lpIn, INT nIn)
209 {
210 if (SHELL_OsIsUnicode())
211 return OleStrToStrNW (lpOut, nOut, lpIn, nIn);
212 return OleStrToStrNA (lpOut, nOut, lpIn, nIn);
213 }
214
215
216 /*************************************************************************
217 * CheckEscapesA [SHELL32.@]
218 *
219 * Checks a string for special characters which are not allowed in a path
220 * and encloses it in quotes if that is the case.
221 *
222 * PARAMS
223 * string [I/O] string to check and on return eventually quoted
224 * len [I] length of string
225 *
226 * RETURNS
227 * length of actual string
228 *
229 * NOTES
230 * Not really sure if this function returns actually a value at all.
231 */
232 DWORD WINAPI CheckEscapesA(
233 LPSTR string, /* [I/O] string to check ??*/
234 DWORD len) /* [I] is 0 */
235 {
236 LPWSTR wString;
237 DWORD ret = 0;
238
239 TRACE("(%s %d)\n", debugstr_a(string), len);
240 wString = LocalAlloc(LPTR, len * sizeof(WCHAR));
241 if (wString)
242 {
243 MultiByteToWideChar(CP_ACP, 0, string, len, wString, len);
244 ret = CheckEscapesW(wString, len);
245 WideCharToMultiByte(CP_ACP, 0, wString, len, string, len, NULL, NULL);
246 LocalFree(wString);
247 }
248 return ret;
249 }
250
251 static const WCHAR strEscapedChars[] = {' ','"',',',';','^',0};
252
253 /*************************************************************************
254 * CheckEscapesW [SHELL32.@]
255 *
256 * See CheckEscapesA.
257 */
258 DWORD WINAPI CheckEscapesW(
259 LPWSTR string,
260 DWORD len)
261 {
262 DWORD size = lstrlenW(string);
263 LPWSTR s, d;
264
265 TRACE("(%s %d) stub\n", debugstr_w(string), len);
266
267 if (StrPBrkW(string, strEscapedChars) && size + 2 <= len)
268 {
269 s = &string[size - 1];
270 d = &string[size + 2];
271 *d-- = 0;
272 *d-- = '"';
273 for (;d > string;)
274 *d-- = *s--;
275 *d = '"';
276 return size + 2;
277 }
278 return size;
279 }