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.

345 lines
9.0 KiB

  1. /*++
  2. Copyright (c) 1995-1999 Microsoft Corporation, All Rights Reserved
  3. Module Name:
  4. CRTWORD.C
  5. ++*/
  6. #include <windows.h>
  7. #include <winerror.h>
  8. #include <immdev.h>
  9. #include <imedefs.h>
  10. #ifdef UNICODE
  11. #define NUMENCODEAREA 8
  12. #else
  13. #define NUMENCODEAREA 252
  14. #endif
  15. typedef struct tagENCODEAREA {
  16. DWORD PreCount;
  17. DWORD StartEncode;
  18. DWORD EndEncode;
  19. } ENCODEAREA,FAR *LPENCODEAREA;
  20. DWORD EncodeToXgbNo(LPTSTR, DWORD , DWORD, HANDLE );
  21. BOOL ConvGetEncode(HANDLE ,LPENCODEAREA ,LPDWORD ,LPDWORD ,LPMAININDEX);
  22. void ConvCreateWord(HWND hWnd,LPCTSTR MBFileName,LPTSTR szWordStr,LPTSTR lpCode)
  23. {
  24. int nWordLen=lstrlen(szWordStr)*sizeof(TCHAR)/2;
  25. DWORD i,j,k,dwCodeLen;
  26. TCHAR szDBCS[3],szCode[13];
  27. BOOL bReturn=FALSE;
  28. HANDLE hMBFile;
  29. DWORD dwNumEncodeArea;
  30. DWORD dwNumXgbEncodes;
  31. DESCRIPTION Descript;
  32. HANDLE hRuler0, hEncode;
  33. LPRULER lpRuler;
  34. LPENCODEAREA lpEncode;
  35. MAININDEX MainIndex[NUMTABLES];
  36. PSECURITY_ATTRIBUTES psa;
  37. // DWORD dwOffset=lpMainIndex[TAG_CRTWORDCODE-1].dwOffset;
  38. lpCode[0] = 0;
  39. if(lstrlen(MBFileName)==0 || lstrlen(szWordStr) == 0)
  40. return;
  41. psa = CreateSecurityAttributes();
  42. hMBFile = CreateFile(MBFileName,GENERIC_READ,FILE_SHARE_READ,psa,OPEN_EXISTING,0,NULL);
  43. FreeSecurityAttributes(psa);
  44. if (hMBFile == (HANDLE)-1) {
  45. FatalMessage(hWnd, IDS_ERROR_OPENFILE);
  46. return;
  47. }
  48. if(!ConvGetMainIndex(hWnd,hMBFile,MainIndex)) {
  49. CloseHandle(hMBFile);
  50. return;
  51. }
  52. ConvReadDescript(hMBFile,&Descript, MainIndex);
  53. if(Descript.wNumRulers == 0) {
  54. CloseHandle(hMBFile);
  55. return;
  56. }
  57. hRuler0 = GlobalAlloc(GMEM_MOVEABLE|GMEM_ZEROINIT,sizeof(RULER)*Descript.wNumRulers);
  58. if(!hRuler0) {
  59. CloseHandle(hMBFile);
  60. return;
  61. }
  62. hEncode = GlobalAlloc(GMEM_MOVEABLE|GMEM_ZEROINIT, sizeof(ENCODEAREA)*NUMENCODEAREA);
  63. if(!hEncode) {
  64. CloseHandle(hMBFile);
  65. GlobalFree(hRuler0);
  66. return;
  67. }
  68. if(!(lpRuler = GlobalLock(hRuler0)) ) {
  69. CloseHandle(hMBFile);
  70. GlobalFree(hRuler0);
  71. return;
  72. }
  73. ConvReadRuler(hMBFile,Descript.wNumRulers ,lpRuler, MainIndex);
  74. lpEncode = (LPENCODEAREA) GlobalLock(hEncode);
  75. if(!lpEncode){
  76. CloseHandle(hMBFile);
  77. GlobalFree(hRuler0);
  78. return;
  79. }
  80. ConvGetEncode(hMBFile, lpEncode, &dwNumXgbEncodes,
  81. &dwNumEncodeArea,MainIndex);
  82. for(i=0; i<Descript.wNumRulers; i++)
  83. if( (lpRuler[i].byLogicOpra == 0 && nWordLen == lpRuler[i].byLength)
  84. ||(lpRuler[i].byLogicOpra == 1 && nWordLen >= lpRuler[i].byLength)
  85. ||(lpRuler[i].byLogicOpra == 2 && nWordLen <= lpRuler[i].byLength) ) {
  86. int retCodeLen = 0;
  87. for(j=0; j<lpRuler[i].wNumCodeUnits; j++) {
  88. k = lpRuler[i].CodeUnit[j].wDBCSPosition;
  89. if(k > (DWORD)nWordLen) k = (DWORD)nWordLen;
  90. if(lpRuler[i].CodeUnit[j].dwDirectMode == 0)
  91. lstrcpyn(szDBCS,&szWordStr[2*(k-1)/sizeof(TCHAR)],2/sizeof(TCHAR)+1);
  92. else
  93. lstrcpyn(szDBCS,&szWordStr[2*(nWordLen-k)/sizeof(TCHAR)],2/sizeof(TCHAR)+1);
  94. szDBCS[2/sizeof(TCHAR)] = 0;
  95. k = EncodeToXgbNo(szDBCS, dwNumEncodeArea,dwNumXgbEncodes,hEncode);
  96. if((long)k < 0 || k > dwNumXgbEncodes) {
  97. lpCode[j] = (j > 0)?lpCode[j-1]:Descript.szUsedCode[0];
  98. lpCode[j+1] = 0;
  99. }
  100. else
  101. {
  102. SetFilePointer(hMBFile,MainIndex[TAG_CRTWORDCODE-1].dwOffset+Descript.wMaxCodes*k*sizeof(TCHAR),
  103. 0,FILE_BEGIN);
  104. ReadFile(hMBFile,szCode,Descript.wMaxCodes*sizeof(TCHAR),&k,NULL);
  105. szCode[Descript.wMaxCodes] = 0;
  106. dwCodeLen = lstrlen(szCode);
  107. k = lpRuler[i].CodeUnit[j].wCodePosition;
  108. if(k > dwCodeLen) k = dwCodeLen;
  109. if(k == 0) {
  110. if(retCodeLen + dwCodeLen > Descript.wMaxCodes)
  111. szCode[Descript.wMaxCodes - retCodeLen] = 0;
  112. lstrcat(lpCode,szCode);
  113. }
  114. else {
  115. if(k > dwCodeLen) k = dwCodeLen;
  116. lpCode[j] = (szCode[k-1] == 0)?((k > 1)? szCode[k-2]:Descript.szUsedCode[0]):szCode[k-1];
  117. lpCode[j+1] = 0;
  118. }
  119. retCodeLen = lstrlen(lpCode);
  120. }
  121. }
  122. bReturn = TRUE;
  123. break;
  124. }
  125. CloseHandle(hMBFile);
  126. GlobalFree(hRuler0);
  127. return;
  128. }
  129. BOOL ConvGetMainIndex(HANDLE hWnd, HANDLE hMBFile, LPMAININDEX lpMainIndex)
  130. {
  131. DWORD dwBytes;
  132. int i;
  133. BOOL retVal;
  134. if(SetFilePointer(hMBFile,ID_LENGTH,0,FILE_BEGIN) != ID_LENGTH)
  135. return FALSE;
  136. retVal = ReadFile(hMBFile,lpMainIndex,sizeof(MAININDEX)*NUMTABLES,&dwBytes,NULL);
  137. if (retVal == FALSE )
  138. return retVal;
  139. if(dwBytes < sizeof(MAININDEX)*NUMTABLES ) {
  140. return FALSE;
  141. }
  142. else
  143. return TRUE;
  144. for(i=0; i<NUMTABLES; i++) {
  145. dwBytes = lpMainIndex[i].dwTag+
  146. lpMainIndex[i].dwOffset+
  147. lpMainIndex[i].dwLength;
  148. if(lpMainIndex[i].dwCheckSum != dwBytes) {
  149. return FALSE;
  150. }
  151. }
  152. }
  153. #ifdef IDEBUG
  154. extern void OutputDbgWord( );
  155. #endif
  156. BOOL ConvReadDescript(HANDLE hMBFile,
  157. LPDESCRIPTION lpDescript,
  158. LPMAININDEX lpMainIndex)
  159. //*** read description from .MB file ****
  160. {
  161. DWORD dwBytes;
  162. BOOL retVal;
  163. DWORD dwOffset = lpMainIndex[TAG_DESCRIPTION-1].dwOffset;
  164. if(SetFilePointer(hMBFile,dwOffset,0,FILE_BEGIN) != dwOffset)
  165. return FALSE;
  166. retVal = ReadFile(hMBFile,lpDescript,sizeof(DESCRIPTION),&dwBytes,NULL);
  167. if ( retVal == FALSE )
  168. return retVal;
  169. #ifdef IDEBUG
  170. {
  171. DWORD dwtmp;
  172. OutputDebugString(L"Under ConvReadDescript\n");
  173. OutputDebugString(L"dwBytes=");
  174. OutputDbgWord(dwBytes);
  175. OutputDebugString(L"Sizeof(MBDESC)=");
  176. dwtmp = (DWORD)sizeof(MBDESC);
  177. OutputDbgWord(dwtmp);
  178. OutputDebugString(L"\n");
  179. OutputDebugString(L"szName=");
  180. OutputDebugString(lpDescript->szName);
  181. OutputDebugString(L"\n");
  182. OutputDebugString(L"wMaxCodes=");
  183. dwtmp = (DWORD)(lpDescript->wMaxCodes);
  184. OutputDbgWord( dwtmp );
  185. OutputDebugString(L"\n");
  186. OutputDebugString(L"wNumCodes=");
  187. dwtmp = (DWORD)(lpDescript->wNumCodes);
  188. OutputDbgWord(dwtmp);
  189. OutputDebugString(L"\n");
  190. OutputDebugString(L"byMaxElement=");
  191. dwtmp = (DWORD)(lpDescript->byMaxElement) & 0x0000000f;
  192. OutputDbgWord(dwtmp);
  193. OutputDebugString(L"\n");
  194. OutputDebugString(L"cWildChar=");
  195. dwtmp = (DWORD)(lpDescript->cWildChar);
  196. OutputDbgWord( dwtmp );
  197. OutputDebugString(L"\n");
  198. OutputDebugString(L"wNumRulers=");
  199. dwtmp = (DWORD)(lpDescript->wNumRulers);
  200. OutputDbgWord( dwtmp );
  201. OutputDebugString(L"\n");
  202. }
  203. #endif
  204. if(dwBytes < sizeof(DESCRIPTION) )
  205. return FALSE;
  206. else
  207. return TRUE;
  208. }
  209. BOOL ConvReadRuler(HANDLE hMBFile,
  210. int nRulerNum,
  211. LPRULER lpRuler,
  212. LPMAININDEX lpMainIndex)
  213. //*** read create word ruler from .MB file ****
  214. {
  215. DWORD dwBytes;
  216. BOOL retVal;
  217. DWORD dwOffset = lpMainIndex[TAG_RULER-1].dwOffset;
  218. if(SetFilePointer(hMBFile,dwOffset,0,FILE_BEGIN) != dwOffset)
  219. return FALSE;
  220. retVal = ReadFile(hMBFile,lpRuler,nRulerNum*sizeof(RULER),&dwBytes,NULL);
  221. if ( retVal == FALSE )
  222. return retVal;
  223. if(dwBytes < nRulerNum*sizeof(RULER) )
  224. return FALSE;
  225. else
  226. return TRUE;
  227. }
  228. BOOL ConvGetEncode(HANDLE hMBFile,
  229. LPENCODEAREA lpEncode,
  230. LPDWORD fdwNumSWords,
  231. LPDWORD fdwNumEncodeArea,
  232. LPMAININDEX lpMainIndex)
  233. {
  234. DWORD dwBytes;
  235. BOOL retVal;
  236. DWORD dwOffset=lpMainIndex[TAG_INTERCODE-1].dwOffset;
  237. if(SetFilePointer(hMBFile,dwOffset,0,FILE_BEGIN) != dwOffset)
  238. return FALSE;
  239. retVal = ReadFile(hMBFile,fdwNumSWords,4,&dwBytes,NULL);
  240. if ( retVal == FALSE )
  241. return retVal;
  242. retVal = ReadFile(hMBFile,fdwNumEncodeArea,4,&dwBytes,NULL);
  243. if ( retVal == FALSE )
  244. return retVal;
  245. retVal = ReadFile(hMBFile,lpEncode,
  246. lpMainIndex[TAG_INTERCODE-1].dwLength,&dwBytes,NULL);
  247. if ( retVal == FALSE )
  248. return retVal;
  249. if(dwBytes < lpMainIndex[TAG_INTERCODE-1].dwLength)
  250. return FALSE;
  251. else
  252. return TRUE;
  253. }
  254. DWORD EncodeToXgbNo(LPTSTR szDBCS,
  255. DWORD dwNumEncodeArea,
  256. DWORD dwNumXgbEncodes,
  257. HANDLE hEncode)
  258. {
  259. LPENCODEAREA lpEncode;
  260. WORD wCode;
  261. DWORD dwNo = 0xffffffff, i;
  262. #ifdef UNICODE
  263. wCode = szDBCS[0];
  264. #else
  265. wCode = (WORD)((UCHAR)szDBCS[0])*256 + (WORD)(UCHAR)szDBCS[1];
  266. #endif
  267. lpEncode = (LPENCODEAREA) GlobalLock(hEncode);
  268. if(!lpEncode){
  269. return dwNo;
  270. }
  271. for( i = dwNumEncodeArea -1; (long)i>=0; i--) {
  272. if(wCode >= lpEncode[i].StartEncode) {
  273. dwNo = lpEncode[i].PreCount;
  274. dwNo += wCode - lpEncode[i].StartEncode;
  275. break;
  276. }
  277. }
  278. if(dwNo > dwNumXgbEncodes )
  279. dwNo = 0xffffffff;
  280. GlobalUnlock(hEncode);
  281. return dwNo;
  282. }