ccutil/debugwin.cpp

Go to the documentation of this file.
00001 
00020 #include          "mfcpch.h"     //precompiled headers
00021 #include          <stdarg.h>
00022 #include          "debugwin.h"
00023 
00026 DLLSYM INT_VAR (debug_lines, 256, "Number of lines in debug window");
00029 #ifndef GRAPHICS_DISABLED
00030 
00031 #ifdef __MAC__
00032 #include          <ltextedit.h>
00033 #include          <lwindow.h>
00034 //#include                                              <console.h>
00035 
00036 #define scrl_SCROLLER   101
00037 #define text_FLOWED     100
00038 
00039 static LCommander *pCommander = NULL;
00040 #endif
00041 
00042                                  //NT implementation
00043 #if defined(__MSW32__) && !defined(_CONSOLE)
00044 
00045 #include          <io.h>
00046 #define ID_DEBUG_MSG       32779
00047 
00059 DEBUG_WIN::DEBUG_WIN(                    //constructor
00060                      const char *title,  //of window
00061                      INT32 xpos,         //initial position
00062                      INT32 ypos,         //in pixels
00063                      INT32 xsize,        //initial size
00064                      INT32 ysize,        //in pixels
00065                      INT32 buflines      //default scroll size
00066                     ) {
00067   char cmd[1024];
00068   int parm;                      //output from scrolrwin
00069   STARTUPINFO start_info;        //process control
00070   PROCESS_INFORMATION proc_info; //process ids
00071   SECURITY_ATTRIBUTES security;  //for handles
00072 
00073   handle = NULL;
00074   shm_hand = NULL;
00075   shm_mem = NULL;
00076   msg_end = NULL;
00077   dbg_process = NULL;            //save handles
00078   dbg_thread = NULL;
00079   security.nLength = sizeof (security);
00080   security.lpSecurityDescriptor = NULL;
00081   security.bInheritHandle = TRUE;//make it inheritable
00082                                  //anonymous
00083   shm_hand = CreateFileMapping ((HANDLE) 0xffffffff,
00084    &security, PAGE_READWRITE, 0, 4096, NULL);
00085   if (shm_hand == NULL)
00086     return;                      //failed
00087   shm_mem = (char *) MapViewOfFile (shm_hand, FILE_MAP_WRITE, 0, 0, 0);
00088   if (shm_mem == NULL)
00089     return;
00090   shm_mem[5] = 0;
00091   sprintf (cmd, "scrolwin.exe %d %d", buflines, shm_hand);
00092   GetStartupInfo(&start_info);  //clone our stuff
00093   if (!CreateProcess (NULL, cmd, NULL, NULL, TRUE,
00094     CREATE_NO_WINDOW | DETACHED_PROCESS | CREATE_SUSPENDED,
00095     NULL, NULL, &start_info, &proc_info))
00096     return;
00097 
00098                                  //save handles
00099   dbg_process = proc_info.hProcess;
00100   dbg_thread = proc_info.hThread;
00101   if (ResumeThread (dbg_thread) != 1)
00102     return;
00103   do
00104   Sleep (100);
00105   while (shm_mem[5] == 0);       //wait for handle
00106   parm = ((((UINT8) shm_mem[4] << 8) + (UINT8) shm_mem[3] << 8)
00107     + (UINT8) shm_mem[2] << 8) + (UINT8) shm_mem[1];
00108   handle = (HWND) parm;
00109   if (handle != NULL) {
00110                                  //setup window
00111     ::SetWindowText (handle, title);
00112     ::MoveWindow (handle, xpos, ypos, xsize, ysize, TRUE);
00113     ::ShowWindow (handle, SW_SHOW);
00114   }
00115 }
00116 
00117 
00118 /* ================== */
00122 DEBUG_WIN::~DEBUG_WIN (
00123 ) {
00124   if (IsWindow (handle))
00125     ::SendMessage (handle, WM_COMMAND, IDOK, 0);
00126   if (shm_mem != NULL)
00127     UnmapViewOfFile(shm_mem); 
00128   if (shm_hand != NULL)
00129     CloseHandle(shm_hand); 
00130   if (dbg_thread != NULL)
00131     CloseHandle(dbg_thread); 
00132   if (dbg_process == NULL)
00133     CloseHandle(dbg_process); 
00134 
00135 }
00136 
00137 
00138 /* ================== */
00145 void
00146 DEBUG_WIN::dprintf (             //debug printf
00147 const char *format, ...          //special message
00148 ) {
00149   va_list args;                  //variable args
00150   char *msg_start;               //for printing
00151 
00152   if (!IsWindow (handle))
00153     return;                      //destroyed
00154   if (msg_end == NULL)
00155     msg_end = shm_mem + 1;
00156   va_start(args, format);  //variable list
00157                                  //Format into msg
00158   vsprintf(msg_end, format, args); 
00159   va_end(args); 
00160   if (*msg_end == '\0')
00161     return;
00162   msg_start = shm_mem + 1;
00163   do {
00164                                  //end of line
00165     msg_end = strchr (msg_start, '\n');
00166     if (msg_end == NULL) {
00167       if (msg_start != shm_mem + 1)
00168                                  //bring to front
00169         strcpy (shm_mem + 1, msg_start);
00170                                  //current end
00171       msg_end = shm_mem + 1 + strlen (shm_mem + 1);
00172       return;
00173     }
00174     *msg_end = '\0';
00175     while (IsWindow (handle) && shm_mem[0])
00176       Sleep (500);
00177     if (IsWindow (handle)) {
00178                                  //Visual C++2.0 macro
00179       ::SendMessage (handle, WM_COMMAND, ID_DEBUG_MSG, (DWORD) (msg_start - shm_mem));
00180     }
00181     msg_start = msg_end + 1;
00182   }
00183   while (*msg_start != '\0');
00184   msg_end = shm_mem + 1;         //buffer empty
00185 }
00186 
00187 
00188 /* ================== */
00194 void DEBUG_WIN::await_destruction() {  //wait for user to close
00195   WaitForSingleObject (dbg_process, (unsigned long) -1);
00196 }
00197 #endif                           //NT Implmentation
00198 
00199                                  //UNIX implementation
00200 #if defined(__UNIX__) || defined(_CONSOLE)
00201 #ifdef __UNIX__
00202 #include          <unistd.h>
00203 #include          <signal.h>
00204 #endif
00205 //#include                                      "basefile.h"
00206 
00207 /* ================== */
00213 DEBUG_WIN::DEBUG_WIN(                    //constructor
00214                      const char *title,  //of window
00215                      INT32 xpos,         //initial position
00216                      INT32 ypos,         //in pixels
00217                      INT32 xsize,        //initial size
00218                      INT32 ysize,        //in pixels
00219                      INT32 buflines      //default scroll size
00220                     ) {
00221   #ifdef __UNIX__
00222   INT32 length;                  /*length of name */
00223   char command[MAX_PATH];        /*pipe command */
00224   pid_t pid;                     /*process id */
00225   char host[MAX_PATH];           //remote host
00226   BOOL8 remote;                  //remote host
00227 
00228   //      remote=remote_display(host);                                                                    //check remote host
00229   remote = FALSE;
00230   if (remote)
00231                                  //do it remotely
00232     length = sprintf (command, "remsh %s 'DISPLAY=%s;export DISPLAY;",
00233       host, getenv ("DISPLAY"));
00234   else
00235     length = 0;
00236   length += sprintf (command + length, "trap \"\" 1 2 3 13 15\n");
00237   length +=
00238     sprintf (command + length,
00239     "/usr/bin/X11/xterm -sb -sl " INT32FORMAT " -geometry "
00240     INT32FORMAT "x" INT32FORMAT "", buflines, xsize / 8, ysize / 16);
00241   if (xpos >= 0)
00242     command[length++] = '+';
00243   length += sprintf (command + length, INT32FORMAT, xpos);
00244   if (ypos >= 0)
00245     command[length++] = '+';
00246   length +=
00247     sprintf (command + length,
00248     INT32FORMAT " -title \"%s\" -n \"%s\" -e /bin/sh -c ", ypos,
00249     title, title);
00250   pid = getpid ();               /*random number */
00251   length +=
00252     sprintf (command + length,
00253     "\"stty opost; tty >/tmp/debug%d; while [ -s /tmp/debug%d ]\ndo\nsleep 1\ndone\" &\n",
00254     pid, pid);
00255   length +=
00256     sprintf (command + length, "trap \"rm -f /tmp/debug%d; kill -9 $!\" 0\n",
00257     pid);
00258   length += sprintf (command + length, "trap \"exit\" 1 2 3 13 15\n");
00259   length +=
00260     sprintf (command + length,
00261     "while [ ! -s /tmp/debug%d ]\ndo\nsleep 1\ndone\n", pid);
00262   length += sprintf (command + length, "trap \"\" 1 2 3 13 15\n");
00263   length += sprintf (command + length, "ofile=`cat /tmp/debug%d`\n", pid);
00264   length +=
00265     sprintf (command + length, "cat -u - >$ofile; rm /tmp/debug%d\n", pid);
00266   if (remote) {
00267     command[length++] = '\'';    //terminate remsh
00268     command[length] = '\0';
00269   }
00270   fp = popen (command, "w");     /*create window */
00271   if (fp != NULL) {
00272                                  /*set no buffering */
00273     if (setvbuf (fp, NULL, _IONBF, BUFSIZ)) {
00274       pclose(fp); 
00275       fp = NULL;
00276     }
00277   }
00278   #endif
00279 }
00280 
00281 
00282 /* ================== */
00286 DEBUG_WIN::~DEBUG_WIN (
00287 ) {
00288   #ifdef __UNIX__
00289   pclose(fp); 
00290   #endif
00291 }
00292 
00293 
00294 /* ================== */
00301 void
00302 DEBUG_WIN::dprintf (             //debug printf
00303 const char *format, ...          //special message
00304 ) {
00305   va_list args;                  //variable args
00306 
00307   va_start(args, format);  //variable list
00308   #ifdef __UNIX__
00309   vfprintf(fp, format, args);  //Format into msg
00310   #else
00311                                  //Format into msg
00312   vfprintf(stderr, format, args); 
00313   #endif
00314   va_end(args); 
00315 }
00316 
00317 
00318 /* ================== */
00322 void DEBUG_WIN::await_destruction() {  //wait for user to close
00323   #ifdef __UNIX__
00324   signal(SIGPIPE, SIG_IGN); 
00325   while (!ferror (fp)) {
00326     sleep (1);
00327     fputc (0, fp);               //send nulls until error
00328   }
00329   #endif
00330 }
00331 #endif                           //UNIX Implmentation
00332 
00333 #ifdef __MAC__                   //NT implementation
00334 #include          <stdio.h>
00335 //#include       "textwindow.h"
00336 #include          <lwindow.h>
00337 #include          "ipcbase.h"    //must be last include
00338 
00339 /* ================== */
00347 void DEBUG_WIN::SetCommander(LCommander *pNew) { 
00348   pCommander = pNew;
00349 }
00350 
00351 
00352 /* ================== */
00358 DEBUG_WIN::DEBUG_WIN(                    //constructor
00359                      const char *title,  //of window
00360                      INT32 xpos,         //initial position
00361                      INT32 ypos,         //in pixels
00362                      INT32 xsize,        //initial size
00363                      INT32 ysize,        //in pixels
00364                      INT32 buflines      //default scroll size
00365                     ) {
00366   INT32 length;                  /*length of name */
00367 
00368   // don't replace this DebugStr() with a call to DEBUG_WIN!
00369 
00370   //if (pCommander==NULL) DebugStr("\pDEBUG_WIN::DEBUG_WIN(), Commander not set");
00371 
00372   // create the window
00373 
00374   //pWindow=LWindow::CreateWindow(2700,pCommander);
00375 }
00376 
00377 
00378 /* ================== */
00382 DEBUG_WIN::~DEBUG_WIN (
00383 ) {
00384 }
00385 
00386 
00387 /* ================== */
00394 void
00395 DEBUG_WIN::dprintf (             //debug printf
00396 const char *format, ...          //special message
00397 ) {
00398   #if 0
00399   LTextEdit *pTextEdit;
00400   va_list args;                  //variable args
00401   static char msg[1024];
00402 
00403   INT32 i;
00404   INT32 OriginalLength;
00405   INT32 NewLength;
00406   TEHandle hTextEdit;
00407   char *pTempBuffer;
00408   CharsHandle hChar;
00409   char *pOriginalText;
00410   INT32 StringLength;
00411 
00412   pTextEdit = (LTextEdit *) pWindow->FindPaneByID (text_FLOWED);
00413   if (pTextEdit == NULL)
00414     DebugStr ("\pwhoops");
00415 
00416   // get a C String from the format and args passed in
00417 
00418   va_start(args, format);  //variable list
00419   vsprintf(msg, format, args);  //Format into msg
00420   va_end(args); 
00421 
00422   StringLength = strlen (msg);
00423 
00424   // get the handle for the text
00425 
00426   hTextEdit = pTextEdit->GetMacTEH ();
00427   if (hTextEdit == NULL)
00428     DebugStr ("\pDEBUG_WIN,WriteCharsToConsole()");
00429 
00430   // get a pointer to the characters and the length of the character stream
00431 
00432   hChar = TEGetText (hTextEdit);
00433   if (hChar == NULL)
00434     DebugStr ("\pDEBUG_WIN,WriteCharsToConsole()");
00435 
00436   pOriginalText = *hChar;        // get pointer to existing text
00437 
00438                                  // get the length of the original data
00439   OriginalLength = (*hTextEdit)->teLength;
00440 
00441   // setup a temporary buffer for the new text
00442 
00443   NewLength = OriginalLength + StringLength;
00444 
00445   pTempBuffer = NewPtr (NewLength);
00446   if (pTempBuffer == NULL)
00447     DebugStr ("\pDEBUG_WIN,WriteCharsToConsole()");
00448 
00449   // copy the original data into the new buffer
00450 
00451   for (i = 0; i < OriginalLength; i++)
00452     pTempBuffer[i] = pOriginalText[i];
00453 
00454   // append the new data onto the end of the original buffer
00455 
00456   for (i = 0; i < StringLength; i++) {
00457     if (msg[i] == '\n')
00458       pTempBuffer[i + OriginalLength] = '\r';
00459     else
00460       pTempBuffer[i + OriginalLength] = msg[i];
00461   }
00462 
00463   // put the new text into the text edit item
00464 
00465   TESetText(pTempBuffer, NewLength, hTextEdit); 
00466 
00467   // clean up
00468 
00469   DisposePtr(pTempBuffer); 
00470   #endif
00471 }
00472 #endif                           //Mac Implmentation
00473 
00474 #else                            // Non graphical debugger
00475 
00476 DEBUG_WIN::DEBUG_WIN( const char*, INT32, INT32, INT32, INT32, INT32 ) {
00477 }
00478 
00479 DEBUG_WIN::~DEBUG_WIN () {
00480 }
00481 
00482 void DEBUG_WIN::dprintf (const char *format, ...) {
00483   va_list ap;
00484   va_start(ap, format);
00485   vfprintf(stderr, format, ap);
00486   va_end(ap);
00487 }
00488 
00489 void await_destruction() {
00490 }
00491 
00492 
00493 #endif
00494 

Generated on Wed Feb 28 19:49:09 2007 for Tesseract by  doxygen 1.5.1