string.c 71.8 KB
Newer Older
1 2 3 4
/*
 * Shlwapi string functions
 *
 * Copyright 1998 Juergen Schmied
5
 * Copyright 2002 Jon Griffiths
6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 21
 */

22 23 24
#include "config.h"
#include "wine/port.h"

25
#include <math.h>
26
#include <stdarg.h>
27 28 29
#include <stdio.h>
#include <string.h>

30 31
#define NONAMELESSUNION
#define NONAMELESSSTRUCT
32
#include "windef.h"
33
#include "winbase.h"
Jon Griffiths's avatar
Jon Griffiths committed
34
#define NO_SHLWAPI_REG
35
#define NO_SHLWAPI_STREAM
36
#include "shlwapi.h"
37 38
#include "wingdi.h"
#include "winuser.h"
39
#include "shlobj.h"
40
#include "mlang.h"
Jon Griffiths's avatar
Jon Griffiths committed
41
#include "ddeml.h"
42
#include "wine/unicode.h"
43
#include "wine/debug.h"
44

45 46
#include "resource.h"

47
WINE_DEFAULT_DEBUG_CHANNEL(shell);
48

49
extern HINSTANCE shlwapi_hInstance;
Jon Griffiths's avatar
Jon Griffiths committed
50

51 52
static HRESULT _SHStrDupAA(LPCSTR,LPSTR*);
static HRESULT _SHStrDupAW(LPCWSTR,LPSTR*);
53

54

55 56
static void FillNumberFmt(NUMBERFMTW *fmt, LPWSTR decimal_buffer, int decimal_bufwlen,
                          LPWSTR thousand_buffer, int thousand_bufwlen)
57 58 59 60 61 62 63
{
  WCHAR grouping[64];
  WCHAR *c;

  GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_ILZERO|LOCALE_RETURN_NUMBER, (LPWSTR)&fmt->LeadingZero, sizeof(fmt->LeadingZero)/sizeof(WCHAR));
  GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_INEGNUMBER|LOCALE_RETURN_NUMBER, (LPWSTR)&fmt->LeadingZero, sizeof(fmt->NegativeOrder)/sizeof(WCHAR));
  fmt->NumDigits = 0;
64 65
  GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, decimal_buffer, decimal_bufwlen);
  GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, thousand_buffer, thousand_bufwlen);
66 67
  fmt->lpThousandSep = thousand_buffer;
  fmt->lpDecimalSep = decimal_buffer;
68

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
  /* 
   * Converting grouping string to number as described on 
   * http://blogs.msdn.com/oldnewthing/archive/2006/04/18/578251.aspx
   */
  fmt->Grouping = 0;
  GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SGROUPING, grouping, sizeof(grouping)/sizeof(WCHAR));
  for (c = grouping; *c; c++)
    if (*c >= '0' && *c < '9')
    {
      fmt->Grouping *= 10;
      fmt->Grouping += *c - '0';
    }

  if (fmt->Grouping % 10 == 0)
    fmt->Grouping /= 10;
  else
    fmt->Grouping *= 10;
}

88 89 90 91 92 93
/*************************************************************************
 * FormatInt   [internal]
 *
 * Format an integer according to the current locale
 *
 * RETURNS
94
 *  The number of characters written on success or 0 on failure
95 96 97 98 99 100 101 102 103
 */
static int FormatInt(LONGLONG qdwValue, LPWSTR pszBuf, int cchBuf)
{
  NUMBERFMTW fmt;
  WCHAR decimal[8], thousand[8];
  WCHAR buf[24];
  WCHAR *c;
  BOOL neg = (qdwValue < 0);

104 105 106
  FillNumberFmt(&fmt, decimal, sizeof decimal / sizeof (WCHAR),
                thousand, sizeof thousand / sizeof (WCHAR));

107 108 109 110 111 112 113 114 115 116 117 118 119
  c = &buf[24];
  *(--c) = 0;
  do
  {
    *(--c) = '0' + (qdwValue%10);
    qdwValue /= 10;
  } while (qdwValue > 0);
  if (neg)
    *(--c) = '-';
  
  return GetNumberFormatW(LOCALE_USER_DEFAULT, 0, c, &fmt, pszBuf, cchBuf);
}

120 121 122 123 124 125 126
/*************************************************************************
 * FormatDouble   [internal]
 *
 * Format an integer according to the current locale. Prints the specified number of digits
 * after the decimal point
 *
 * RETURNS
127
 *  The number of characters written on success or 0 on failure
128 129 130 131 132 133 134 135 136 137
 */
static int FormatDouble(double value, int decimals, LPWSTR pszBuf, int cchBuf)
{
  static const WCHAR flfmt[] = {'%','f',0};
  WCHAR buf[64];
  NUMBERFMTW fmt;
  WCHAR decimal[8], thousand[8];
  
  snprintfW(buf, 64, flfmt, value);

138 139
  FillNumberFmt(&fmt, decimal, sizeof decimal / sizeof (WCHAR),
                 thousand, sizeof thousand / sizeof (WCHAR));
140 141 142 143
  fmt.NumDigits = decimals;
  return GetNumberFormatW(LOCALE_USER_DEFAULT, 0, buf, &fmt, pszBuf, cchBuf);
}

144
/*************************************************************************
145
 * SHLWAPI_ChrCmpHelperA
146
 *
147 148 149
 * Internal helper for SHLWAPI_ChrCmpA/ChrCMPIA.
 *
 * NOTES
150
 *  Both this function and its Unicode counterpart are very inefficient. To
151 152 153 154 155
 *  fix this, CompareString must be completely implemented and optimised
 *  first. Then the core character test can be taken out of that function and
 *  placed here, so that it need never be called at all. Until then, do not
 *  attempt to optimise this code unless you are willing to test that it
 *  still performs correctly.
156
 */
157
static BOOL SHLWAPI_ChrCmpHelperA(WORD ch1, WORD ch2, DWORD dwFlags)
158
{
159 160 161
  char str1[3], str2[3];

  str1[0] = LOBYTE(ch1);
162
  if (IsDBCSLeadByte(str1[0]))
163 164 165 166 167 168 169 170
  {
    str1[1] = HIBYTE(ch1);
    str1[2] = '\0';
  }
  else
    str1[1] = '\0';

  str2[0] = LOBYTE(ch2);
171
  if (IsDBCSLeadByte(str2[0]))
172 173 174 175 176 177 178
  {
    str2[1] = HIBYTE(ch2);
    str2[2] = '\0';
  }
  else
    str2[1] = '\0';

179
  return CompareStringA(GetThreadLocale(), dwFlags, str1, -1, str2, -1) - CSTR_EQUAL;
180 181
}

182
/*************************************************************************
183 184 185
 * SHLWAPI_ChrCmpA
 *
 * Internal helper function.
186
 */
187
static BOOL WINAPI SHLWAPI_ChrCmpA(WORD ch1, WORD ch2)
188
{
189
  return SHLWAPI_ChrCmpHelperA(ch1, ch2, 0);
190 191 192
}

/*************************************************************************
Jon Griffiths's avatar
Jon Griffiths committed
193
 * ChrCmpIA	(SHLWAPI.385)
194
 *
195 196 197 198 199 200 201 202 203
 * Compare two characters, ignoring case.
 *
 * PARAMS
 *  ch1 [I] First character to compare
 *  ch2 [I] Second character to compare
 *
 * RETURNS
 *  FALSE, if the characters are equal.
 *  Non-zero otherwise.
204
 */
205
BOOL WINAPI ChrCmpIA(WORD ch1, WORD ch2)
206
{
207 208 209
  TRACE("(%d,%d)\n", ch1, ch2);

  return SHLWAPI_ChrCmpHelperA(ch1, ch2, NORM_IGNORECASE);
210 211 212
}

/*************************************************************************
213
 * ChrCmpIW	[SHLWAPI.386]
214 215
 *
 * See ChrCmpIA.
216
 */
217
BOOL WINAPI ChrCmpIW(WCHAR ch1, WCHAR ch2)
218
{
219
  return CompareStringW(GetThreadLocale(), NORM_IGNORECASE, &ch1, 1, &ch2, 1) - CSTR_EQUAL;
220 221 222
}

/*************************************************************************
223 224 225 226 227 228 229 230 231 232 233 234
 * StrChrA	[SHLWAPI.@]
 *
 * Find a given character in a string.
 *
 * PARAMS
 *  lpszStr [I] String to search in.
 *  ch      [I] Character to search for.
 *
 * RETURNS
 *  Success: A pointer to the first occurrence of ch in lpszStr, or NULL if
 *           not found.
 *  Failure: NULL, if any arguments are invalid.
235
 */
236
LPSTR WINAPI StrChrA(LPCSTR lpszStr, WORD ch)
237
{
238 239 240 241 242 243 244 245 246 247 248 249
  TRACE("(%s,%i)\n", debugstr_a(lpszStr), ch);

  if (lpszStr)
  {
    while (*lpszStr)
    {
      if (!SHLWAPI_ChrCmpA(*lpszStr, ch))
        return (LPSTR)lpszStr;
      lpszStr = CharNextA(lpszStr);
    }
  }
  return NULL;
250 251 252
}

/*************************************************************************
253 254 255
 * StrChrW	[SHLWAPI.@]
 *
 * See StrChrA.
256
 */
257
LPWSTR WINAPI StrChrW(LPCWSTR lpszStr, WCHAR ch)
258
{
259 260 261 262 263 264 265
  LPWSTR lpszRet = NULL;

  TRACE("(%s,%i)\n", debugstr_w(lpszStr), ch);

  if (lpszStr)
    lpszRet = strchrW(lpszStr, ch);
  return lpszRet;
266 267 268
}

/*************************************************************************
269 270 271 272 273 274 275 276 277 278 279 280
 * StrChrIA	[SHLWAPI.@]
 *
 * Find a given character in a string, ignoring case.
 *
 * PARAMS
 *  lpszStr [I] String to search in.
 *  ch      [I] Character to search for.
 *
 * RETURNS
 *  Success: A pointer to the first occurrence of ch in lpszStr, or NULL if
 *           not found.
 *  Failure: NULL, if any arguments are invalid.
281
 */
282
LPSTR WINAPI StrChrIA(LPCSTR lpszStr, WORD ch)
283
{
284 285 286 287 288 289 290 291 292 293 294 295
  TRACE("(%s,%i)\n", debugstr_a(lpszStr), ch);

  if (lpszStr)
  {
    while (*lpszStr)
    {
      if (!ChrCmpIA(*lpszStr, ch))
        return (LPSTR)lpszStr;
      lpszStr = CharNextA(lpszStr);
    }
  }
  return NULL;
296 297
}

298
/*************************************************************************
299 300 301
 * StrChrIW	[SHLWAPI.@]
 *
 * See StrChrA.
302
 */
303
LPWSTR WINAPI StrChrIW(LPCWSTR lpszStr, WCHAR ch)
304
{
305 306 307 308 309 310 311 312 313
  TRACE("(%s,%i)\n", debugstr_w(lpszStr), ch);

  if (lpszStr)
  {
    ch = toupperW(ch);
    while (*lpszStr)
    {
      if (toupperW(*lpszStr) == ch)
        return (LPWSTR)lpszStr;
314
      lpszStr++;
315 316 317 318
    }
    lpszStr = NULL;
  }
  return (LPWSTR)lpszStr;
319 320
}

321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
/*************************************************************************
 * StrChrNW	[SHLWAPI.@]
 */
LPWSTR WINAPI StrChrNW(LPCWSTR lpszStr, WCHAR ch, UINT cchMax)
{
  TRACE("(%s(%i),%i)\n", debugstr_wn(lpszStr,cchMax), cchMax, ch);

  if (lpszStr)
  {
    while (*lpszStr && cchMax-- > 0)
    {
      if (*lpszStr == ch)
        return (LPWSTR)lpszStr;
      lpszStr++;
    }
  }
  return NULL;
}

340
/*************************************************************************
341 342 343 344 345 346 347 348 349 350 351
 * StrCmpIW	[SHLWAPI.@]
 *
 * Compare two strings, ignoring case.
 *
 * PARAMS
 *  lpszStr  [I] First string to compare
 *  lpszComp [I] Second string to compare
 *
 * RETURNS
 *  An integer less than, equal to or greater than 0, indicating that
 *  lpszStr is less than, the same, or greater than lpszComp.
352
 */
353
int WINAPI StrCmpIW(LPCWSTR lpszStr, LPCWSTR lpszComp)
354
{
355
  TRACE("(%s,%s)\n", debugstr_w(lpszStr),debugstr_w(lpszComp));
356
  return CompareStringW(GetThreadLocale(), NORM_IGNORECASE, lpszStr, -1, lpszComp, -1) - CSTR_EQUAL;
357
}
358 359

/*************************************************************************
360 361 362 363 364 365 366
 * StrCmpNA	[SHLWAPI.@]
 *
 * Compare two strings, up to a maximum length.
 *
 * PARAMS
 *  lpszStr  [I] First string to compare
 *  lpszComp [I] Second string to compare
367
 *  iLen     [I] Number of chars to compare
368 369 370 371
 *
 * RETURNS
 *  An integer less than, equal to or greater than 0, indicating that
 *  lpszStr is less than, the same, or greater than lpszComp.
372
 */
373
INT WINAPI StrCmpNA(LPCSTR lpszStr, LPCSTR lpszComp, INT iLen)
374
{
375
  TRACE("(%s,%s,%i)\n", debugstr_a(lpszStr), debugstr_a(lpszComp), iLen);
376
  return CompareStringA(GetThreadLocale(), 0, lpszStr, iLen, lpszComp, iLen) - CSTR_EQUAL;
377 378
}

