home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / ADDON.PAK / MSGTEST.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  6.3 KB  |  248 lines

  1. /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  2.  
  3.    msgtest.cpp
  4.    Created: 1/3/96
  5.    Copyright (c) 1987, 1996 Borland International Inc.  All Rights Reserved.
  6.    $Revision:   1.13  $
  7.     
  8. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/ 
  9. #ifndef __AOEXPCH_H
  10.   #include "aoexpch.h"
  11. #endif
  12. #pragma hdrstop
  13.  
  14. #include <stdio.h> 
  15. #include <ideaddon\iide.h>
  16. #include "msgtest.h"
  17.  
  18. GenericMessageFolder::GenericMessageFolder(LPCSTR name) {
  19.   IMessageSystem* pMsgSys;
  20.  
  21.   pMsgSys = GET_INTERFACE(IMessageSystem);
  22.  
  23.   d_folder = pMsgSys->NewFolder(MakePolyString(name));
  24.  
  25.   pMsgSys->Release();
  26.   d_msgItem = 0;
  27. };
  28.  
  29. void GenericMessageFolder::OpenFile(LPCSTR fileName, LPCSTR msg) {
  30.   d_msgItem = d_folder->NewFileMessage( fileName ? MakePolyString(fileName) : 0
  31.                                       , MakePolyString(msg));
  32. }
  33.  
  34. void GenericMessageFolder::Log(LPCSTR msg) {
  35.   d_folder->NewMessage(d_msgItem, MakePolyString(msg));
  36. }
  37.  
  38. #define BUFSIZE 4096 
  39.  
  40. VOID GenericMessageFolder::ReadFromPipe(HANDLE hFile) { 
  41.   /* Read output from child, and write it to parent's STDOUT. */ 
  42.  
  43.   DWORD dwRead, dwWritten; 
  44.   CHAR chBuf[BUFSIZE+1]; 
  45.   //
  46.   // append a null terminator. this is needed by strtok
  47.   //
  48.   chBuf[BUFSIZE] = 0;
  49.   for (;;) { 
  50.       if (! ReadFile(hFile, chBuf, BUFSIZE, &dwRead, NULL) || 
  51.           dwRead == 0) break; 
  52.  
  53.       chBuf[dwRead] = 0;
  54.       if (! WriteMessage(chBuf, dwRead, &dwWritten)) 
  55.           break; 
  56.   }
  57.  
  58. BOOL GenericMessageFolder::WriteMessage(LPSTR chBuf, DWORD /*dwRead*/, DWORD* /*dwWritten*/) {
  59.   LPSTR pH = chBuf;
  60.   LPSTR p = strtok(pH, "\n\r");
  61.   while (p != NULL) {
  62.     Log(p);
  63.     p = strtok(NULL, "\n\r");
  64.   };
  65.   return 1;
  66. }
  67.  
  68. DWORD ThreadFunc(LPDWORD lpdwParam) {
  69.   GenericMessageFolder* folder = (GenericMessageFolder*)lpdwParam;
  70.   folder->Run();
  71.   return 0;
  72. }
  73.  
  74. unsigned GenericMessageFolder::DoThread(LPCSTR cmd) {
  75.   d_currentCmd = cmd;
  76.  
  77.   DWORD dwThreadId;
  78.   HANDLE hThread;
  79.   hThread = CreateThread(
  80.       NULL,                       /* no security attributes       */
  81.       0,                           /* use default stack size        */
  82.       (LPTHREAD_START_ROUTINE) ThreadFunc, /* thread function      */
  83.       (LPVOID)this,               /* argument to thread function   */
  84.       0,                           /* use default creation flags    */
  85.       &dwThreadId);               /* returns the thread identifier */
  86.  
  87.   /* Check the return value for success. */
  88.  
  89.   if (hThread == NULL)
  90.     return 0;
  91.  
  92.   return 1;
  93. };
  94.  
  95. void GenericMessageFolder::Run() {
  96.   CreatePipedProcess((LPSTR)d_currentCmd);
  97. };
  98.  
  99. DWORD GenericMessageFolder::CreatePipedProcess(LPSTR commandLine) { 
  100.  
  101.   HRESULT                hr     = NOERROR;
  102.   SECURITY_ATTRIBUTES    sAttr   =
  103.   {
  104.     sizeof(SECURITY_ATTRIBUTES),
  105.     0,
  106.     TRUE
  107.   };
  108.  
  109.   STARTUPINFO    siStartInfo;
  110.   BOOL            bSuccess;
  111.  
  112.   memset(&siStartInfo, 0, sizeof(STARTUPINFO));
  113.   siStartInfo.cb = sizeof(STARTUPINFO);
  114.  
  115.   // Create a pipe for the child's stdout.
  116.   HANDLE          hPipeRead;      // The child's stdin.
  117.   HANDLE          hPipeWrite;    // The child's stdout.
  118.  
  119.   if (!CreatePipe(  &hPipeRead,
  120.                     &hPipeWrite,
  121.                     &sAttr,
  122.                     0)) {
  123.     return  ResultFromScode(E_FAIL);
  124.   }
  125.  
  126.   // Don't allow the child program to inherit the read end of the pipe.
  127.   DuplicateHandle(GetCurrentProcess(),
  128.                   hPipeRead,
  129.                   GetCurrentProcess(),
  130.                   0,
  131.                   0,
  132.                   FALSE,
  133.                   DUPLICATE_SAME_ACCESS);
  134.  
  135.   siStartInfo.dwFlags    = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
  136.   siStartInfo.hStdOutput  = hPipeWrite;
  137.   siStartInfo.wShowWindow = SW_HIDE;
  138.  
  139.   // Start the child process.
  140.   PROCESS_INFORMATION piProcInfo; 
  141.   bSuccess = CreateProcess(NULL,
  142.                            commandLine,
  143.                            &sAttr,
  144.                            &sAttr,
  145.                            TRUE,
  146.                            CREATE_NEW_PROCESS_GROUP,
  147.                            0,
  148.                            0,
  149.                            &siStartInfo,
  150.                            &piProcInfo);
  151.  
  152.   CloseHandle(hPipeWrite);
  153.   
  154.   if (bSuccess)
  155.   {
  156.     // Close the handles to the child process and thread.
  157.     CloseHandle(piProcInfo.hThread);
  158.     CloseHandle(piProcInfo.hProcess);
  159.     ReadFromPipe(hPipeRead); 
  160.   }
  161.  
  162.   CloseHandle(hPipeRead);
  163.   
  164.   return  hr;
  165. }
  166.  
  167. /* ****************************************************************************
  168.  *
  169.  *
  170.  */
  171. MsgSystemTest::MsgSystemTest() {
  172.    d_msgFolder1 = NULL;
  173.    d_msgFolder2 = NULL;
  174. }
  175.  
  176. MsgSystemTest::~MsgSystemTest() {
  177.   UnInit();
  178. }
  179. //.............................................................................
  180. BOOL MsgSystemTest::Init() 
  181. {
  182.   d_msgFolder1 = new GenericMessageFolder("Addon");
  183.   d_msgFolder2 = new GenericMessageFolder("Addon");
  184.  
  185.   OutStr( "MsgSystemTest::Init()" );
  186.   return FALSE;
  187. }
  188.  
  189. void MsgSystemTest::UnInit() 
  190. {
  191.   OutStr( "MsgSystemTest::UnInit()" );
  192.   if ( d_msgFolder1 ) {
  193.      delete d_msgFolder1;
  194.   }
  195.   if ( d_msgFolder2 ) {
  196.      delete d_msgFolder2;
  197.   }    
  198. }
  199.  
  200. const char * MsgSystemTest::GetName() 
  201. {
  202.   return "Message System Test";
  203. }
  204.  
  205. const char * MsgSystemTest::GetTestDescription( int testNum) 
  206. {
  207.   switch ( testNum )
  208.   {
  209.     case 1:
  210.       return "Create a message folder associated with a file";
  211.  
  212.     case 2:
  213.       return "Display a meesage in folder created by test1";
  214.  
  215.     case 3:
  216.       return "Create a message folder";
  217.  
  218.     case 4:
  219.       return "Display a meesage in folder created by test3";
  220.   }
  221.   return "This test not implemented.";
  222. }
  223.  
  224. void MsgSystemTest::DoTest( int testNum ) 
  225. {
  226.   switch ( testNum ) 
  227.   {
  228.     case 1:
  229.       d_msgFolder1->OpenFile("msgtest.cpp", "Click this line to open file msgtest.cpp");
  230.       break;
  231.     case 2:
  232.       // my test here 
  233.       d_msgFolder1->DoThread("where find.*");
  234.       d_msgFolder1->Log("Test output");
  235.       break;
  236.     case 3:
  237.       d_msgFolder2->OpenFile(NULL, "Click this line to expand/collapse node");
  238.       break;
  239.     case 4:
  240.       d_msgFolder2->Log("Test output");
  241.       break;
  242.     default: 
  243.       OutStr( FormatStr( "Test #%d Not Implemented!", testNum ) );
  244.       break;
  245.   }
  246. }
  247.