ReactOS  0.4.13-dev-259-g5ca9c9c
shellstring.c
Go to the documentation of this file.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18 
19 #define WIN32_NO_STATUS
20 #define _INC_WINDOWS
21 #define NONAMELESSUNION
22 #define NONAMELESSSTRUCT
23 
24 #include <windef.h>
25 #include <winbase.h>
26 #include <shlobj.h>
27 #include <shlwapi.h>
28 #include <wine/unicode.h>
29 #include <wine/debug.h>
30 
31 #include "shell32_main.h"
32 #include "undocshell.h"
33 
35 
36 /************************* STRRET functions ****************************/
37 
38 static const char *debugstr_strret(STRRET *s)
39 {
40  switch (s->uType)
41  {
42  case STRRET_WSTR:
43  return "STRRET_WSTR";
44  case STRRET_CSTR:
45  return "STRRET_CSTR";
46  case STRRET_OFFSET:
47  return "STRRET_OFFSET";
48  default:
49  return "STRRET_???";
50  }
51 }
52 
54 {
55  TRACE("dest=%p len=0x%x strret=%p(%s) pidl=%p\n", dest, len, src, debugstr_strret(src), pidl);
56 
57  if (!dest)
58  return FALSE;
59 
60  switch (src->uType)
61  {
62  case STRRET_WSTR:
63  WideCharToMultiByte(CP_ACP, 0, src->u.pOleStr, -1, dest, len, NULL, NULL);
64  CoTaskMemFree(src->u.pOleStr);
65  break;
66  case STRRET_CSTR:
67  lstrcpynA(dest, src->u.cStr, len);
68  break;
69  case STRRET_OFFSET:
70  lstrcpynA(dest, ((LPCSTR)&pidl->mkid)+src->u.uOffset, len);
71  break;
72  default:
73  FIXME("unknown type %u!\n", src->uType);
74  if (len)
75  *dest = '\0';
76  return FALSE;
77  }
78  TRACE("-- %s\n", debugstr_a(dest) );
79  return TRUE;
80 }
81 
82 /************************************************************************/
83 
85 {
86  TRACE("dest=%p len=0x%x strret=%p(%s) pidl=%p\n", dest, len, src, debugstr_strret(src), pidl);
87 
88  if (!dest)
89  return FALSE;
90 
91  switch (src->uType)
92  {
93  case STRRET_WSTR:
94  lstrcpynW(dest, src->u.pOleStr, len);
95  CoTaskMemFree(src->u.pOleStr);
96  break;
97  case STRRET_CSTR:
98  if (!MultiByteToWideChar(CP_ACP, 0, src->u.cStr, -1, dest, len) && len)
99  dest[len-1] = 0;
100  break;
101  case STRRET_OFFSET:
102  if (!MultiByteToWideChar(CP_ACP, 0, ((LPCSTR)&pidl->mkid)+src->u.uOffset, -1, dest, len)
103  && len)
104  dest[len-1] = 0;
105  break;
106  default:
107  FIXME("unknown type %u!\n", src->uType);
108  if (len)
109  *dest = '\0';
110  return FALSE;
111  }
112  return TRUE;
113 }
114 
115 
116 /*************************************************************************
117  * StrRetToStrN [SHELL32.96]
118  *
119  * converts a STRRET to a normal string
120  *
121  * NOTES
122  * the pidl is for STRRET OFFSET
123  */
125 {
126  if(SHELL_OsIsUnicode())
127  return StrRetToStrNW(dest, len, src, pidl);
128  else
129  return StrRetToStrNA(dest, len, src, pidl);
130 }
131 
132 /************************* OLESTR functions ****************************/
133 
134 /************************************************************************
135  * StrToOleStr [SHELL32.163]
136  *
137  */
138 static int StrToOleStrA (LPWSTR lpWideCharStr, LPCSTR lpMultiByteString)
139 {
140  TRACE("(%p, %p %s)\n",
141  lpWideCharStr, lpMultiByteString, debugstr_a(lpMultiByteString));
142 
143  return MultiByteToWideChar(CP_ACP, 0, lpMultiByteString, -1, lpWideCharStr, MAX_PATH);
144 
145 }
146 static int StrToOleStrW (LPWSTR lpWideCharStr, LPCWSTR lpWString)
147 {
148  TRACE("(%p, %p %s)\n",
149  lpWideCharStr, lpWString, debugstr_w(lpWString));
150 
151  strcpyW (lpWideCharStr, lpWString );
152  return strlenW(lpWideCharStr);
153 }
154 
155 BOOL WINAPI StrToOleStrAW (LPWSTR lpWideCharStr, LPCVOID lpString)
156 {
157  if (SHELL_OsIsUnicode())
158  return StrToOleStrW (lpWideCharStr, lpString);
159  return StrToOleStrA (lpWideCharStr, lpString);
160 }
161 
162 /*************************************************************************
163  * StrToOleStrN [SHELL32.79]
164  * lpMulti, nMulti, nWide [IN]
165  * lpWide [OUT]
166  */
167 static BOOL StrToOleStrNA (LPWSTR lpWide, INT nWide, LPCSTR lpStrA, INT nStr)
168 {
169  TRACE("(%p, %x, %s, %x)\n", lpWide, nWide, debugstr_an(lpStrA,nStr), nStr);
170  return MultiByteToWideChar (CP_ACP, 0, lpStrA, nStr, lpWide, nWide);
171 }
172 static BOOL StrToOleStrNW (LPWSTR lpWide, INT nWide, LPCWSTR lpStrW, INT nStr)
173 {
174  TRACE("(%p, %x, %s, %x)\n", lpWide, nWide, debugstr_wn(lpStrW, nStr), nStr);
175 
176  if (lstrcpynW (lpWide, lpStrW, nWide))
177  { return lstrlenW (lpWide);
178  }
179  return FALSE;
180 }
181 
182 BOOL WINAPI StrToOleStrNAW (LPWSTR lpWide, INT nWide, LPCVOID lpStr, INT nStr)
183 {
184  if (SHELL_OsIsUnicode())
185  return StrToOleStrNW (lpWide, nWide, lpStr, nStr);
186  return StrToOleStrNA (lpWide, nWide, lpStr, nStr);
187 }
188 
189 /*************************************************************************
190  * OleStrToStrN [SHELL32.78]
191  */
192 static BOOL OleStrToStrNA (LPSTR lpStr, INT nStr, LPCWSTR lpOle, INT nOle)
193 {
194  TRACE("(%p, %x, %s, %x)\n", lpStr, nStr, debugstr_wn(lpOle,nOle), nOle);
195  return WideCharToMultiByte (CP_ACP, 0, lpOle, nOle, lpStr, nStr, NULL, NULL);
196 }
197 
198 static BOOL OleStrToStrNW (LPWSTR lpwStr, INT nwStr, LPCWSTR lpOle, INT nOle)
199 {
200  TRACE("(%p, %x, %s, %x)\n", lpwStr, nwStr, debugstr_wn(lpOle,nOle), nOle);
201 
202  if (lstrcpynW ( lpwStr, lpOle, nwStr))
203  { return lstrlenW (lpwStr);
204  }
205  return FALSE;
206 }
207 
209 {
210  if (SHELL_OsIsUnicode())
211  return OleStrToStrNW (lpOut, nOut, lpIn, nIn);
212  return OleStrToStrNA (lpOut, nOut, lpIn, nIn);
213 }
214 
215 
216 /*************************************************************************
217  * CheckEscapesA [SHELL32.@]
218  *
219  * Checks a string for special characters which are not allowed in a path
220  * and encloses it in quotes if that is the case.
221  *
222  * PARAMS
223  * string [I/O] string to check and on return eventually quoted
224  * len [I] length of string
225  *
226  * RETURNS
227  * length of actual string
228  *
229  * NOTES
230  * Not really sure if this function returns actually a value at all.
231  */
233  LPSTR string, /* [I/O] string to check ??*/
234  DWORD len) /* [I] is 0 */
235 {
236  LPWSTR wString;
237  DWORD ret = 0;
238 
239  TRACE("(%s %d)\n", debugstr_a(string), len);
240  wString = LocalAlloc(LPTR, len * sizeof(WCHAR));
241  if (wString)
242  {
243  MultiByteToWideChar(CP_ACP, 0, string, len, wString, len);
244  ret = CheckEscapesW(wString, len);
245  WideCharToMultiByte(CP_ACP, 0, wString, len, string, len, NULL, NULL);
246  LocalFree(wString);
247  }
248  return ret;
249 }
250 
251 static const WCHAR strEscapedChars[] = {' ','"',',',';','^',0};
252 
253 /*************************************************************************
254  * CheckEscapesW [SHELL32.@]
255  *
256  * See CheckEscapesA.
257  */
259  LPWSTR string,
260  DWORD len)
261 {
262  DWORD size = lstrlenW(string);
263  LPWSTR s, d;
264 
265  TRACE("(%s %d) stub\n", debugstr_w(string), len);
266 
267  if (StrPBrkW(string, strEscapedChars) && size + 2 <= len)
268  {
269  s = &string[size - 1];
270  d = &string[size + 2];
271  *d-- = 0;
272  *d-- = '"';
273  for (;d > string;)
274  *d-- = *s--;
275  *d = '"';
276  return size + 2;
277  }
278  return size;
279 }
LPWSTR WINAPI StrPBrkW(LPCWSTR lpszStr, LPCWSTR lpszMatch)
Definition: string.c:1276
#define TRUE
Definition: types.h:120
VOID WINAPI CoTaskMemFree(LPVOID ptr)
Definition: ifs.c:422
WINE_UNICODE_INLINE unsigned int strlenW(const WCHAR *str)
Definition: unicode.h:212
#define WideCharToMultiByte
Definition: compat.h:101
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185
#define CP_ACP
Definition: compat.h:99
static BOOL OleStrToStrNA(LPSTR lpStr, INT nStr, LPCWSTR lpOle, INT nOle)
Definition: shellstring.c:192
static __inline BOOL SHELL_OsIsUnicode(void)
Definition: shell32_main.h:130
char * LPSTR
Definition: xmlstorage.h:182
#define lstrlenW
Definition: compat.h:407
int32_t INT
Definition: typedefs.h:56
#define lstrcpynW
Definition: compat.h:397
static int StrToOleStrA(LPWSTR lpWideCharStr, LPCSTR lpMultiByteString)
Definition: shellstring.c:138
static const WCHAR strEscapedChars[]
Definition: shellstring.c:251
unsigned int BOOL
Definition: ntddk_ex.h:94
static BOOL StrToOleStrNW(LPWSTR lpWide, INT nWide, LPCWSTR lpStrW, INT nStr)
Definition: shellstring.c:172
#define debugstr_w
Definition: kernel32.h:32
#define FIXME(fmt,...)
Definition: debug.h:110
smooth NULL
Definition: ftsmooth.c:416
static BOOL StrToOleStrNA(LPWSTR lpWide, INT nWide, LPCSTR lpStrA, INT nStr)
Definition: shellstring.c:167
const char * LPCSTR
Definition: xmlstorage.h:183
#define TRACE(s)
Definition: solgame.cpp:4
GLsizeiptr size
Definition: glext.h:5919
#define d
Definition: ke_i.h:81
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define debugstr_a
Definition: kernel32.h:31
WINE_DEFAULT_DEBUG_CHANNEL(shell)
#define MAX_PATH
Definition: compat.h:26
#define WINAPI
Definition: msvc.h:8
unsigned long DWORD
Definition: ntddk_ex.h:95
int nOut
Definition: unzcrash.c:44
int ret
static __inline const char * debugstr_an(const char *s, int n)
Definition: compat.h:47
DWORD WINAPI CheckEscapesW(LPWSTR string, DWORD len)
Definition: shellstring.c:258
GLenum GLsizei len
Definition: glext.h:6722
GLdouble s
Definition: gl.h:2039
DWORD WINAPI CheckEscapesA(LPSTR string, DWORD len)
Definition: shellstring.c:232
GLenum src
Definition: glext.h:6340
#define debugstr_wn
Definition: kernel32.h:33
char string[160]
Definition: util.h:11
WINE_UNICODE_INLINE WCHAR * strcpyW(WCHAR *dst, const WCHAR *src)
Definition: unicode.h:219
static const char * debugstr_strret(STRRET *s)
Definition: shellstring.c:38
void shell(int argc, const char *argv[])
Definition: cmds.c:1231
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1577
#define lstrcpynA
Definition: compat.h:408
#define MultiByteToWideChar
Definition: compat.h:100
CONST void * LPCVOID
Definition: windef.h:191
static int StrToOleStrW(LPWSTR lpWideCharStr, LPCWSTR lpWString)
Definition: shellstring.c:146
#define LPTR
Definition: winbase.h:362
static BOOL OleStrToStrNW(LPWSTR lpwStr, INT nwStr, LPCWSTR lpOle, INT nOle)
Definition: shellstring.c:198
static char * dest
Definition: rtl.c:135
SHITEMID mkid
Definition: shtypes.idl:34
HLOCAL NTAPI LocalAlloc(UINT uFlags, SIZE_T dwBytes)
Definition: heapmem.c:1373
WCHAR * LPWSTR
Definition: xmlstorage.h:184
BOOL WINAPI StrToOleStrNAW(LPWSTR lpWide, INT nWide, LPCVOID lpStr, INT nStr)
Definition: shellstring.c:182
BOOL WINAPI StrRetToStrNAW(LPVOID dest, DWORD len, LPSTRRET src, const ITEMIDLIST *pidl)
Definition: shellstring.c:124
BOOL WINAPI StrToOleStrAW(LPWSTR lpWideCharStr, LPCVOID lpString)
Definition: shellstring.c:155
BOOL WINAPI StrRetToStrNA(LPSTR dest, DWORD len, LPSTRRET src, const ITEMIDLIST *pidl)
Definition: shellstring.c:53
BOOL WINAPI StrRetToStrNW(LPWSTR dest, DWORD len, LPSTRRET src, const ITEMIDLIST *pidl)
Definition: shellstring.c:84
int nIn
Definition: unzcrash.c:44
BOOL WINAPI OleStrToStrNAW(LPVOID lpOut, INT nOut, LPCVOID lpIn, INT nIn)
Definition: shellstring.c:208