379 380 381 382 383 384 385 386
/*************************************************************************
 * StrCmpNW	[SHLWAPI.@]
 *
 * See StrCmpNA.
 */
INT WINAPI StrCmpNW(LPCWSTR lpszStr, LPCWSTR lpszComp, INT iLen)
{
  TRACE("(%s,%s,%i)\n", debugstr_w(lpszStr), debugstr_w(lpszComp), iLen);
387
  return CompareStringW(GetThreadLocale(), 0, lpszStr, iLen, lpszComp, iLen) - CSTR_EQUAL;
388
}
389

390
/*************************************************************************
391 392 393 394 395 396 397
 * StrCmpNIA	[SHLWAPI.@]
 *
 * Compare two strings, up to a maximum length, ignoring case.
 *
 * PARAMS
 *  lpszStr  [I] First string to compare
 *  lpszComp [I] Second string to compare
398
 *  iLen     [I] Number of chars to compare
399 400 401 402
 *
 * RETURNS
 *  An integer less than, equal to or greater than 0, indicating that
 *  lpszStr is less than, the same, or greater than lpszComp.
403
 */
404
int WINAPI StrCmpNIA(LPCSTR lpszStr, LPCSTR lpszComp, int iLen)
405
{
406
  TRACE("(%s,%s,%i)\n", debugstr_a(lpszStr), debugstr_a(lpszComp), iLen);
407
  return CompareStringA(GetThreadLocale(), NORM_IGNORECASE, lpszStr, iLen, lpszComp, iLen) - CSTR_EQUAL;
408 409
}

410 411 412 413 414 415 416 417
/*************************************************************************
 * StrCmpNIW	[SHLWAPI.@]
 *
 * See StrCmpNIA.
 */
INT WINAPI StrCmpNIW(LPCWSTR lpszStr, LPCWSTR lpszComp, int iLen)
{
  TRACE("(%s,%s,%i)\n", debugstr_w(lpszStr), debugstr_w(lpszComp), iLen);
418
  return CompareStringW(GetThreadLocale(), NORM_IGNORECASE, lpszStr, iLen, lpszComp, iLen) - CSTR_EQUAL;
419
}
420

421
/*************************************************************************
422 423 424 425 426 427 428 429 430 431 432
 * StrCmpW	[SHLWAPI.@]
 *
 * Compare two strings.
 *
 * PARAMS
 *  lpszStr  [I] First string to compare
 *  lpszComp [I] Second string to compare
 *
 * RETURNS
 *  An integer less than, equal to or greater than 0, indicating that
 *  lpszStr is less than, the same, or greater than lpszComp.
433
 */
434
int WINAPI StrCmpW(LPCWSTR lpszStr, LPCWSTR lpszComp)
435
{
436
  TRACE("(%s,%s)\n", debugstr_w(lpszStr), debugstr_w(lpszComp));
437
  return CompareStringW(GetThreadLocale(), 0, lpszStr, -1, lpszComp, -1) - CSTR_EQUAL;
438 439 440 441 442
}

/*************************************************************************
 * StrCatW	[SHLWAPI.@]
 *
Austin English's avatar
Austin English committed
443
 * Concatenate two strings.
444 445 446
 *
 * PARAMS
 *  lpszStr [O] Initial string
Austin English's avatar
Austin English committed
447
 *  lpszSrc [I] String to concatenate
448 449 450 451 452 453 454 455
 *
 * RETURNS
 *  lpszStr.
 */
LPWSTR WINAPI StrCatW(LPWSTR lpszStr, LPCWSTR lpszSrc)
{
  TRACE("(%s,%s)\n", debugstr_w(lpszStr), debugstr_w(lpszSrc));

456 457
  if (lpszStr && lpszSrc)
    strcatW(lpszStr, lpszSrc);
458 459 460
  return lpszStr;
}

461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
/*************************************************************************
 * StrCatChainW	[SHLWAPI.@]
 *
 * Concatenates two unicode strings.
 *
 * PARAMS
 *  lpszStr [O] Initial string
 *  cchMax  [I] Length of destination buffer
 *  ichAt   [I] Offset from the destination buffer to begin concatenation
 *  lpszCat [I] String to concatenate
 *
 * RETURNS
 *  The offset from the beginning of pszDst to the terminating NULL.
 */
DWORD WINAPI StrCatChainW(LPWSTR lpszStr, DWORD cchMax, DWORD ichAt, LPCWSTR lpszCat)
{
  TRACE("(%s,%u,%d,%s)\n", debugstr_w(lpszStr), cchMax, ichAt, debugstr_w(lpszCat));

  if (ichAt == -1)
    ichAt = strlenW(lpszStr);

  if (!cchMax)
    return ichAt;

  if (ichAt == cchMax)
    ichAt--;

  if (lpszCat && ichAt < cchMax)
  {
    lpszStr += ichAt;
    while (ichAt < cchMax - 1 && *lpszCat)
    {
      *lpszStr++ = *lpszCat++;
      ichAt++;
    }
    *lpszStr = 0;
  }

  return ichAt;
}

502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
/*************************************************************************
 * StrCpyW	[SHLWAPI.@]
 *
 * Copy a string to another string.
 *
 * PARAMS
 *  lpszStr [O] Destination string
 *  lpszSrc [I] Source string
 *
 * RETURNS
 *  lpszStr.
 */
LPWSTR WINAPI StrCpyW(LPWSTR lpszStr, LPCWSTR lpszSrc)
{
  TRACE("(%p,%s)\n", lpszStr, debugstr_w(lpszSrc));

518 519
  if (lpszStr && lpszSrc)
    strcpyW(lpszStr, lpszSrc);
520 521 522 523 524 525 526 527 528
  return lpszStr;
}

/*************************************************************************
 * StrCpyNW	[SHLWAPI.@]
 *
 * Copy a string to another string, up to a maximum number of characters.
 *
 * PARAMS
529 530 531
 *  dst    [O] Destination string
 *  src    [I] Source string
 *  count  [I] Maximum number of chars to copy
532 533
 *
 * RETURNS
534
 *  dst.
535
 */
536
LPWSTR WINAPI StrCpyNW(LPWSTR dst, LPCWSTR src, int count)
537
{
538 539
  LPWSTR d = dst;
  LPCWSTR s = src;
540

541
  TRACE("(%p,%s,%i)\n", dst, debugstr_w(src), count);
542

543 544 545 546 547 548 549 550 551
  if (s)
  {
    while ((count > 1) && *s)
    {
      count--;
      *d++ = *s++;
    }
  }
  if (count) *d = 0;
552

553 554
  return dst;
}
555 556 557 558 559 560

/*************************************************************************
 * SHLWAPI_StrStrHelperA
 *
 * Internal implementation of StrStrA/StrStrIA
 */
561
static LPSTR SHLWAPI_StrStrHelperA(LPCSTR lpszStr, LPCSTR lpszSearch,
562
                                   INT (WINAPI *pStrCmpFn)(LPCSTR,LPCSTR,INT))
563 564
{
  size_t iLen;
565
  LPCSTR end;
566 567

  if (!lpszStr || !lpszSearch || !*lpszSearch)
568
    return NULL;
569 570

  iLen = strlen(lpszSearch);
571
  end = lpszStr + strlen(lpszStr);
572

573
  while (lpszStr + iLen <= end)
574 575 576 577 578 579
  {
    if (!pStrCmpFn(lpszStr, lpszSearch, iLen))
      return (LPSTR)lpszStr;
    lpszStr = CharNextA(lpszStr);
  }
  return NULL;
580 581
}

582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
/*************************************************************************
 * StrStrA	[SHLWAPI.@]
 *
 * Find a substring within a string.
 *
 * PARAMS
 *  lpszStr    [I] String to search in
 *  lpszSearch [I] String to look for
 *
 * RETURNS
 *  The start of lpszSearch within lpszStr, or NULL if not found.
 */
LPSTR WINAPI StrStrA(LPCSTR lpszStr, LPCSTR lpszSearch)
{
  TRACE("(%s,%s)\n", debugstr_a(lpszStr), debugstr_a(lpszSearch));

598
  return SHLWAPI_StrStrHelperA(lpszStr, lpszSearch, StrCmpNA);
599 600 601
}

/*************************************************************************
602 603 604 605 606 607
 * StrStrW	[SHLWAPI.@]
 *
 * See StrStrA.
 */
LPWSTR WINAPI StrStrW(LPCWSTR lpszStr, LPCWSTR lpszSearch)
{
608 609 610
    TRACE("(%s, %s)\n", debugstr_w(lpszStr), debugstr_w(lpszSearch));

    if (!lpszStr || !lpszSearch || !*lpszSearch) return NULL;
611
    return strstrW( lpszStr, lpszSearch );
612 613 614 615 616
}

/*************************************************************************
 * StrRStrIA	[SHLWAPI.@]
 *
617
 * Find the last occurrence of a substring within a string.
618 619 620 621 622 623 624
 *
 * PARAMS
 *  lpszStr    [I] String to search in
 *  lpszEnd    [I] End of lpszStr
 *  lpszSearch [I] String to look for
 *
 * RETURNS
625
 *  The last occurrence lpszSearch within lpszStr, or NULL if not found.
626
 */
627
LPSTR WINAPI StrRStrIA(LPCSTR lpszStr, LPCSTR lpszEnd, LPCSTR lpszSearch)
628
{
629
  LPSTR lpszRet = NULL;
630 631 632 633 634 635 636 637 638
  WORD ch1, ch2;
  INT iLen;

  TRACE("(%s,%s)\n", debugstr_a(lpszStr), debugstr_a(lpszSearch));

  if (!lpszStr || !lpszSearch || !*lpszSearch)
    return NULL;

  if (IsDBCSLeadByte(*lpszSearch))
639
    ch1 = *lpszSearch << 8 | (UCHAR)lpszSearch[1];
640 641 642 643
  else
    ch1 = *lpszSearch;
  iLen = lstrlenA(lpszSearch);

644 645 646 647 648 649
  if (!lpszEnd)
    lpszEnd = lpszStr + lstrlenA(lpszStr);
  else /* reproduce the broken behaviour on Windows */
    lpszEnd += min(iLen - 1, lstrlenA(lpszEnd));

  while (lpszStr + iLen <= lpszEnd && *lpszStr)
650
  {
651
    ch2 = IsDBCSLeadByte(*lpszStr)? *lpszStr << 8 | (UCHAR)lpszStr[1] : *lpszStr;
652
    if (!ChrCmpIA(ch1, ch2))
653
    {
654 655
      if (!StrCmpNIA(lpszStr, lpszSearch, iLen))
        lpszRet = (LPSTR)lpszStr;
656
    }
657 658 659
    lpszStr = CharNextA(lpszStr);
  }
  return lpszRet;
660 661 662
}

/*************************************************************************
663 664 665
 * StrRStrIW	[SHLWAPI.@]
 *
 * See StrRStrIA.
666
 */
667
LPWSTR WINAPI StrRStrIW(LPCWSTR lpszStr, LPCWSTR lpszEnd, LPCWSTR lpszSearch)
668
{
669
  LPWSTR lpszRet = NULL;
670 671 672 673 674 675 676
  INT iLen;

  TRACE("(%s,%s)\n", debugstr_w(lpszStr), debugstr_w(lpszSearch));

  if (!lpszStr || !lpszSearch || !*lpszSearch)
    return NULL;

677 678
  iLen = strlenW(lpszSearch);

679 680
  if (!lpszEnd)
    lpszEnd = lpszStr + strlenW(lpszStr);
681 682
  else /* reproduce the broken behaviour on Windows */
    lpszEnd += min(iLen - 1, lstrlenW(lpszEnd));
683

684
  while (lpszStr + iLen <= lpszEnd && *lpszStr)
685
  {
686 687 688 689 690 691
    if (!ChrCmpIW(*lpszSearch, *lpszStr))
    {
      if (!StrCmpNIW(lpszStr, lpszSearch, iLen))
        lpszRet = (LPWSTR)lpszStr;
    }
    lpszStr++;
692
  }
693
  return lpszRet;
694 695 696
}

/*************************************************************************
697 698 699 700 701 702 703 704 705 706
 * StrStrIA	[SHLWAPI.@]
 *
 * Find a substring within a string, ignoring case.
 *
 * PARAMS
 *  lpszStr    [I] String to search in
 *  lpszSearch [I] String to look for
 *
 * RETURNS
 *  The start of lpszSearch within lpszStr, or NULL if not found.
707
 */
708
LPSTR WINAPI StrStrIA(LPCSTR lpszStr, LPCSTR lpszSearch)
709
{
710 711
  TRACE("(%s,%s)\n", debugstr_a(lpszStr), debugstr_a(lpszSearch));

712
  return SHLWAPI_StrStrHelperA(lpszStr, lpszSearch, StrCmpNIA);
713 714 715
}

/*************************************************************************
716 717 718
 * StrStrIW	[SHLWAPI.@]
 *
 * See StrStrIA.
719
 */
720
LPWSTR WINAPI StrStrIW(LPCWSTR lpszStr, LPCWSTR lpszSearch)
721
{
722
  int iLen;
723
  LPCWSTR end;
724

725
  TRACE("(%s,%s)\n", debugstr_w(lpszStr), debugstr_w(lpszSearch));
726

727 728 729 730
  if (!lpszStr || !lpszSearch || !*lpszSearch)
    return NULL;

  iLen = strlenW(lpszSearch);
731
  end = lpszStr + strlenW(lpszStr);
732

733
  while (lpszStr + iLen <= end)
734 735 736 737 738 739
  {
    if (!StrCmpNIW(lpszStr, lpszSearch, iLen))
      return (LPWSTR)lpszStr;
    lpszStr++;
  }
  return NULL;
740 741
}

