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.

288 lines
6.5 KiB

  1. #include "stdafx.h"
  2. #include "ConvEng.h"
  3. #include "Msg.h"
  4. #include "TextFile.h"
  5. #include "FileConv.h"
  6. static BYTE* g_pbyData = NULL;
  7. DWORD GetFileSize(
  8. PCTCH ptszFileName)
  9. {
  10. WIN32_FIND_DATA sFindFileData;
  11. HANDLE hFind;
  12. hFind = FindFirstFile(ptszFileName, &sFindFileData);
  13. if (hFind == INVALID_HANDLE_VALUE) {
  14. return 0;
  15. } else {
  16. FindClose(hFind);
  17. // Can't handle file great than 4G
  18. if (sFindFileData.nFileSizeHigh) {
  19. return 0;
  20. }
  21. return sFindFileData.nFileSizeLow;
  22. }
  23. }
  24. BOOL OpenFile(
  25. PCTCH ptszFileName)
  26. {
  27. FILE* pFile = NULL;
  28. BOOL fSuccess = FALSE;
  29. DWORD dwFileSize = 0;
  30. if (lstrlen(ptszFileName) >= MAX_PATH) {
  31. goto Exit;
  32. }
  33. dwFileSize = GetFileSize(ptszFileName);
  34. if (!dwFileSize) {
  35. goto Exit;
  36. }
  37. if (g_pbyData) {
  38. // There must be something wrong, however, we can try to handle this fail.
  39. ASSERT(FALSE);
  40. delete[] g_pbyData;
  41. g_pbyData = NULL;
  42. }
  43. g_pbyData = new BYTE[dwFileSize];
  44. if (!g_pbyData) {
  45. goto Exit;
  46. }
  47. pFile = _tfopen(ptszFileName, TEXT("rb"));
  48. if (!pFile) {
  49. goto Exit;
  50. }
  51. if (fread(g_pbyData, dwFileSize, 1, pFile) != 1) {
  52. goto Exit;
  53. }
  54. fSuccess = TRUE;
  55. Exit:
  56. if (pFile) {
  57. fclose(pFile);
  58. pFile = NULL;
  59. }
  60. if (!fSuccess && g_pbyData) {
  61. delete[] g_pbyData;
  62. g_pbyData = NULL;
  63. }
  64. if (fSuccess) {
  65. return dwFileSize;
  66. }
  67. return 0;
  68. }
  69. void CloseFile(void)
  70. {
  71. if (g_pbyData) {
  72. delete[] g_pbyData;
  73. g_pbyData = NULL;
  74. }
  75. }
  76. BOOL Convert(
  77. PCTCH tszSourceFileName,
  78. PCTCH tszTargetFileName,
  79. BOOL fAnsiToUnicode)
  80. {
  81. enum {
  82. FILETYPE_TEXT,
  83. FILETYPE_RTF,
  84. FILETYPE_HTML,
  85. FILETYPE_XML,
  86. } eFileType;
  87. BOOL fRet = FALSE;
  88. DWORD dwFileSize;
  89. int nTargetFileSize;
  90. BYTE* pbyTarget = NULL;
  91. FILE* pFile = NULL;
  92. DWORD dwTargetBufSize = 0;
  93. PCTCH tszExt = NULL;
  94. PTCH tszExtBuf = NULL;
  95. // File format
  96. // Find last '.'
  97. if (lstrlen(tszSourceFileName) > MAX_PATH) {
  98. MsgOpenSourceFileError(tszSourceFileName);
  99. goto Exit;
  100. }
  101. tszExt = tszSourceFileName + lstrlen(tszSourceFileName);
  102. for (; tszExt >= tszSourceFileName && *tszExt != TEXT('.'); tszExt--);
  103. if (tszExt < tszSourceFileName) {
  104. // not find '.', no ext
  105. tszExt = tszSourceFileName + lstrlen(tszSourceFileName);
  106. } else {
  107. // find '.', skip dot
  108. tszExt ++;
  109. }
  110. tszExtBuf = new TCHAR
  111. [tszSourceFileName + lstrlen(tszSourceFileName) - tszExt + 1];
  112. if (!tszExtBuf) {
  113. MsgOverflow();
  114. goto Exit;
  115. }
  116. lstrcpy(tszExtBuf, tszExt);
  117. _strlwr(tszExtBuf);
  118. if (_tcscmp(tszExtBuf, TEXT("txt")) == 0) {
  119. eFileType = FILETYPE_TEXT;
  120. #ifdef RTF_SUPPORT
  121. } else if (_tcscmp(tszExtBuf, TEXT("rtf")) == 0) {
  122. eFileType = FILETYPE_RTF;
  123. #endif
  124. } else if (_tcscmp(tszExtBuf, TEXT("htm")) == 0
  125. || _tcscmp(tszExtBuf, TEXT("html")) == 0) {
  126. eFileType = FILETYPE_HTML;
  127. #ifdef XLM_SUPPORT
  128. } else if (_tcscmp(tszExtBuf, TEXT("xml")) == 0) {
  129. eFileType = FILETYPE_XML;
  130. #endif
  131. } else {
  132. MsgUnrecognizedFileType(tszSourceFileName);
  133. goto Exit;
  134. }
  135. if (0 == (dwFileSize = OpenFile(tszSourceFileName))) {
  136. MsgOpenSourceFileError(tszSourceFileName);
  137. goto Exit;
  138. }
  139. if (dwFileSize == 0 || dwFileSize == (DWORD)(-1)) {
  140. MsgOpenSourceFileError(tszSourceFileName);
  141. goto Exit;
  142. }
  143. // In worst case the text file size will double in each direct convert.
  144. // 64, for Htlm head information convert and Unicode file flag
  145. dwTargetBufSize = dwFileSize*2 + 64;
  146. pbyTarget = new BYTE[dwTargetBufSize];
  147. if (!pbyTarget) {
  148. MsgOverflow();
  149. goto Exit;
  150. }
  151. pFile = _tfopen(tszTargetFileName, TEXT("r"));
  152. if (pFile) {
  153. MsgTargetFileExist(tszTargetFileName);
  154. fclose(pFile);
  155. pFile = NULL;
  156. goto Exit;
  157. }
  158. // Convert
  159. switch(eFileType) {
  160. case FILETYPE_TEXT:
  161. if (!ConvertTextFile(g_pbyData, dwFileSize, pbyTarget,
  162. dwTargetBufSize, fAnsiToUnicode, &nTargetFileSize)) {
  163. goto Exit;
  164. }
  165. break;
  166. #ifdef RTF_SUPPORT
  167. case FILETYPE_RTF:
  168. if (!ConvertRtfFile(g_pbyData, dwFileSize, pbyTarget,
  169. dwTargetBufSize, fAnsiToUnicode, &nTargetFileSize)) {
  170. goto Exit;
  171. }
  172. break;
  173. #endif
  174. case FILETYPE_HTML:
  175. if (!ConvertHtmlFile(g_pbyData, dwFileSize, pbyTarget,
  176. dwTargetBufSize, fAnsiToUnicode, &nTargetFileSize)) {
  177. goto Exit;
  178. }
  179. break;
  180. #ifdef XML_SUPPORT
  181. case FILETYPE_XML:
  182. if (!ConvertXmlFile(g_pbyData, dwFileSize, pbyTarget,
  183. dwTargetBufSize, fAnsiToUnicode, &nTargetFileSize)) {
  184. goto Exit;
  185. }
  186. break;
  187. #endif
  188. default:
  189. break;
  190. }
  191. pFile = _tfopen(tszTargetFileName, TEXT("wb"));
  192. if (!pFile) {
  193. goto Exit;
  194. }
  195. dwFileSize = (DWORD)fwrite(pbyTarget, nTargetFileSize, 1, pFile);
  196. if (dwFileSize < 1) {
  197. MsgWriteFileError();
  198. goto Exit;
  199. }
  200. fRet = TRUE;
  201. Exit:
  202. if (tszExtBuf) {
  203. delete[] tszExtBuf;
  204. tszExtBuf = NULL;
  205. }
  206. if (pbyTarget) {
  207. delete[] pbyTarget;
  208. pbyTarget = NULL;
  209. }
  210. if (pFile) {
  211. fclose(pFile);
  212. pFile = NULL;
  213. }
  214. CloseFile();
  215. return fRet;
  216. }
  217. BOOL GenerateTargetFileName(
  218. PCTCH tszSrc,
  219. CString* pstrTar,
  220. BOOL fAnsiToUnicode)
  221. {
  222. // Find last '.'
  223. PCTCH tszExt = tszSrc + lstrlen(tszSrc);
  224. for (; tszExt >= tszSrc && *tszExt != TEXT('.'); tszExt--);
  225. if (tszExt < tszSrc) {
  226. // not find '.', no ext
  227. tszExt = tszSrc + lstrlen(tszSrc);
  228. } else {
  229. // find '.'
  230. }
  231. try {
  232. *pstrTar = tszSrc;
  233. *pstrTar = pstrTar->Left((int)(tszExt - tszSrc));
  234. *pstrTar += fAnsiToUnicode ? TEXT(".Unicode") : TEXT(".GB");
  235. *pstrTar += tszExt;
  236. }
  237. catch (...) {
  238. MsgOverflow();
  239. return FALSE;
  240. }
  241. if (pstrTar->GetLength() >= MAX_PATH) {
  242. return FALSE;
  243. }
  244. return TRUE;
  245. }