home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / Apps / DevTools / Commentate.1.2 / Source / Controller.m < prev    next >
Encoding:
Text File  |  1994-11-21  |  7.1 KB  |  273 lines

  1.  
  2. #import "Controller.h"
  3.  
  4. @implementation Controller
  5.  
  6. - appDidInit:sender
  7. {
  8.     [[NXApp appListener] setServicesDelegate:self];
  9.     return self;
  10. }
  11.  
  12. - app:sender willShowHelpPanel:panel
  13. {
  14.     char path[MAXPATHLEN + 1];
  15.  
  16.     sprintf (path, "%s/%s", [panel helpDirectory],
  17.       "GettingStarted.rtfd");
  18.     [panel showFile:path atMarker:NULL];
  19.     return self;
  20. }
  21.  
  22. - comment:(id)pasteboard
  23.     userData:(const char *)language
  24.     error:(char **)errorMessage
  25. {
  26.     const char *inData;
  27.     char *outData; 
  28.     int   inLength, outLength;
  29.     const char *const *types;
  30.     int hasAscii, i;
  31.     types = [pasteboard types];
  32.     hasAscii=0;
  33.     for (i=0; !hasAscii && types[i] ; i++) 
  34.         if (!strcmp(types[i], NXAsciiPboardType)) hasAscii=1;
  35.     if (hasAscii) {
  36.         [pasteboard readType:NXAsciiPboardType data:&inData
  37.             length:&inLength];
  38.         if (inData && inLength) {
  39.             if (strcmp(language,"C")==0) {
  40.                 int i;
  41.                 outLength=inLength+6;
  42.                 outData=malloc(outLength);
  43.                 outData[0]='/';
  44.                 outData[1]='*';
  45.                 outData[2]='\n';
  46.                 for(i=0;i<inLength;i++)
  47.                     outData[i+3]=inData[i];
  48.                 outData[inLength+3]='*';
  49.                 outData[inLength+4]='/';
  50.                 outData[inLength+5]='\n';
  51.                 [pasteboard declareTypes:&NXAsciiPboardType num:1 owner:self];
  52.                 [pasteboard writeType:NXAsciiPboardType data:outData
  53.                     length:outLength];
  54.                 free(outData);
  55.             } else if (strcmp(language,"ObjC")==0) {
  56.                 int i=0, j=2, incLength;
  57.                 outLength=inLength+1000;
  58.                 outData=malloc(outLength);
  59.                 outData[0]='/';
  60.                 outData[1]='/';
  61.                 incLength=2;
  62.                 while (i<inLength) {
  63.                     if (inData[i] != '\n' || i == inLength-1) {
  64.                         outData[j]=inData[i];
  65.                         i++;
  66.                         j++;
  67.                     } else {
  68.                         outData[j]=inData[i];
  69.                         outData[j+1]='/';
  70.                         outData[j+2]='/';
  71.                         i++;
  72.                         j+=3;
  73.                         incLength+=2;
  74.                     }
  75.                 }
  76.                 [pasteboard declareTypes:&NXAsciiPboardType num:1 owner:self];
  77.                 [pasteboard writeType:NXAsciiPboardType data:outData
  78.                     length:inLength+incLength];
  79.                 free(outData);
  80.             } else if (strcmp(language,"Postscript")==0) {
  81.                 int i=0, j=1, incLength;
  82.                 outLength=inLength+1000;
  83.                 outData=malloc(outLength);
  84.                 outData[0]='%';
  85.                 incLength=1;
  86.                 while (i<inLength) {
  87.                     if (inData[i] != '\n' || i == inLength-1) {
  88.                         outData[j]=inData[i];
  89.                         i++;
  90.                         j++;
  91.                     } else {
  92.                         outData[j]=inData[i];
  93.                         outData[j+1]='%';
  94.                         i++;
  95.                         j+=2;
  96.                         incLength+=1;
  97.                     }
  98.                 }
  99.                 [pasteboard declareTypes:&NXAsciiPboardType num:1 owner:self];
  100.                 [pasteboard writeType:NXAsciiPboardType data:outData
  101.                     length:inLength+incLength];
  102.                 free(outData);
  103.             } else if (strcmp(language,"Sybase")==0) {
  104.                 int i=0, j=2, incLength;
  105.                 outLength=inLength+1000;
  106.                 outData=malloc(outLength);
  107.                 outData[0]='-';
  108.                 outData[1]='-';
  109.                 incLength=2;
  110.                 while (i<inLength) {
  111.                     if (inData[i] != '\n' || i == inLength-1) {
  112.                         outData[j]=inData[i];
  113.                         i++;
  114.                         j++;
  115.                     } else {
  116.                         outData[j]=inData[i];
  117.                         outData[j+1]='-';
  118.                         outData[j+2]='-';
  119.                         i++;
  120.                         j+=3;
  121.                         incLength+=2;
  122.                     }
  123.                 }
  124.                 [pasteboard declareTypes:&NXAsciiPboardType num:1 owner:self];
  125.                 [pasteboard writeType:NXAsciiPboardType data:outData
  126.                     length:inLength+incLength];
  127.                 free(outData);
  128.             } else if (strcmp(language,"Debug")==0) {
  129.                 int i;
  130.                 outLength=inLength+20;
  131.                 outData=malloc(outLength);
  132.                 strcpy(outData,"#ifdef DEBUG\n");
  133.                 for(i=0;i<inLength;i++)
  134.                     outData[i+13]=inData[i];
  135.                 if (inData[inLength-1]!='\n')
  136.                     strcpy(&(outData[inLength+13]),"\n#endif");
  137.                 else
  138.                     strcpy(&(outData[inLength+13]),"#endif\n");
  139.                 [pasteboard declareTypes:&NXAsciiPboardType num:1 owner:self];
  140.                 [pasteboard writeType:NXAsciiPboardType data:outData
  141.                     length:outLength];
  142.                 free(outData);
  143.             } else if (strcmp(language,"Shell")==0) {
  144.                 int i=0, j=1, incLength;
  145.                 outLength=inLength+1000;
  146.                 outData=malloc(outLength);
  147.                 outData[0]='#';
  148.                 incLength=1;
  149.                 while (i<inLength) {
  150.                     if (inData[i] != '\n' || i == inLength-1) {
  151.                         outData[j]=inData[i];
  152.                         i++;
  153.                         j++;
  154.                     } else {
  155.                         outData[j]=inData[i];
  156.                         outData[j+1]='#';
  157.                         i++;
  158.                         j+=2;
  159.                         incLength+=1;
  160.                     }
  161.                 }
  162.                 [pasteboard declareTypes:&NXAsciiPboardType num:1 owner:self];
  163.                 [pasteboard writeType:NXAsciiPboardType data:outData
  164.                     length:inLength+incLength];
  165.                 free(outData);
  166.             }
  167.         }
  168.     }
  169.     return self;
  170. }
  171.  
  172. - unComment:(id)pasteboard
  173.     userData:(const char *)language
  174.     error:(char **)errorMessage
  175. {
  176.     BOOL inString;
  177.     const char *inData;
  178.     char *outData; 
  179.     int   inLength, outLength, currLength;
  180.     const char *const *types;
  181.     int hasAscii, i;
  182.     types = [pasteboard types];
  183.     hasAscii=0;
  184.     outLength=0;
  185.     currLength=0;
  186.     inString=NO;
  187.     for (i=0; !hasAscii && types[i] ; i++) 
  188.         if (!strcmp(types[i], NXAsciiPboardType)) hasAscii=1;
  189.     if (hasAscii) {
  190.         [pasteboard readType:NXAsciiPboardType data:&inData
  191.             length:&inLength];
  192.         if (inData && inLength) {
  193.             outData=malloc(inLength);
  194.             while (currLength < inLength) {
  195.                 if (!inString && inData[currLength]=='/') {
  196.                     if (inData[currLength+1]=='*') {
  197.                         currLength+=2;
  198.                         if (inData[currLength]=='\n' &&
  199.                                 currLength<inLength)
  200.                             currLength++;
  201.                         while (!(inData[currLength]=='*' &&
  202.                                 inData[currLength+1]=='/') &&
  203.                                 currLength < inLength)
  204.                             outData[outLength++]=inData[currLength++];
  205.                         currLength+=2;
  206.                         if (inData[currLength]=='\n' &&
  207.                                 currLength<inLength) {
  208.                             currLength++;
  209.                         } else if (currLength > inLength) {
  210.                             *errorMessage="End of C comment not found.";
  211.                             return self;
  212.                         }
  213.                     } else if (inData[currLength+1]=='/') {
  214.                         currLength+=2;
  215.                         while (inData[currLength]!='\n' &&
  216.                                 currLength<inLength)
  217.                             outData[outLength++]=inData[currLength++];
  218.                     } else {
  219.                         outData[outLength++]=inData[currLength++];
  220.                     }
  221.                 } else if (!inString && inData[currLength]=='%') {
  222.                         currLength+=1;
  223.                         while (inData[currLength]!='\n' &&
  224.                                 currLength<inLength)
  225.                             outData[outLength++]=inData[currLength++];
  226.                 } else if (!inString && inData[currLength]=='-'
  227.                     && inData[currLength+1]=='-') {
  228.                         currLength+=2;
  229.                         while (inData[currLength]!='\n' &&
  230.                                 currLength<inLength)
  231.                             outData[outLength++]=inData[currLength++];
  232.                 } else if (!inString && inData[currLength]=='#') {
  233.                     if (strncmp(&(inData[currLength+1]),"ifdef DEBUG",11) == 0) {
  234.                         currLength+=12;
  235.                         if (inData[currLength]=='\n' &&
  236.                                 currLength<inLength)
  237.                             currLength++;
  238.                         while ((strncmp(&(inData[currLength]),"\n#endif",7) != 0) &&
  239.                             currLength < inLength)
  240.                             outData[outLength++]=inData[currLength++];
  241.                         currLength+=7;
  242.                         if (currLength > inLength) {
  243.                             *errorMessage="End of DEBUG statement not found.";
  244.                             return self;
  245.                         }
  246.                     } else {
  247.                         currLength+=1;
  248.                         while (inData[currLength]!='\n' &&
  249.                                 currLength<inLength)
  250.                             outData[outLength++]=inData[currLength++];
  251.                     }
  252.                 } else {
  253.                     outData[outLength++]=inData[currLength++];
  254.                 }
  255.             }
  256.             [pasteboard declareTypes:&NXAsciiPboardType num:1 owner:self];
  257.             [pasteboard writeType:NXAsciiPboardType data:outData
  258.                 length:outLength];
  259.             free(outData);
  260.         }
  261.     }
  262.     return self;
  263. }
  264.  
  265. - showInfoPanel:sender
  266. {
  267.     if (!infoPanel)
  268.         [NXApp loadNibSection:"InfoPanel.nib" owner:self];
  269.     [infoPanel makeKeyAndOrderFront:self];
  270.     return self;
  271. }
  272. @end
  273.