742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775
/*************************************************************************
 * StrStrNW	[SHLWAPI.@]
 *
 * Find a substring within a string up to a given number of initial characters.
 *
 * PARAMS
 *  lpFirst    [I] String to search in
 *  lpSrch     [I] String to look for
 *  cchMax     [I] Maximum number of initial search characters
 *
 * RETURNS
 *  The start of lpFirst within lpSrch, or NULL if not found.
 */
LPWSTR WINAPI StrStrNW(LPCWSTR lpFirst, LPCWSTR lpSrch, UINT cchMax)
{
    UINT i;
    int len;

    TRACE("(%s, %s, %u)\n", debugstr_w(lpFirst), debugstr_w(lpSrch), cchMax);

    if (!lpFirst || !lpSrch || !*lpSrch || !cchMax)
        return NULL;

    len = strlenW(lpSrch);

    for (i = cchMax; *lpFirst && (i > 0); i--, lpFirst++)
    {
        if (!strncmpW(lpFirst, lpSrch, len))
            return (LPWSTR)lpFirst;
    }

    return NULL;
}

776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
/*************************************************************************
 * StrStrNIW	[SHLWAPI.@]
 *
 * Find a substring within a string up to a given number of initial characters,
 * ignoring case.
 *
 * PARAMS
 *  lpFirst    [I] String to search in
 *  lpSrch     [I] String to look for
 *  cchMax     [I] Maximum number of initial search characters
 *
 * RETURNS
 *  The start of lpFirst within lpSrch, or NULL if not found.
 */
LPWSTR WINAPI StrStrNIW(LPCWSTR lpFirst, LPCWSTR lpSrch, UINT cchMax)
{
    UINT i;
    int len;

    TRACE("(%s, %s, %u)\n", debugstr_w(lpFirst), debugstr_w(lpSrch), cchMax);

    if (!lpFirst || !lpSrch || !*lpSrch || !cchMax)
        return NULL;

    len = strlenW(lpSrch);

    for (i = cchMax; *lpFirst && (i > 0); i--, lpFirst++)
    {
        if (!strncmpiW(lpFirst, lpSrch, len))
            return (LPWSTR)lpFirst;
    }

    return NULL;
}

811
/*************************************************************************
812 813
 * StrToIntA	[SHLWAPI.@]
 *
814
 * Read a signed integer from a string.
815 816 817 818 819
 *
 * PARAMS
 *  lpszStr [I] String to read integer from
 *
 * RETURNS
820
 *   The signed integer value represented by the string, or 0 if no integer is
821 822 823 824
 *   present.
 *
 * NOTES
 *  No leading space is allowed before the number, although a leading '-' is.
825
 */
826
int WINAPI StrToIntA(LPCSTR lpszStr)
827
{
828 829 830 831 832 833 834 835 836 837 838 839 840
  int iRet = 0;

  TRACE("(%s)\n", debugstr_a(lpszStr));

  if (!lpszStr)
  {
    WARN("Invalid lpszStr would crash under Win32!\n");
    return 0;
  }

  if (*lpszStr == '-' || isdigit(*lpszStr))
    StrToIntExA(lpszStr, 0, &iRet);
  return iRet;
841 842 843
}

/*************************************************************************
844 845 846
 * StrToIntW	[SHLWAPI.@]
 *
 * See StrToIntA.
847
 */
848
int WINAPI StrToIntW(LPCWSTR lpszStr)
849
{
850 851 852 853 854 855 856 857 858 859 860 861 862
  int iRet = 0;

  TRACE("(%s)\n", debugstr_w(lpszStr));

  if (!lpszStr)
  {
    WARN("Invalid lpszStr would crash under Win32!\n");
    return 0;
  }

  if (*lpszStr == '-' || isdigitW(*lpszStr))
    StrToIntExW(lpszStr, 0, &iRet);
  return iRet;
863 864
}

865
/*************************************************************************
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880
 * StrToIntExA	[SHLWAPI.@]
 *
 * Read an integer from a string.
 *
 * PARAMS
 *  lpszStr [I] String to read integer from
 *  dwFlags [I] Flags controlling the conversion
 *  lpiRet  [O] Destination for read integer.
 *
 * RETURNS
 *  Success: TRUE. lpiRet contains the integer value represented by the string.
 *  Failure: FALSE, if the string is invalid, or no number is present.
 *
 * NOTES
 *  Leading whitespace, '-' and '+' are allowed before the number. If
881
 *  dwFlags includes STIF_SUPPORT_HEX, hexadecimal numbers are allowed, if
882
 *  preceded by '0x'. If this flag is not set, or there is no '0x' prefix,
883
 *  the string is treated as a decimal string. A leading '-' is ignored for
884
 *  hexadecimal numbers.
885
 */
886
BOOL WINAPI StrToIntExA(LPCSTR lpszStr, DWORD dwFlags, LPINT lpiRet)
887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
{
  LONGLONG li;
  BOOL bRes;

  TRACE("(%s,%08X,%p)\n", debugstr_a(lpszStr), dwFlags, lpiRet);

  bRes = StrToInt64ExA(lpszStr, dwFlags, &li);
  if (bRes) *lpiRet = li;
  return bRes;
}

/*************************************************************************
 * StrToInt64ExA	[SHLWAPI.@]
 *
 * See StrToIntExA.
 */
BOOL WINAPI StrToInt64ExA(LPCSTR lpszStr, DWORD dwFlags, LONGLONG *lpiRet)
904
{
905
  BOOL bNegative = FALSE;
906
  LONGLONG iRet = 0;
907

908
  TRACE("(%s,%08X,%p)\n", debugstr_a(lpszStr), dwFlags, lpiRet);
909 910 911 912 913 914

  if (!lpszStr || !lpiRet)
  {
    WARN("Invalid parameter would crash under Win32!\n");
    return FALSE;
  }
915
  if (dwFlags > STIF_SUPPORT_HEX) WARN("Unknown flags %08x\n", dwFlags);
916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963

  /* Skip leading space, '+', '-' */
  while (isspace(*lpszStr))
    lpszStr = CharNextA(lpszStr);

  if (*lpszStr == '-')
  {
    bNegative = TRUE;
    lpszStr++;
  }
  else if (*lpszStr == '+')
    lpszStr++;

  if (dwFlags & STIF_SUPPORT_HEX &&
      *lpszStr == '0' && tolower(lpszStr[1]) == 'x')
  {
    /* Read hex number */
    lpszStr += 2;

    if (!isxdigit(*lpszStr))
      return FALSE;

    while (isxdigit(*lpszStr))
    {
      iRet = iRet * 16;
      if (isdigit(*lpszStr))
        iRet += (*lpszStr - '0');
      else
        iRet += 10 + (tolower(*lpszStr) - 'a');
      lpszStr++;
    }
    *lpiRet = iRet;
    return TRUE;
  }

  /* Read decimal number */
  if (!isdigit(*lpszStr))
    return FALSE;

  while (isdigit(*lpszStr))
  {
    iRet = iRet * 10;
    iRet += (*lpszStr - '0');
    lpszStr++;
  }
  *lpiRet = bNegative ? -iRet : iRet;
  return TRUE;
}
964

965 966 967 968 969 970
/*************************************************************************
 * StrToIntExW	[SHLWAPI.@]
 *
 * See StrToIntExA.
 */
BOOL WINAPI StrToIntExW(LPCWSTR lpszStr, DWORD dwFlags, LPINT lpiRet)
971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987
{
  LONGLONG li;
  BOOL bRes;

  TRACE("(%s,%08X,%p)\n", debugstr_w(lpszStr), dwFlags, lpiRet);

  bRes = StrToInt64ExW(lpszStr, dwFlags, &li);
  if (bRes) *lpiRet = li;
  return bRes;
}

/*************************************************************************
 * StrToInt64ExW	[SHLWAPI.@]
 *
 * See StrToIntExA.
 */
BOOL WINAPI StrToInt64ExW(LPCWSTR lpszStr, DWORD dwFlags, LONGLONG *lpiRet)
988 989
{
  BOOL bNegative = FALSE;
990
  LONGLONG iRet = 0;
991

992
  TRACE("(%s,%08X,%p)\n", debugstr_w(lpszStr), dwFlags, lpiRet);
993 994 995 996 997 998

  if (!lpszStr || !lpiRet)
  {
    WARN("Invalid parameter would crash under Win32!\n");
    return FALSE;
  }
999
  if (dwFlags > STIF_SUPPORT_HEX) WARN("Unknown flags %08x\n", dwFlags);
1000 1001

  /* Skip leading space, '+', '-' */
1002
  while (isspaceW(*lpszStr)) lpszStr++;
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046

  if (*lpszStr == '-')
  {
    bNegative = TRUE;
    lpszStr++;
  }
  else if (*lpszStr == '+')
    lpszStr++;

  if (dwFlags & STIF_SUPPORT_HEX &&
      *lpszStr == '0' && tolowerW(lpszStr[1]) == 'x')
  {
    /* Read hex number */
    lpszStr += 2;

    if (!isxdigitW(*lpszStr))
      return FALSE;

    while (isxdigitW(*lpszStr))
    {
      iRet = iRet * 16;
      if (isdigitW(*lpszStr))
        iRet += (*lpszStr - '0');
      else
        iRet += 10 + (tolowerW(*lpszStr) - 'a');
      lpszStr++;
    }
    *lpiRet = iRet;
    return TRUE;
  }

  /* Read decimal number */
  if (!isdigitW(*lpszStr))
    return FALSE;

  while (isdigitW(*lpszStr))
  {
    iRet = iRet * 10;
    iRet += (*lpszStr - '0');
    lpszStr++;
  }
  *lpiRet = bNegative ? -iRet : iRet;
  return TRUE;
}
1047

1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
/*************************************************************************
 * StrDupA	[SHLWAPI.@]
 *
 * Duplicate a string.
 *
 * PARAMS
 *  lpszStr [I] String to duplicate.
 *
 * RETURNS
 *  Success: A pointer to a new string containing the contents of lpszStr
 *  Failure: NULL, if memory cannot be allocated
 *
 * NOTES
Jon Griffiths's avatar
Jon Griffiths committed
1061 1062
 *  The string memory is allocated with LocalAlloc(), and so should be released
 *  by calling LocalFree().
1063 1064 1065 1066 1067 1068 1069 1070 1071
 */
LPSTR WINAPI StrDupA(LPCSTR lpszStr)
{
  int iLen;
  LPSTR lpszRet;

  TRACE("(%s)\n",debugstr_a(lpszStr));

  iLen = lpszStr ? strlen(lpszStr) + 1 : 1;
1072
  lpszRet = LocalAlloc(LMEM_FIXED, iLen);
1073 1074 1075 1076 1077 1078 1079 1080 1081

  if (lpszRet)
  {
    if (lpszStr)
      memcpy(lpszRet, lpszStr, iLen);
    else
      *lpszRet = '\0';
  }
  return lpszRet;
1082 1083 1084
}

/*************************************************************************
1085 1086 1087
 * StrDupW	[SHLWAPI.@]
 *
 * See StrDupA.
1088
 */
1089
LPWSTR WINAPI StrDupW(LPCWSTR lpszStr)
1090
{
1091 1092 1093 1094 1095 1096
  int iLen;
  LPWSTR lpszRet;

  TRACE("(%s)\n",debugstr_w(lpszStr));

  iLen = (lpszStr ? strlenW(lpszStr) + 1 : 1) * sizeof(WCHAR);
1097
  lpszRet = LocalAlloc(LMEM_FIXED, iLen);
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107

  if (lpszRet)
  {
    if (lpszStr)
      memcpy(lpszRet, lpszStr, iLen);
    else
      *lpszRet = '\0';
  }
  return lpszRet;
}
1108

1109 1110 1111 1112 1113
/*************************************************************************
 * SHLWAPI_StrSpnHelperA
 *
 * Internal implementation of StrSpnA/StrCSpnA/StrCSpnIA
 */
1114 1115 1116
static int SHLWAPI_StrSpnHelperA(LPCSTR lpszStr, LPCSTR lpszMatch,
                                 LPSTR (WINAPI *pStrChrFn)(LPCSTR,WORD),
                                 BOOL bInvert)
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
{
  LPCSTR lpszRead = lpszStr;
  if (lpszStr && *lpszStr && lpszMatch)
  {
    while (*lpszRead)
    {
      LPCSTR lpszTest = pStrChrFn(lpszMatch, *lpszRead);

      if (!bInvert && !lpszTest)
        break;
      if (bInvert && lpszTest)
        break;
      lpszRead = CharNextA(lpszRead);
    };
  }
  return lpszRead - lpszStr;
}
1134 1135

/*************************************************************************
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
 * StrSpnA	[SHLWAPI.@]
 *
 * Find the length of the start of a string that contains only certain
 * characters.
 *
 * PARAMS
 *  lpszStr   [I] String to search
 *  lpszMatch [I] Characters that can be in the substring
 *
 * RETURNS
 *  The length of the part of lpszStr containing only chars from lpszMatch,
 *  or 0 if any parameter is invalid.
1148
 */
1149
int WINAPI StrSpnA(LPCSTR lpszStr, LPCSTR lpszMatch)
1150
{
1151
  TRACE("(%s,%s)\n",debugstr_a(lpszStr), debugstr_a(lpszMatch));
1152

1153 1154
  return SHLWAPI_StrSpnHelperA(lpszStr, lpszMatch, StrChrA, FALSE);
}
1155

1156 1157 1158 1159 1160 1161 1162
/*************************************************************************
 * StrSpnW	[SHLWAPI.@]
 *
 * See StrSpnA.
 */
int WINAPI StrSpnW(LPCWSTR lpszStr, LPCWSTR lpszMatch)
{
1163 1164
    if (!lpszStr || !lpszMatch) return 0;
    return strspnW( lpszStr, lpszMatch );
1165 1166 1167
}

