Don't mistake frames for bytes.
[wine/multimedia.git] / dlls / shell32 / shellstring.c
blob3581768d72d0d1820255bf81bdd9c4875b182b47
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 <stdarg.h>
21 #include <stdio.h>
22 #include <ctype.h>
23 #include <stdlib.h>
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"
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"
43 WINE_DEFAULT_DEBUG_CHANNEL(shell);
45 /************************* STRRET functions ****************************/
47 BOOL WINAPI StrRetToStrNA(LPSTR dest, DWORD len, LPSTRRET src, const ITEMIDLIST *pidl)
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);
56 if (!dest)
57 return FALSE;
59 switch (src->uType)
61 case STRRET_WSTR:
62 WideCharToMultiByte(CP_ACP, 0, src->u.pOleStr, -1, dest, len, NULL, NULL);
63 CoTaskMemFree(src->u.pOleStr);
64 break;
66 case STRRET_CSTR:
67 lstrcpynA(dest, src->u.cStr, len);
68 break;
70 case STRRET_OFFSET:
71 lstrcpynA(dest, ((LPCSTR)&pidl->mkid)+src->u.uOffset, len);
72 break;
74 default:
75 FIXME("unknown type!\n");
76 if (len) *dest = '\0';
77 return FALSE;
79 TRACE("-- %s\n", debugstr_a(dest) );
80 return TRUE;
83 /************************************************************************/
85 BOOL WINAPI StrRetToStrNW(LPWSTR dest, DWORD len, LPSTRRET src, const ITEMIDLIST *pidl)
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);
94 if (!dest)
95 return FALSE;
97 switch (src->uType)
99 case STRRET_WSTR:
100 lstrcpynW(dest, src->u.pOleStr, len);
101 CoTaskMemFree(src->u.pOleStr);
102 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;
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;
114 default:
115 FIXME("unknown type!\n");
116 if (len) *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 int WINAPI StrToOleStrA (LPWSTR lpWideCharStr, LPCSTR lpMultiByteString)
147 TRACE("(%p, %p %s)\n",
148 lpWideCharStr, lpMultiByteString, debugstr_a(lpMultiByteString));
150 return MultiByteToWideChar(0, 0, lpMultiByteString, -1, lpWideCharStr, MAX_PATH);
153 int WINAPI 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 BOOL WINAPI 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 (0, 0, lpStrA, nStr, lpWide, nWide);
179 BOOL WINAPI 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 0;
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 BOOL WINAPI 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 (0, 0, lpOle, nOle, lpStr, nStr, NULL, NULL);
205 BOOL WINAPI 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 0;
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 %ld)\n", debugstr_a(string), len);
247 wString = (LPWSTR)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 %ld) 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;