Release 20030408.
[wine/gsoc-2012-control.git] / dlls / shell32 / shellstring.c
blob43af25ede50e28841a8bfd3fbc6411c7c30917e2
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include <string.h>
20 #include <stdio.h>
21 #include <ctype.h>
22 #include <stdlib.h>
24 #define NONAMELESSUNION
25 #define NONAMELESSSTRUCT
26 #include "winnls.h"
27 #include "winerror.h"
28 #include "winreg.h"
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"
38 WINE_DEFAULT_DEBUG_CHANNEL(shell);
40 /************************* STRRET functions ****************************/
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 *****
51 HRESULT WINAPI StrRetToStrNA (LPVOID dest, DWORD len, LPSTRRET src, const ITEMIDLIST *pidl)
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);
60 switch (src->uType)
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;
67 case STRRET_CSTR:
68 lstrcpynA((LPSTR)dest, src->u.cStr, len);
69 break;
71 case STRRET_OFFSET:
72 lstrcpynA((LPSTR)dest, ((LPCSTR)&pidl->mkid)+src->u.uOffset, len);
73 break;
75 default:
76 FIXME("unknown type!\n");
77 if (len) *(LPSTR)dest = '\0';
78 return(FALSE);
80 TRACE("-- %s\n", debugstr_a(dest) );
81 return S_OK;
84 /************************************************************************/
86 HRESULT WINAPI StrRetToStrNW (LPVOID dest, DWORD len, LPSTRRET src, const ITEMIDLIST *pidl)
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);
95 switch (src->uType)
97 case STRRET_WSTR:
98 lstrcpynW((LPWSTR)dest, src->u.pOleStr, len);
99 CoTaskMemFree(src->u.pOleStr);
100 break;
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;
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;
112 default:
113 FIXME("unknown type!\n");
114 if (len) *(LPWSTR)dest = '\0';
115 return(FALSE);
117 return S_OK;
121 /*************************************************************************
122 * StrRetToStrN [SHELL32.96]
124 * converts a STRRET to a normal string
126 * NOTES
127 * the pidl is for STRRET OFFSET
129 HRESULT WINAPI StrRetToStrNAW (LPVOID dest, DWORD len, LPSTRRET src, const ITEMIDLIST *pidl)
131 if(SHELL_OsIsUnicode())
132 return StrRetToStrNW (dest, len, src, pidl);
133 return StrRetToStrNA (dest, len, src, pidl);
136 /************************* OLESTR functions ****************************/
138 /************************************************************************
139 * StrToOleStr [SHELL32.163]
142 int WINAPI StrToOleStrA (LPWSTR lpWideCharStr, LPCSTR lpMultiByteString)
144 TRACE("(%p, %p %s)\n",
145 lpWideCharStr, lpMultiByteString, debugstr_a(lpMultiByteString));
147 return MultiByteToWideChar(0, 0, lpMultiByteString, -1, lpWideCharStr, MAX_PATH);
150 int WINAPI StrToOleStrW (LPWSTR lpWideCharStr, LPCWSTR lpWString)
152 TRACE("(%p, %p %s)\n",
153 lpWideCharStr, lpWString, debugstr_w(lpWString));
155 strcpyW (lpWideCharStr, lpWString );
156 return strlenW(lpWideCharStr);
159 BOOL WINAPI StrToOleStrAW (LPWSTR lpWideCharStr, LPCVOID lpString)
161 if (SHELL_OsIsUnicode())
162 return StrToOleStrW (lpWideCharStr, lpString);
163 return StrToOleStrA (lpWideCharStr, lpString);
166 /*************************************************************************
167 * StrToOleStrN [SHELL32.79]
168 * lpMulti, nMulti, nWide [IN]
169 * lpWide [OUT]
171 BOOL WINAPI StrToOleStrNA (LPWSTR lpWide, INT nWide, LPCSTR lpStrA, INT nStr)
173 TRACE("(%p, %x, %s, %x)\n", lpWide, nWide, debugstr_an(lpStrA,nStr), nStr);
174 return MultiByteToWideChar (0, 0, lpStrA, nStr, lpWide, nWide);
176 BOOL WINAPI StrToOleStrNW (LPWSTR lpWide, INT nWide, LPCWSTR lpStrW, INT nStr)
178 TRACE("(%p, %x, %s, %x)\n", lpWide, nWide, debugstr_wn(lpStrW, nStr), nStr);
180 if (lstrcpynW (lpWide, lpStrW, nWide))
181 { return lstrlenW (lpWide);
183 return 0;
186 BOOL WINAPI StrToOleStrNAW (LPWSTR lpWide, INT nWide, LPCVOID lpStr, INT nStr)
188 if (SHELL_OsIsUnicode())
189 return StrToOleStrNW (lpWide, nWide, lpStr, nStr);
190 return StrToOleStrNA (lpWide, nWide, lpStr, nStr);
193 /*************************************************************************
194 * OleStrToStrN [SHELL32.78]
196 BOOL WINAPI OleStrToStrNA (LPSTR lpStr, INT nStr, LPCWSTR lpOle, INT nOle)
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);
202 BOOL WINAPI OleStrToStrNW (LPWSTR lpwStr, INT nwStr, LPCWSTR lpOle, INT nOle)
204 TRACE("(%p, %x, %s, %x)\n", lpwStr, nwStr, debugstr_wn(lpOle,nOle), nOle);
206 if (lstrcpynW ( lpwStr, lpOle, nwStr))
207 { return lstrlenW (lpwStr);
209 return 0;
212 BOOL WINAPI OleStrToStrNAW (LPVOID lpOut, INT nOut, LPCVOID lpIn, INT nIn)
214 if (SHELL_OsIsUnicode())
215 return OleStrToStrNW (lpOut, nOut, lpIn, nIn);
216 return OleStrToStrNA (lpOut, nOut, lpIn, nIn);
220 /*************************************************************************
221 * CheckEscapesA [SHELL32.@]
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.
226 * PARAMS
227 * string [I/O] string to check and on return eventually quoted
228 * len [I] length of string
230 * RETURNS
231 * length of actual string
233 * NOTES
234 * Not really sure if this function returns actually a value at all.
236 DWORD WINAPI CheckEscapesA(
237 LPSTR string, /* [I/O] string to check ??*/
238 DWORD len) /* [I] is 0 */
240 LPWSTR wString;
241 DWORD ret = 0;
243 TRACE("(%s %ld)\n", debugstr_a(string), len);
244 wString = (LPWSTR)LocalAlloc(LPTR, len * sizeof(WCHAR));
245 if (wString)
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);
252 return ret;
255 static const WCHAR strEscapedChars[] = {' ','"',',',';','^',0};
257 /*************************************************************************
258 * CheckEscapesW [SHELL32.@]
260 * see CheckEscapesA
262 DWORD WINAPI CheckEscapesW(
263 LPWSTR string,
264 DWORD len)
266 DWORD size = lstrlenW(string);
267 LPWSTR s, d;
269 TRACE("(%s %ld) stub\n", debugstr_w(string), len);
271 if (StrPBrkW(string, strEscapedChars) && size + 2 <= len)
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;
282 return size;