/*************************************************************************
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
 * StrCSpnA	[SHLWAPI.@]
 *
 * Find the length of the start of a string that does not contain certain
 * characters.
 *
 * PARAMS
 *  lpszStr   [I] String to search
 *  lpszMatch [I] Characters that cannot be in the substring
 *
 * RETURNS
 *  The length of the part of lpszStr containing only chars not in lpszMatch,
 *  or 0 if any parameter is invalid.
1180
 */
1181
int WINAPI StrCSpnA(LPCSTR lpszStr, LPCSTR lpszMatch)
1182
{
1183
  TRACE("(%s,%s)\n",debugstr_a(lpszStr), debugstr_a(lpszMatch));
1184

1185 1186
  return SHLWAPI_StrSpnHelperA(lpszStr, lpszMatch, StrChrA, TRUE);
}
1187

1188 1189 1190 1191 1192 1193 1194
/*************************************************************************
 * StrCSpnW	[SHLWAPI.@]
 *
 * See StrCSpnA.
 */
int WINAPI StrCSpnW(LPCWSTR lpszStr, LPCWSTR lpszMatch)
{
1195 1196
    if (!lpszStr || !lpszMatch) return 0;
    return strcspnW( lpszStr, lpszMatch );
1197 1198
}

1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
/*************************************************************************
 * StrCSpnIA	[SHLWAPI.@]
 *
 * Find the length of the start of a string that does not contain certain
 * characters, ignoring case.
 *
 * PARAMS
 *  lpszStr   [I] String to search
 *  lpszMatch [I] Characters that cannot be in the substring
 *
 * RETURNS
 *  The length of the part of lpszStr containing only chars not in lpszMatch,
 *  or 0 if any parameter is invalid.
 */
int WINAPI StrCSpnIA(LPCSTR lpszStr, LPCSTR lpszMatch)
{
  TRACE("(%s,%s)\n",debugstr_a(lpszStr), debugstr_a(lpszMatch));

  return SHLWAPI_StrSpnHelperA(lpszStr, lpszMatch, StrChrIA, TRUE);
}

/*************************************************************************
 * StrCSpnIW	[SHLWAPI.@]
1222
 *
1223
 * See StrCSpnIA.
1224
 */
1225
int WINAPI StrCSpnIW(LPCWSTR lpszStr, LPCWSTR lpszMatch)
1226
{
1227 1228
  LPCWSTR lpszRead = lpszStr;

1229
  TRACE("(%s,%s)\n",debugstr_w(lpszStr), debugstr_w(lpszMatch));
1230

1231 1232 1233 1234 1235 1236 1237 1238 1239
  if (lpszStr && *lpszStr && lpszMatch)
  {
    while (*lpszRead)
    {
      if (StrChrIW(lpszMatch, *lpszRead)) break;
      lpszRead++;
    }
  }
  return lpszRead - lpszStr;
1240
}
1241

1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261
/*************************************************************************
 * StrPBrkA	[SHLWAPI.@]
 *
 * Search a string for any of a group of characters.
 *
 * PARAMS
 *  lpszStr   [I] String to search
 *  lpszMatch [I] Characters to match
 *
 * RETURNS
 *  A pointer to the first matching character in lpszStr, or NULL if no
 *  match was found.
 */
LPSTR WINAPI StrPBrkA(LPCSTR lpszStr, LPCSTR lpszMatch)
{
  TRACE("(%s,%s)\n",debugstr_a(lpszStr), debugstr_a(lpszMatch));

  if (lpszStr && lpszMatch && *lpszMatch)
  {
    while (*lpszStr)
1262
    {
1263 1264 1265
      if (StrChrA(lpszMatch, *lpszStr))
        return (LPSTR)lpszStr;
      lpszStr = CharNextA(lpszStr);
1266
    }
1267 1268
  }
  return NULL;
1269 1270
}

1271 1272 1273 1274 1275 1276 1277
/*************************************************************************
 * StrPBrkW	[SHLWAPI.@]
 *
 * See StrPBrkA.
 */
LPWSTR WINAPI StrPBrkW(LPCWSTR lpszStr, LPCWSTR lpszMatch)
{
1278 1279
    if (!lpszStr || !lpszMatch) return NULL;
    return strpbrkW( lpszStr, lpszMatch );
1280
}
1281

1282 1283
/*************************************************************************
 * SHLWAPI_StrRChrHelperA
1284
 *
1285
 * Internal implementation of StrRChrA/StrRChrIA.
1286
 */
1287 1288 1289
static LPSTR SHLWAPI_StrRChrHelperA(LPCSTR lpszStr,
                                    LPCSTR lpszEnd, WORD ch,
                                    BOOL (WINAPI *pChrCmpFn)(WORD,WORD))
1290
{
1291
  LPCSTR lpszRet = NULL;
1292

1293 1294 1295
  if (lpszStr)
  {
    WORD ch2;
1296

1297 1298
    if (!lpszEnd)
      lpszEnd = lpszStr + lstrlenA(lpszStr);
1299

1300 1301 1302
    while (*lpszStr && lpszStr <= lpszEnd)
    {
      ch2 = IsDBCSLeadByte(*lpszStr)? *lpszStr << 8 | lpszStr[1] : *lpszStr;
1303

1304 1305 1306 1307 1308 1309 1310
      if (!pChrCmpFn(ch, ch2))
        lpszRet = lpszStr;
      lpszStr = CharNextA(lpszStr);
    }
  }
  return (LPSTR)lpszRet;
}
1311

1312 1313 1314
/**************************************************************************
 * StrRChrA	[SHLWAPI.@]
 *
1315
 * Find the last occurrence of a character in string.
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332
 *
 * PARAMS
 *  lpszStr [I] String to search in
 *  lpszEnd [I] Place to end search, or NULL to search until the end of lpszStr
 *  ch      [I] Character to search for.
 *
 * RETURNS
 *  Success: A pointer to the last occurrence of ch in lpszStr before lpszEnd,
 *           or NULL if not found.
 *  Failure: NULL, if any arguments are invalid.
 */
LPSTR WINAPI StrRChrA(LPCSTR lpszStr, LPCSTR lpszEnd, WORD ch)
{
  TRACE("(%s,%s,%x)\n", debugstr_a(lpszStr), debugstr_a(lpszEnd), ch);

  return SHLWAPI_StrRChrHelperA(lpszStr, lpszEnd, ch, SHLWAPI_ChrCmpA);
}
1333 1334

/**************************************************************************
1335
 * StrRChrW	[SHLWAPI.@]
1336
 *
1337
 * See StrRChrA.
1338
 */
1339
LPWSTR WINAPI StrRChrW(LPCWSTR str, LPCWSTR end, WORD ch)
1340
{
1341
    WCHAR *ret = NULL;
1342

1343 1344 1345 1346 1347 1348 1349 1350
    if (!str) return NULL;
    if (!end) end = str + strlenW(str);
    while (str < end)
    {
        if (*str == ch) ret = (WCHAR *)str;
        str++;
    }
    return ret;
1351
}
1352

1353 1354 1355
/**************************************************************************
 * StrRChrIA	[SHLWAPI.@]
 *
1356
 * Find the last occurrence of a character in string, ignoring case.
1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
 *
 * PARAMS
 *  lpszStr [I] String to search in
 *  lpszEnd [I] Place to end search, or NULL to search until the end of lpszStr
 *  ch      [I] Character to search for.
 *
 * RETURNS
 *  Success: A pointer to the last occurrence of ch in lpszStr before lpszEnd,
 *           or NULL if not found.
 *  Failure: NULL, if any arguments are invalid.
 */
LPSTR WINAPI StrRChrIA(LPCSTR lpszStr, LPCSTR lpszEnd, WORD ch)
{
  TRACE("(%s,%s,%x)\n", debugstr_a(lpszStr), debugstr_a(lpszEnd), ch);
1371

1372
  return SHLWAPI_StrRChrHelperA(lpszStr, lpszEnd, ch, ChrCmpIA);
1373 1374
}

1375 1376 1377 1378 1379
/**************************************************************************
 * StrRChrIW	[SHLWAPI.@]
 *
 * See StrRChrIA.
 */
1380
LPWSTR WINAPI StrRChrIW(LPCWSTR str, LPCWSTR end, WORD ch)
1381
{
1382
    WCHAR *ret = NULL;
1383

1384 1385 1386 1387 1388 1389 1390 1391
    if (!str) return NULL;
    if (!end) end = str + strlenW(str);
    while (str < end)
    {
        if (!ChrCmpIW(*str, ch)) ret = (WCHAR *)str;
        str++;
    }
    return ret;
1392
}
1393

1394
/*************************************************************************
1395
 * StrCatBuffA	[SHLWAPI.@]
1396
 *
1397
 * Concatenate two strings together.
1398
 *
1399 1400 1401 1402 1403 1404 1405 1406 1407
 * PARAMS
 *  lpszStr [O] String to concatenate to
 *  lpszCat [I] String to add to lpszCat
 *  cchMax  [I] Maximum number of characters for the whole string
 *
 * RETURNS
 *  lpszStr.
 *
 * NOTES
Jon Griffiths's avatar
Jon Griffiths committed
1408
 *  cchMax determines the number of characters in the final length of the
1409
 *  string, not the number appended to lpszStr from lpszCat.
1410
 */
1411
LPSTR WINAPI StrCatBuffA(LPSTR lpszStr, LPCSTR lpszCat, INT cchMax)
1412
{
1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424
  INT iLen;

  TRACE("(%p,%s,%d)\n", lpszStr, debugstr_a(lpszCat), cchMax);

  if (!lpszStr)
  {
    WARN("Invalid lpszStr would crash under Win32!\n");
    return NULL;
  }

  iLen = strlen(lpszStr);
  cchMax -= iLen;
1425

1426 1427 1428
  if (cchMax > 0)
    StrCpyNA(lpszStr + iLen, lpszCat, cchMax);
  return lpszStr;
1429 1430 1431
}

/*************************************************************************
1432
 * StrCatBuffW	[SHLWAPI.@]
1433
 *
1434
 * See StrCatBuffA.
1435
 */
1436
LPWSTR WINAPI StrCatBuffW(LPWSTR lpszStr, LPCWSTR lpszCat, INT cchMax)
1437
{
1438 1439 1440 1441 1442 1443 1444 1445 1446
  INT iLen;

  TRACE("(%p,%s,%d)\n", lpszStr, debugstr_w(lpszCat), cchMax);

  if (!lpszStr)
  {
    WARN("Invalid lpszStr would crash under Win32!\n");
    return NULL;
  }
1447

1448 1449 1450 1451 1452 1453
  iLen = strlenW(lpszStr);
  cchMax -= iLen;

  if (cchMax > 0)
    StrCpyNW(lpszStr + iLen, lpszCat, cchMax);
  return lpszStr;
1454 1455 1456 1457
}

/*************************************************************************
 * StrRetToBufA					[SHLWAPI.@]
1458
 *
1459
 * Convert a STRRET to a normal string.
1460
 *
1461 1462
 * PARAMS
 *  lpStrRet [O] STRRET to convert
1463
 *  pIdl     [I] ITEMIDLIST for lpStrRet->uType == STRRET_OFFSET
1464 1465
 *  lpszDest [O] Destination for normal string
 *  dwLen    [I] Length of lpszDest
1466
 *
1467 1468 1469
 * RETURNS
 *  Success: S_OK. lpszDest contains up to dwLen characters of the string.
 *           If lpStrRet is of type STRRET_WSTR, its memory is freed with
Jon Griffiths's avatar
Jon Griffiths committed
1470
 *           CoTaskMemFree() and its type set to STRRET_CSTRA.
1471
 *  Failure: E_FAIL, if any parameters are invalid.
1472
 */
1473
HRESULT WINAPI StrRetToBufA (LPSTRRET src, const ITEMIDLIST *pidl, LPSTR dest, UINT len)
1474
{
1475 1476 1477 1478 1479
	/* NOTE:
	 *  This routine is identical to that in dlls/shell32/shellstring.c.
	 *  It was duplicated because not every version of Shlwapi.dll exports
	 *  StrRetToBufA. If you change one routine, change them both.
	 */
1480
        TRACE("dest=%p len=0x%x strret=%p pidl=%p\n", dest, len, src, pidl);
1481

1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494
	if (!src)
	{
	  WARN("Invalid lpStrRet would crash under Win32!\n");
	  if (dest)
	    *dest = '\0';
	  return E_FAIL;
	}

	if (!dest || !len)
	  return E_FAIL;

	*dest = '\0';

1495 1496 1497
	switch (src->uType)
	{
	  case STRRET_WSTR:
1498
	    WideCharToMultiByte(CP_ACP, 0, src->u.pOleStr, -1, dest, len, NULL, NULL);
1499
	    CoTaskMemFree(src->u.pOleStr);
1500 1501
	    break;

1502
	  case STRRET_CSTR:
1503
            lstrcpynA(dest, src->u.cStr, len);
1504 1505
	    break;

1506
	  case STRRET_OFFSET:
1507 1508 1509 1510 1511
	    lstrcpynA((LPSTR)dest, ((LPCSTR)&pidl->mkid)+src->u.uOffset, len);
	    break;

	  default:
	    FIXME("unknown type!\n");
1512
	    return FALSE;
1513 1514 1515 1516 1517
	}
	return S_OK;
}

/*************************************************************************
1518
 * StrRetToBufW	[SHLWAPI.@]
1519
 *
1520
 * See StrRetToBufA.
1521
 */
