Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
6.4 KiB

  1. #include "precomp.h"
  2. int StrPrepend(LPTSTR pszSource, UINT cchSource, LPCTSTR pszAdd, UINT cchAdd /*= 0*/)
  3. {
  4. int iLen;
  5. if (pszSource == NULL)
  6. return -1;
  7. iLen = StrLen(pszSource);
  8. if (cchAdd == 0)
  9. cchAdd = StrLen(pszAdd);
  10. if (cchAdd == 0)
  11. return iLen;
  12. if (iLen + cchAdd >= cchSource)
  13. return -1;
  14. MoveMemory(pszSource + cchAdd, pszSource, StrCbFromCch(iLen + 1));
  15. CopyMemory(pszSource, pszAdd, StrCbFromCch(cchAdd));
  16. return iLen + cchAdd;
  17. }
  18. void StrRemoveAllWhiteSpace(LPTSTR pszBuf)
  19. {
  20. LPTSTR pszSearch;
  21. TCHAR tchar;
  22. int i = 0;
  23. for (i = 0, pszSearch = pszBuf; *pszSearch; i++, pszSearch++)
  24. {
  25. tchar = *pszSearch;
  26. while ((tchar == TEXT(' ')) || (tchar == TEXT('\t')) ||
  27. (tchar == TEXT('\r')) || (tchar == TEXT('\n')))
  28. {
  29. pszSearch++;
  30. tchar = *pszSearch;
  31. }
  32. pszBuf[i] = *pszSearch;
  33. if (!*pszSearch)
  34. break;
  35. }
  36. pszBuf[i] = TEXT('\0');
  37. }
  38. LPTSTR StrGetNextField(LPTSTR *ppszData, LPCTSTR pcszDeLims, DWORD dwFlags)
  39. // If (dwFlags & IGNORE_QUOTES) is TRUE, then look for any char in pcszDeLims in *ppszData. If found,
  40. // replace it with the '\0' char and set *ppszData to point to the beginning of the next field and return
  41. // pointer to current field.
  42. //
  43. // If (dwFlags & IGNORE_QUOTES) is FALSE, then look for any char in pcszDeLims outside of balanced quoted sub-strings
  44. // in *ppszData. If found, replace it with the '\0' char and set *ppszData to point to the beginning of
  45. // the next field and return pointer to current field.
  46. //
  47. // If (dwFlags & REMOVE_QUOTES) is TRUE, then remove the surrounding quotes and replace two consecutive quotes by one.
  48. //
  49. // NOTE: If IGNORE_QUOTES and REMOVE_QUOTES are both specified, then IGNORE_QUOTES takes precedence over REMOVE_QUOTES.
  50. //
  51. // If you just want to remove the quotes from a string, call this function as
  52. // GetNextField(&pszData, "\"" or "'" or "", REMOVE_QUOTES).
  53. //
  54. // If you call this function as GetNextField(&pszData, "\"" or "'" or "", 0), you will get back the
  55. // entire pszData as the field.
  56. //
  57. {
  58. LPTSTR pszRetPtr, pszPtr;
  59. BOOL fWithinQuotes = FALSE, fRemoveQuote;
  60. TCHAR chQuote = TEXT('\0');
  61. if (ppszData == NULL || *ppszData == NULL || **ppszData == TEXT('\0'))
  62. return NULL;
  63. for (pszRetPtr = pszPtr = *ppszData; *pszPtr; pszPtr = CharNext(pszPtr))
  64. {
  65. if (!(dwFlags & IGNORE_QUOTES) && (*pszPtr == TEXT('"') || *pszPtr == TEXT('\'')))
  66. {
  67. fRemoveQuote = FALSE;
  68. if (*pszPtr == *(pszPtr + 1)) // two consecutive quotes become one
  69. {
  70. pszPtr++;
  71. if (dwFlags & REMOVE_QUOTES)
  72. fRemoveQuote = TRUE;
  73. else
  74. {
  75. // if pcszDeLims is '"' or '\'', then *pszPtr == pcszDeLims would
  76. // be TRUE and we would break out of the loop against the design specs;
  77. // to prevent this just continue
  78. continue;
  79. }
  80. }
  81. else if (!fWithinQuotes)
  82. {
  83. fWithinQuotes = TRUE;
  84. chQuote = *pszPtr; // save the quote char
  85. fRemoveQuote = dwFlags & REMOVE_QUOTES;
  86. }
  87. else
  88. {
  89. if (*pszPtr == chQuote) // match the correct quote char
  90. {
  91. fWithinQuotes = FALSE;
  92. fRemoveQuote = dwFlags & REMOVE_QUOTES;
  93. }
  94. }
  95. if (fRemoveQuote)
  96. {
  97. // shift the entire string one char to the left to get rid of the quote char
  98. MoveMemory(pszPtr, pszPtr + 1, StrCbFromCch(StrLen(pszPtr)));
  99. }
  100. }
  101. // BUGBUG: Is type casting pszPtr to UNALIGNED necessary? -- copied it from ANSIStrChr
  102. // check if pszPtr is pointing to one of the chars in pcszDeLims
  103. if (!fWithinQuotes && StrChr(pcszDeLims, *pszPtr) != NULL)
  104. break;
  105. }
  106. // NOTE: if fWithinQuotes is TRUE here, then we have an unbalanced quoted string; but we don't care!
  107. // the entire string after the beginning quote becomes the field
  108. if (*pszPtr) // pszPtr is pointing to a char in pcszDeLims
  109. {
  110. *ppszData = CharNext(pszPtr); // save the pointer to the beginning of next field in *ppszData
  111. *pszPtr = TEXT('\0'); // replace the DeLim char with the '\0' char
  112. }
  113. else
  114. *ppszData = pszPtr; // we have reached the end of the string; next call to this function
  115. // would return NULL
  116. return pszRetPtr;
  117. }
  118. // constructs a string using the format specified in pcszFormatString
  119. LPTSTR WINAPIV FormatString(LPCTSTR pcszFormatString, ...)
  120. {
  121. va_list vaArgs;
  122. LPTSTR pszOutString = NULL;
  123. va_start(vaArgs, pcszFormatString);
  124. FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_STRING,
  125. (LPCVOID) pcszFormatString, 0, 0, (LPTSTR) &pszOutString, 0, &vaArgs);
  126. va_end(vaArgs);
  127. return pszOutString;
  128. }
  129. /////////////////////////////////////////////////////////////////////////////
  130. // String Conversion Routines
  131. LPWSTR StrAnsiToUnicode(LPWSTR pszTarget, LPCSTR pszSource, UINT cchTarget /*= 0*/)
  132. {
  133. int cchResult;
  134. cchResult = SHAnsiToUnicode(pszSource, pszTarget, int((cchTarget != 0) ? cchTarget : StrLenA(pszSource)+1));
  135. if (0 == cchResult)
  136. return NULL;
  137. return pszTarget;
  138. }
  139. LPSTR StrUnicodeToAnsi(LPSTR pszTarget, LPCWSTR pszSource, UINT cchTarget /*= 0*/)
  140. {
  141. int cchResult;
  142. // NOTE: pass in twice the size of the source for the target in case we have DBCS
  143. // chars. We're assuming that the target buffer is sufficient here.
  144. cchResult = SHUnicodeToAnsi(pszSource, pszTarget,
  145. (cchTarget != 0) ? cchTarget : (StrLenW(pszSource)+1) * 2);
  146. if (0 == cchResult)
  147. return NULL;
  148. return pszTarget;
  149. }
  150. LPTSTR StrSameToSame(LPTSTR pszTarget, LPCTSTR pszSource, UINT cchTarget /*= 0*/)
  151. {
  152. CopyMemory(pszTarget, pszSource, StrCbFromCch((cchTarget != 0) ? cchTarget : StrLen(pszSource)+1));
  153. return pszTarget;
  154. }