Spelling fixes.
[wine] / dlls / shell32 / 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #include <string.h>
20 #include <stdio.h>
21 #include <ctype.h>
22 #include <stdlib.h>
23
24 #define NONAMELESSUNION
25 #define NONAMELESSSTRUCT
26 #include "winnls.h"
27 #include "winerror.h"
28 #include "winreg.h"
29
30 #include "shlobj.h"
31 #include "shellapi.h"
32 #include "shlwapi.h"
33 #include "shell32_main.h"
34 #include "undocshell.h"
35 #include "wine/unicode.h"
36 #include "wine/debug.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(shell);
39
40 /************************* STRRET functions ****************************/
41
42 /*
43  * ***** NOTE *****
44  *  These routines are identical to StrRetToBuf[AW] in dlls/shlwapi/string.c.
45  *  They were duplicated here because not every version of Shlwapi.dll exports
46  *  StrRetToBuf[AW]. If you change one routine, change them both. YOU HAVE BEEN
47  *  WARNED.
48  * ***** NOTE *****
49  */
50
51 HRESULT WINAPI StrRetToStrNA (LPVOID dest, DWORD len, LPSTRRET src, const ITEMIDLIST *pidl)
52 {
53         TRACE("dest=%p len=0x%lx strret=%p(%s) pidl=%p\n",
54             dest,len,src,
55             (src->uType == STRRET_WSTR) ? "STRRET_WSTR" :
56             (src->uType == STRRET_CSTR) ? "STRRET_CSTR" :
57             (src->uType == STRRET_OFFSET) ? "STRRET_OFFSET" : "STRRET_???",
58             pidl);
59
60         switch (src->uType)
61         {
62           case STRRET_WSTR:
63             WideCharToMultiByte(CP_ACP, 0, src->u.pOleStr, -1, (LPSTR)dest, len, NULL, NULL);
64             CoTaskMemFree(src->u.pOleStr);
65             break;
66
67           case STRRET_CSTR:
68             lstrcpynA((LPSTR)dest, src->u.cStr, len);
69             break;
70
71           case STRRET_OFFSET:
72             lstrcpynA((LPSTR)dest, ((LPCSTR)&pidl->mkid)+src->u.uOffset, len);
73             break;
74
75           default:
76             FIXME("unknown type!\n");
77             if (len) *(LPSTR)dest = '\0';
78             return(FALSE);
79         }
80         TRACE("-- %s\n", debugstr_a(dest) );
81         return S_OK;
82 }
83
84 /************************************************************************/
85
86 HRESULT WINAPI StrRetToStrNW (LPVOID dest, DWORD len, LPSTRRET src, const ITEMIDLIST *pidl)
87 {
88         TRACE("dest=%p len=0x%lx strret=%p(%s) pidl=%p\n",
89             dest,len,src,
90             (src->uType == STRRET_WSTR) ? "STRRET_WSTR" :
91             (src->uType == STRRET_CSTR) ? "STRRET_CSTR" :
92             (src->uType == STRRET_OFFSET) ? "STRRET_OFFSET" : "STRRET_???",
93             pidl);
94
95         switch (src->uType)
96         {
97           case STRRET_WSTR:
98             lstrcpynW((LPWSTR)dest, src->u.pOleStr, len);
99             CoTaskMemFree(src->u.pOleStr);
100             break;
101
102           case STRRET_CSTR:
103             if (!MultiByteToWideChar( CP_ACP, 0, src->u.cStr, -1, dest, len ) && len)
104                   ((LPWSTR)dest)[len-1] = 0;
105             break;
106
107           case STRRET_OFFSET:
108             if (!MultiByteToWideChar( CP_ACP, 0, ((LPCSTR)&pidl->mkid)+src->u.uOffset, -1, dest, len ) && len)
109                   ((LPWSTR)dest)[len-1] = 0;
110             break;
111
112           default:
113             FIXME("unknown type!\n");
114             if (len) *(LPWSTR)dest = '\0';
115             return(FALSE);
116         }
117         return S_OK;
118 }
119
120
121 /*************************************************************************
122  * StrRetToStrN                         [SHELL32.96]
123  *
124  * converts a STRRET to a normal string
125  *
126  * NOTES
127  *  the pidl is for STRRET OFFSET
128  */
129 HRESULT WINAPI StrRetToStrNAW (LPVOID dest, DWORD len, LPSTRRET src, const ITEMIDLIST *pidl)
130 {
131         if(SHELL_OsIsUnicode())
132           return StrRetToStrNW (dest, len, src, pidl);
133         return StrRetToStrNA (dest, len, src, pidl);
134 }
135
136 /************************* OLESTR functions ****************************/
137
138 /************************************************************************
139  *      StrToOleStr                     [SHELL32.163]
140  *
141  */
142 int WINAPI StrToOleStrA (LPWSTR lpWideCharStr, LPCSTR lpMultiByteString)
143 {
144         TRACE("(%p, %p %s)\n",
145         lpWideCharStr, lpMultiByteString, debugstr_a(lpMultiByteString));
146
147         return MultiByteToWideChar(0, 0, lpMultiByteString, -1, lpWideCharStr, MAX_PATH);
148
149 }
150 int WINAPI StrToOleStrW (LPWSTR lpWideCharStr, LPCWSTR lpWString)
151 {
152         TRACE("(%p, %p %s)\n",
153         lpWideCharStr, lpWString, debugstr_w(lpWString));
154
155         strcpyW (lpWideCharStr, lpWString );
156         return strlenW(lpWideCharStr);
157 }
158
159 BOOL WINAPI StrToOleStrAW (LPWSTR lpWideCharStr, LPCVOID lpString)
160 {
161         if (SHELL_OsIsUnicode())
162           return StrToOleStrW (lpWideCharStr, lpString);
163         return StrToOleStrA (lpWideCharStr, lpString);
164 }
165
166 /*************************************************************************
167  * StrToOleStrN                                 [SHELL32.79]
168  *  lpMulti, nMulti, nWide [IN]
169  *  lpWide [OUT]
170  */
171 BOOL WINAPI StrToOleStrNA (LPWSTR lpWide, INT nWide, LPCSTR lpStrA, INT nStr)
172 {
173         TRACE("(%p, %x, %s, %x)\n", lpWide, nWide, debugstr_an(lpStrA,nStr), nStr);
174         return MultiByteToWideChar (0, 0, lpStrA, nStr, lpWide, nWide);
175 }
176 BOOL WINAPI StrToOleStrNW (LPWSTR lpWide, INT nWide, LPCWSTR lpStrW, INT nStr)
177 {
178         TRACE("(%p, %x, %s, %x)\n", lpWide, nWide, debugstr_wn(lpStrW, nStr), nStr);
179
180         if (lstrcpynW (lpWide, lpStrW, nWide))
181         { return lstrlenW (lpWide);
182         }
183         return 0;
184 }
185
186 BOOL WINAPI StrToOleStrNAW (LPWSTR lpWide, INT nWide, LPCVOID lpStr, INT nStr)
187 {
188         if (SHELL_OsIsUnicode())
189           return StrToOleStrNW (lpWide, nWide, lpStr, nStr);
190         return StrToOleStrNA (lpWide, nWide, lpStr, nStr);
191 }
192
193 /*************************************************************************
194  * OleStrToStrN                                 [SHELL32.78]
195  */
196 BOOL WINAPI OleStrToStrNA (LPSTR lpStr, INT nStr, LPCWSTR lpOle, INT nOle)
197 {
198         TRACE("(%p, %x, %s, %x)\n", lpStr, nStr, debugstr_wn(lpOle,nOle), nOle);
199         return WideCharToMultiByte (0, 0, lpOle, nOle, lpStr, nStr, NULL, NULL);
200 }
201
202 BOOL WINAPI OleStrToStrNW (LPWSTR lpwStr, INT nwStr, LPCWSTR lpOle, INT nOle)
203 {
204         TRACE("(%p, %x, %s, %x)\n", lpwStr, nwStr, debugstr_wn(lpOle,nOle), nOle);
205
206         if (lstrcpynW ( lpwStr, lpOle, nwStr))
207         { return lstrlenW (lpwStr);
208         }
209         return 0;
210 }
211
212 BOOL WINAPI OleStrToStrNAW (LPVOID lpOut, INT nOut, LPCVOID lpIn, INT nIn)
213 {
214         if (SHELL_OsIsUnicode())
215           return OleStrToStrNW (lpOut, nOut, lpIn, nIn);
216         return OleStrToStrNA (lpOut, nOut, lpIn, nIn);
217 }
218
219
220 /*************************************************************************
221  * CheckEscapesA             [SHELL32.@]
222  *
223  * Checks a string for special characters which are not allowed in a path
224  * and encloses it in quotes if that is the case.
225  *
226  * PARAMS
227  *  string     [I/O] string to check and on return eventually quoted
228  *  len        [I]   length of string
229  *
230  * RETURNS
231  *  length of actual string
232  *
233  * NOTES
234  *  Not really sure if this function returns actually a value at all. 
235  */
236 DWORD WINAPI CheckEscapesA(
237         LPSTR   string,         /* [I/O]   string to check ??*/
238         DWORD   len)            /* [I]      is 0 */
239 {
240         LPWSTR wString;
241         DWORD ret = 0;
242
243         TRACE("(%s %ld)\n", debugstr_a(string), len);
244         wString = (LPWSTR)LocalAlloc(LPTR, len * sizeof(WCHAR));
245         if (wString)
246         {
247           MultiByteToWideChar(CP_ACP, 0, string, len, wString, len);
248           ret = CheckEscapesW(wString, len);
249           WideCharToMultiByte(CP_ACP, 0, wString, len, string, len, NULL, NULL);
250           LocalFree(wString);
251         }
252         return ret;
253 }
254
255 static const WCHAR strEscapedChars[] = {' ','"',',',';','^',0};
256
257 /*************************************************************************
258  * CheckEscapesW             [SHELL32.@]
259  *
260  * see CheckEscapesA
261  */
262 DWORD WINAPI CheckEscapesW(
263         LPWSTR  string,
264         DWORD   len)
265 {
266         DWORD size = lstrlenW(string);
267         LPWSTR s, d;
268
269         TRACE("(%s %ld) stub\n", debugstr_w(string), len);
270
271         if (StrPBrkW(string, strEscapedChars) && size + 2 <= len)
272         {
273           s = &string[size - 1];
274           d = &string[size + 2];
275           *d-- = 0;
276           *d-- = '"';
277           for (;d > string;)
278             *d-- = *s--;
279           *d = '"';
280           return size + 2;
281         }
282         return size;
283 }