1522
HRESULT WINAPI StrRetToBufW (LPSTRRET src, const ITEMIDLIST *pidl, LPWSTR dest, UINT len)
1523
{
1524
        TRACE("dest=%p len=0x%x strret=%p pidl=%p\n", dest, len, src, pidl);
1525

1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538
	if (!src)
	{
	  WARN("Invalid lpStrRet would crash under Win32!\n");
	  if (dest)
	    *dest = '\0';
	  return E_FAIL;
	}

	if (!dest || !len)
	  return E_FAIL;

	*dest = '\0';

1539 1540 1541
	switch (src->uType)
	{
	  case STRRET_WSTR:
1542
            lstrcpynW(dest, src->u.pOleStr, len);
1543
	    CoTaskMemFree(src->u.pOleStr);
1544 1545
	    break;

1546
	  case STRRET_CSTR:
1547
              if (!MultiByteToWideChar( CP_ACP, 0, src->u.cStr, -1, dest, len ))
1548
                  dest[len-1] = 0;
1549 1550
	    break;

1551
	  case STRRET_OFFSET:
1552 1553
	    if (pidl)
	    {
1554
              if (!MultiByteToWideChar( CP_ACP, 0, ((LPCSTR)&pidl->mkid)+src->u.uOffset, -1,
1555
                                        dest, len ))
1556
                  dest[len-1] = 0;
1557 1558 1559 1560 1561
	    }
	    break;

	  default:
	    FIXME("unknown type!\n");
1562
	    return FALSE;
1563 1564 1565 1566
	}
	return S_OK;
}

1567 1568 1569
/*************************************************************************
 * StrRetToStrA					[SHLWAPI.@]
 *
Jon Griffiths's avatar
Jon Griffiths committed
1570 1571 1572 1573
 * Converts a STRRET to a normal string.
 *
 * PARAMS
 *  lpStrRet [O] STRRET to convert
1574
 *  pidl     [I] ITEMIDLIST for lpStrRet->uType == STRRET_OFFSET
Jon Griffiths's avatar
Jon Griffiths committed
1575 1576 1577 1578 1579
 *  ppszName [O] Destination for converted string
 *
 * RETURNS
 *  Success: S_OK. ppszName contains the new string, allocated with CoTaskMemAlloc().
 *  Failure: E_FAIL, if any parameters are invalid.
1580
 */
Jon Griffiths's avatar
Jon Griffiths committed
1581
HRESULT WINAPI StrRetToStrA(LPSTRRET lpStrRet, const ITEMIDLIST *pidl, LPSTR *ppszName)
1582
{
Jon Griffiths's avatar
Jon Griffiths committed
1583
  HRESULT hRet = E_FAIL;
1584

Jon Griffiths's avatar
Jon Griffiths committed
1585 1586 1587 1588 1589 1590
  switch (lpStrRet->uType)
  {
  case STRRET_WSTR:
    hRet = _SHStrDupAW(lpStrRet->u.pOleStr, ppszName);
    CoTaskMemFree(lpStrRet->u.pOleStr);
    break;
1591

Jon Griffiths's avatar
Jon Griffiths committed
1592 1593 1594
  case STRRET_CSTR:
    hRet = _SHStrDupAA(lpStrRet->u.cStr, ppszName);
    break;
1595

Jon Griffiths's avatar
Jon Griffiths committed
1596 1597 1598
  case STRRET_OFFSET:
    hRet = _SHStrDupAA(((LPCSTR)&pidl->mkid) + lpStrRet->u.uOffset, ppszName);
    break;
1599

Jon Griffiths's avatar
Jon Griffiths committed
1600 1601 1602 1603 1604
  default:
    *ppszName = NULL;
  }

  return hRet;
1605 1606 1607 1608 1609
}

/*************************************************************************
 * StrRetToStrW					[SHLWAPI.@]
 *
Jon Griffiths's avatar
Jon Griffiths committed
1610
 * See StrRetToStrA.
1611
 */
Jon Griffiths's avatar
Jon Griffiths committed
1612
HRESULT WINAPI StrRetToStrW(LPSTRRET lpStrRet, const ITEMIDLIST *pidl, LPWSTR *ppszName)
1613
{
Jon Griffiths's avatar
Jon Griffiths committed
1614
  HRESULT hRet = E_FAIL;
1615

Jon Griffiths's avatar
Jon Griffiths committed
1616 1617 1618 1619 1620 1621
  switch (lpStrRet->uType)
  {
  case STRRET_WSTR:
    hRet = SHStrDupW(lpStrRet->u.pOleStr, ppszName);
    CoTaskMemFree(lpStrRet->u.pOleStr);
    break;
1622

Jon Griffiths's avatar
Jon Griffiths committed
1623 1624 1625
  case STRRET_CSTR:
    hRet = SHStrDupA(lpStrRet->u.cStr, ppszName);
    break;
1626

Jon Griffiths's avatar
Jon Griffiths committed
1627 1628 1629
  case STRRET_OFFSET:
    hRet = SHStrDupA(((LPCSTR)&pidl->mkid) + lpStrRet->u.uOffset, ppszName);
    break;
1630

Jon Griffiths's avatar
Jon Griffiths committed
1631 1632 1633 1634 1635
  default:
    *ppszName = NULL;
  }

  return hRet;
1636 1637
}

1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702
/* Create an ASCII string copy using SysAllocString() */
static HRESULT _SHStrDupAToBSTR(LPCSTR src, BSTR *pBstrOut)
{
    *pBstrOut = NULL;

    if (src)
    {
        INT len = MultiByteToWideChar(CP_ACP, 0, src, -1, NULL, 0);
        WCHAR* szTemp = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));

        if (szTemp)
        {
            MultiByteToWideChar(CP_ACP, 0, src, -1, szTemp, len);
            *pBstrOut = SysAllocString(szTemp);
            HeapFree(GetProcessHeap(), 0, szTemp);

            if (*pBstrOut)
                return S_OK;
        }
    }
    return E_OUTOFMEMORY;
}

/*************************************************************************
 * StrRetToBSTR	[SHLWAPI.@]
 *
 * Converts a STRRET to a BSTR.
 *
 * PARAMS
 *  lpStrRet [O] STRRET to convert
 *  pidl     [I] ITEMIDLIST for lpStrRet->uType = STRRET_OFFSET
 *  pBstrOut [O] Destination for converted BSTR
 *
 * RETURNS
 *  Success: S_OK. pBstrOut contains the new string.
 *  Failure: E_FAIL, if any parameters are invalid.
 */
HRESULT WINAPI StrRetToBSTR(STRRET *lpStrRet, LPCITEMIDLIST pidl, BSTR* pBstrOut)
{
  HRESULT hRet = E_FAIL;

  switch (lpStrRet->uType)
  {
  case STRRET_WSTR:
    *pBstrOut = SysAllocString(lpStrRet->u.pOleStr);
    if (*pBstrOut)
      hRet = S_OK;
    CoTaskMemFree(lpStrRet->u.pOleStr);
    break;

  case STRRET_CSTR:
    hRet = _SHStrDupAToBSTR(lpStrRet->u.cStr, pBstrOut);
    break;

  case STRRET_OFFSET:
    hRet = _SHStrDupAToBSTR(((LPCSTR)&pidl->mkid) + lpStrRet->u.uOffset, pBstrOut);
    break;

  default:
    *pBstrOut = NULL;
  }

  return hRet;
}

1703
/*************************************************************************
1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717
 * StrFormatKBSizeA	[SHLWAPI.@]
 *
 * Create a formatted string containing a byte count in Kilobytes.
 *
 * PARAMS
 *  llBytes  [I] Byte size to format
 *  lpszDest [I] Destination for formatted string
 *  cchMax   [I] Size of lpszDest
 *
 * RETURNS
 *  lpszDest.
 */
LPSTR WINAPI StrFormatKBSizeA(LONGLONG llBytes, LPSTR lpszDest, UINT cchMax)
{
1718 1719 1720 1721 1722 1723
  WCHAR wszBuf[256];
  
  if (!StrFormatKBSizeW(llBytes, wszBuf, 256))
    return NULL;
  if (!WideCharToMultiByte(CP_ACP, 0, wszBuf, -1, lpszDest, cchMax, NULL, NULL))
    return NULL;
1724 1725 1726 1727 1728 1729 1730 1731 1732 1733
  return lpszDest;
}

/*************************************************************************
 * StrFormatKBSizeW	[SHLWAPI.@]
 *
 * See StrFormatKBSizeA.
 */
LPWSTR WINAPI StrFormatKBSizeW(LONGLONG llBytes, LPWSTR lpszDest, UINT cchMax)
{
1734 1735 1736
  static const WCHAR kb[] = {' ','K','B',0};
  LONGLONG llKB = (llBytes + 1023) >> 10;
  int len;
1737

1738
  TRACE("(0x%s,%p,%d)\n", wine_dbgstr_longlong(llBytes), lpszDest, cchMax);
1739

1740 1741
  if (!FormatInt(llKB, lpszDest, cchMax))
    return NULL;
1742

1743 1744 1745 1746
  len = lstrlenW(lpszDest);
  if (cchMax - len < 4)
      return NULL;
  lstrcatW(lpszDest, kb);
1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763
  return lpszDest;
}

/*************************************************************************
 * StrNCatA	[SHLWAPI.@]
 *
 * Concatenate two strings together.
 *
 * PARAMS
 *  lpszStr [O] String to concatenate to
 *  lpszCat [I] String to add to lpszCat
 *  cchMax  [I] Maximum number of characters to concatenate
 *
 * RETURNS
 *  lpszStr.
 *
 * NOTES
Jon Griffiths's avatar
Jon Griffiths committed
1764
 *  cchMax determines the number of characters that are appended to lpszStr,
1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786
 *  not the total length of the string.
 */
LPSTR WINAPI StrNCatA(LPSTR lpszStr, LPCSTR lpszCat, INT cchMax)
{
  LPSTR lpszRet = lpszStr;

  TRACE("(%s,%s,%i)\n", debugstr_a(lpszStr), debugstr_a(lpszCat), cchMax);

  if (!lpszStr)
  {
    WARN("Invalid lpszStr would crash under Win32!\n");
    return NULL;
  }

  StrCpyNA(lpszStr + strlen(lpszStr), lpszCat, cchMax);
  return lpszRet;
}

/*************************************************************************
 * StrNCatW	[SHLWAPI.@]
 *
 * See StrNCatA.
1787
 */
1788
LPWSTR WINAPI StrNCatW(LPWSTR lpszStr, LPCWSTR lpszCat, INT cchMax)
1789
{
1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801
  LPWSTR lpszRet = lpszStr;

  TRACE("(%s,%s,%i)\n", debugstr_w(lpszStr), debugstr_w(lpszCat), cchMax);

  if (!lpszStr)
  {
    WARN("Invalid lpszStr would crash under Win32\n");
    return NULL;
  }

  StrCpyNW(lpszStr + strlenW(lpszStr), lpszCat, cchMax);
  return lpszRet;
1802 1803 1804
}

/*************************************************************************
1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815
 * StrTrimA	[SHLWAPI.@]
 *
 * Remove characters from the start and end of a string.
 *
 * PARAMS
 *  lpszStr  [O] String to remove characters from
 *  lpszTrim [I] Characters to remove from lpszStr
 *
 * RETURNS
 *  TRUE  If lpszStr was valid and modified
 *  FALSE Otherwise
1816
 */
1817
BOOL WINAPI StrTrimA(LPSTR lpszStr, LPCSTR lpszTrim)
1818
{
1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850
  DWORD dwLen;
  LPSTR lpszRead = lpszStr;
  BOOL bRet = FALSE;

  TRACE("(%s,%s)\n", debugstr_a(lpszStr), debugstr_a(lpszTrim));

  if (lpszRead && *lpszRead)
  {
    while (*lpszRead && StrChrA(lpszTrim, *lpszRead))
      lpszRead = CharNextA(lpszRead); /* Skip leading matches */

    dwLen = strlen(lpszRead);

    if (lpszRead != lpszStr)
    {
      memmove(lpszStr, lpszRead, dwLen + 1);
      bRet = TRUE;
    }
    if (dwLen > 0)
    {
      lpszRead = lpszStr + dwLen;
      while (StrChrA(lpszTrim, lpszRead[-1]))
        lpszRead = CharPrevA(lpszStr, lpszRead); /* Skip trailing matches */

      if (lpszRead != lpszStr + dwLen)
      {
        *lpszRead = '\0';
        bRet = TRUE;
      }
    }
  }
  return bRet;
1851 1852
}

1853
/*************************************************************************
1854 1855 1856
 * StrTrimW	[SHLWAPI.@]
 *
 * See StrTrimA.
1857
 */
1858
BOOL WINAPI StrTrimW(LPWSTR lpszStr, LPCWSTR lpszTrim)
1859
{
1860 1861 1862
  DWORD dwLen;
  LPWSTR lpszRead = lpszStr;
  BOOL bRet = FALSE;
1863

1864 1865 1866 1867
  TRACE("(%s,%s)\n", debugstr_w(lpszStr), debugstr_w(lpszTrim));

  if (lpszRead && *lpszRead)
  {
1868
    while (*lpszRead && StrChrW(lpszTrim, *lpszRead)) lpszRead++;
1869 1870 1871 1872

    dwLen = strlenW(lpszRead);

    if (lpszRead != lpszStr)
1873
    {
1874 1875
      memmove(lpszStr, lpszRead, (dwLen + 1) * sizeof(WCHAR));
      bRet = TRUE;
1876
    }
1877 1878 1879 1880
    if (dwLen > 0)
    {
      lpszRead = lpszStr + dwLen;
      while (StrChrW(lpszTrim, lpszRead[-1]))
1881
        lpszRead--; /* Skip trailing matches */
1882 1883 1884 1885 1886 1887 1888 1889 1890

      if (lpszRead != lpszStr + dwLen)
      {
        *lpszRead = '\0';
        bRet = TRUE;
      }
    }
  }
  return bRet;
1891
}
Juergen Schmied's avatar
Juergen Schmied committed
1892

