home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1994 June / NEBULA_SE.ISO / SourceCode / MiscKit / Examples / MiscStringService / Controller.m < prev    next >
Encoding:
Text File  |  1994-01-09  |  2.1 KB  |  84 lines

  1.  
  2. #import "Controller.h"
  3. #import <misckit/MiscString.h>
  4.  
  5. @implementation Controller
  6.  
  7. //This code was pretty much taken from the example in the documentation for
  8. //Services.  It's pretty basic; it only does ascii transfers.
  9. //To cut down on code, I just used the User Data: field in the services.txt
  10. //file to be different numbers, then I just atoi that here to figure out
  11. //which service is requested.  I probably should've #defined some things
  12. //or at least made an enumerated type to make it more readable, but oh well.
  13. //It's not so big as this should be a problem.
  14.  
  15. - alterText:(id)pasteboard userData:(const char*)userData error:(char **)msg;
  16. {
  17.   const char *types[1];
  18.   id theString;
  19.   char *data;
  20.   int length, i = atoi(userData);
  21.   
  22. //  printf("%s\n",userData);
  23.   
  24.   [pasteboard types];    // pretend to check the pasteboard types
  25.  
  26.   if ([pasteboard readType:NXAsciiPboardType data:&data length:&length]) {
  27.     theString = [MiscString newWithString:""];
  28.     [theString cat:data n:length];
  29.     switch (i) {
  30.       case 0:
  31.         // I assume someone played with the User Data: fields if this happens
  32.         *msg = "Don't play with services.txt!!";
  33.     [theString free];
  34.     return self;
  35.     break;
  36.       case 1: 
  37.         [theString toLower];
  38.     break;
  39.       case 2:
  40.         [theString toUpper];
  41.     break;
  42.       case 3:
  43.         [theString invertCases];
  44.     break;
  45.       case 4:
  46.         [theString reverse];
  47.     break;
  48.       case 5:
  49.         [theString capitalizeEachWord];
  50.     break;
  51.       case 6:
  52.         [theString trimSpaces];
  53.     break;
  54.       case 7:
  55.         [theString trimLeadSpaces];
  56.     break;
  57.       case 8:
  58.         [theString trimTailSpaces];
  59.     break;
  60.       case 9:
  61.         [theString squashSpaces];
  62.     break;
  63.      }
  64.     types[0] = NXAsciiPboardType;
  65.     [pasteboard declareTypes:types num:1 owner:nil];
  66.     [pasteboard writeType:NXAsciiPboardType data:(char *)[theString stringValue] length:[theString length]];
  67.     
  68.     [theString free];
  69.    }
  70.   else *msg = "Error: couldn't alter text.";
  71.   
  72.   return self;
  73. }
  74.  
  75. // Have to let the workspace know where to find us
  76. - appDidInit:sender
  77. {
  78.   id theListener = [NXApp appListener];
  79.   [theListener setServicesDelegate:self];
  80.   return self;
  81. }
  82.  
  83. @end
  84.