Source code of Windows XP (NT5)
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.

150 lines
3.4 KiB

  1. // StResource.cpp -- String Resource helper routines
  2. // (c) Copyright Schlumberger Technology Corp., unpublished work, created
  3. // 1998. This computer program includes Confidential, Proprietary
  4. // Information and is a Trade Secret of Schlumberger Technology Corp. All
  5. // use, disclosure, and/or reproduction is prohibited unless authorized
  6. // in writing. All Rights Reserved.
  7. #if defined(_UNICODE)
  8. #if !defined(UNICODE)
  9. #define UNICODE
  10. #endif //!UNICODE
  11. #endif //_UNICODE
  12. #if defined(UNICODE)
  13. #if !defined(_UNICODE)
  14. #define _UNICODE
  15. #endif //!_UNICODE
  16. #endif //UNICODE
  17. #include "stdafx.h"
  18. #include <string>
  19. #include <scuOsExc.h>
  20. #include "CspProfile.h"
  21. #include "StResource.h"
  22. #include "Blob.h"
  23. using namespace std;
  24. using namespace ProviderProfile;
  25. // Maximum string resource length as defined by MS
  26. static const size_t cMaxResourceLength = 4095;
  27. StringResource::StringResource(UINT uID)
  28. : m_s()
  29. {
  30. static _TCHAR szBuffer[cMaxResourceLength]; // include null terminator
  31. if (0 == LoadString(CspProfile::Instance().Resources(), uID, szBuffer,
  32. (sizeof szBuffer / sizeof szBuffer[0])))
  33. throw scu::OsException(ERROR_RESOURCE_NOT_PRESENT);
  34. string stmp(AsCCharP(szBuffer), (_tcslen(szBuffer)+1)*sizeof _TCHAR);
  35. m_s = stmp;
  36. CString cstmp(szBuffer);
  37. m_cs = cstmp;
  38. }
  39. const string
  40. StringResource::AsString() const
  41. {
  42. return m_s;
  43. }
  44. const CString
  45. StringResource::AsCString() const
  46. {
  47. return m_cs;
  48. }
  49. const string
  50. StringResource::AsciiFromUnicode(LPCTSTR szSource)
  51. {
  52. string sTarget;
  53. int nChars = _tcslen(szSource);
  54. sTarget.resize(nChars);
  55. for(int i =0; i<nChars; i++)
  56. sTarget[i] = static_cast<char>(*(szSource+i));
  57. return sTarget;
  58. }
  59. const string
  60. StringResource::CheckAsciiFromUnicode(LPCTSTR szSource)
  61. {
  62. string sTarget;
  63. int nChars = _tcslen(szSource);
  64. //Here we check every incoming character for being
  65. //a proper ASCII character before assigning it to the
  66. //output buffer. We set the output to '\xFF' if the ascii
  67. //test fails.
  68. sTarget.resize(nChars);
  69. for(int i =0; i<nChars; i++)
  70. {
  71. if(iswascii(*(szSource+i)))
  72. {
  73. sTarget[i] = static_cast<char>(*(szSource+i));
  74. }
  75. else
  76. {
  77. sTarget[i] = '\xFF';
  78. }
  79. }
  80. return sTarget;
  81. }
  82. bool
  83. StringResource::IsASCII(LPCTSTR szSource)
  84. {
  85. bool RetValue = true;
  86. int nChars = _tcslen(szSource);
  87. //Here we check every incoming character for being
  88. //a proper ASCII character. If one of them is non ASCII
  89. //we return false
  90. for(int i =0; i<nChars; i++)
  91. {
  92. if(!iswascii(*(szSource+i)))
  93. {
  94. return false;
  95. }
  96. }
  97. return RetValue;
  98. }
  99. const CString
  100. StringResource::UnicodeFromAscii(string const &rsSource)
  101. {
  102. CString csTarget;
  103. int nChars = rsSource.length();
  104. if(nChars)
  105. {
  106. LPTSTR pCharBuffer = csTarget.GetBufferSetLength(nChars);
  107. int itChar = 0;
  108. for(int iChar=0; iChar<nChars; iChar++)
  109. {
  110. if(rsSource[iChar] != '\0')
  111. *(pCharBuffer+itChar++) = rsSource[iChar];
  112. }
  113. //Set the final null terminator
  114. *(pCharBuffer+itChar)='\0';
  115. csTarget.ReleaseBuffer(-1);//Let CString set its length appropriately
  116. }
  117. return csTarget;
  118. }
  119. HANDLE
  120. GetImageResource(DWORD dwId,
  121. DWORD dwType)
  122. {
  123. return LoadImage(CspProfile::Instance().Resources(),
  124. MAKEINTRESOURCE(dwId), dwType, 0, 0, LR_SHARED);
  125. }