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.

128 lines
3.4 KiB

  1. // cutil.cpp
  2. //
  3. // file to put misc utility classes implementation
  4. //
  5. #include "private.h"
  6. #include "xstring.h"
  7. #include "cregkey.h"
  8. #include "strary.h"
  9. #include "cutil.h"
  10. static const char c_szSpeechRecognizersKey[] = "Software\\Microsoft\\Speech\\Recognizers";
  11. static const char c_szSpeechRecognizersTokensKey[] = "Software\\Microsoft\\Speech\\Recognizers\\Tokens";
  12. static const char c_szDefault[] = "DefaultTokenId";
  13. static const char c_szAttribute[] = "Attributes";
  14. static const char c_szLanguage[] = "Language";
  15. _inline BOOL _IsCompatibleLangid(LANGID langidReq, LANGID langidCmp)
  16. {
  17. if (PRIMARYLANGID(langidReq) == LANG_CHINESE)
  18. {
  19. return langidReq == langidCmp;
  20. }
  21. else
  22. {
  23. return PRIMARYLANGID(langidReq) == PRIMARYLANGID(langidCmp);
  24. }
  25. }
  26. BOOL CDetectSRUtil::_IsSREnabledForLangInReg(LANGID langidReq)
  27. {
  28. //
  29. // We want to see if any installed recognizer can satisfy
  30. // the langid requested.
  31. //
  32. if (m_langidRecognizers.Count() == 0)
  33. {
  34. CMyRegKey regkey;
  35. char szRecognizerName[MAX_PATH];
  36. LONG lret = regkey.Open(HKEY_LOCAL_MACHINE,
  37. c_szSpeechRecognizersTokensKey,
  38. KEY_READ);
  39. if(ERROR_SUCCESS == lret)
  40. {
  41. CMyRegKey regkeyReco;
  42. DWORD dwIndex = 0;
  43. while (ERROR_SUCCESS ==
  44. regkey.EnumKey(dwIndex, szRecognizerName, ARRAYSIZE(szRecognizerName)))
  45. {
  46. lret = regkeyReco.Open(regkey.m_hKey, szRecognizerName, KEY_READ);
  47. if (ERROR_SUCCESS == lret)
  48. {
  49. LANGID langid=_GetLangIdFromRecognizerToken(regkeyReco.m_hKey);
  50. if (langid)
  51. {
  52. LANGID *pl = m_langidRecognizers.Append(1);
  53. if (pl)
  54. *pl = langid;
  55. }
  56. regkeyReco.Close();
  57. }
  58. dwIndex++;
  59. }
  60. }
  61. }
  62. BOOL fEnabled = FALSE;
  63. for (int i = 0 ; i < m_langidRecognizers.Count(); i++)
  64. {
  65. LANGID *p= m_langidRecognizers.GetPtr(i);
  66. if (p)
  67. {
  68. if (_IsCompatibleLangid(langidReq, *p))
  69. {
  70. fEnabled = TRUE;
  71. break;
  72. }
  73. }
  74. }
  75. return fEnabled;
  76. }
  77. LANGID CDetectSRUtil::_GetLangIdFromRecognizerToken(HKEY hkeyToken)
  78. {
  79. LANGID langid = 0;
  80. char szLang[MAX_PATH];
  81. CMyRegKey regkeyAttr;
  82. LONG lret = regkeyAttr.Open(hkeyToken, c_szAttribute, KEY_READ);
  83. if (ERROR_SUCCESS == lret)
  84. {
  85. lret = regkeyAttr.QueryValueCch(szLang, c_szLanguage, ARRAYSIZE(szLang));
  86. }
  87. if (ERROR_SUCCESS == lret)
  88. {
  89. char *psz = szLang;
  90. while(*psz && *psz != ';')
  91. {
  92. langid = langid << 4;
  93. if (*psz >= 'a' && *psz <= 'f')
  94. {
  95. *psz -= ('a' - 'A');
  96. }
  97. if (*psz >= 'A' && *psz <= 'F')
  98. {
  99. langid += *psz - 'A' + 10;
  100. }
  101. else if (*psz >= '0' && *psz <= '9')
  102. {
  103. langid += *psz - '0';
  104. }
  105. psz++;
  106. }
  107. }
  108. return langid;
  109. }