string.c 67 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 94 95 96 97 98 99 100 101 102 103
/*************************************************************************
 * FormatInt   [internal]
 *
 * Format an integer according to the current locale
 *
 * RETURNS
 *  The number of bytes written on success or 0 on failure
 */
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 127 128 129 130 131 132 133 134 135 136 137
/*************************************************************************
 * FormatDouble   [internal]
 *
 * Format an integer according to the current locale. Prints the specified number of digits
 * after the decimal point
 *
 * RETURNS
 *  The number of bytes written on success or 0 on failure
 */
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 179
  {
    str2[1] = HIBYTE(ch2);
    str2[2] = '\0';
  }
  else
    str2[1] = '\0';

  return CompareStringA(GetThreadLocale(), dwFlags, str1, -1, str2, -1) - 2;
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) - 2;
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
 * 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.
333
 */
334
int WINAPI StrCmpIW(LPCWSTR lpszStr, LPCWSTR lpszComp)
335
{
336
  int iRet;
337 338 339

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

340 341
  iRet = CompareStringW(GetThreadLocale(), NORM_IGNORECASE, lpszStr, -1, lpszComp, -1);
  return iRet == CSTR_LESS_THAN ? -1 : iRet == CSTR_GREATER_THAN ? 1 : 0;
342
}
343 344

/*************************************************************************
345 346 347 348 349 350 351 352 353 354 355 356
 * StrCmpNA	[SHLWAPI.@]
 *
 * Compare two strings, up to a maximum length.
 *
 * PARAMS
 *  lpszStr  [I] First string to compare
 *  lpszComp [I] Second string to compare
 *  iLen     [I] Maximum number of chars 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.
357
 */
358
INT WINAPI StrCmpNA(LPCSTR lpszStr, LPCSTR lpszComp, INT iLen)
359
{
360 361
  INT iRet;

362 363
  TRACE("(%s,%s,%i)\n", debugstr_a(lpszStr), debugstr_a(lpszComp), iLen);

364 365
  iRet = CompareStringA(GetThreadLocale(), 0, lpszStr, iLen, lpszComp, iLen);
  return iRet == CSTR_LESS_THAN ? -1 : iRet == CSTR_GREATER_THAN ? 1 : 0;
366 367
}

368 369 370 371 372 373 374 375 376 377 378
/*************************************************************************
 * StrCmpNW	[SHLWAPI.@]
 *
 * See StrCmpNA.
 */
INT WINAPI StrCmpNW(LPCWSTR lpszStr, LPCWSTR lpszComp, INT iLen)
{
  INT iRet;

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

379 380
  iRet = CompareStringW(GetThreadLocale(), 0, lpszStr, iLen, lpszComp, iLen);
  return iRet == CSTR_LESS_THAN ? -1 : iRet == CSTR_GREATER_THAN ? 1 : 0;
381
}
382

383
/*************************************************************************
384 385 386 387 388 389 390 391 392 393 394 395
 * 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
 *  iLen     [I] Maximum number of chars 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.
396
 */
397
int WINAPI StrCmpNIA(LPCSTR lpszStr, LPCSTR lpszComp, int iLen)
398
{
399 400
  INT iRet;

401 402
  TRACE("(%s,%s,%i)\n", debugstr_a(lpszStr), debugstr_a(lpszComp), iLen);

403 404
  iRet = CompareStringA(GetThreadLocale(), NORM_IGNORECASE, lpszStr, iLen, lpszComp, iLen);
  return iRet == CSTR_LESS_THAN ? -1 : iRet == CSTR_GREATER_THAN ? 1 : 0;
405 406
}

407 408 409 410 411 412 413 414 415 416 417
/*************************************************************************
 * StrCmpNIW	[SHLWAPI.@]
 *
 * See StrCmpNIA.
 */
INT WINAPI StrCmpNIW(LPCWSTR lpszStr, LPCWSTR lpszComp, int iLen)
{
  INT iRet;

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

418 419
  iRet = CompareStringW(GetThreadLocale(), NORM_IGNORECASE, lpszStr, iLen, lpszComp, iLen);
  return iRet == CSTR_LESS_THAN ? -1 : iRet == CSTR_GREATER_THAN ? 1 : 0;
420
}
421

422
/*************************************************************************
423 424 425 426 427 428 429 430 431 432 433
 * 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.
434
 */
435
int WINAPI StrCmpW(LPCWSTR lpszStr, LPCWSTR lpszComp)
436
{
437 438 439 440
  INT iRet;

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

441 442
  iRet = CompareStringW(GetThreadLocale(), 0, lpszStr, -1, lpszComp, -1);
  return iRet == CSTR_LESS_THAN ? -1 : iRet == CSTR_GREATER_THAN ? 1 : 0;
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 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 502 503 504 505 506 507 508 509 510 511 512
}

/*************************************************************************
 * StrCatW	[SHLWAPI.@]
 *
 * Concatanate two strings.
 *
 * PARAMS
 *  lpszStr [O] Initial string
 *  lpszSrc [I] String to concatanate
 *
 * RETURNS
 *  lpszStr.
 */
LPWSTR WINAPI StrCatW(LPWSTR lpszStr, LPCWSTR lpszSrc)
{
  TRACE("(%s,%s)\n", debugstr_w(lpszStr), debugstr_w(lpszSrc));

  strcatW(lpszStr, lpszSrc);
  return lpszStr;
}

/*************************************************************************
 * 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));

  strcpyW(lpszStr, lpszSrc);
  return lpszStr;
}

/*************************************************************************
 * StrCpyNW	[SHLWAPI.@]
 *
 * Copy a string to another string, up to a maximum number of characters.
 *
 * PARAMS
 *  lpszStr  [O] Destination string
 *  lpszSrc  [I] Source string
 *  iLen     [I] Maximum number of chars to copy
 *
 * RETURNS
 *  lpszStr.
 */
LPWSTR WINAPI StrCpyNW(LPWSTR lpszStr, LPCWSTR lpszSrc, int iLen)
{
  TRACE("(%p,%s,%i)\n", lpszStr, debugstr_w(lpszSrc), iLen);

  lstrcpynW(lpszStr, lpszSrc, iLen);
  return lpszStr;
}



/*************************************************************************
 * SHLWAPI_StrStrHelperA
 *
 * Internal implementation of StrStrA/StrStrIA
 */
