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