home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / Apps / DevTools / ClassBuilder / Source / WorkspaceManager.m < prev    next >
Encoding:
Text File  |  1993-01-25  |  3.9 KB  |  133 lines

  1.  
  2. /* Generated by Interface Builder */
  3.  
  4. #import "WorkspaceManager.h"
  5. #import "MenuManager.h"
  6. #import <appkit/Text.h>
  7. #import <appkit/Panel.h>
  8. #import <objc/objc-load.h>
  9. #import <stdlib.h>
  10. #import <strings.h>
  11. #import  <sys/file.h>
  12. #import <sys/types.h>
  13. #import <sys/stat.h>
  14. #import <libc.h>
  15.  
  16. @implementation WorkspaceManager
  17.  
  18. - evalSelection:sender
  19. { // compile and run the workspace selection
  20.   char *textBuf, *headerBuf, *cursor, *trueBuf, comBuf[128] ;
  21.   char tmpFile[20] = "CBXXXXXX" ;
  22.   char *moduleNames[2] ;
  23.   NXStream *aStream ;
  24.   NXSelPt start, end ;
  25.   FILE *fp ;
  26.   int textLen, maxLen ;
  27.  
  28.   [self message: "Creating temporary class..."] ;
  29.   // get the selected text
  30.   [textView getSel:&start :&end] ;
  31.   textLen = end.cp-start.cp ;
  32.   textBuf = trueBuf = (char *) malloc(textLen) ; 
  33.   [textView getSubstring:textBuf start:start.cp length:textLen] ;
  34.   textBuf[textLen] = '\0' ;
  35.   // now find out if we have any header
  36.   headerBuf = textBuf ;
  37.   while(*headerBuf == ' ' || *headerBuf == '\n' || *headerBuf == '\t')
  38.     headerBuf++ ;  // scan past any initial whiteSpace
  39.   if(!strncmp(headerBuf,"#header",7)) // then we have a header
  40.   { headerBuf += 7 ; // header starts after #header
  41.     cursor = headerBuf ;
  42.     while(strncmp(cursor,"#endheader", 10)) // find end of header 
  43.     {  cursor++ ;
  44.        cursor = index(cursor,'#') ; // try next hash sign
  45.        if(cursor == (char *) 0) // then no #endheader
  46.        { NXRunAlertPanel("CB","Error: no #endheader line found",
  47.              NULL,NULL,NULL) ;
  48.      free(textBuf) ;
  49.          return self ;
  50.        }
  51.     }
  52.     textBuf = &cursor[10] ;
  53.     *cursor = '\0' ; // chop string into 2 pieces    
  54.   }
  55.   else
  56.     cursor = headerBuf = "" ;   // no header at all
  57.  
  58.   // now hack up the file...
  59.   mktemp(tmpFile) ;
  60.   sprintf(comBuf,"/tmp/%s.m",tmpFile) ;
  61.   fp = fopen(comBuf,"w+") ;
  62.   fprintf(fp,
  63.    "%s\n"
  64.    "#import <objc/Object.h>\n"
  65.    "#import <objc/HashTable.h>\n"
  66.    "@interface DictManager: Object ;\n"
  67.    "+ initialize ;\n"
  68.    "+ inserKey: (char *) aKey value: (void *) data type: (char *) aType ;\n"
  69.    "+ (void *) valueForKey: (char *) aKey type: (char *) aType ;\n"
  70.    "@end\n"
  71.    "extern id NXApp;\n"
  72.    "@interface Doit: Object\n"
  73.    "+(void) doit ;\n"
  74.    "@end\n\n"
  75.    "@implementation Doit\n"
  76.    "+ (void) doit\n"
  77.    "{\n"
  78.    "#line 1\n"
  79.    "%s\n"
  80.    "}\n"
  81.    "@end",
  82.    headerBuf, textBuf) ;
  83.   fflush(fp) ;
  84.   fclose(fp) ;
  85.   
  86.   // free up alloc'ed memory
  87.   if(headerBuf[0] != '\0') // string was cut into 2 pieces...
  88.     *cursor = ' ' ;   // reassemble
  89.   free(trueBuf) ; // trueBuf always points to beginning of malloc'ed string
  90.  
  91.   // compile and load the file
  92.   [self message: "Compiling..."] ;
  93.   sprintf(comBuf, "cc -c -I /usr/include/objc -Wall -o /tmp/%s.o /tmp/%s.m"
  94.           " 2> /tmp/%s", tmpFile, tmpFile, tmpFile) ;
  95.   if(system(comBuf)) // non-zero exit status == must report the errors
  96.   { sprintf(comBuf,"/tmp/%s",tmpFile) ;
  97.     [NXApp appendFileToTranscript: comBuf] ;
  98.     NXRunAlertPanel("CB","Compilation errors: see Transcript Window\n",
  99.          NULL,NULL,NULL) ;
  100.     sprintf(comBuf,"rm /tmp/%s.m /tmp/%s.o /tmp/%s 2> /dev/null",
  101.          tmpFile,tmpFile,tmpFile) ;
  102.     system(comBuf) ;
  103.     [self message: ""] ;
  104.     return self ;
  105.   } 
  106.   [self message: "Loading..."] ;
  107.   aStream = NXOpenMemory(NULL, 0, NX_READWRITE) ;
  108.   sprintf(comBuf,"/tmp/%s.o",tmpFile) ;
  109.   moduleNames[0] = comBuf ;
  110.   moduleNames[1] = NULL ;
  111.   objc_loadModules(moduleNames, aStream, NULL, NULL, NULL) ;
  112.   NXPutc(aStream,'\0') ; 
  113.   NXGetMemoryBuffer(aStream, &textBuf, &textLen, &maxLen);
  114.   [NXApp printf: textBuf];
  115.   [self message: "Executing..."] ;
  116.   objc_msgSend(objc_getClass("Doit"),
  117.      sel_getUid("doit")) ;
  118.   [self message: ""] ;
  119.   objc_unloadModules(aStream, NULL) ;
  120.   NXCloseMemory(aStream,NX_TRUNCATEBUFFER) ;
  121.   sprintf(comBuf,"rm /tmp/%s.m /tmp/%s.o /tmp/%s 2> /dev/null",
  122.          tmpFile,tmpFile,tmpFile) ;
  123.   system(comBuf) ;
  124.   return self ;
  125. }
  126.  
  127. - (char *) extension ;
  128. { // provide file extension
  129.   return "wsp" ;
  130. }
  131.  
  132. @end
  133.