shellstring.c 7.82 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright 2000 Juergen Schmied
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 22
#include <stdio.h>
#include <ctype.h>
23
#include <stdlib.h>
24

25
#define NONAMELESSUNION
26

27
#include "windef.h"
28
#include "winbase.h"
29
#include "winnls.h"
30 31
#include "wingdi.h"
#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 44

/************************* STRRET functions ****************************/

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
static const char *debugstr_strret(STRRET *s)
{
    switch (s->uType)
    {
        case STRRET_WSTR:
            return "STRRET_WSTR";
        case STRRET_CSTR:
            return "STRRET_CSTR";
        case STRRET_OFFSET:
            return "STRRET_OFFSET";
        default:
            return "STRRET_???";
    }
}

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 65 66
    if (!dest)
        return FALSE;

67 68 69 70 71 72 73 74 75 76 77 78 79
    switch (src->uType)
    {
        case STRRET_WSTR:
            WideCharToMultiByte(CP_ACP, 0, src->u.pOleStr, -1, dest, len, NULL, NULL);
            CoTaskMemFree(src->u.pOleStr);
            break;
        case STRRET_CSTR:
            lstrcpynA(dest, src->u.cStr, len);
            break;
        case STRRET_OFFSET:
            lstrcpynA(dest, ((LPCSTR)&pidl->mkid)+src->u.uOffset, len);
            break;
        default:
80
            FIXME("unknown type %u!\n", src->uType);
81 82 83 84 85 86
            if (len)
                *dest = '\0';
            return FALSE;
    }
    TRACE("-- %s\n", debugstr_a(dest) );
    return TRUE;
87 88
}

89
/************************************************************************/
Patrik Stridvall's avatar
Patrik Stridvall committed
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 96 97
    if (!dest)
        return FALSE;

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
    switch (src->uType)
    {
        case STRRET_WSTR:
            lstrcpynW(dest, src->u.pOleStr, len);
            CoTaskMemFree(src->u.pOleStr);
            break;
        case STRRET_CSTR:
            if (!MultiByteToWideChar(CP_ACP, 0, src->u.cStr, -1, dest, len) && len)
                dest[len-1] = 0;
            break;
        case STRRET_OFFSET:
            if (!MultiByteToWideChar(CP_ACP, 0, ((LPCSTR)&pidl->mkid)+src->u.uOffset, -1, dest, len)
                    && len)
                dest[len-1] = 0;
            break;
        default:
114
            FIXME("unknown type %u!\n", src->uType);
115 116 117 118 119
            if (len)
                *dest = '\0';
            return FALSE;
    }
    return TRUE;
120
}
121

Patrik Stridvall's avatar
Patrik Stridvall committed
122

123
/*************************************************************************
124
 * StrRetToStrN    [SHELL32.96]
125
 *
126 127 128 129 130
 * converts a STRRET to a normal string
 *
 * NOTES
 *  the pidl is for STRRET OFFSET
 */
131
BOOL WINAPI StrRetToStrNAW(LPVOID dest, DWORD len, LPSTRRET src, const ITEMIDLIST *pidl)
132
{
133
    if(SHELL_OsIsUnicode())
134 135 136
        return StrRetToStrNW(dest, len, src, pidl);
    else
        return StrRetToStrNA(dest, len, src, pidl);
137 138 139 140 141 142 143 144
}

/************************* OLESTR functions ****************************/

/************************************************************************
 *	StrToOleStr			[SHELL32.163]
 *
 */
145
static int StrToOleStrA (LPWSTR lpWideCharStr, LPCSTR lpMultiByteString)
146 147 148 149
{
	TRACE("(%p, %p %s)\n",
	lpWideCharStr, lpMultiByteString, debugstr_a(lpMultiByteString));

150
	return MultiByteToWideChar(CP_ACP, 0, lpMultiByteString, -1, lpWideCharStr, MAX_PATH);
151 152

}
153
static int StrToOleStrW (LPWSTR lpWideCharStr, LPCWSTR lpWString)
154 155 156 157
{
	TRACE("(%p, %p %s)\n",
	lpWideCharStr, lpWString, debugstr_w(lpWString));

158 159
	strcpyW (lpWideCharStr, lpWString );
	return strlenW(lpWideCharStr);
160 161 162 163
}

