home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / SourceCode / MiniExamples / AppKit / Winfo / WGetWinMem.m < prev    next >
Encoding:
Text File  |  1993-01-12  |  3.8 KB  |  116 lines

  1. #import <objc/objc.h>
  2. #import <libc.h>
  3. #import <dpsclient/dpsclient.h>
  4.  
  5. static char *responseBuffer = NULL;    /* Pointer to character buffer */
  6. static int   responseBufCount = 0;    /* Number of bytes in buffer, currently */
  7. static int   responseBufSize = 0;    /* Total size of buffer */
  8. #define MINRESPBUFSIZE 100
  9.  
  10. /* Implementation of myTextProc.  All that myTextProc does
  11.    is accumulate all the text that comes in on the context while
  12.    it's active into responseBuffer, resizing it when
  13.    necessary.   */
  14.  
  15. static void myTextProc(DPSContext context, char *buf, unsigned long count)
  16. {
  17.     /* Expand the buffer if necessary */
  18.  
  19.     while(((int)count + responseBufCount + 1) > responseBufSize)
  20.     {    /* Must expand buffer */
  21.     if (responseBuffer == NULL)
  22.     {
  23.         responseBuffer=NXZoneMalloc(NXDefaultMallocZone(),MINRESPBUFSIZE);
  24.         responseBufSize = MINRESPBUFSIZE;
  25.     }
  26.     else
  27.     {
  28.         responseBuffer = NXZoneRealloc(NXDefaultMallocZone(), 
  29.                        responseBuffer,responseBufSize*2);
  30.         responseBufSize *= 2;
  31.     }
  32.     }
  33.  
  34.     /* Put the new data in the buffer and update the count */
  35.  
  36.     bcopy(buf,responseBuffer+responseBufCount,count);
  37.     responseBufCount += count;
  38.  
  39.     /* And null-terminate the buffer (this is used by the main procedure) */
  40.  
  41.     *(responseBuffer+responseBufCount) = 0;
  42. }
  43.  
  44. /* Note that because the output of the dumpwindow/dumpwindows operators are not defined, this routine might fail under a future release of NeXTSTEP. It's OK to use this code in little test apps like this, but please don't rely on these operators for commercial applications.
  45. */
  46. BOOL getWindowBytes (int windowNum, int *bytes, int *type)
  47. {
  48.     DPSTextProc oldTextProc;
  49.  
  50.     /* Reset the character buffer variables */
  51.     responseBufCount = 0;
  52.  
  53.     DPSWaitContext(DPSGetCurrentContext());
  54.  
  55.     /* Now install our text proc.  We're about to call the
  56.     dumpwindows operator, which will write out a whole bunch of
  57.     stuff on the output stream back to us.  We need to install
  58.     our text proc before we do that, so that when the output
  59.     comes out we will catch it.  Also, we remember the old text
  60.     proc so we can put it back afterwards. */
  61.  
  62.     oldTextProc = DPSGetCurrentContext()->textProc;
  63.     DPSSetTextProc(DPSGetCurrentContext(),myTextProc);
  64.  
  65.     /* Now call the dumpwindows operator, and our text proc
  66.     catches all the responses and puts them in the
  67.     reponseBuffer. */
  68.  
  69.     DPSPrintf(DPSGetCurrentContext()," 0 %d dumpwindow flush\n", windowNum);
  70.  
  71.     /* Call DPSWaitContext to be sure we)re really done, and all
  72.     of the responses are really in the responseBuffer. */
  73.  
  74.     DPSWaitContext(DPSGetCurrentContext());
  75.  
  76.     /* OK, now we've got all the text in responseBuffer.  We can
  77.     take our text proc back out. */
  78.  
  79.     DPSSetTextProc(DPSGetCurrentContext(),oldTextProc);
  80.  
  81.     /* The real work.  We get a line at a time out of
  82.     reponseBuffer, and look at it to see if it looks like a
  83.     line generated by the dumpwindows operator.  If so, we
  84.     parse the memory usage number out of it; if not, we pass it
  85.     to the old text proc for whatever normal processing
  86.     occurs. */
  87.  
  88.     if (responseBuffer)
  89.     {
  90.     char *newBuf, *buf = responseBuffer;
  91.  
  92.     /* Find a line & Null-terminate it. We need the newline */
  93.     newBuf = index(buf,'\n');
  94.     if (newBuf) *newBuf = 0;
  95.     /* Parse the line. dumpwindows returns lines that look like:
  96.         *
  97.         * Buffered: (0,448),(672,832) 129024+ bytes (131072)
  98.         *
  99.         * where the "+" is optional.
  100.         */
  101.     if ((newBuf = index(buf, ':')) &&
  102.         (sscanf(newBuf+1," (%*d,%*d),(%*d,%*d) %d%*c%*s (%*d)",  bytes) == 1)) {
  103.         *newBuf = 0;            
  104.         if (!strcmp(buf,"Nonretained")) *type = NX_NONRETAINED;
  105.         else if (!strcmp(buf,"Retained")) *type = NX_RETAINED;
  106.         else if (!strcmp(buf,"Buffered")) *type = NX_BUFFERED;
  107.         else *type = -1;
  108.     } else {
  109.         (*oldTextProc)(DPSGetCurrentContext(),buf,strlen(buf));
  110.         return NO;
  111.     }
  112.     }
  113.     return YES;
  114. }
  115.  
  116.