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.

150 lines
3.6 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 scu;
  25. using namespace ProviderProfile;
  26. // Maximum string resource length as defined by MS
  27. static const size_t cMaxResourceLength = 4095;
  28. StringResource::StringResource(UINT uID)
  29. : m_s()
  30. {
  31. static _TCHAR szBuffer[cMaxResourceLength]; // include null terminator
  32. if (0 == LoadString(CspProfile::Instance().Resources(), uID, szBuffer,
  33. (sizeof szBuffer / sizeof szBuffer[0])))
  34. throw scu::OsException(ERROR_RESOURCE_NOT_PRESENT);
  35. string stmp(AsCCharP(szBuffer), (_tcslen(szBuffer)+1)*sizeof _TCHAR);
  36. m_s = stmp;
  37. CString cstmp(szBuffer);
  38. m_cs = cstmp;
  39. }
  40. const string
  41. StringResource::AsString() const
  42. {
  43. return m_s;
  44. }
  45. const CString
  46. StringResource::AsCString() const
  47. {
  48. return m_cs;
  49. }
  50. const string
  51. StringResource::AsciiFromUnicode(LPCTSTR szSource)
  52. {
  53. string sTarget;
  54. int nChars = _tcslen(szSource);
  55. sTarget.resize(nChars);
  56. for(int i =0; i<nChars; i++)
  57. sTarget[i] = static_cast<char>(*(szSource+i));
  58. return sTarget;
  59. }
  60. const SecureArray<char>
  61. StringResource::CheckAsciiFromUnicode(LPCTSTR szSource)
  62. {
  63. int nChars = _tcslen(szSource);
  64. SecureArray<char> sTarget(nChars+1);// for null termination
  65. //Here we check every incoming character for being
  66. //a proper ASCII character before assigning it to the
  67. //output buffer. We set the output to '\xFF' if the ascii
  68. //test fails.
  69. int i=0;
  70. for(i=0; i<nChars; i++)
  71. {
  72. if(iswascii(*(szSource+i)))
  73. {
  74. sTarget[i] = static_cast<char>(*(szSource+i));
  75. }
  76. else
  77. {
  78. sTarget[i] = '\xFF';
  79. }
  80. }
  81. sTarget[i] = '\0';
  82. return sTarget;
  83. }
  84. bool
  85. StringResource::IsASCII(LPCTSTR szSource)
  86. {
  87. bool RetValue = true;
  88. int nChars = _tcslen(szSource);
  89. //Here we check every incoming character for being
  90. //a proper ASCII character. If one of them is non ASCII
  91. //we return false
  92. for(int i =0; i<nChars; i++)
  93. {
  94. if(!iswascii(*(szSource+i)))
  95. {
  96. return false;
  97. }
  98. }
  99. return RetValue;
  100. }
  101. const CString
  102. StringResource::UnicodeFromAscii(string const &rsSource)
  103. {
  104. CString csTarget;
  105. int nChars = rsSource.length();
  106. if(nChars)
  107. {
  108. LPTSTR pCharBuffer = csTarget.GetBufferSetLength(nChars);
  109. int itChar = 0;
  110. for(int iChar=0; iChar<nChars; iChar++)
  111. {
  112. if(rsSource[iChar] != '\0')
  113. *(pCharBuffer+itChar++) = rsSource[iChar];
  114. }
  115. //Set the final null terminator
  116. *(pCharBuffer+itChar)='\0';
  117. csTarget.ReleaseBuffer(-1);//Let CString set its length appropriately
  118. }
  119. return csTarget;
  120. }
  121. HANDLE
  122. GetImageResource(DWORD dwId,
  123. DWORD dwType)
  124. {
  125. return LoadImage(CspProfile::Instance().Resources(),
  126. MAKEINTRESOURCE(dwId), dwType, 0, 0, LR_SHARED);
  127. }