513 514
static LPSTR SHLWAPI_StrStrHelperA(LPCSTR lpszStr, LPCSTR lpszSearch,
                                   int (*pStrCmpFn)(LPCSTR,LPCSTR,size_t))
515 516 517 518
{
  size_t iLen;

  if (!lpszStr || !lpszSearch || !*lpszSearch)
519
    return NULL;
520 521 522 523 524 525 526 527 528 529

  iLen = strlen(lpszSearch);

  while (*lpszStr)
  {
    if (!pStrCmpFn(lpszStr, lpszSearch, iLen))
      return (LPSTR)lpszStr;
    lpszStr = CharNextA(lpszStr);
  }
  return NULL;
530 531
}

532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
/*************************************************************************
 * 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));

  return SHLWAPI_StrStrHelperA(lpszStr, lpszSearch, strncmp);
549 550 551
}

/*************************************************************************
552 553 554 555 556 557
 * StrStrW	[SHLWAPI.@]
 *
 * See StrStrA.
 */
LPWSTR WINAPI StrStrW(LPCWSTR lpszStr, LPCWSTR lpszSearch)
{
558 559
    if (!lpszStr || !lpszSearch) return NULL;
    return strstrW( lpszStr, lpszSearch );
560 561 562 563 564
}

/*************************************************************************
 * StrRStrIA	[SHLWAPI.@]
 *
565
 * Find the last occurrence of a substring within a string.
566 567 568 569 570 571 572
 *
 * PARAMS
 *  lpszStr    [I] String to search in
 *  lpszEnd    [I] End of lpszStr
 *  lpszSearch [I] String to look for
 *
 * RETURNS
573
 *  The last occurrence lpszSearch within lpszStr, or NULL if not found.
574
 */
575
LPSTR WINAPI StrRStrIA(LPCSTR lpszStr, LPCSTR lpszEnd, LPCSTR lpszSearch)
576
{
577 578 579 580 581 582 583 584 585 586
  WORD ch1, ch2;
  INT iLen;

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

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

  if (!lpszEnd)
    lpszEnd = lpszStr + lstrlenA(lpszStr);
587 588
  if (lpszEnd == lpszStr)
    return NULL;
589 590

  if (IsDBCSLeadByte(*lpszSearch))
591
    ch1 = *lpszSearch << 8 | (UCHAR)lpszSearch[1];
592 593 594 595
  else
    ch1 = *lpszSearch;
  iLen = lstrlenA(lpszSearch);

596
  do
597
  {
598 599
    lpszEnd = CharPrevA(lpszStr, lpszEnd);
    ch2 = IsDBCSLeadByte(*lpszEnd)? *lpszEnd << 8 | (UCHAR)lpszEnd[1] : *lpszEnd;
600
    if (!ChrCmpIA(ch1, ch2))
601
    {
602 603
      if (!StrCmpNIA(lpszEnd, lpszSearch, iLen))
        return (LPSTR)lpszEnd;
604
    }
605 606
  } while (lpszEnd > lpszStr);
  return NULL;
607 608 609
}

/*************************************************************************
610 611 612
 * StrRStrIW	[SHLWAPI.@]
 *
 * See StrRStrIA.
613
 */
614
LPWSTR WINAPI StrRStrIW(LPCWSTR lpszStr, LPCWSTR lpszEnd, LPCWSTR lpszSearch)
615
{
616 617 618 619 620 621 622 623 624 625 626
  INT iLen;

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

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

  if (!lpszEnd)
    lpszEnd = lpszStr + strlenW(lpszStr);

  iLen = strlenW(lpszSearch);
627 628

  while (lpszEnd > lpszStr)
629
  {
630 631 632 633
    lpszEnd--;
    if (!StrCmpNIW(lpszEnd, lpszSearch, iLen))
      return (LPWSTR)lpszEnd;
  }
634
  return NULL;
635 636 637
}

/*************************************************************************
638 639 640 641 642 643 644 645 646 647
 * 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.
648
 */
649
LPSTR WINAPI StrStrIA(LPCSTR lpszStr, LPCSTR lpszSearch)
650
{
651 652 653
  TRACE("(%s,%s)\n", debugstr_a(lpszStr), debugstr_a(lpszSearch));

  return SHLWAPI_StrStrHelperA(lpszStr, lpszSearch, strncasecmp);
654 655 656
}

/*************************************************************************
657 658 659
 * StrStrIW	[SHLWAPI.@]
 *
 * See StrStrIA.
660
 */
661
LPWSTR WINAPI StrStrIW(LPCWSTR lpszStr, LPCWSTR lpszSearch)
662
{
663 664
  int iLen;

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

667 668 669 670 671 672 673 674 675 676 677 678
  if (!lpszStr || !lpszSearch || !*lpszSearch)
    return NULL;

  iLen = strlenW(lpszSearch);

  while (*lpszStr)
  {
    if (!StrCmpNIW(lpszStr, lpszSearch, iLen))
      return (LPWSTR)lpszStr;
    lpszStr++;
  }
  return NULL;
679 680
}

681
/*************************************************************************
682 683
 * StrToIntA	[SHLWAPI.@]
 *
684
 * Read a signed integer from a string.
685 686 687 688 689
 *
 * PARAMS
 *  lpszStr [I] String to read integer from
 *
 * RETURNS
690
 *   The signed integer value represented by the string, or 0 if no integer is
691 692 693 694
 *   present.
 *
 * NOTES
 *  No leading space is allowed before the number, although a leading '-' is.
695
 */
696
int WINAPI StrToIntA(LPCSTR lpszStr)
697
{
698 699 700 701 702 703 704 705 706 707 708 709 710
  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;
711 712 713
}

/*************************************************************************
714 715 716
 * StrToIntW	[SHLWAPI.@]
 *
 * See StrToIntA.
717
 */
718
int WINAPI StrToIntW(LPCWSTR lpszStr)
719
{
720 721 722 723 724 725 726 727 728 729 730 731 732
  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;
733 734
}

735
/*************************************************************************
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
 * 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
751
 *  dwFlags includes STIF_SUPPORT_HEX, hexadecimal numbers are allowed, if
752
 *  preceded by '0x'. If this flag is not set, or there is no '0x' prefix,
753
 *  the string is treated as a decimal string. A leading '-' is ignored for
754
 *  hexadecimal numbers.
755
 */
