netstat: Initial implementation.
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 "wingdi.h"
31 #include "winuser.h"
32 #include "winreg.h"
33
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"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(shell);
42
43 /************************* STRRET functions ****************************/
44
45 static const char *debugstr_strret(STRRET *s)
46 {
47     switch (s->uType)
48     {
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_???";
57     }
58 }
59
60 BOOL WINAPI StrRetToStrNA(LPSTR dest, DWORD len, LPSTRRET src, const ITEMIDLIST *pidl)
61 {
62     TRACE("dest=%p len=0x%x strret=%p(%s) pidl=%p\n", dest, len, src, debugstr_strret(src), pidl);
63
64     if (!dest)
65         return FALSE;
66
67     switch (src->uType)
68     {
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;
84     }
85     TRACE("-- %s\n", debugstr_a(dest) );
86     return TRUE;
87 }
88
89 /************************************************************************/
90
91 BOOL WINAPI StrRetToStrNW(LPWSTR dest, DWORD len, LPSTRRET src, const ITEMIDLIST *pidl)
92 {
93     TRACE("dest=%p len=0x%x strret=%p(%s) pidl=%p\n", dest, len, src, debugstr_strret(src), pidl);
94
95     if (!dest)
96         return FALSE;
97
98     switch (src->uType)
99     {
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;
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 static int StrToOleStrA (LPWSTR lpWideCharStr, LPCSTR lpMultiByteString)
146 {
147         TRACE("(%p, %p %s)\n",
148         lpWideCharStr, lpMultiByteString, debugstr_a(lpMultiByteString));
149
150         return MultiByteToWideChar(CP_ACP, 0, lpMultiByteString, -1, lpWideCharStr, MAX_PATH);
151
152 }
153 static int 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 static BOOL 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 (CP_ACP, 0, lpStrA, nStr, lpWide, nWide);
178 }
179 static BOOL 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 static BOOL 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 (CP_ACP, 0, lpOle, nOle, lpStr, nStr, NULL, NULL);
203 }
204
205 static BOOL 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 %d)\n", debugstr_a(string), len);
247         wString = 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 %d) 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 }