Windows NT 4.0 source code leak
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.

333 lines
6.2 KiB

4 years ago
  1. #include "stdafx.h"
  2. #pragma hdrstop
  3. // This function must be supplied by the application
  4. PCSTR STDCALL GetStringResource(UINT idString);
  5. #ifndef _OUTPUT_INCLUDED
  6. #include "coutput.h"
  7. #endif
  8. #include <stdlib.h>
  9. #ifndef SEEK_SET
  10. #define SEEK_SET 0 // seek to an absolute position
  11. #define SEEK_CUR 1 // seek relative to current position
  12. #define SEEK_END 2 // seek relative to end of file
  13. #endif //ifndef SEEK_SET
  14. #ifdef _DEBUG
  15. #undef THIS_FILE
  16. static char THIS_FILE[] = __FILE__;
  17. #endif
  18. /***************************************************************************
  19. FUNCTION: COutput::COutput
  20. PURPOSE: Open a file for writing
  21. PARAMETERS:
  22. pszFileName -- name fo the file
  23. cbBuf -- size of the output buffer to allocate
  24. fEnTab -- TRUE to entabify the output
  25. fAppend -- TRUE to append to the end of an existing file
  26. RETURNS:
  27. COuput::fInitialized is set to TRUE if the file was opened
  28. COMMENTS:
  29. If NULL is specified for pszFileName, no file will be written,
  30. but all functions may be used (debugging mode).
  31. Check fInitialized before deleting the class. If it is FALSE,
  32. then the write failed.
  33. MODIFICATION DATES:
  34. 26-Aug-1993 [ralphw]
  35. ***************************************************************************/
  36. COutput::COutput(const char* pszFileName, UINT cbBuf, BOOL fEnTab,
  37. BOOL fAppend)
  38. {
  39. // If no filename is specified, then no output will be sent
  40. if (pszFileName == NULL || !*pszFileName) {
  41. fNull = TRUE;
  42. fInitialized = FALSE;
  43. return;
  44. }
  45. else
  46. fNull = FALSE;
  47. fNewLine = TRUE;
  48. fEntabOutput = fEnTab;
  49. fOpenForAppend = fAppend;
  50. if (fEntabOutput)
  51. pszTmpBuf = (PSTR) lcMalloc(512);
  52. else
  53. pszTmpBuf = NULL;
  54. pszOutBuf = (PSTR) LocalAlloc(LMEM_FIXED, cbBuf);
  55. pszCurOutBuf = pszOutBuf;
  56. pszEndOutBuf = pszOutBuf + cbBuf;
  57. /*
  58. * If the file can't be opened, call CloseOuput to free the buffer
  59. * allocated for output, and to reset the handle to zero.
  60. */
  61. OFSTRUCT of;
  62. of.cBytes = sizeof(of);
  63. if ((hfOutput = OpenFile(pszFileName, &of,
  64. OF_WRITE | OF_CREATE)) == HFILE_ERROR) {
  65. fInitialized = FALSE;
  66. return;
  67. }
  68. if (fOpenForAppend) {
  69. fOpenForAppend = FALSE;
  70. // seek to end of file
  71. _llseek(hfOutput, 0, SEEK_END);
  72. }
  73. fInitialized = TRUE;
  74. }
  75. COutput::~COutput(void)
  76. {
  77. if (hfOutput != HFILE_ERROR) {
  78. outflush();
  79. _lclose(hfOutput);
  80. }
  81. if (pszTmpBuf)
  82. lcFree(pszTmpBuf);
  83. if (pszOutBuf)
  84. LocalFree((HLOCAL) pszOutBuf);
  85. }
  86. /***************************************************************************
  87. FUNCTION: outstring
  88. PURPOSE:
  89. RETURNS:
  90. COMMENTS:
  91. MODIFICATION DATES:
  92. 21-Jul-1990 [ralphw]
  93. ***************************************************************************/
  94. void STDCALL COutput::outstring(PCSTR pszString)
  95. {
  96. if (!pszString)
  97. return;
  98. if (fEntabOutput) {
  99. strcpy(pszTmpBuf, pszString);
  100. EntabString(pszTmpBuf);
  101. pszString = pszTmpBuf;
  102. }
  103. UINT cbString;
  104. FastSection:
  105. if ((UINT) (cbString = (UINT) strlen(pszString)) <
  106. (UINT) (pszEndOutBuf - pszCurOutBuf) &&
  107. strchr(pszString, '\n') == NULL) {
  108. strcpy(pszCurOutBuf, pszString);
  109. pszCurOutBuf += cbString;
  110. }
  111. else {
  112. while (*pszString) {
  113. if (*pszString != '\n') {
  114. if (*pszString == '\r') {
  115. pszString++;
  116. continue;
  117. }
  118. *pszCurOutBuf++ = *pszString++;
  119. if (pszCurOutBuf >= pszEndOutBuf) {
  120. outflush();
  121. goto FastSection;
  122. }
  123. }
  124. else {
  125. if (*pszString == '\n') {
  126. *pszCurOutBuf++ = '\r';
  127. if (pszCurOutBuf >= pszEndOutBuf)
  128. outflush();
  129. if (!fNewLine)
  130. return;
  131. }
  132. *pszCurOutBuf++ = *pszString++;
  133. if (pszCurOutBuf >= pszEndOutBuf)
  134. outflush();
  135. }
  136. }
  137. }
  138. }
  139. /***************************************************************************
  140. FUNCTION: outchar
  141. PURPOSE:
  142. RETURNS:
  143. COMMENTS:
  144. MODIFICATION DATES:
  145. 21-Jul-1990 [ralphw]
  146. ***************************************************************************/
  147. void STDCALL COutput::outchar(char c)
  148. {
  149. if (c == '\n') {
  150. *pszCurOutBuf++ = '\r';
  151. if (pszCurOutBuf >= pszEndOutBuf)
  152. outflush();
  153. if (!fNewLine)
  154. return;
  155. }
  156. *pszCurOutBuf++ = c;
  157. if (pszCurOutBuf >= pszEndOutBuf)
  158. outflush();
  159. }
  160. /***************************************************************************
  161. FUNCTION: outflush
  162. PURPOSE:
  163. RETURNS:
  164. COMMENTS:
  165. MODIFICATION DATES:
  166. 18-Mar-1990 [ralphw]
  167. ***************************************************************************/
  168. void STDCALL NEAR COutput::outflush(void)
  169. {
  170. UINT cbWrite = (UINT) (pszCurOutBuf - pszOutBuf);
  171. if (fNull || !cbWrite || hfOutput == HFILE_ERROR)
  172. return;
  173. if (_lwrite(hfOutput, pszOutBuf, cbWrite) != cbWrite) {
  174. fInitialized = FALSE;
  175. fNull = TRUE;
  176. _lclose(hfOutput);
  177. hfOutput = HFILE_ERROR;
  178. }
  179. pszCurOutBuf = pszOutBuf;
  180. }
  181. /***************************************************************************
  182. FUNCTION: outstring_eol
  183. PURPOSE:
  184. RETURNS:
  185. COMMENTS:
  186. MODIFICATION DATES:
  187. 21-Jul-1990 [ralphw]
  188. ***************************************************************************/
  189. void STDCALL COutput::outstring_eol(PCSTR pszString)
  190. {
  191. outstring(pszString);
  192. outchar('\n');
  193. }
  194. #ifndef CHAR_SPACE
  195. const char CHAR_SPACE = ' ';
  196. const char CHAR_TAB = '\t';
  197. #endif
  198. void STDCALL NEAR COutput::EntabString(PSTR pszLine)
  199. {
  200. int i = 0;
  201. PSTR psz = pszLine, pszStart;
  202. while (*psz) {
  203. if (psz[0] == CHAR_SPACE && psz[1] == CHAR_SPACE && i < 7) {
  204. pszStart = psz;
  205. do {
  206. i++;
  207. psz++;
  208. } while (i % 8 && *psz == CHAR_SPACE);
  209. if ((i % 8) == 0) {
  210. *pszStart = '\t';
  211. strcpy(pszStart + 1, psz);
  212. psz = pszStart + 1;
  213. i = 0;
  214. continue;
  215. }
  216. }
  217. i++;
  218. psz++;
  219. if (i == 8)
  220. i = 0;
  221. }
  222. }
  223. void STDCALL COutput::outint(int val)
  224. {
  225. char szBuf[10];
  226. _itoa(val, szBuf, 10);
  227. outstring(szBuf);
  228. }
  229. void STDCALL COutput::outhex(int val)
  230. {
  231. char szBuf[10];
  232. strcpy(szBuf, "0x");
  233. _itoa(val, szBuf + strlen(szBuf), 16);
  234. outstring(szBuf);
  235. }
  236. void STDCALL COutput::outint(int idResource, int val)
  237. {
  238. char szBuf[256];
  239. wsprintf(szBuf, GetStringResource(idResource), val);
  240. outstring(szBuf);
  241. }
  242. void STDCALL COutput::outint(int idResource, int val1, int val2)
  243. {
  244. char szBuf[256];
  245. wsprintf(szBuf, GetStringResource(idResource), val1, val2);
  246. outstring(szBuf);
  247. }
  248. void STDCALL COutput::WriteTable(CTable* ptbl)
  249. {
  250. if (!ptbl)
  251. return;
  252. for (int i = 1; i <= ptbl->CountStrings(); i++)
  253. outstring_eol(ptbl->GetPointer(i));
  254. }