DictionaryObj.cpp
上传用户:biuytresa
上传日期:2007-12-07
资源大小:721k
文件大小:9k
源码类别:

DNA

开发平台:

Visual C++

  1. // DictionaryObj.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "DictComp.h"
  5. #include <comutil.h>
  6. #include "DictionaryObj.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CDictionaryObj
  14. IMPLEMENT_DYNCREATE(CDictionaryObj, CCmdTarget)
  15. CDictionaryObj::CDictionaryObj()
  16. {
  17. m_nWordNumber = 0;
  18. m_nStructNumber = 0;
  19. m_pData = NULL;
  20. }
  21. CDictionaryObj::~CDictionaryObj()
  22. {
  23. if (m_pData != NULL) 
  24. {
  25. delete [] m_pData;
  26. }
  27. }
  28. BEGIN_MESSAGE_MAP(CDictionaryObj, CCmdTarget)
  29. //{{AFX_MSG_MAP(CDictionaryObj)
  30. // NOTE - the ClassWizard will add and remove mapping macros here.
  31. //}}AFX_MSG_MAP
  32. END_MESSAGE_MAP()
  33. extern "C" const IID IID_Dictionary = 
  34. { 0x54bf6568, 0x1007, 0x11d1,
  35. { 0xb0, 0xaa, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00} } ;
  36. extern "C" const IID IID_SpellCheck = 
  37. { 0x54bf6569, 0x1007, 0x11d1,
  38. { 0xb0, 0xaa, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00} } ;
  39. BEGIN_INTERFACE_MAP(CDictionaryObj, CCmdTarget)
  40. INTERFACE_PART(CDictionaryObj, IID_Dictionary, Dictionary)
  41. INTERFACE_PART(CDictionaryObj, IID_SpellCheck, SpellCheck)
  42. END_INTERFACE_MAP()
  43. // {54BF6567-1007-11D1-B0AA-444553540000}
  44. IMPLEMENT_OLECREATE(CDictionaryObj, "Dictionary.Object", 
  45. 0x54bf6567, 0x1007, 0x11d1, 0xb0, 0xaa, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00)
  46. /////////////////////////////////////////////////////////////////////////////
  47. // CDictionaryObj::XDictionary
  48. STDMETHODIMP_(ULONG) CDictionaryObj::XDictionary::AddRef()
  49. {
  50. METHOD_PROLOGUE_EX_(CDictionaryObj, Dictionary)
  51. return (ULONG)pThis->ExternalAddRef();
  52. }
  53. STDMETHODIMP_(ULONG) CDictionaryObj::XDictionary::Release()
  54. {
  55. METHOD_PROLOGUE_EX_(CDictionaryObj, Dictionary)
  56. return (ULONG)pThis->ExternalRelease();
  57. }
  58. STDMETHODIMP CDictionaryObj::XDictionary::QueryInterface(
  59. REFIID iid, LPVOID* ppvObj)
  60. {
  61. METHOD_PROLOGUE_EX_(CDictionaryObj, Dictionary)
  62. return (HRESULT)pThis->ExternalQueryInterface(&iid, ppvObj);
  63. }
  64. STDMETHODIMP_(BOOL) CDictionaryObj::XDictionary::Initialize()
  65. {
  66. METHOD_PROLOGUE_EX_(CDictionaryObj, Dictionary)
  67. pThis->m_nWordNumber = 0;
  68. pThis->m_nStructNumber = 0;
  69. if (pThis->m_pData != NULL) 
  70. {
  71. delete [] pThis->m_pData;
  72. }
  73. pThis->m_pData = NULL;
  74. return TRUE;
  75. }
  76. STDMETHODIMP_(BOOL) CDictionaryObj::XDictionary::LoadLibrary(LPOLESTR filename)
  77. {
  78. METHOD_PROLOGUE_EX_(CDictionaryObj, Dictionary)
  79. char *pFileName = _com_util::ConvertBSTRToString(filename);
  80. FILE *fp;
  81. if( (fp = fopen( pFileName, "rt" )) == NULL ) {
  82. printf("Open dictionary file : %s failed.n", pFileName);
  83. delete pFileName;
  84. return FALSE;
  85. }
  86. char LineBuffer[128];
  87. if (feof(fp)) {
  88. printf("It is a null file!n");
  89. fclose(fp);
  90. delete pFileName;
  91. return FALSE; 
  92. }
  93. if (fgets(LineBuffer, 128, fp) == NULL) {
  94. printf("Read TotalNumber failed!n");
  95. fclose(fp);
  96. delete pFileName;
  97. return FALSE; 
  98. }
  99. int nTotalNumber = 0;
  100. sscanf(LineBuffer, "%d", &nTotalNumber);
  101. if ( (nTotalNumber < 1) && (nTotalNumber > 5000) ) {
  102. printf("The Number of words is invalid!n");
  103. fclose(fp);
  104. delete pFileName;
  105. return FALSE; 
  106. }
  107. Initialize();
  108. pThis->m_nStructNumber = nTotalNumber+100;
  109. pThis->m_pData = new DictWord[pThis->m_nStructNumber];
  110. pThis->m_nWordNumber = 0;
  111. while(!feof(fp)) {
  112. if (fgets(LineBuffer, MaxWordLength, fp) == NULL) {
  113. printf("Read the first string failed!n");
  114. break;
  115. }
  116. sscanf(LineBuffer, "%s", pThis->m_pData[pThis->m_nWordNumber].wordForLang1);
  117. if (fgets(LineBuffer, MaxWordLength, fp) == NULL) {
  118. printf("Read the second string failed!n");
  119. break;
  120. }
  121. sscanf(LineBuffer, "%s", pThis->m_pData[pThis->m_nWordNumber].wordForLang2);
  122. pThis->m_nWordNumber ++;
  123. if (pThis->m_nWordNumber == nTotalNumber)
  124. break;
  125. if (pThis->m_nWordNumber > pThis->m_nStructNumber)
  126. break;
  127. }
  128. fclose(fp);
  129. delete pFileName;
  130. return TRUE;
  131. }
  132. STDMETHODIMP_(BOOL) CDictionaryObj::XDictionary::InsertWord(LPOLESTR word1, LPOLESTR word2)
  133. {
  134. METHOD_PROLOGUE_EX_(CDictionaryObj, Dictionary)
  135. char *pWord1, *pWord2;
  136. if (pThis->m_nWordNumber < pThis->m_nStructNumber) {
  137. pWord1 = _com_util::ConvertBSTRToString(word1);
  138. pWord2 = _com_util::ConvertBSTRToString(word2);
  139. if (strlen(pWord1) > MaxWordLength)
  140. *(pWord1+MaxWordLength-1) = '';
  141. if (strlen(pWord2) > MaxWordLength)
  142. *(pWord2+MaxWordLength-1) = '';
  143. strcpy(pThis->m_pData[pThis->m_nWordNumber].wordForLang1, pWord1);
  144. strcpy(pThis->m_pData[pThis->m_nWordNumber].wordForLang2, pWord2);
  145. pThis->m_nWordNumber ++ ;
  146. delete pWord1;
  147. delete pWord2;
  148. return TRUE;
  149. }
  150. return FALSE;
  151. }
  152. STDMETHODIMP_(void) CDictionaryObj::XDictionary::DeleteWord(LPOLESTR word)
  153. {
  154. METHOD_PROLOGUE_EX_(CDictionaryObj, Dictionary)
  155. char *pWord = _com_util::ConvertBSTRToString(word);
  156. char *pUpperWord = strupr(pWord);
  157. for (int i = 0; i < pThis->m_nWordNumber; i++)
  158. {
  159. char *tmpWord = strupr(pThis->m_pData[i].wordForLang1);
  160. if (strcmp(tmpWord, pWord) == 0) {
  161. for(int j = i + 1; j < pThis->m_nWordNumber; j++) {
  162. strcpy( pThis->m_pData[j].wordForLang1, pThis->m_pData[j + 1].wordForLang1);
  163. strcpy( pThis->m_pData[j].wordForLang2, pThis->m_pData[j + 1].wordForLang2);
  164. }
  165. pThis->m_nWordNumber ++ ;
  166. break;
  167. }
  168. }
  169. delete pWord;
  170. }
  171. STDMETHODIMP_(BOOL) CDictionaryObj::XDictionary::LookupWord(LPOLESTR word, LPOLESTR *resultWord)
  172. {
  173. METHOD_PROLOGUE_EX_(CDictionaryObj, Dictionary)
  174. char *pWord = _com_util::ConvertBSTRToString(word);
  175. char *pUpperWord = strupr(pWord);
  176. for (int i = 0; i < pThis->m_nWordNumber; i++)
  177. {
  178. char *tmpWord = strupr(pThis->m_pData[i].wordForLang1);
  179. if (strcmp(tmpWord, pWord) == 0) {
  180. *resultWord = _com_util::ConvertStringToBSTR(pThis->m_pData[i].wordForLang2);
  181. delete pWord;
  182. return TRUE;
  183. }
  184. }
  185. *resultWord = NULL;
  186. delete pWord;
  187. return FALSE;
  188. }
  189. STDMETHODIMP_(BOOL) CDictionaryObj::XDictionary::RestoreLibrary(LPOLESTR filename)
  190. {
  191. METHOD_PROLOGUE_EX_(CDictionaryObj, Dictionary)
  192. char *pFileName = _com_util::ConvertBSTRToString(filename);
  193. FILE *fp;
  194. if( (fp = fopen( pFileName, "wt" )) == NULL ) {
  195. printf("Open dictionary file : %s failed.n", pFileName);
  196. delete pFileName;
  197. return FALSE;
  198. }
  199. char LineBuffer[128];
  200. sprintf(LineBuffer, "%dn", pThis->m_nWordNumber);
  201. if (fputs(LineBuffer, fp) == EOF) {
  202. printf("Write TotalNumber failed!n");
  203. fclose(fp);
  204. delete pFileName;
  205. return FALSE; 
  206. }
  207. for(int i = 0; i < pThis->m_nWordNumber; i ++ ) {
  208. if (fputs(pThis->m_pData[i].wordForLang1, fp) == EOF) {
  209. printf("Write the first string failed!n");
  210. fclose(fp);
  211. delete pFileName;
  212. return FALSE; 
  213. }
  214. fputs("n", fp);
  215. if (fputs(pThis->m_pData[i].wordForLang2, fp) == EOF) {
  216. printf("Write the first string failed!n");
  217. fclose(fp);
  218. delete pFileName;
  219. return FALSE; 
  220. }
  221. fputs("n", fp);
  222. }
  223. fclose(fp);
  224. delete pFileName;
  225. return TRUE;
  226. }
  227. STDMETHODIMP_(void) CDictionaryObj::XDictionary::FreeLibrary()
  228. {
  229. Initialize();
  230. }
  231. /////////////////////////////////////////////////////////////////////////////
  232. // CDictionaryObj::XSpellCheck
  233. STDMETHODIMP_(ULONG) CDictionaryObj::XSpellCheck::AddRef()
  234. {
  235. METHOD_PROLOGUE_EX_(CDictionaryObj, SpellCheck)
  236. return (ULONG)pThis->ExternalAddRef();
  237. }
  238. STDMETHODIMP_(ULONG) CDictionaryObj::XSpellCheck::Release()
  239. {
  240. METHOD_PROLOGUE_EX_(CDictionaryObj, SpellCheck)
  241. return (ULONG)pThis->ExternalRelease();
  242. }
  243. STDMETHODIMP CDictionaryObj::XSpellCheck::QueryInterface(
  244. REFIID iid, LPVOID* ppvObj)
  245. {
  246. METHOD_PROLOGUE_EX_(CDictionaryObj, SpellCheck)
  247. return (HRESULT)pThis->ExternalQueryInterface(&iid, ppvObj);
  248. }
  249. STDMETHODIMP_(BOOL) CDictionaryObj::XSpellCheck::CheckWord (LPOLESTR word, LPOLESTR *resultWord)
  250. {
  251. METHOD_PROLOGUE_EX_(CDictionaryObj, SpellCheck)
  252. char *pWord = _com_util::ConvertBSTRToString(word);
  253. char *pUpperWord = strupr(pWord);
  254. char *pMinMaxWord, *pMaxMinWord;
  255. int  nMinIndex = -1, nMaxIndex = -1;
  256. pMinMaxWord = pMaxMinWord = NULL;
  257. for (int i = 0; i < pThis->m_nWordNumber; i++)
  258. {
  259. char *tmpWord = strupr(pThis->m_pData[i].wordForLang1);
  260. if (strcmp(tmpWord, pWord) == 0) {
  261. delete pWord;
  262. return TRUE;
  263. } else if (strcmp(tmpWord, pWord) < 0) {
  264. if ((pMinMaxWord == NULL) || (strcmp(tmpWord, pMinMaxWord) > 0)) 
  265. {
  266. pMinMaxWord = tmpWord;
  267. nMinIndex = i;
  268. }
  269. } else {
  270. if ((pMaxMinWord == NULL) || (strcmp(tmpWord, pMaxMinWord) < 0)) 
  271. {
  272. pMaxMinWord = tmpWord;
  273. nMaxIndex = i;
  274. }
  275. }
  276. }
  277. *resultWord = NULL;
  278. if (nMinIndex != -1)
  279. *resultWord = _com_util::ConvertStringToBSTR(pThis->m_pData[nMinIndex].wordForLang1);
  280. else if (nMaxIndex != -1)
  281. *resultWord = _com_util::ConvertStringToBSTR(pThis->m_pData[nMaxIndex].wordForLang1);
  282. delete pWord;
  283. return FALSE;
  284. }
  285. /////////////////////////////////////////////////////////////////////////////
  286. // CDictionaryObj message handlers