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.

196 lines
3.6 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1993.
  5. //
  6. // File: output.cpp
  7. //
  8. // Contents: String output functions for displaying text on the main
  9. // edit window
  10. //
  11. // Classes:
  12. //
  13. // Functions: OutputString
  14. // SaveToFile
  15. //
  16. // History: dd-mmm-yy Author Comment
  17. // 22-Mar-94 alexgo author
  18. //
  19. //--------------------------------------------------------------------------
  20. #include "oletest.h"
  21. #include <commdlg.h>
  22. #ifndef WIN32
  23. #include <stdarg.h>
  24. #endif
  25. //
  26. // handle to memory where the text is stored
  27. //
  28. // Please note this is really burfy (having all these globals). But for
  29. // the purposes of a simple driver app, it is the easiest.
  30. //
  31. static HGLOBAL hText; // handle to the Text
  32. static ULONG cbText;
  33. static ULONG iText;
  34. //+-------------------------------------------------------------------------
  35. //
  36. // Function: OutputString
  37. //
  38. // Synopsis: Dumps the string in printf format to the screen
  39. //
  40. // Effects:
  41. //
  42. // Arguments: [szFormat] -- the format string
  43. // [...] -- variable arguments
  44. //
  45. // Requires:
  46. //
  47. // Returns: int, the number of characters written (returned by sprintf)
  48. //
  49. // Signals:
  50. //
  51. // Modifies:
  52. //
  53. // Algorithm:
  54. //
  55. // History: dd-mmm-yy Author Comment
  56. //
  57. // Notes:
  58. //
  59. //--------------------------------------------------------------------------
  60. int OutputString( char *szFormat, ... )
  61. {
  62. LPSTR psz;
  63. va_list ap;
  64. int cbWritten;
  65. va_start(ap, szFormat);
  66. if( !hText )
  67. {
  68. hText = GlobalAlloc( GMEM_MOVEABLE , 2048 );
  69. assert(hText);
  70. cbText = 2048;
  71. }
  72. // double the size of the array if we need to
  73. if( iText > cbText / 2 )
  74. {
  75. hText = GlobalReAlloc(hText, cbText * 2, GMEM_MOVEABLE );
  76. assert(hText);
  77. cbText *= 2;
  78. }
  79. psz = (LPSTR)GlobalLock(hText);
  80. assert(psz);
  81. cbWritten = wvsprintf( psz + iText, szFormat, ap);
  82. iText += cbWritten;
  83. va_end(ap);
  84. SetWindowText(vApp.m_hwndEdit, psz);
  85. GlobalUnlock(hText);
  86. return cbWritten;
  87. }
  88. //+-------------------------------------------------------------------------
  89. //
  90. // Function: SaveToFile
  91. //
  92. // Synopsis: Gets a filename from the user and save the text buffer into it
  93. //
  94. // Effects:
  95. //
  96. // Arguments: void
  97. //
  98. // Requires:
  99. //
  100. // Returns: void
  101. //
  102. // Signals:
  103. //
  104. // Modifies:
  105. //
  106. // Algorithm:
  107. //
  108. // History: dd-mmm-yy Author Comment
  109. // 24-Mar-94 alexgo author
  110. //
  111. // Notes:
  112. //
  113. //--------------------------------------------------------------------------
  114. void SaveToFile( void )
  115. {
  116. char szFileName[MAX_PATH];
  117. OPENFILENAME ofn;
  118. static char * szFilter[] = { "Log Files (*.log)", "*.log",
  119. "All Files (*.*)", "*.*", ""};
  120. FILE * fp;
  121. LPSTR psz;
  122. memset(&ofn, 0, sizeof(OPENFILENAME));
  123. ofn.lStructSize = sizeof(OPENFILENAME);
  124. ofn.hwndOwner = vApp.m_hwndMain;
  125. ofn.lpstrFilter = szFilter[0];
  126. ofn.nFilterIndex = 0;
  127. szFileName[0] = '\0';
  128. ofn.lpstrFile = szFileName;
  129. ofn.nMaxFile = MAX_PATH;
  130. ofn.Flags = OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT;
  131. //
  132. // Get the file
  133. //
  134. if (GetSaveFileName(&ofn) == FALSE)
  135. {
  136. // user hit cancel
  137. return;
  138. }
  139. // the 'b' specifies binary mode, so \n --> \r\n translations are
  140. // not done.
  141. if( !(fp = fopen( szFileName, "wb")) )
  142. {
  143. MessageBox( NULL, "Can't open file!", "OleTest Driver",
  144. MB_ICONEXCLAMATION );
  145. return;
  146. }
  147. psz = (LPSTR)GlobalLock(hText);
  148. assert(psz);
  149. fwrite(psz, iText, sizeof(char), fp);
  150. fclose(fp);
  151. GlobalUnlock(hText);
  152. return;
  153. }