winemac: Don't process QUERY_IME_CHAR_RECT while waiting in OnMainThread().
[wine.git] / dlls / shell32 / shellstring.c
blob956fd50871948c0ca2e7f315f1edd7c5e45f6ccd
1 /*
2 * Copyright 2000 Juergen Schmied
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.
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.
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
19 #include <string.h>
20 #include <stdarg.h>
21 #include <stdio.h>
22 #include <ctype.h>
23 #include <stdlib.h>
25 #define NONAMELESSUNION
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winnls.h"
30 #include "wingdi.h"
31 #include "winuser.h"
32 #include "winreg.h"
34 #include "shlobj.h"
35 #include "shlwapi.h"
36 #include "shell32_main.h"
37 #include "undocshell.h"
38 #include "wine/unicode.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(shell);
43 /************************* STRRET functions ****************************/
45 static const char *debugstr_strret(STRRET *s)
47 switch (s->uType)
49 case STRRET_WSTR:
50 return "STRRET_WSTR";
51 case STRRET_CSTR:
52 return "STRRET_CSTR";
53 case STRRET_OFFSET:
54 return "STRRET_OFFSET";
55 default:
56 return "STRRET_???";
60 BOOL WINAPI StrRetToStrNA(LPSTR dest, DWORD len, LPSTRRET src, const ITEMIDLIST *pidl)
62 TRACE("dest=%p len=0x%x strret=%p(%s) pidl=%p\n", dest, len, src, debugstr_strret(src), pidl);
64 if (!dest)
65 return FALSE;
67 switch (src->uType)
69 case STRRET_WSTR:
70 WideCharToMultiByte(CP_ACP, 0, src->u.pOleStr, -1, dest, len, NULL, NULL);
71 CoTaskMemFree(src->u.pOleStr);
72 break;
73 case STRRET_CSTR:
74 lstrcpynA(dest, src->u.cStr, len);
75 break;
76 case STRRET_OFFSET:
77 lstrcpynA(dest, ((LPCSTR)&pidl->mkid)+src->u.uOffset, len);
78 break;
79 default:
80 FIXME("unknown type %u!\n", src->uType);
81 if (len)
82 *dest = '\0';
83 return FALSE;
85 TRACE("-- %s\n", debugstr_a(dest) );
86 return TRUE;
89 /************************************************************************/
91 BOOL WINAPI StrRetToStrNW(LPWSTR dest, DWORD len, LPSTRRET src, const ITEMIDLIST *pidl)
93 TRACE("dest=%p len=0x%x strret=%p(%s) pidl=%p\n", dest, len, src, debugstr_strret(src), pidl);
95 if (!dest)
96 return FALSE;
98 switch (src->uType)
100 case STRRET_WSTR:
101 lstrcpynW(dest, src->u.pOleStr, len);
102 CoTaskMemFree(src->u.pOleStr);
103 break;
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 case STRRET_OFFSET:
109 if (!MultiByteToWideChar(CP_ACP, 0, ((LPCSTR)&pidl->mkid)+src->u.uOffset, -1, dest, len)
110 && len)
111 dest[len-1] = 0;
112 break;
113 default:
114 FIXME("unknown type %u!\n", src->uType);
115 if (len)
116 *dest = '\0';
117 return FALSE;
119 return TRUE;
123 /*************************************************************************
124 * StrRetToStrN [SHELL32.96]
126 * converts a STRRET to a normal string
128 * NOTES
129 * the pidl is for STRRET OFFSET
131 BOOL WINAPI StrRetToStrNAW(LPVOID dest, DWORD len, LPSTRRET src, const ITEMIDLIST *pidl)
133 if(SHELL_OsIsUnicode())
134 return StrRetToStrNW(dest, len, src, pidl);
135 else
136 return StrRetToStrNA(dest, len, src, pidl);
139 /************************* OLESTR functions ****************************/
141 /************************************************************************
142 * StrToOleStr [SHELL32.163]
145 static int StrToOleStrA (LPWSTR lpWideCharStr, LPCSTR lpMultiByteString)
147 TRACE("(%p, %p %s)\n",
148 lpWideCharStr, lpMultiByteString, debugstr_a(lpMultiByteString));
150 return MultiByteToWideChar(CP_ACP, 0, lpMultiByteString, -1, lpWideCharStr, MAX_PATH);
153 static int StrToOleStrW (LPWSTR lpWideCharStr, LPCWSTR lpWString)
155 TRACE("(%p, %p %s)\n",
156 lpWideCharStr, lpWString, debugstr_w(lpWString));
158 strcpyW (lpWideCharStr, lpWString );
159 return strlenW(lpWideCharStr);
162 BOOL WINAPI StrToOleStrAW (LPWSTR lpWideCharStr, LPCVOID lpString)
164 if (SHELL_OsIsUnicode())
165 return StrToOleStrW (lpWideCharStr, lpString);
166 return StrToOleStrA (lpWideCharStr, lpString);
169 /*************************************************************************
170 * StrToOleStrN [SHELL32.79]
171 * lpMulti, nMulti, nWide [IN]
172 * lpWide [OUT]
174 static BOOL StrToOleStrNA (LPWSTR lpWide, INT nWide, LPCSTR lpStrA, INT nStr)
176 TRACE("(%p, %x, %s, %x)\n", lpWide, nWide, debugstr_an(lpStrA,nStr), nStr);
177 return MultiByteToWideChar (CP_ACP, 0, lpStrA, nStr, lpWide, nWide);
179 static BOOL StrToOleStrNW (LPWSTR lpWide, INT nWide, LPCWSTR lpStrW, INT nStr)
181 TRACE("(%p, %x, %s, %x)\n", lpWide, nWide, debugstr_wn(lpStrW, nStr), nStr);
183 if (lstrcpynW (lpWide, lpStrW, nWide))
184 { return lstrlenW (lpWide);
186 return FALSE;
189 BOOL WINAPI StrToOleStrNAW (LPWSTR lpWide, INT nWide, LPCVOID lpStr, INT nStr)
191 if (SHELL_OsIsUnicode())
192 return StrToOleStrNW (lpWide, nWide, lpStr, nStr);
193 return StrToOleStrNA (lpWide, nWide, lpStr, nStr);
196 /*************************************************************************
197 * OleStrToStrN [SHELL32.78]
199 static BOOL OleStrToStrNA (LPSTR lpStr, INT nStr, LPCWSTR lpOle, INT nOle)
201 TRACE("(%p, %x, %s, %x)\n", lpStr, nStr, debugstr_wn(lpOle,nOle), nOle);
202 return WideCharToMultiByte (CP_ACP, 0, lpOle, nOle, lpStr, nStr, NULL, NULL);
205 static BOOL OleStrToStrNW (LPWSTR lpwStr, INT nwStr, LPCWSTR lpOle, INT nOle)
207 TRACE("(%p, %x, %s, %x)\n", lpwStr, nwStr, debugstr_wn(lpOle,nOle), nOle);
209 if (lstrcpynW ( lpwStr, lpOle, nwStr))
210 { return lstrlenW (lpwStr);
212 return FALSE;
215 BOOL WINAPI OleStrToStrNAW (LPVOID lpOut, INT nOut, LPCVOID lpIn, INT nIn)
217 if (SHELL_OsIsUnicode())
218 return OleStrToStrNW (lpOut, nOut, lpIn, nIn);
219 return OleStrToStrNA (lpOut, nOut, lpIn, nIn);
223 /*************************************************************************
224 * CheckEscapesA [SHELL32.@]
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.
229 * PARAMS
230 * string [I/O] string to check and on return eventually quoted
231 * len [I] length of string
233 * RETURNS
234 * length of actual string
236 * NOTES
237 * Not really sure if this function returns actually a value at all.
239 DWORD WINAPI CheckEscapesA(
240 LPSTR string, /* [I/O] string to check ??*/
241 DWORD len) /* [I] is 0 */
243 LPWSTR wString;
244 DWORD ret = 0;
246 TRACE("(%s %d)\n", debugstr_a(string), len);
247 wString = LocalAlloc(LPTR, len * sizeof(WCHAR));
248 if (wString)
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);
255 return ret;
258 static const WCHAR strEscapedChars[] = {' ','"',',',';','^',0};
260 /*************************************************************************
261 * CheckEscapesW [SHELL32.@]
263 * See CheckEscapesA.
265 DWORD WINAPI CheckEscapesW(
266 LPWSTR string,
267 DWORD len)
269 DWORD size = lstrlenW(string);
270 LPWSTR s, d;
272 TRACE("(%s %d) stub\n", debugstr_w(string), len);
274 if (StrPBrkW(string, strEscapedChars) && size + 2 <= len)
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;
285 return size;