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.

131 lines
3.5 KiB

  1. #ifndef _ESCSTR_H
  2. #define _ESCSTR_H
  3. #define ESC_CHAR_LIST_1 L",=+<>#;"
  4. #define ESC_CHAR_LIST_2 L"/,=\r\n+<>#;\"\\"
  5. #define ESC_CHAR_LIST_3 L"/"
  6. #define ESC_CHAR_LIST_4 L"\\"
  7. // ----------------------------------------------------------------------------
  8. // EscapeString()
  9. // ----------------------------------------------------------------------------
  10. inline void EscapeString(LPWSTR szInOut, DWORD dwList=0) // WSTR so that it can't be compiled ANSI
  11. {
  12. if ( !szInOut || !wcslen(szInOut) ) // Empty string?
  13. return;
  14. WCHAR *szTmp = new WCHAR[wcslen(szInOut) * 2];
  15. WCHAR *pIn = NULL;
  16. WCHAR *pOut = NULL;
  17. WCHAR *szList = NULL;
  18. if ( !szTmp )
  19. {
  20. _ASSERT(FALSE);
  21. return;
  22. }
  23. switch(dwList)
  24. {
  25. case 0:
  26. case 1:
  27. szList = ESC_CHAR_LIST_1;
  28. break;
  29. case 2:
  30. szList = ESC_CHAR_LIST_2;
  31. break;
  32. case 3:
  33. szList = ESC_CHAR_LIST_3;
  34. break;
  35. case 4:
  36. szList = ESC_CHAR_LIST_4;
  37. break;
  38. default:
  39. szList = ESC_CHAR_LIST_1;
  40. break;
  41. }
  42. for ( pIn = szInOut, pOut = szTmp; *pIn != 0; pIn++ )
  43. {
  44. if ( wcschr(szList, *pIn) ) // If it's a bad char...
  45. *pOut++ = L'\\'; // then add the '\'
  46. *pOut++ = *pIn; // then append the char
  47. }
  48. *pOut = 0;
  49. wcscpy(szInOut, szTmp); // Copy the new string to "szInOut"
  50. delete[] szTmp; // cause we're modifying in place.
  51. return;
  52. }
  53. // ----------------------------------------------------------------------------
  54. // EscapeString()
  55. // ----------------------------------------------------------------------------
  56. inline CString EscapeString(LPCWSTR szIn, DWORD dwList=0) // WSTR so that it can't be compiled ANSI
  57. {
  58. CString csOut = _T("");
  59. if( !szIn || !wcslen(szIn) ) return csOut;
  60. WCHAR *szOut = new WCHAR[wcslen(szIn) * 2];
  61. WCHAR *pIn = NULL;
  62. WCHAR *pOut = NULL;
  63. WCHAR *szList = NULL;
  64. if ( !szOut )
  65. {
  66. _ASSERT(FALSE);
  67. csOut = szIn;
  68. return csOut;
  69. }
  70. switch(dwList)
  71. {
  72. case 0:
  73. case 1:
  74. szList = ESC_CHAR_LIST_1;
  75. break;
  76. case 2:
  77. szList = ESC_CHAR_LIST_2;
  78. break;
  79. case 3:
  80. szList = ESC_CHAR_LIST_3;
  81. break;
  82. case 4:
  83. szList = ESC_CHAR_LIST_4;
  84. break;
  85. default:
  86. szList = ESC_CHAR_LIST_1;
  87. break;
  88. }
  89. for ( pIn = (LPWSTR)szIn, pOut = szOut; *pIn != 0; pIn++ )
  90. {
  91. if( wcschr(szList, *pIn) ) // // If it's a bad char...
  92. *pOut++ = L'\\'; // then add the '\'
  93. *pOut++ = *pIn; // then append the char
  94. }
  95. *pOut = 0;
  96. csOut = szOut;
  97. delete[] szOut;
  98. return csOut;
  99. }
  100. // ----------------------------------------------------------------------------
  101. // EscapeString()
  102. // ----------------------------------------------------------------------------
  103. inline CString EscapeString(CString csIn, DWORD dwList=0)
  104. {
  105. CString csOut = _T("");
  106. csOut = EscapeString((LPCTSTR)csIn, dwList);
  107. return csOut;
  108. }
  109. #endif