1893
/*************************************************************************
Jon Griffiths's avatar
Jon Griffiths committed
1894
 *      _SHStrDupAA	[INTERNAL]
1895 1896 1897
 *
 * Duplicates a ASCII string to ASCII. The destination buffer is allocated.
 */
1898
static HRESULT _SHStrDupAA(LPCSTR src, LPSTR * dest)
1899 1900 1901 1902 1903
{
	HRESULT hr;
	int len = 0;

	if (src) {
Jon Griffiths's avatar
Jon Griffiths committed
1904
	    len = lstrlenA(src) + 1;
1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920
	    *dest = CoTaskMemAlloc(len);
	} else {
	    *dest = NULL;
	}

	if (*dest) {
	    lstrcpynA(*dest,src, len);
	    hr = S_OK;
	} else {
	    hr = E_OUTOFMEMORY;
	}

	TRACE("%s->(%p)\n", debugstr_a(src), *dest);
	return hr;
}

Juergen Schmied's avatar
Juergen Schmied committed
1921
/*************************************************************************
Jon Griffiths's avatar
Jon Griffiths committed
1922
 * SHStrDupA	[SHLWAPI.@]
1923
 *
Jon Griffiths's avatar
Jon Griffiths committed
1924
 * Return a Unicode copy of a string, in memory allocated by CoTaskMemAlloc().
Juergen Schmied's avatar
Juergen Schmied committed
1925
 *
1926 1927 1928 1929 1930 1931 1932 1933
 * PARAMS
 *  lpszStr   [I] String to copy
 *  lppszDest [O] Destination for the new string copy
 *
 * RETURNS
 *  Success: S_OK. lppszDest contains the new string in Unicode format.
 *  Failure: E_OUTOFMEMORY, If any arguments are invalid or memory allocation
 *           fails.
Juergen Schmied's avatar
Juergen Schmied committed
1934
 */
Jon Griffiths's avatar
Jon Griffiths committed
1935
HRESULT WINAPI SHStrDupA(LPCSTR lpszStr, LPWSTR * lppszDest)
Juergen Schmied's avatar
Juergen Schmied committed
1936
{
Jon Griffiths's avatar
Jon Griffiths committed
1937 1938
  HRESULT hRet;
  int len = 0;
Juergen Schmied's avatar
Juergen Schmied committed
1939

Jon Griffiths's avatar
Jon Griffiths committed
1940 1941
  if (lpszStr)
  {
1942
    len = MultiByteToWideChar(CP_ACP, 0, lpszStr, -1, NULL, 0) * sizeof(WCHAR);
Jon Griffiths's avatar
Jon Griffiths committed
1943 1944 1945 1946
    *lppszDest = CoTaskMemAlloc(len);
  }
  else
    *lppszDest = NULL;
Juergen Schmied's avatar
Juergen Schmied committed
1947

Jon Griffiths's avatar
Jon Griffiths committed
1948 1949
  if (*lppszDest)
  {
1950
    MultiByteToWideChar(CP_ACP, 0, lpszStr, -1, *lppszDest, len/sizeof(WCHAR));
Jon Griffiths's avatar
Jon Griffiths committed
1951 1952 1953 1954
    hRet = S_OK;
  }
  else
    hRet = E_OUTOFMEMORY;
Juergen Schmied's avatar
Juergen Schmied committed
1955

Jon Griffiths's avatar
Jon Griffiths committed
1956 1957
  TRACE("%s->(%p)\n", debugstr_a(lpszStr), *lppszDest);
  return hRet;
Juergen Schmied's avatar
Juergen Schmied committed
1958 1959
}

1960 1961 1962 1963 1964
/*************************************************************************
 *      _SHStrDupAW	[INTERNAL]
 *
 * Duplicates a UNICODE to a ASCII string. The destination buffer is allocated.
 */
1965
static HRESULT _SHStrDupAW(LPCWSTR src, LPSTR * dest)
1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987
{
	HRESULT hr;
	int len = 0;

	if (src) {
	    len = WideCharToMultiByte(CP_ACP, 0, src, -1, NULL, 0, NULL, NULL);
	    *dest = CoTaskMemAlloc(len);
	} else {
	    *dest = NULL;
	}

	if (*dest) {
	    WideCharToMultiByte(CP_ACP, 0, src, -1, *dest, len, NULL, NULL);
	    hr = S_OK;
	} else {
	    hr = E_OUTOFMEMORY;
	}

	TRACE("%s->(%p)\n", debugstr_w(src), *dest);
	return hr;
}

Juergen Schmied's avatar
Juergen Schmied committed
1988
/*************************************************************************
Jon Griffiths's avatar
Jon Griffiths committed
1989
 * SHStrDupW	[SHLWAPI.@]
Juergen Schmied's avatar
Juergen Schmied committed
1990
 *
1991
 * See SHStrDupA.
Juergen Schmied's avatar
Juergen Schmied committed
1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014
 */
HRESULT WINAPI SHStrDupW(LPCWSTR src, LPWSTR * dest)
{
	HRESULT hr;
	int len = 0;

	if (src) {
	    len = (lstrlenW(src) + 1) * sizeof(WCHAR);
	    *dest = CoTaskMemAlloc(len);
	} else {
	    *dest = NULL;
	}

	if (*dest) {
	    memcpy(*dest, src, len);
	    hr = S_OK;
	} else {
	    hr = E_OUTOFMEMORY;
	}

	TRACE("%s->(%p)\n", debugstr_w(src), *dest);
	return hr;
}
2015 2016 2017 2018 2019 2020

/*************************************************************************
 * SHLWAPI_WriteReverseNum
 *
 * Internal helper for SHLWAPI_WriteTimeClass.
 */
2021
static inline LPWSTR SHLWAPI_WriteReverseNum(LPWSTR lpszOut, DWORD dwNum)
2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040
{
  *lpszOut-- = '\0';

  /* Write a decimal number to a string, backwards */
  do
  {
    DWORD dwNextDigit = dwNum % 10;
    *lpszOut-- = '0' + dwNextDigit;
    dwNum = (dwNum - dwNextDigit) / 10;
  } while (dwNum > 0);

  return lpszOut;
}

/*************************************************************************
 * SHLWAPI_FormatSignificant
 *
 * Internal helper for SHLWAPI_WriteTimeClass.
 */
2041
static inline int SHLWAPI_FormatSignificant(LPWSTR lpszNum, int dwDigits)
2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061
{
  /* Zero non significant digits, return remaining significant digits */
  while (*lpszNum)
  {
    lpszNum++;
    if (--dwDigits == 0)
    {
      while (*lpszNum)
        *lpszNum++ = '0';
      return 0;
    }
  }
  return dwDigits;
}

/*************************************************************************
 * SHLWAPI_WriteTimeClass
 *
 * Internal helper for StrFromTimeIntervalW.
 */
2062 2063
static int SHLWAPI_WriteTimeClass(LPWSTR lpszOut, DWORD dwValue,
                                  UINT uClassStringId, int iDigits)
2064 2065 2066 2067 2068 2069
{
  WCHAR szBuff[64], *szOut = szBuff + 32;

  szOut = SHLWAPI_WriteReverseNum(szOut, dwValue);
  iDigits = SHLWAPI_FormatSignificant(szOut + 1, iDigits);
  *szOut = ' ';
2070
  LoadStringW(shlwapi_hInstance, uClassStringId, szBuff + 32, 32);
2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091
  strcatW(lpszOut, szOut);
  return iDigits;
}

/*************************************************************************
 * StrFromTimeIntervalA	[SHLWAPI.@]
 *
 * Format a millisecond time interval into a string
 *
 * PARAMS
 *  lpszStr  [O] Output buffer for formatted time interval
 *  cchMax   [I] Size of lpszStr
 *  dwMS     [I] Number of milliseconds
 *  iDigits  [I] Number of digits to print
 *
 * RETURNS
 *  The length of the formatted string, or 0 if any parameter is invalid.
 *
 * NOTES
 *  This implementation mimics the Win32 behaviour of always writing a leading
 *  space before the time interval begins.
Jon Griffiths's avatar
Jon Griffiths committed
2092
 *
2093 2094 2095 2096 2097 2098
 *  iDigits is used to provide approximate times if accuracy is not important.
 *  This number of digits will be written of the first non-zero time class
 *  (hours/minutes/seconds). If this does not complete the time classification,
 *  the remaining digits are changed to zeros (i.e. The time is _not_ rounded).
 *  If there are digits remaining following the writing of a time class, the
 *  next time class will be written.
Jon Griffiths's avatar
Jon Griffiths committed
2099
 *
2100 2101 2102
 *  For example, given dwMS represents 138 hours,43 minutes and 15 seconds, the
 *  following will result from the given values of iDigits:
 *
Jon Griffiths's avatar
Jon Griffiths committed
2103 2104
 *|  iDigits    1        2        3        4               5               ...
 *|  lpszStr   "100 hr" "130 hr" "138 hr" "138 hr 40 min" "138 hr 43 min"  ...
2105 2106 2107 2108 2109 2110
 */
INT WINAPI StrFromTimeIntervalA(LPSTR lpszStr, UINT cchMax, DWORD dwMS,
                                int iDigits)
{
  INT iRet = 0;

2111
  TRACE("(%p,%d,%d,%d)\n", lpszStr, cchMax, dwMS, iDigits);
2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132

  if (lpszStr && cchMax)
  {
    WCHAR szBuff[128];
    StrFromTimeIntervalW(szBuff, sizeof(szBuff)/sizeof(WCHAR), dwMS, iDigits);
    WideCharToMultiByte(CP_ACP,0,szBuff,-1,lpszStr,cchMax,0,0);
  }
  return iRet;
}


/*************************************************************************
 * StrFromTimeIntervalW	[SHLWAPI.@]
 *
 * See StrFromTimeIntervalA.
 */
INT WINAPI StrFromTimeIntervalW(LPWSTR lpszStr, UINT cchMax, DWORD dwMS,
                                int iDigits)
{
  INT iRet = 0;

2133
  TRACE("(%p,%d,%d,%d)\n", lpszStr, cchMax, dwMS, iDigits);
2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155

  if (lpszStr && cchMax)
  {
    WCHAR szCopy[128];
    DWORD dwHours, dwMinutes;

    if (!iDigits || cchMax == 1)
    {
      *lpszStr = '\0';
      return 0;
    }

    /* Calculate the time classes */
    dwMS = (dwMS + 500) / 1000;
    dwHours = dwMS / 3600;
    dwMS -= dwHours * 3600;
    dwMinutes = dwMS / 60;
    dwMS -= dwMinutes * 60;

    szCopy[0] = '\0';

    if (dwHours)
2156
      iDigits = SHLWAPI_WriteTimeClass(szCopy, dwHours, IDS_TIME_INTERVAL_HOURS, iDigits);
2157 2158

    if (dwMinutes && iDigits)
2159
      iDigits = SHLWAPI_WriteTimeClass(szCopy, dwMinutes, IDS_TIME_INTERVAL_MINUTES, iDigits);
2160 2161

    if (iDigits) /* Always write seconds if we have significant digits */
2162
      SHLWAPI_WriteTimeClass(szCopy, dwMS, IDS_TIME_INTERVAL_SECONDS, iDigits);
2163

2164
    lstrcpynW(lpszStr, szCopy, cchMax);
2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187
    iRet = strlenW(lpszStr);
  }
  return iRet;
}

/*************************************************************************
 * StrIsIntlEqualA	[SHLWAPI.@]
 *
 * Compare two strings.
 *
 * PARAMS
 *  bCase    [I] Whether to compare case sensitively
 *  lpszStr  [I] First string to compare
 *  lpszComp [I] Second string to compare
 *  iLen     [I] Length to compare
 *
 * RETURNS
 *  TRUE  If the strings are equal.
 *  FALSE Otherwise.
 */
BOOL WINAPI StrIsIntlEqualA(BOOL bCase, LPCSTR lpszStr, LPCSTR lpszComp,
                            int iLen)
{
2188
  DWORD dwFlags;
2189 2190 2191 2192

  TRACE("(%d,%s,%s,%d)\n", bCase,
        debugstr_a(lpszStr), debugstr_a(lpszComp), iLen);

2193 2194
  /* FIXME: This flag is undocumented and unknown by our CompareString.
   *        We need a define for it.
2195
   */
2196 2197
  dwFlags = 0x10000000;
  if (!bCase) dwFlags |= NORM_IGNORECASE;
2198

2199
  return (CompareStringA(GetThreadLocale(), dwFlags, lpszStr, iLen, lpszComp, iLen) == CSTR_EQUAL);
2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214
}

/*************************************************************************
 * StrIsIntlEqualW	[SHLWAPI.@]
 *
 * See StrIsIntlEqualA.
 */
BOOL WINAPI StrIsIntlEqualW(BOOL bCase, LPCWSTR lpszStr, LPCWSTR lpszComp,
                            int iLen)
{
  DWORD dwFlags;

  TRACE("(%d,%s,%s,%d)\n", bCase,
        debugstr_w(lpszStr),debugstr_w(lpszComp), iLen);

2215 2216
  /* FIXME: This flag is undocumented and unknown by our CompareString.
   *        We need a define for it.
2217
   */
2218 2219
  dwFlags = 0x10000000;
  if (!bCase) dwFlags |= NORM_IGNORECASE;
2220

2221
  return (CompareStringW(GetThreadLocale(), dwFlags, lpszStr, iLen, lpszComp, iLen) == CSTR_EQUAL);
2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234
}