756
BOOL WINAPI StrToIntExA(LPCSTR lpszStr, DWORD dwFlags, LPINT lpiRet)
757
{
758 759 760
  BOOL bNegative = FALSE;
  int iRet = 0;

761
  TRACE("(%s,%08X,%p)\n", debugstr_a(lpszStr), dwFlags, lpiRet);
762 763 764 765 766 767 768 769 770 771 772 773 774 775 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 811 812 813 814 815 816 817 818 819

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

  /* 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;
}
820

821 822 823 824 825 826 827 828 829 830
/*************************************************************************
 * StrToIntExW	[SHLWAPI.@]
 *
 * See StrToIntExA.
 */
BOOL WINAPI StrToIntExW(LPCWSTR lpszStr, DWORD dwFlags, LPINT lpiRet)
{
  BOOL bNegative = FALSE;
  int iRet = 0;

831
  TRACE("(%s,%08X,%p)\n", debugstr_w(lpszStr), dwFlags, lpiRet);
832 833 834 835 836 837 838 839 840 841 842 843

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

  /* Skip leading space, '+', '-' */
844
  while (isspaceW(*lpszStr)) lpszStr++;
845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888

  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;
}
889

890 891 892 893 894 895 896 897 898 899 900 901 902
/*************************************************************************
 * 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
903 904
 *  The string memory is allocated with LocalAlloc(), and so should be released
 *  by calling LocalFree().
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923
 */
LPSTR WINAPI StrDupA(LPCSTR lpszStr)
{
  int iLen;
  LPSTR lpszRet;

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

  iLen = lpszStr ? strlen(lpszStr) + 1 : 1;
  lpszRet = (LPSTR)LocalAlloc(LMEM_FIXED, iLen);

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

/*************************************************************************
927 928 929
 * StrDupW	[SHLWAPI.@]
 *
 * See StrDupA.
930
 */
931
LPWSTR WINAPI StrDupW(LPCWSTR lpszStr)
932
{
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949
  int iLen;
  LPWSTR lpszRet;

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

  iLen = (lpszStr ? strlenW(lpszStr) + 1 : 1) * sizeof(WCHAR);
  lpszRet = (LPWSTR)LocalAlloc(LMEM_FIXED, iLen);

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

951 952 953 954 955
/*************************************************************************
 * SHLWAPI_StrSpnHelperA
 *
 * Internal implementation of StrSpnA/StrCSpnA/StrCSpnIA
 */
956 957 958
static int SHLWAPI_StrSpnHelperA(LPCSTR lpszStr, LPCSTR lpszMatch,
                                 LPSTR (WINAPI *pStrChrFn)(LPCSTR,WORD),
                                 BOOL bInvert)
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975
{
  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;
}
976 977

/*************************************************************************
978 979 980 981 982 983 984 985 986 987 988 989
 * 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.
990
 */
991
int WINAPI StrSpnA(LPCSTR lpszStr, LPCSTR lpszMatch)
992
{
993
  TRACE("(%s,%s)\n",debugstr_a(lpszStr), debugstr_a(lpszMatch));
994

995 996
  return SHLWAPI_StrSpnHelperA(lpszStr, lpszMatch, StrChrA, FALSE);
}
997

998 999 1000 1001 1002 1003 1004
/*************************************************************************
 * StrSpnW	[SHLWAPI.@]
 *
 * See StrSpnA.
 */
int WINAPI StrSpnW(LPCWSTR lpszStr, LPCWSTR lpszMatch)
{
1005 1006
    if (!lpszStr || !lpszMatch) return 0;
    return strspnW( lpszStr, lpszMatch );
1007 1008 1009
}

/*************************************************************************
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
 * 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.
1022
 */
1023
int WINAPI StrCSpnA(LPCSTR lpszStr, LPCSTR lpszMatch)
1024
{
1025
  TRACE("(%s,%s)\n",debugstr_a(lpszStr), debugstr_a(lpszMatch));
1026

1027 1028
  return SHLWAPI_StrSpnHelperA(lpszStr, lpszMatch, StrChrA, TRUE);
}
1029

1030 1031 1032 1033 1034 1035 1036
/*************************************************************************
 * StrCSpnW	[SHLWAPI.@]
 *
 * See StrCSpnA.
 */
int WINAPI StrCSpnW(LPCWSTR lpszStr, LPCWSTR lpszMatch)
{
1037 1038
    if (!lpszStr || !lpszMatch) return 0;
    return strcspnW( lpszStr, lpszMatch );
1039 1040
}

1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
/*************************************************************************
 * 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.@]
1064
 *
1065
 * See StrCSpnIA.
1066
 */
1067
int WINAPI StrCSpnIW(LPCWSTR lpszStr, LPCWSTR lpszMatch)
1068
{
1069 1070
  LPCWSTR lpszRead = lpszStr;

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

1073 1074 1075 1076 1077 1078 1079 1080 1081
  if (lpszStr && *lpszStr && lpszMatch)
  {
    while (*lpszRead)
    {
      if (StrChrIW(lpszMatch, *lpszRead)) break;
      lpszRead++;
    }
  }
  return lpszRead - lpszStr;
1082
}
1083

1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
/*************************************************************************
 * 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)
1104
    {
1105 1106 1107
      if (StrChrA(lpszMatch, *lpszStr))
        return (LPSTR)lpszStr;
      lpszStr = CharNextA(lpszStr);
1108
    }
1109 1110
  }
  return NULL;
1111 1112
}

1113 1114 1115 1116 1117 1118 1119
/*************************************************************************
 * StrPBrkW	[SHLWAPI.@]
 *
 * See StrPBrkA.
 */
LPWSTR WINAPI StrPBrkW(LPCWSTR lpszStr, LPCWSTR lpszMatch)
{
1120 1121
    if (!lpszStr || !lpszMatch) return NULL;
    return strpbrkW( lpszStr, lpszMatch );
1122
}
1123

1124 1125
/*************************************************************************
 * SHLWAPI_StrRChrHelperA
1126
 *
1127
 * Internal implementation of StrRChrA/StrRChrIA.
1128
 */
1129 1130 1131
static LPSTR SHLWAPI_StrRChrHelperA(LPCSTR lpszStr,
                                    LPCSTR lpszEnd, WORD ch,
                                    BOOL (WINAPI *pChrCmpFn)(WORD,WORD))
1132
{
1133
  LPCSTR lpszRet = NULL;
1134

1135 1136 1137
  if (lpszStr)
  {
    WORD ch2;
1138

1139 1140
    if (!lpszEnd)
      lpszEnd = lpszStr + lstrlenA(lpszStr);
1141

1142 1143 1144
    while (*lpszStr && lpszStr <= lpszEnd)
    {
      ch2 = IsDBCSLeadByte(*lpszStr)? *lpszStr << 8 | lpszStr[1] : *lpszStr;
1145

1146 1147 1148 1149 1150 1151 1152
      if (!pChrCmpFn(ch, ch2))
        lpszRet = lpszStr;
      lpszStr = CharNextA(lpszStr);
    }
  }
  return (LPSTR)lpszRet;
}
1153

1154 1155 1156
/**************************************************************************
 * StrRChrA	[SHLWAPI.@]
 *
1157
 * Find the last occurrence of a character in string.
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
 *
 * 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);
}
1175 1176

/**************************************************************************
1177
 * StrRChrW	[SHLWAPI.@]
1178
 *
1179
 * See StrRChrA.
1180
 */
1181
LPWSTR WINAPI StrRChrW(LPCWSTR str, LPCWSTR end, WORD ch)
1182
{
1183
    WCHAR *ret = NULL;
1184

1185 1186 1187 1188 1189 1190 1191 1192
    if (!str) return NULL;
    if (!end) end = str + strlenW(str);
    while (str < end)
    {
        if (*str == ch) ret = (WCHAR *)str;
        str++;
    }
    return ret;
1193
}
1194

1195 1196 1197
/**************************************************************************
 * StrRChrIA	[SHLWAPI.@]
 *
1198
 * Find the last occurrence of a character in string, ignoring case.
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
 *
 * 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);
1213

1214
  return SHLWAPI_StrRChrHelperA(lpszStr, lpszEnd, ch, ChrCmpIA);
1215 1216
}

1217 1218 1219 1220 1221
/**************************************************************************
 * StrRChrIW	[SHLWAPI.@]
 *
 * See StrRChrIA.
 */
1222
LPWSTR WINAPI StrRChrIW(LPCWSTR str, LPCWSTR end, WORD ch)
1223
{
1224
    WCHAR *ret = NULL;
1225

1226 1227 1228 1229 1230 1231 1232 1233
    if (!str) return NULL;
    if (!end) end = str + strlenW(str);
    while (str < end)
    {
        if (!ChrCmpIW(*str, ch)) ret = (WCHAR *)str;
        str++;
    }
    return ret;
1234
}
1235

1236
/*************************************************************************
1237
 * StrCatBuffA	[SHLWAPI.@]
1238
 *
1239
 * Concatenate two strings together.
1240
 *
1241 1242 1243 1244 1245 1246 1247 1248 1249
 * 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
1250
 *  cchMax determines the number of characters in the final length of the
1251
 *  string, not the number appended to lpszStr from lpszCat.
1252
 */
1253
LPSTR WINAPI StrCatBuffA(LPSTR lpszStr, LPCSTR lpszCat, INT cchMax)
1254
{
1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
  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;
1267

1268 1269 1270
  if (cchMax > 0)
    StrCpyNA(lpszStr + iLen, lpszCat, cchMax);
  return lpszStr;
1271 1272 1273
}

/*************************************************************************
1274
 * StrCatBuffW	[SHLWAPI.@]
1275
 *
1276
 * See StrCatBuffA.
1277
 */
1278
LPWSTR WINAPI StrCatBuffW(LPWSTR lpszStr, LPCWSTR lpszCat, INT cchMax)
1279
{
1280 1281 1282 1283 1284 1285 1286 1287 1288
  INT iLen;

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

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

1290 1291 1292 1293 1294 1295
  iLen = strlenW(lpszStr);
  cchMax -= iLen;

  if (cchMax > 0)
    StrCpyNW(lpszStr + iLen, lpszCat, cchMax);
  return lpszStr;
1296 1297 1298 1299
}

/*************************************************************************
 * StrRetToBufA					[SHLWAPI.@]
1300
 *
1301
 * Convert a STRRET to a normal string.
1302
 *
1303 1304
 * PARAMS
 *  lpStrRet [O] STRRET to convert
1305
 *  pIdl     [I] ITEMIDLIST for lpStrRet->uType == STRRET_OFFSET
1306 1307
 *  lpszDest [O] Destination for normal string
 *  dwLen    [I] Length of lpszDest
1308
 *
1309 1310 1311
 * 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
1312
 *           CoTaskMemFree() and its type set to STRRET_CSTRA.
1313
 *  Failure: E_FAIL, if any parameters are invalid.
1314
 */
1315
HRESULT WINAPI StrRetToBufA (LPSTRRET src, const ITEMIDLIST *pidl, LPSTR dest, UINT len)
1316
{
1317 1318 1319 1320 1321
	/* 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.
	 */
1322
	TRACE("dest=%p len=0x%x strret=%p pidl=%p stub\n",dest,len,src,pidl);
1323

1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336
	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';

1337 1338 1339
	switch (src->uType)
	{
	  case STRRET_WSTR:
1340
	    WideCharToMultiByte(CP_ACP, 0, src->u.pOleStr, -1, dest, len, NULL, NULL);
1341
	    CoTaskMemFree(src->u.pOleStr);
1342 1343
	    break;

1344
	  case STRRET_CSTR:
1345 1346 1347
	    lstrcpynA((LPSTR)dest, src->u.cStr, len);
	    break;

1348
	  case STRRET_OFFSET:
1349 1350 1351 1352 1353
	    lstrcpynA((LPSTR)dest, ((LPCSTR)&pidl->mkid)+src->u.uOffset, len);
	    break;

	  default:
	    FIXME("unknown type!\n");
1354
	    return FALSE;
1355 1356 1357 1358 1359
	}
	return S_OK;
}

/*************************************************************************
1360
 * StrRetToBufW	[SHLWAPI.@]
1361
 *
1362
 * See StrRetToBufA.
1363
 */
1364
HRESULT WINAPI StrRetToBufW (LPSTRRET src, const ITEMIDLIST *pidl, LPWSTR dest, UINT len)
1365
{
1366
	TRACE("dest=%p len=0x%x strret=%p pidl=%p stub\n",dest,len,src,pidl);
1367

1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380
	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';

1381 1382 1383 1384
	switch (src->uType)
	{
	  case STRRET_WSTR:
	    lstrcpynW((LPWSTR)dest, src->u.pOleStr, len);
1385
	    CoTaskMemFree(src->u.pOleStr);
1386 1387
	    break;

1388
	  case STRRET_CSTR:
1389 1390
              if (!MultiByteToWideChar( CP_ACP, 0, src->u.cStr, -1, dest, len ) && len)
                  dest[len-1] = 0;
1391 1392
	    break;

1393
	  case STRRET_OFFSET:
1394 1395
	    if (pidl)
	    {
1396 1397 1398
              if (!MultiByteToWideChar( CP_ACP, 0, ((LPCSTR)&pidl->mkid)+src->u.uOffset, -1,
                                        dest, len ) && len)
                  dest[len-1] = 0;
1399 1400 1401 1402 1403
	    }
	    break;

	  default:
	    FIXME("unknown type!\n");
1404
	    return FALSE;
1405 1406 1407 1408
	}
	return S_OK;
}

1409 1410 1411
/*************************************************************************
 * StrRetToStrA					[SHLWAPI.@]
 *
Jon Griffiths's avatar
Jon Griffiths committed
1412 1413 1414 1415
 * Converts a STRRET to a normal string.
 *
 * PARAMS
 *  lpStrRet [O] STRRET to convert
1416
 *  pidl     [I] ITEMIDLIST for lpStrRet->uType == STRRET_OFFSET
Jon Griffiths's avatar
Jon Griffiths committed
1417 1418 1419 1420 1421
 *  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.
1422
 */
Jon Griffiths's avatar
Jon Griffiths committed
1423
HRESULT WINAPI StrRetToStrA(LPSTRRET lpStrRet, const ITEMIDLIST *pidl, LPSTR *ppszName)
1424
{
Jon Griffiths's avatar
Jon Griffiths committed
1425
  HRESULT hRet = E_FAIL;
1426

Jon Griffiths's avatar
Jon Griffiths committed
1427 1428 1429 1430 1431 1432
  switch (lpStrRet->uType)
  {
  case STRRET_WSTR:
    hRet = _SHStrDupAW(lpStrRet->u.pOleStr, ppszName);
    CoTaskMemFree(lpStrRet->u.pOleStr);
    break;
1433

Jon Griffiths's avatar
Jon Griffiths committed
1434 1435 1436
  case STRRET_CSTR:
    hRet = _SHStrDupAA(lpStrRet->u.cStr, ppszName);
    break;
1437

Jon Griffiths's avatar
Jon Griffiths committed
1438 1439 1440
  case STRRET_OFFSET:
    hRet = _SHStrDupAA(((LPCSTR)&pidl->mkid) + lpStrRet->u.uOffset, ppszName);
    break;
1441

Jon Griffiths's avatar
Jon Griffiths committed
1442 1443 1444 1445 1446
  default:
    *ppszName = NULL;
  }

  return hRet;
1447 1448 1449 1450 1451
}

/*************************************************************************
 * StrRetToStrW					[SHLWAPI.@]
 *
Jon Griffiths's avatar
Jon Griffiths committed
1452
 * See StrRetToStrA.
1453
 */
Jon Griffiths's avatar
Jon Griffiths committed
1454
HRESULT WINAPI StrRetToStrW(LPSTRRET lpStrRet, const ITEMIDLIST *pidl, LPWSTR *ppszName)
1455
{
Jon Griffiths's avatar
Jon Griffiths committed
1456
  HRESULT hRet = E_FAIL;
1457

Jon Griffiths's avatar
Jon Griffiths committed
1458 1459 1460 1461 1462 1463
  switch (lpStrRet->uType)
  {
  case STRRET_WSTR:
    hRet = SHStrDupW(lpStrRet->u.pOleStr, ppszName);
    CoTaskMemFree(lpStrRet->u.pOleStr);
    break;
1464

Jon Griffiths's avatar
Jon Griffiths committed
1465 1466 1467
  case STRRET_CSTR:
    hRet = SHStrDupA(lpStrRet->u.cStr, ppszName);
    break;
1468

Jon Griffiths's avatar
Jon Griffiths committed
1469 1470 1471
  case STRRET_OFFSET:
    hRet = SHStrDupA(((LPCSTR)&pidl->mkid) + lpStrRet->u.uOffset, ppszName);
    break;
1472

Jon Griffiths's avatar
Jon Griffiths committed
1473 1474 1475 1476 1477
  default:
    *ppszName = NULL;
  }

  return hRet;
1478 1479
}

1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544
/* 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;
}

1545
/*************************************************************************
1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559
 * 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)
{
1560 1561 1562 1563 1564 1565
  WCHAR wszBuf[256];
  
  if (!StrFormatKBSizeW(llBytes, wszBuf, 256))
    return NULL;
  if (!WideCharToMultiByte(CP_ACP, 0, wszBuf, -1, lpszDest, cchMax, NULL, NULL))
    return NULL;
1566 1567 1568 1569 1570 1571 1572 1573 1574 1575
  return lpszDest;
}

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

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

1582 1583
  if (!FormatInt(llKB, lpszDest, cchMax))
    return NULL;
1584

1585 1586 1587 1588
  len = lstrlenW(lpszDest);
  if (cchMax - len < 4)
      return NULL;
  lstrcatW(lpszDest, kb);
1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605
  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
1606
 *  cchMax determines the number of characters that are appended to lpszStr,
1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628
 *  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.
1629
 */
1630
LPWSTR WINAPI StrNCatW(LPWSTR lpszStr, LPCWSTR lpszCat, INT cchMax)
1631
{
1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643
  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;
1644 1645 1646
}

/*************************************************************************
1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657
 * 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
1658
 */
1659
BOOL WINAPI StrTrimA(LPSTR lpszStr, LPCSTR lpszTrim)
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
  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;
1693 1694
}

1695
/*************************************************************************
1696 1697 1698
 * StrTrimW	[SHLWAPI.@]
 *
 * See StrTrimA.
1699
 */
1700
BOOL WINAPI StrTrimW(LPWSTR lpszStr, LPCWSTR lpszTrim)
1701
{
1702 1703 1704
  DWORD dwLen;
  LPWSTR lpszRead = lpszStr;
  BOOL bRet = FALSE;
1705

1706 1707 1708 1709
  TRACE("(%s,%s)\n", debugstr_w(lpszStr), debugstr_w(lpszTrim));

  if (lpszRead && *lpszRead)
  {
1710
    while (*lpszRead && StrChrW(lpszTrim, *lpszRead)) lpszRead++;
1711 1712 1713 1714

    dwLen = strlenW(lpszRead);

    if (lpszRead != lpszStr)
1715
    {
1716 1717
      memmove(lpszStr, lpszRead, (dwLen + 1) * sizeof(WCHAR));
      bRet = TRUE;
1718
    }
1719 1720 1721 1722
    if (dwLen > 0)
    {
      lpszRead = lpszStr + dwLen;
      while (StrChrW(lpszTrim, lpszRead[-1]))
1723
        lpszRead--; /* Skip trailing matches */
1724 1725 1726 1727 1728 1729 1730 1731 1732

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

1735
/*************************************************************************
Jon Griffiths's avatar
Jon Griffiths committed
1736
 *      _SHStrDupAA	[INTERNAL]
1737 1738 1739
 *
 * Duplicates a ASCII string to ASCII. The destination buffer is allocated.
 */
1740
static HRESULT _SHStrDupAA(LPCSTR src, LPSTR * dest)
1741 1742 1743 1744 1745
{
	HRESULT hr;
	int len = 0;

	if (src) {
Jon Griffiths's avatar
Jon Griffiths committed
1746
	    len = lstrlenA(src) + 1;
1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762
	    *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
1763
/*************************************************************************
Jon Griffiths's avatar
Jon Griffiths committed
1764
 * SHStrDupA	[SHLWAPI.@]
1765
 *
Jon Griffiths's avatar
Jon Griffiths committed
1766
 * Return a Unicode copy of a string, in memory allocated by CoTaskMemAlloc().
Juergen Schmied's avatar
Juergen Schmied committed
1767
 *
1768 1769 1770 1771 1772 1773 1774 1775
 * 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
1776
 */
Jon Griffiths's avatar
Jon Griffiths committed
1777
HRESULT WINAPI SHStrDupA(LPCSTR lpszStr, LPWSTR * lppszDest)
Juergen Schmied's avatar
Juergen Schmied committed
1778
{
Jon Griffiths's avatar
Jon Griffiths committed
1779 1780
  HRESULT hRet;
  int len = 0;
Juergen Schmied's avatar
Juergen Schmied committed
1781

Jon Griffiths's avatar
Jon Griffiths committed
1782 1783 1784 1785 1786 1787 1788
  if (lpszStr)
  {
    len = MultiByteToWideChar(0, 0, lpszStr, -1, 0, 0) * sizeof(WCHAR);
    *lppszDest = CoTaskMemAlloc(len);
  }
  else
    *lppszDest = NULL;
Juergen Schmied's avatar
Juergen Schmied committed
1789

Jon Griffiths's avatar
Jon Griffiths committed
1790 1791
  if (*lppszDest)
  {
1792
    MultiByteToWideChar(0, 0, lpszStr, -1, *lppszDest, len/sizeof(WCHAR));
Jon Griffiths's avatar
Jon Griffiths committed
1793 1794 1795 1796
    hRet = S_OK;
  }
  else
    hRet = E_OUTOFMEMORY;
Juergen Schmied's avatar
Juergen Schmied committed
1797

Jon Griffiths's avatar
Jon Griffiths committed
1798 1799
  TRACE("%s->(%p)\n", debugstr_a(lpszStr), *lppszDest);
  return hRet;
Juergen Schmied's avatar
Juergen Schmied committed
1800 1801
}

1802 1803 1804 1805 1806
/*************************************************************************
 *      _SHStrDupAW	[INTERNAL]
 *
 * Duplicates a UNICODE to a ASCII string. The destination buffer is allocated.
 */
1807
static HRESULT _SHStrDupAW(LPCWSTR src, LPSTR * dest)
1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829
{
	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
1830
/*************************************************************************
Jon Griffiths's avatar
Jon Griffiths committed
1831
 * SHStrDupW	[SHLWAPI.@]
Juergen Schmied's avatar
Juergen Schmied committed
1832
 *
1833
 * See SHStrDupA.
Juergen Schmied's avatar
Juergen Schmied committed
1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856
 */
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;
}
1857 1858 1859 1860 1861 1862

/*************************************************************************
 * SHLWAPI_WriteReverseNum
 *
 * Internal helper for SHLWAPI_WriteTimeClass.
 */
1863
static inline LPWSTR SHLWAPI_WriteReverseNum(LPWSTR lpszOut, DWORD dwNum)
1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882
{
  *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.
 */
1883
static inline int SHLWAPI_FormatSignificant(LPWSTR lpszNum, int dwDigits)
1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903
{
  /* 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.
 */
1904 1905
static int SHLWAPI_WriteTimeClass(LPWSTR lpszOut, DWORD dwValue,
                                  UINT uClassStringId, int iDigits)
1906 1907 1908 1909 1910 1911
{
  WCHAR szBuff[64], *szOut = szBuff + 32;

  szOut = SHLWAPI_WriteReverseNum(szOut, dwValue);
  iDigits = SHLWAPI_FormatSignificant(szOut + 1, iDigits);
  *szOut = ' ';
1912
  LoadStringW(shlwapi_hInstance, uClassStringId, szBuff + 32, 32);
1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933
  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
1934
 *
1935 1936 1937 1938 1939 1940
 *  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
1941
 *
1942 1943 1944
 *  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
1945 1946
 *|  iDigits    1        2        3        4               5               ...
 *|  lpszStr   "100 hr" "130 hr" "138 hr" "138 hr 40 min" "138 hr 43 min"  ...
1947 1948 1949 1950 1951 1952
 */
INT WINAPI StrFromTimeIntervalA(LPSTR lpszStr, UINT cchMax, DWORD dwMS,
                                int iDigits)
{
  INT iRet = 0;

1953
  TRACE("(%p,%d,%d,%d)\n", lpszStr, cchMax, dwMS, iDigits);
1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974

  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;

1975
  TRACE("(%p,%d,%d,%d)\n", lpszStr, cchMax, dwMS, iDigits);
1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997

  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)
1998
      iDigits = SHLWAPI_WriteTimeClass(szCopy, dwHours, IDS_TIME_INTERVAL_HOURS, iDigits);
1999 2000

    if (dwMinutes && iDigits)
2001
      iDigits = SHLWAPI_WriteTimeClass(szCopy, dwMinutes, IDS_TIME_INTERVAL_MINUTES, iDigits);
2002 2003

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

2006
    lstrcpynW(lpszStr, szCopy, cchMax);
2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029
    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)
{
2030
  DWORD dwFlags;
2031 2032 2033 2034

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

2035 2036
  /* FIXME: This flag is undocumented and unknown by our CompareString.
   *        We need a define for it.
2037
   */
2038 2039
  dwFlags = 0x10000000;
  if (!bCase) dwFlags |= NORM_IGNORECASE;
2040

2041
  return (CompareStringA(GetThreadLocale(), dwFlags, lpszStr, iLen, lpszComp, iLen) == CSTR_EQUAL);
2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056
}

/*************************************************************************
 * 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);

2057 2058
  /* FIXME: This flag is undocumented and unknown by our CompareString.
   *        We need a define for it.
2059
   */
2060 2061
  dwFlags = 0x10000000;
  if (!bCase) dwFlags |= NORM_IGNORECASE;
2062

2063
  return (CompareStringW(GetThreadLocale(), dwFlags, lpszStr, iLen, lpszComp, iLen) == CSTR_EQUAL);
2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076
}

/*************************************************************************
 * @    [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
2077
 *  Success: A pointer to the last character written to lpszDest.
2078 2079
 *  Failure: lpszDest, if any arguments are invalid.
 */
2080
LPSTR WINAPI StrCpyNXA(LPSTR lpszDest, LPCSTR lpszSrc, int iLen)
2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096
{
  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]
 *
2097
 * Unicode version of StrCpyNXA.
2098
 */
2099
LPWSTR WINAPI StrCpyNXW(LPWSTR lpszDest, LPCWSTR lpszSrc, int iLen)
2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111
{
  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;
}
2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164

/*************************************************************************
 * 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
      {
2165
        iDiff = ChrCmpIW(*lpszStr,*lpszComp);
2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186
        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;
2187
  int      nDecimals;
Jacek Caban's avatar
Jacek Caban committed
2188
  WCHAR     wPrefix;
2189 2190 2191
} SHLWAPI_BYTEFORMATS;

/*************************************************************************
Jacek Caban's avatar
Jacek Caban committed
2192
 * StrFormatByteSizeW	[SHLWAPI.@]
2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204
 *
 * 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
2205
 *  There is no StrFormatByteSize64W function, it is called StrFormatByteSizeW().
2206
 */
Jacek Caban's avatar
Jacek Caban committed
2207
LPWSTR WINAPI StrFormatByteSizeW(LONGLONG llBytes, LPWSTR lpszDest, UINT cchMax)
2208
{
2209 2210 2211 2212 2213 2214
#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)

2215 2216
  static const SHLWAPI_BYTEFORMATS bfFormats[] =
  {
2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232
    { 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 */
2233
  };
Jacek Caban's avatar
Jacek Caban committed
2234
  WCHAR wszAdd[] = {' ','?','B',0};
2235 2236 2237
  double dBytes;
  UINT i = 0;

2238
  TRACE("(0x%s,%p,%d)\n", wine_dbgstr_longlong(llBytes), lpszDest, cchMax);
2239 2240 2241 2242 2243 2244

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

  if (llBytes < 1024)  /* 1K */
  {
2245 2246 2247
    WCHAR wszBytesFormat[64];
    LoadStringW(shlwapi_hInstance, IDS_BYTES_FORMAT, wszBytesFormat, 64);
    snprintfW(lpszDest, cchMax, wszBytesFormat, (long)llBytes);
2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272
    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)
    dBytes = (double)(llBytes >> 20) + 0.001; /* Scale down by I MB */
  else
    dBytes = (double)llBytes + 0.00001;

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

2273 2274
  if (!FormatDouble(dBytes, bfFormats[i].nDecimals, lpszDest, cchMax))
    return NULL;
Jacek Caban's avatar
Jacek Caban committed
2275
  wszAdd[1] = bfFormats[i].wPrefix;
2276
  StrCatBuffW(lpszDest, wszAdd, cchMax);
2277 2278 2279 2280
  return lpszDest;
}

/*************************************************************************
Jacek Caban's avatar
Jacek Caban committed
2281
 * StrFormatByteSize64A	[SHLWAPI.@]
2282
 *
Jacek Caban's avatar
Jacek Caban committed
2283
 * See StrFormatByteSizeW.
2284
 */
Jacek Caban's avatar
Jacek Caban committed
2285
LPSTR WINAPI StrFormatByteSize64A(LONGLONG llBytes, LPSTR lpszDest, UINT cchMax)
2286
{
Jacek Caban's avatar
Jacek Caban committed
2287
  WCHAR wszBuff[32];
2288

Jacek Caban's avatar
Jacek Caban committed
2289
  StrFormatByteSizeW(llBytes, wszBuff, sizeof(wszBuff)/sizeof(WCHAR));
2290 2291

  if (lpszDest)
Jacek Caban's avatar
Jacek Caban committed
2292
    WideCharToMultiByte(CP_ACP, 0, wszBuff, -1, lpszDest, cchMax, 0, 0);
2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309
  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
2310 2311
 *  The Ascii and Unicode versions of this function accept a different
 *  integer type for dwBytes. See StrFormatByteSize64A().
2312 2313 2314
 */
LPSTR WINAPI StrFormatByteSizeA(DWORD dwBytes, LPSTR lpszDest, UINT cchMax)
{
2315
  TRACE("(%d,%p,%d)\n", dwBytes, lpszDest, cchMax);
2316 2317 2318

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

Jon Griffiths's avatar
Jon Griffiths committed
2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351
/*************************************************************************
 *      @	[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
2352
/*************************************************************************
2353
 *      @	[SHLWAPI.203]
Jon Griffiths's avatar
Jon Griffiths committed
2354 2355 2356 2357 2358 2359 2360 2361 2362 2363
 *
 * 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.
 */
2364
char WINAPI SHStripMneumonicA(LPCSTR lpszStr)
Jon Griffiths's avatar
Jon Griffiths committed
2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391
{
  LPSTR lpszIter, lpszTmp;
  char ch;

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

  ch = *lpszStr;

  if ((lpszIter = StrChrA(lpszStr, '&')))
  {
    lpszTmp = CharNextA(lpszIter);
    if (lpszTmp && *lpszTmp)
    {
      if (*lpszTmp != '&')
        ch =  *lpszTmp;

      while (lpszIter && *lpszIter)
      {
        lpszTmp = CharNextA(lpszIter);
        *lpszIter = *lpszTmp;
        lpszIter = lpszTmp;
      }
    }
  }

  return ch;
}
Jon Griffiths's avatar
Jon Griffiths committed
2392 2393

/*************************************************************************
2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408
 *      @	[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, '&')))
  {
2409
    lpszTmp = lpszIter + 1;
2410 2411 2412 2413 2414 2415 2416
    if (lpszTmp && *lpszTmp)
    {
      if (*lpszTmp != '&')
        ch =  *lpszTmp;

      while (lpszIter && *lpszIter)
      {
2417
        lpszTmp = lpszIter + 1;
2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428
        *lpszIter = *lpszTmp;
        lpszIter = lpszTmp;
      }
    }
  }

  return ch;
}

/*************************************************************************
 *      @	[SHLWAPI.216]
Jon Griffiths's avatar
Jon Griffiths committed
2429 2430 2431 2432
 *
 * Convert an Ascii string to Unicode.
 *
 * PARAMS
2433
 * dwCp     [I] Code page for the conversion
Jon Griffiths's avatar
Jon Griffiths committed
2434 2435 2436 2437 2438 2439 2440
 * 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.
 */
2441
DWORD WINAPI SHAnsiToUnicodeCP(DWORD dwCp, LPCSTR lpSrcStr, LPWSTR lpDstStr, int iLen)
Jon Griffiths's avatar
Jon Griffiths committed
2442 2443 2444
{
  DWORD dwRet;

2445
  dwRet = MultiByteToWideChar(dwCp, 0, lpSrcStr, -1, lpDstStr, iLen);
2446
  TRACE("%s->%s,ret=%d\n", debugstr_a(lpSrcStr), debugstr_w(lpDstStr), dwRet);
Jon Griffiths's avatar
Jon Griffiths committed
2447 2448 2449
  return dwRet;
}

2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470
/*************************************************************************
 *      @	[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
2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488
/*************************************************************************
 *      @	[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
 *  lpiLen   [I/O] Input length of lpDstStr/destination for length of lpDstStr
 *
 * RETURNS
 *  Success: The number of characters that result from the conversion.
 *  Failure: 0.
 */
INT WINAPI SHUnicodeToAnsiCP(UINT CodePage, LPCWSTR lpSrcStr, LPSTR lpDstStr,
                             LPINT lpiLen)
{
2489
  static const WCHAR emptyW[] = { '\0' };
Jon Griffiths's avatar
Jon Griffiths committed
2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513
  int len , reqLen;
  LPSTR mem;

  if (!lpDstStr || !lpiLen)
    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;
      INT nWideCharCount = len - 1;

2514
      if (!ConvertINetUnicodeToMultiByte(&dwMode, CodePage, lpSrcStr, &nWideCharCount, lpDstStr,
Jon Griffiths's avatar
Jon Griffiths committed
2515 2516 2517 2518 2519
                                          lpiLen))
        return 0;

      if (nWideCharCount < len - 1)
      {
2520
        mem = HeapAlloc(GetProcessHeap(), 0, *lpiLen);
Jon Griffiths's avatar
Jon Griffiths committed
2521 2522 2523 2524 2525
        if (!mem)
          return 0;

        *lpiLen = 0;

2526
        if (ConvertINetUnicodeToMultiByte(&dwMode, CodePage, lpSrcStr, &len, mem, lpiLen))
Jon Griffiths's avatar
Jon Griffiths committed
2527 2528 2529
        {
          SHTruncateString(mem, *lpiLen);
          lstrcpynA(lpDstStr, mem, *lpiLen + 1);
2530
          HeapFree(GetProcessHeap(), 0, mem);
Jon Griffiths's avatar
Jon Griffiths committed
2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550
          return *lpiLen + 1;
        }
        HeapFree(GetProcessHeap(), 0, mem);
        return *lpiLen;
      }
      lpDstStr[*lpiLen] = '\0';
      return *lpiLen;
    }
  default:
    break;
  }

  reqLen = WideCharToMultiByte(CodePage, 0, lpSrcStr, len, lpDstStr,
                               *lpiLen, NULL, NULL);

  if (!reqLen && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
  {
    reqLen = WideCharToMultiByte(CodePage, 0, lpSrcStr, len, NULL, 0, NULL, NULL);
    if (reqLen)
    {
2551
      mem = HeapAlloc(GetProcessHeap(), 0, reqLen);
Jon Griffiths's avatar
Jon Griffiths committed
2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591
      if (mem)
      {
        reqLen = WideCharToMultiByte(CodePage, 0, lpSrcStr, len, mem,
                                     reqLen, NULL, NULL);

        reqLen = SHTruncateString(mem, *lpiLen);
        reqLen++;

        lstrcpynA(lpDstStr, mem, *lpiLen);

        HeapFree(GetProcessHeap(), 0, mem);
      }
    }
  }
  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)
{
    INT myint = iLen;

    return SHUnicodeToAnsiCP(CP_ACP, lpSrcStr, lpDstStr, &myint);
}

2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630
/*************************************************************************
 *      @	[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
2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671
/*************************************************************************
 *      @	[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);
}
2672 2673 2674 2675

/*************************************************************************
 *      SHLoadIndirectString    [SHLWAPI.@]
 *
2676
 * If passed a string that begins with '@', extract the string from the
2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711
 * 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
2712
            FIXME("can't handle non-negative indices (%d)\n", index);
2713 2714 2715 2716 2717 2718 2719 2720
    }
    else
    {
        if(dst != src)
            lstrcpynW(dst, src, dst_len);
        hr = S_OK;
    }

2721
    TRACE("returning %s\n", debugstr_w(dst));
2722 2723 2724 2725 2726
end:
    if(hmod) FreeLibrary(hmod);
    HeapFree(GetProcessHeap(), 0, dllname);
    return hr;
}