home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / Utilities / FileSpy-1.1-MIHS / cServices.m < prev    next >
Encoding:
Text File  |  1996-04-12  |  3.8 KB  |  140 lines

  1. /*
  2.  *   This sourcecode is part of FileSpy, a logfile observing utility.
  3.  *   Copyright (C) 1996  Felix Rauch
  4.  *
  5.  *   This program is free software; you can redistribute it and/or modify
  6.  *   it under the terms of the GNU General Public License as published by
  7.  *   the Free Software Foundation; either version 2 of the License, or
  8.  *   (at your option) any later version.
  9.  *   
  10.  *   Notice that this program may not be possessed, used, copied,
  11.  *   distributed or modified by people having to do with nuclear
  12.  *   weapons. See the file CONDITIONS for details.
  13.  *
  14.  *   This program is distributed in the hope that it will be useful,
  15.  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  *   GNU General Public License for more details.
  18.  *
  19.  *   You should have received a copy of the GNU General Public License
  20.  *   along with this program; if not, write to the Free Software
  21.  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  *
  23.  *   To contact the original author, try:
  24.  *   e-mail: Felix.Rauch@nice.ch
  25.  *   Traditional mail:    Felix Rauch
  26.  *            Sempacherstrasse 33
  27.  *            8032 Zurich
  28.  *            Switzerland
  29.  */
  30.  
  31. #import "Controller.h"
  32.  
  33. @implementation Controller(Services)
  34.  
  35. - registerServices:sender
  36. {
  37.     const char *sendTypes[2];
  38.     const char *returnTypes[1];
  39.     sendTypes[0] = NXFilenamePboardType;
  40.     sendTypes[1] = NULL;
  41.     returnTypes[0] = NULL;
  42.     [NXApp registerServicesMenuSendTypes:sendTypes andReturnTypes:returnTypes];
  43.  
  44.     return self;
  45. }
  46.  
  47. - validRequestorForSendType:(NXAtom)typeSent andReturnType:(NXAtom)typeReturned
  48. {
  49.     if ((typeSent == NXFilenamePboardType || typeSent == NULL) && (typeReturned == NULL)) {
  50.     List *cellList;
  51.     
  52.     if(!tmpList)
  53.         tmpList = [[List alloc] init];
  54.     cellList = [tmpList empty];
  55.     
  56.     [myMatrix getSelectedCells:cellList];
  57.         if (([cellList count] > 0 || typeSent == NULL) && (typeReturned == NULL))
  58.             return self;
  59.     }
  60.     return nil;
  61. }
  62.  
  63. - (BOOL)writeSelectionToPasteboard:(Pasteboard *)pboard types:(NXAtom *)types
  64. {
  65.     while (types && *types) {
  66.     if (*types == NXFilenamePboardType)
  67.         break;
  68.     types++;
  69.     }
  70.  
  71.     if(types && *types)
  72.     {
  73.         const char *types[2] = {NXFilenamePboardType, ""};
  74.     List *cellList = [tmpList empty];
  75.     unsigned int i, max, len = 0;
  76.     char *string, *orig;
  77.     const char *filename;
  78.     
  79.     [myMatrix getSelectedCells:cellList];
  80.     max = [cellList count];
  81.     for(i = 0; i < max; i++)
  82.         len += strlen([[cellList objectAt:i] stringValue]) + 1;
  83.     string = orig = (char *)malloc(len*sizeof(char));
  84.     *string = '\000';
  85.     for(i = 0; i < max; i++) {
  86.         filename = [[cellList objectAt:i] stringValue];
  87.         strcat(string, filename);
  88.         strcat(string, "\t");
  89.         string += strlen(filename) + 1;
  90.     }
  91.     if(max > 0)
  92.         string[-1] = '\000';
  93.  
  94.     [pboard declareTypes:types num:1 owner:nil];
  95.     [pboard writeType:NXFilenamePboardType data:orig length:len];
  96.     return YES;
  97.     } else {
  98.     return NO;
  99.     }
  100. }
  101.  
  102. - spyFileFromService:pasteboard userData:(const char *)userData error:(char **)msg
  103. {
  104.     char *buffer, *data, c, *p, *q;
  105.     int length;
  106.     
  107.     [pasteboard types];    // pretend to check the pasteboard types
  108.     
  109.     if ([pasteboard readType:NXFilenamePboardType data:&data length:&length])
  110.     {
  111.         buffer = malloc(length+1);
  112.     
  113.     p = buffer;
  114.     strncpy(buffer, data, length);
  115.     buffer[length] = '\000';
  116.     while(*p != '\000') {
  117.         q = p;
  118.         while((*q != '\000') && (*q != '\t'))    // search end of current filename
  119.         q++;
  120.         c = *q;                    // remember tab or \0
  121.         if(c == '\t')                // if it's just a tab..
  122.         *q = '\000';                // .. then terminate string temporarily
  123.         [self addFileWithName:p];
  124.         if(c == '\t')
  125.         p = q + 1;
  126.         else
  127.         p = q;
  128.     }
  129.         
  130.         free(buffer);
  131.     [pasteboard deallocatePasteboardData:data length:length];
  132.     }
  133.     else
  134.     *msg = "Error: couldn't spy file.";    // eventually improve this
  135.     
  136.     return self;
  137. }
  138.  
  139. @end
  140.