/*************************************************************************
 * @    [SHLWAPI.399]
 *
 * Copy a string to another string, up to a maximum number of characters.
 *
 * PARAMS
 *  lpszDest [O] Destination string
 *  lpszSrc  [I] Source string
 *  iLen     [I] Maximum number of chars to copy
 *
 * RETURNS
2235
 *  Success: A pointer to the last character written to lpszDest.
2236 2237
 *  Failure: lpszDest, if any arguments are invalid.
 */
2238
LPSTR WINAPI StrCpyNXA(LPSTR lpszDest, LPCSTR lpszSrc, int iLen)
2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254
{
  TRACE("(%p,%s,%i)\n", lpszDest, debugstr_a(lpszSrc), iLen);

  if (lpszDest && lpszSrc && iLen > 0)
  {
    while ((iLen-- > 1) && *lpszSrc)
      *lpszDest++ = *lpszSrc++;
    if (iLen >= 0)
     *lpszDest = '\0';
  }
  return lpszDest;
}

/*************************************************************************
 * @    [SHLWAPI.400]
 *
2255
 * Unicode version of StrCpyNXA.
2256
 */
2257
LPWSTR WINAPI StrCpyNXW(LPWSTR lpszDest, LPCWSTR lpszSrc, int iLen)
2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269
{
  TRACE("(%p,%s,%i)\n", lpszDest, debugstr_w(lpszSrc), iLen);

  if (lpszDest && lpszSrc && iLen > 0)
  {
    while ((iLen-- > 1) && *lpszSrc)
      *lpszDest++ = *lpszSrc++;
    if (iLen >= 0)
     *lpszDest = '\0';
  }
  return lpszDest;
}
2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322

/*************************************************************************
 * StrCmpLogicalW	[SHLWAPI.@]
 *
 * Compare two strings, ignoring case and comparing digits as numbers.
 *
 * PARAMS
 *  lpszStr  [I] First string to compare
 *  lpszComp [I] Second string to compare
 *  iLen     [I] Length to compare
 *
 * RETURNS
 *  TRUE  If the strings are equal.
 *  FALSE Otherwise.
 */
INT WINAPI StrCmpLogicalW(LPCWSTR lpszStr, LPCWSTR lpszComp)
{
  INT iDiff;

  TRACE("(%s,%s)\n", debugstr_w(lpszStr), debugstr_w(lpszComp));

  if (lpszStr && lpszComp)
  {
    while (*lpszStr)
    {
      if (!*lpszComp)
        return 1;
      else if (isdigitW(*lpszStr))
      {
        int iStr, iComp;

        if (!isdigitW(*lpszComp))
          return -1;

        /* Compare the numbers */
        StrToIntExW(lpszStr, 0, &iStr);
        StrToIntExW(lpszComp, 0, &iComp);

        if (iStr < iComp)
          return -1;
        else if (iStr > iComp)
          return 1;

        /* Skip */
        while (isdigitW(*lpszStr))
          lpszStr++;
        while (isdigitW(*lpszComp))
          lpszComp++;
      }
      else if (isdigitW(*lpszComp))
        return 1;
      else
      {
2323
        iDiff = ChrCmpIW(*lpszStr,*lpszComp);
2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344
        if (iDiff > 0)
          return 1;
        else if (iDiff < 0)
          return -1;

        lpszStr++;
        lpszComp++;
      }
    }
    if (*lpszComp)
      return -1;
  }
  return 0;
}

/* Structure for formatting byte strings */
typedef struct tagSHLWAPI_BYTEFORMATS
{
  LONGLONG dLimit;
  double   dDivisor;
  double   dNormaliser;
2345
  int      nDecimals;
Jacek Caban's avatar
Jacek Caban committed
2346
  WCHAR     wPrefix;
2347 2348 2349
} SHLWAPI_BYTEFORMATS;

/*************************************************************************
Jacek Caban's avatar
Jacek Caban committed
2350
 * StrFormatByteSizeW	[SHLWAPI.@]
2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362
 *
 * Create a string containing an abbreviated byte count of up to 2^63-1.
 *
 * PARAMS
 *  llBytes  [I] Byte size to format
 *  lpszDest [I] Destination for formatted string
 *  cchMax   [I] Size of lpszDest
 *
 * RETURNS
 *  lpszDest.
 *
 * NOTES
Jon Griffiths's avatar
Jon Griffiths committed
2363
 *  There is no StrFormatByteSize64W function, it is called StrFormatByteSizeW().
2364
 */
Jacek Caban's avatar
Jacek Caban committed
2365
LPWSTR WINAPI StrFormatByteSizeW(LONGLONG llBytes, LPWSTR lpszDest, UINT cchMax)
2366
{
2367 2368 2369 2370 2371 2372
#define KB ((ULONGLONG)1024)
#define MB (KB*KB)
#define GB (KB*KB*KB)
#define TB (KB*KB*KB*KB)
#define PB (KB*KB*KB*KB*KB)

2373 2374
  static const SHLWAPI_BYTEFORMATS bfFormats[] =
  {
2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390
    { 10*KB, 10.24, 100.0, 2, 'K' }, /* 10 KB */
    { 100*KB, 102.4, 10.0, 1, 'K' }, /* 100 KB */
    { 1000*KB, 1024.0, 1.0, 0, 'K' }, /* 1000 KB */
    { 10*MB, 10485.76, 100.0, 2, 'M' }, /* 10 MB */
    { 100*MB, 104857.6, 10.0, 1, 'M' }, /* 100 MB */
    { 1000*MB, 1048576.0, 1.0, 0, 'M' }, /* 1000 MB */
    { 10*GB, 10737418.24, 100.0, 2, 'G' }, /* 10 GB */
    { 100*GB, 107374182.4, 10.0, 1, 'G' }, /* 100 GB */
    { 1000*GB, 1073741824.0, 1.0, 0, 'G' }, /* 1000 GB */
    { 10*TB, 10485.76, 100.0, 2, 'T' }, /* 10 TB */
    { 100*TB, 104857.6, 10.0, 1, 'T' }, /* 100 TB */
    { 1000*TB, 1048576.0, 1.0, 0, 'T' }, /* 1000 TB */
    { 10*PB, 10737418.24, 100.00, 2, 'P' }, /* 10 PB */
    { 100*PB, 107374182.4, 10.00, 1, 'P' }, /* 100 PB */
    { 1000*PB, 1073741824.0, 1.00, 0, 'P' }, /* 1000 PB */
    { 0, 10995116277.76, 100.00, 2, 'E' } /* EB's, catch all */
2391
  };
Jacek Caban's avatar
Jacek Caban committed
2392
  WCHAR wszAdd[] = {' ','?','B',0};
2393 2394 2395
  double dBytes;
  UINT i = 0;

2396
  TRACE("(0x%s,%p,%d)\n", wine_dbgstr_longlong(llBytes), lpszDest, cchMax);
2397 2398 2399 2400 2401 2402

  if (!lpszDest || !cchMax)
    return lpszDest;

  if (llBytes < 1024)  /* 1K */
  {
2403 2404
    WCHAR wszBytesFormat[64];
    LoadStringW(shlwapi_hInstance, IDS_BYTES_FORMAT, wszBytesFormat, 64);
2405
    snprintfW(lpszDest, cchMax, wszBytesFormat, (int)llBytes);
2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424
    return lpszDest;
  }

  /* Note that if this loop completes without finding a match, i will be
   * pointing at the last entry, which is a catch all for > 1000 PB
   */
  while (i < sizeof(bfFormats) / sizeof(SHLWAPI_BYTEFORMATS) - 1)
  {
    if (llBytes < bfFormats[i].dLimit)
      break;
    i++;
  }
  /* Above 1 TB we encounter problems with FP accuracy. So for amounts above
   * this number we integer shift down by 1 MB first. The table above has
   * the divisors scaled down from the '< 10 TB' entry onwards, to account
   * for this. We also add a small fudge factor to get the correct result for
   * counts that lie exactly on a 1024 byte boundary.
   */
  if (i > 8)
2425
    dBytes = (double)(llBytes >> 20) + 0.001; /* Scale down by 1 MB */
2426 2427 2428 2429 2430
  else
    dBytes = (double)llBytes + 0.00001;

  dBytes = floor(dBytes / bfFormats[i].dDivisor) / bfFormats[i].dNormaliser;

2431 2432
  if (!FormatDouble(dBytes, bfFormats[i].nDecimals, lpszDest, cchMax))
    return NULL;
Jacek Caban's avatar
Jacek Caban committed
2433
  wszAdd[1] = bfFormats[i].wPrefix;
2434
  StrCatBuffW(lpszDest, wszAdd, cchMax);
2435 2436 2437 2438
  return lpszDest;
}

/*************************************************************************
Jacek Caban's avatar
Jacek Caban committed
2439
 * StrFormatByteSize64A	[SHLWAPI.@]
2440
 *
Jacek Caban's avatar
Jacek Caban committed
2441
 * See StrFormatByteSizeW.
2442
 */
Jacek Caban's avatar
Jacek Caban committed
2443
LPSTR WINAPI StrFormatByteSize64A(LONGLONG llBytes, LPSTR lpszDest, UINT cchMax)
2444
{
Jacek Caban's avatar
Jacek Caban committed
2445
  WCHAR wszBuff[32];
2446

Jacek Caban's avatar
Jacek Caban committed
2447
  StrFormatByteSizeW(llBytes, wszBuff, sizeof(wszBuff)/sizeof(WCHAR));
2448 2449

  if (lpszDest)
Jacek Caban's avatar
Jacek Caban committed
2450
    WideCharToMultiByte(CP_ACP, 0, wszBuff, -1, lpszDest, cchMax, 0, 0);
2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467
  return lpszDest;
}

/*************************************************************************
 * StrFormatByteSizeA	[SHLWAPI.@]
 *
 * Create a string containing an abbreviated byte count of up to 2^31-1.
 *
 * PARAMS
 *  dwBytes  [I] Byte size to format
 *  lpszDest [I] Destination for formatted string
 *  cchMax   [I] Size of lpszDest
 *
 * RETURNS
 *  lpszDest.
 *
 * NOTES
Jon Griffiths's avatar
Jon Griffiths committed
2468 2469
 *  The Ascii and Unicode versions of this function accept a different
 *  integer type for dwBytes. See StrFormatByteSize64A().
2470 2471 2472
 */
LPSTR WINAPI StrFormatByteSizeA(DWORD dwBytes, LPSTR lpszDest, UINT cchMax)
{
2473
  TRACE("(%d,%p,%d)\n", dwBytes, lpszDest, cchMax);
2474 2475 2476

  return StrFormatByteSize64A(dwBytes, lpszDest, cchMax);
}
Jon Griffiths's avatar
Jon Griffiths committed
2477

Jon Griffiths's avatar
Jon Griffiths committed
2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509
/*************************************************************************
 *      @	[SHLWAPI.162]
 *
 * Remove a hanging lead byte from the end of a string, if present.
 *
 * PARAMS
 *  lpStr [I] String to check for a hanging lead byte
 *  size  [I] Length of lpStr
 *
 * RETURNS
 *  Success: The new length of the string. Any hanging lead bytes are removed.
 *  Failure: 0, if any parameters are invalid.
 */
DWORD WINAPI SHTruncateString(LPSTR lpStr, DWORD size)
{
  if (lpStr && size)
  {
    LPSTR lastByte = lpStr + size - 1;

    while(lpStr < lastByte)
      lpStr += IsDBCSLeadByte(*lpStr) ? 2 : 1;

    if(lpStr == lastByte && IsDBCSLeadByte(*lpStr))
    {
      *lpStr = '\0';
      size--;
    }
    return size;
  }
  return 0;
}

Jon Griffiths's avatar
Jon Griffiths committed
2510
/*************************************************************************
2511
 *      @	[SHLWAPI.203]
Jon Griffiths's avatar
Jon Griffiths committed
2512 2513 2514 2515 2516 2517 2518 2519 2520 2521
 *
 * Remove a single non-trailing ampersand ('&') from a string.
 *
 * PARAMS
 *  lpszStr [I/O] String to remove ampersand from.
 *
 * RETURNS
 *  The character after the first ampersand in lpszStr, or the first character
 *  in lpszStr if there is no ampersand in the string.
 */
2522
char WINAPI SHStripMneumonicA(LPCSTR lpszStr)
Jon Griffiths's avatar
Jon Griffiths committed
2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533
{
  LPSTR lpszIter, lpszTmp;
  char ch;

  TRACE("(%s)\n", debugstr_a(lpszStr));

  ch = *lpszStr;

  if ((lpszIter = StrChrA(lpszStr, '&')))
  {
    lpszTmp = CharNextA(lpszIter);
2534
    if (*lpszTmp)
Jon Griffiths's avatar
Jon Griffiths committed
2535 2536 2537 2538
    {
      if (*lpszTmp != '&')
        ch =  *lpszTmp;

2539
      memmove( lpszIter, lpszTmp, strlen(lpszTmp) + 1 );
Jon Griffiths's avatar
Jon Griffiths committed
2540 2541 2542 2543 2544
    }
  }

  return ch;
}
Jon Griffiths's avatar
Jon Griffiths committed
2545 2546

/*************************************************************************
2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561
 *      @	[SHLWAPI.225]
 *
 * Unicode version of SHStripMneumonicA.
 */
WCHAR WINAPI SHStripMneumonicW(LPCWSTR lpszStr)
{
  LPWSTR lpszIter, lpszTmp;
  WCHAR ch;

  TRACE("(%s)\n", debugstr_w(lpszStr));

  ch = *lpszStr;

  if ((lpszIter = StrChrW(lpszStr, '&')))
  {
2562
    lpszTmp = lpszIter + 1;
2563
    if (*lpszTmp)
2564 2565 2566 2567
    {
      if (*lpszTmp != '&')
        ch =  *lpszTmp;

2568
      memmove( lpszIter, lpszTmp, (strlenW(lpszTmp) + 1) * sizeof(WCHAR) );
2569 2570 2571 2572 2573 2574 2575 2576
    }
  }

  return ch;
}