BOOL WINAPI StrToOleStrAW (LPWSTR lpWideCharStr, LPCVOID lpString)
{
164
	if (SHELL_OsIsUnicode())
165 166 167 168 169 170 171 172 173
	  return StrToOleStrW (lpWideCharStr, lpString);
	return StrToOleStrA (lpWideCharStr, lpString);
}

/*************************************************************************
 * StrToOleStrN					[SHELL32.79]
 *  lpMulti, nMulti, nWide [IN]
 *  lpWide [OUT]
 */
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 182 183 184 185
{
	TRACE("(%p, %x, %s, %x)\n", lpWide, nWide, debugstr_wn(lpStrW, nStr), nStr);

	if (lstrcpynW (lpWide, lpStrW, nWide))
	{ return lstrlenW (lpWide);
	}
186
	return FALSE;
187 188
}

189
BOOL WINAPI StrToOleStrNAW (LPWSTR lpWide, INT nWide, LPCVOID lpStr, INT nStr)
190
{
191
	if (SHELL_OsIsUnicode())
192 193 194 195 196 197 198
	  return StrToOleStrNW (lpWide, nWide, lpStr, nStr);
	return StrToOleStrNA (lpWide, nWide, lpStr, nStr);
}

/*************************************************************************
 * OleStrToStrN					[SHELL32.78]
 */
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 208 209 210 211
{
	TRACE("(%p, %x, %s, %x)\n", lpwStr, nwStr, debugstr_wn(lpOle,nOle), nOle);

	if (lstrcpynW ( lpwStr, lpOle, nwStr))
	{ return lstrlenW (lpwStr);
	}
212
        return FALSE;
213 214
}

215
BOOL WINAPI OleStrToStrNAW (LPVOID lpOut, INT nOut, LPCVOID lpIn, INT nIn)
216
{
217
	if (SHELL_OsIsUnicode())
218 219 220
	  return OleStrToStrNW (lpOut, nOut, lpIn, nIn);
	return OleStrToStrNA (lpOut, nOut, lpIn, nIn);
}
221 222 223


/*************************************************************************
224 225 226 227 228 229 230 231 232 233 234 235 236 237
 * CheckEscapesA             [SHELL32.@]
 *
 * Checks a string for special characters which are not allowed in a path
 * and encloses it in quotes if that is the case.
 *
 * PARAMS
 *  string     [I/O] string to check and on return eventually quoted
 *  len        [I]   length of string
 *
 * RETURNS
 *  length of actual string
 *
 * NOTES
 *  Not really sure if this function returns actually a value at all. 
238 239
 */
DWORD WINAPI CheckEscapesA(
240 241
	LPSTR	string,         /* [I/O]   string to check ??*/
	DWORD	len)            /* [I]      is 0 */
242
{
243 244 245
	LPWSTR wString;
	DWORD ret = 0;

246
	TRACE("(%s %d)\n", debugstr_a(string), len);
247
	wString = LocalAlloc(LPTR, len * sizeof(WCHAR));
248 249 250 251 252 253 254 255
	if (wString)
	{
	  MultiByteToWideChar(CP_ACP, 0, string, len, wString, len);
	  ret = CheckEscapesW(wString, len);
	  WideCharToMultiByte(CP_ACP, 0, wString, len, string, len, NULL, NULL);
	  LocalFree(wString);
	}
	return ret;
256 257
}

258 259 260 261 262
static const WCHAR strEscapedChars[] = {' ','"',',',';','^',0};

/*************************************************************************
 * CheckEscapesW             [SHELL32.@]
 *
263
 * See CheckEscapesA.
264
 */
265
DWORD WINAPI CheckEscapesW(
266 267
	LPWSTR	string,
	DWORD	len)
268
{
269 270 271
	DWORD size = lstrlenW(string);
	LPWSTR s, d;

272
	TRACE("(%s %d) stub\n", debugstr_w(string), len);
273 274 275 276 277 278 279 280 281 282 283 284 285

	if (StrPBrkW(string, strEscapedChars) && size + 2 <= len)
	{
	  s = &string[size - 1];
	  d = &string[size + 2];
	  *d-- = 0;
	  *d-- = '"';
	  for (;d > string;)
	    *d-- = *s--;
	  *d = '"';
	  return size + 2;
	}
	return size;
286
}