/*************************************************************************
 *      @	[SHLWAPI.216]
Jon Griffiths's avatar
Jon Griffiths committed
2577 2578 2579 2580
 *
 * Convert an Ascii string to Unicode.
 *
 * PARAMS
2581
 * dwCp     [I] Code page for the conversion
Jon Griffiths's avatar
Jon Griffiths committed
2582 2583 2584 2585 2586 2587 2588
 * lpSrcStr [I] Source Ascii string to convert
 * lpDstStr [O] Destination for converted Unicode string
 * iLen     [I] Length of lpDstStr
 *
 * RETURNS
 *  The return value of the MultiByteToWideChar() function called on lpSrcStr.
 */
2589
DWORD WINAPI SHAnsiToUnicodeCP(DWORD dwCp, LPCSTR lpSrcStr, LPWSTR lpDstStr, int iLen)
Jon Griffiths's avatar
Jon Griffiths committed
2590 2591 2592
{
  DWORD dwRet;

2593
  dwRet = MultiByteToWideChar(dwCp, 0, lpSrcStr, -1, lpDstStr, iLen);
2594
  TRACE("%s->%s,ret=%d\n", debugstr_a(lpSrcStr), debugstr_w(lpDstStr), dwRet);
Jon Griffiths's avatar
Jon Griffiths committed
2595 2596 2597
  return dwRet;
}

2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618
/*************************************************************************
 *      @	[SHLWAPI.215]
 *
 * Convert an Ascii string to Unicode.
 *
 * PARAMS
 * lpSrcStr [I] Source Ascii string to convert
 * lpDstStr [O] Destination for converted Unicode string
 * iLen     [I] Length of lpDstStr
 *
 * RETURNS
 *  The return value of the MultiByteToWideChar() function called on lpSrcStr.
 *
 * NOTES
 *  This function simply calls SHAnsiToUnicodeCP with code page CP_ACP.
 */
DWORD WINAPI SHAnsiToUnicode(LPCSTR lpSrcStr, LPWSTR lpDstStr, int iLen)
{
  return SHAnsiToUnicodeCP(CP_ACP, lpSrcStr, lpDstStr, iLen);
}

Jon Griffiths's avatar
Jon Griffiths committed
2619 2620 2621 2622 2623 2624 2625 2626 2627
/*************************************************************************
 *      @	[SHLWAPI.218]
 *
 * Convert a Unicode string to Ascii.
 *
 * PARAMS
 *  CodePage [I] Code page to use for the conversion
 *  lpSrcStr [I] Source Unicode string to convert
 *  lpDstStr [O] Destination for converted Ascii string
2628
 *  dstlen   [I] Length of buffer at lpDstStr
Jon Griffiths's avatar
Jon Griffiths committed
2629 2630
 *
 * RETURNS
2631 2632 2633 2634 2635 2636 2637
 *  Success: The length in bytes of the result at lpDstStr (including the terminator)
 *  Failure: When using CP_UTF8, CP_UTF7 or 0xc350 as codePage, 0 is returned and
 *           the result is not nul-terminated.
 *           When using a different codepage, the length in bytes of the truncated
 *           result at lpDstStr (including the terminator) is returned and
 *           lpDstStr is always nul-terminated.
 *
Jon Griffiths's avatar
Jon Griffiths committed
2638
 */
2639
DWORD WINAPI SHUnicodeToAnsiCP(UINT CodePage, LPCWSTR lpSrcStr, LPSTR lpDstStr, int dstlen)
Jon Griffiths's avatar
Jon Griffiths committed
2640
{
2641
  static const WCHAR emptyW[] = { '\0' };
Jon Griffiths's avatar
Jon Griffiths committed
2642 2643 2644
  int len , reqLen;
  LPSTR mem;

2645
  if (!lpDstStr || !dstlen)
Jon Griffiths's avatar
Jon Griffiths committed
2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663
    return 0;

  if (!lpSrcStr)
    lpSrcStr = emptyW;

  *lpDstStr = '\0';

  len = strlenW(lpSrcStr) + 1;

  switch (CodePage)
  {
  case CP_WINUNICODE:
    CodePage = CP_UTF8; /* Fall through... */
  case 0x0000C350: /* FIXME: CP_ #define */
  case CP_UTF7:
  case CP_UTF8:
    {
      DWORD dwMode = 0;
2664 2665 2666
      INT lenW = len - 1;
      INT needed = dstlen - 1;
      HRESULT hr;
Jon Griffiths's avatar
Jon Griffiths committed
2667

2668 2669 2670
      /* try the user supplied buffer first */
      hr = ConvertINetUnicodeToMultiByte(&dwMode, CodePage, lpSrcStr, &lenW, lpDstStr, &needed);
      if (hr == S_OK)
Jon Griffiths's avatar
Jon Griffiths committed
2671
      {
2672 2673 2674
        lpDstStr[needed] = '\0';
        return needed + 1;
      }
Jon Griffiths's avatar
Jon Griffiths committed
2675

2676 2677 2678 2679 2680 2681 2682
      /* user buffer too small. exclude termination and copy as much as possible */
      lenW = len;
      hr = ConvertINetUnicodeToMultiByte(&dwMode, CodePage, lpSrcStr, &lenW, NULL, &needed);
      needed++;
      mem = HeapAlloc(GetProcessHeap(), 0, needed);
      if (!mem)
        return 0;
Jon Griffiths's avatar
Jon Griffiths committed
2683

2684 2685 2686 2687 2688
      hr = ConvertINetUnicodeToMultiByte(&dwMode, CodePage, lpSrcStr, &len, mem, &needed);
      if (hr == S_OK)
      {
          reqLen = SHTruncateString(mem, dstlen);
          if (reqLen > 0) memcpy(lpDstStr, mem, reqLen-1);
Jon Griffiths's avatar
Jon Griffiths committed
2689
      }
2690 2691
      HeapFree(GetProcessHeap(), 0, mem);
      return 0;
Jon Griffiths's avatar
Jon Griffiths committed
2692 2693 2694 2695 2696
    }
  default:
    break;
  }

2697 2698
  /* try the user supplied buffer first */
  reqLen = WideCharToMultiByte(CodePage, 0, lpSrcStr, len, lpDstStr, dstlen, NULL, NULL);
Jon Griffiths's avatar
Jon Griffiths committed
2699 2700 2701 2702 2703 2704

  if (!reqLen && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
  {
    reqLen = WideCharToMultiByte(CodePage, 0, lpSrcStr, len, NULL, 0, NULL, NULL);
    if (reqLen)
    {
2705
      mem = HeapAlloc(GetProcessHeap(), 0, reqLen);
Jon Griffiths's avatar
Jon Griffiths committed
2706 2707 2708 2709 2710
      if (mem)
      {
        reqLen = WideCharToMultiByte(CodePage, 0, lpSrcStr, len, mem,
                                     reqLen, NULL, NULL);

2711
        reqLen = SHTruncateString(mem, dstlen -1);
Jon Griffiths's avatar
Jon Griffiths committed
2712 2713
        reqLen++;

2714
        lstrcpynA(lpDstStr, mem, reqLen);
Jon Griffiths's avatar
Jon Griffiths committed
2715
        HeapFree(GetProcessHeap(), 0, mem);
2716
        lpDstStr[reqLen-1] = '\0';
Jon Griffiths's avatar
Jon Griffiths committed
2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740
      }
    }
  }
  return reqLen;
}

/*************************************************************************
 *      @	[SHLWAPI.217]
 *
 * Convert a Unicode string to Ascii.
 *
 * PARAMS
 *  lpSrcStr [I] Source Unicode string to convert
 *  lpDstStr [O] Destination for converted Ascii string
 *  iLen     [O] Length of lpDstStr in characters
 *
 * RETURNS
 *  See SHUnicodeToAnsiCP

 * NOTES
 *  This function simply calls SHUnicodeToAnsiCP() with CodePage = CP_ACP.
 */
INT WINAPI SHUnicodeToAnsi(LPCWSTR lpSrcStr, LPSTR lpDstStr, INT iLen)
{
2741
    return SHUnicodeToAnsiCP(CP_ACP, lpSrcStr, lpDstStr, iLen);
Jon Griffiths's avatar
Jon Griffiths committed
2742 2743
}

2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782
/*************************************************************************
 *      @	[SHLWAPI.345]
 *
 * Copy one string to another.
 *
 * PARAMS
 *  lpszSrc [I] Source string to copy
 *  lpszDst [O] Destination for copy
 *  iLen    [I] Length of lpszDst in characters
 *
 * RETURNS
 *  The length of the copied string, including the terminating NUL. lpszDst
 *  contains iLen characters of lpszSrc.
 */
DWORD WINAPI SHAnsiToAnsi(LPCSTR lpszSrc, LPSTR lpszDst, int iLen)
{
    LPSTR lpszRet;

    TRACE("(%s,%p,0x%08x)\n", debugstr_a(lpszSrc), lpszDst, iLen);

    lpszRet = StrCpyNXA(lpszDst, lpszSrc, iLen);
    return lpszRet - lpszDst + 1;
}

/*************************************************************************
 *      @	[SHLWAPI.346]
 *
 * Unicode version of SSHAnsiToAnsi.
 */
DWORD WINAPI SHUnicodeToUnicode(LPCWSTR lpszSrc, LPWSTR lpszDst, int iLen)
{
    LPWSTR lpszRet;

    TRACE("(%s,%p,0x%08x)\n", debugstr_w(lpszSrc), lpszDst, iLen);

    lpszRet = StrCpyNXW(lpszDst, lpszSrc, iLen);
    return lpszRet - lpszDst + 1;
}

Jon Griffiths's avatar
Jon Griffiths committed
2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823
/*************************************************************************
 *      @	[SHLWAPI.364]
 *
 * Determine if an Ascii string converts to Unicode and back identically.
 *
 * PARAMS
 *  lpSrcStr [I] Source Unicode string to convert
 *  lpDst    [O] Destination for resulting Ascii string
 *  iLen     [I] Length of lpDst in characters
 *
 * RETURNS
 *  TRUE, since Ascii strings always convert identically.
 */
BOOL WINAPI DoesStringRoundTripA(LPCSTR lpSrcStr, LPSTR lpDst, INT iLen)
{
  lstrcpynA(lpDst, lpSrcStr, iLen);
  return TRUE;
}

/*************************************************************************
 *      @	[SHLWAPI.365]
 *
 * Determine if a Unicode string converts to Ascii and back identically.
 *
 * PARAMS
 *  lpSrcStr [I] Source Unicode string to convert
 *  lpDst    [O] Destination for resulting Ascii string
 *  iLen     [I] Length of lpDst in characters
 *
 * RETURNS
 *  TRUE, if lpSrcStr converts to Ascii and back identically,
 *  FALSE otherwise.
 */
BOOL WINAPI DoesStringRoundTripW(LPCWSTR lpSrcStr, LPSTR lpDst, INT iLen)
{
    WCHAR szBuff[MAX_PATH];

    SHUnicodeToAnsi(lpSrcStr, lpDst, iLen);
    SHAnsiToUnicode(lpDst, szBuff, MAX_PATH);
    return !strcmpW(lpSrcStr, szBuff);
}
2824 2825 2826 2827

/*************************************************************************
 *      SHLoadIndirectString    [SHLWAPI.@]
 *
2828
 * If passed a string that begins with '@', extract the string from the
2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863
 * appropriate resource, otherwise do a straight copy.
 *
 */
HRESULT WINAPI SHLoadIndirectString(LPCWSTR src, LPWSTR dst, UINT dst_len, void **reserved)
{
    WCHAR *dllname = NULL;
    HMODULE hmod = NULL;
    HRESULT hr = E_FAIL;

    TRACE("(%s %p %08x %p)\n", debugstr_w(src), dst, dst_len, reserved);

    if(src[0] == '@')
    {
        WCHAR *index_str;
        int index;

        dst[0] = 0;
        dllname = StrDupW(src + 1);
        index_str = strchrW(dllname, ',');

        if(!index_str) goto end;

        *index_str = 0;
        index_str++;
        index = atoiW(index_str);
  
        hmod = LoadLibraryW(dllname);
        if(!hmod) goto end;

        if(index < 0)
        {
            if(LoadStringW(hmod, -index, dst, dst_len))
                hr = S_OK;
        }
        else
2864
            FIXME("can't handle non-negative indices (%d)\n", index);
2865 2866 2867 2868 2869 2870 2871 2872
    }
    else
    {
        if(dst != src)
            lstrcpynW(dst, src, dst_len);
        hr = S_OK;
    }

2873
    TRACE("returning %s\n", debugstr_w(dst));
2874 2875
end:
    if(hmod) FreeLibrary(hmod);
2876
    LocalFree(dllname);
2877 2878
    return hr;
}
2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903

BOOL WINAPI IsCharSpaceA(CHAR c)
{
    WORD CharType;
    return GetStringTypeA(GetSystemDefaultLCID(), CT_CTYPE1, &c, 1, &CharType) && (CharType & C1_SPACE);
}

/*************************************************************************
 *      @	[SHLWAPI.29]
 *
 * Determine if a Unicode character is a space.
 *
 * PARAMS
 *  wc [I] Character to check.
 *
 * RETURNS
 *  TRUE, if wc is a space,
 *  FALSE otherwise.
 */
BOOL WINAPI IsCharSpaceW(WCHAR wc)
{
    WORD CharType;

    return GetStringTypeW(CT_CTYPE1, &wc, 1, &CharType) && (CharType & C1_SPACE);
}