home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Graphics / Misc / aa_m68k_Only / NDCamera.0.21 / Source / MiscSources.subproj / MiscStringNEXTSTEP.m < prev    next >
Encoding:
Text File  |  1995-04-12  |  5.5 KB  |  166 lines

  1. //
  2. //    MiscStringNEXTSTEP.m
  3. //        Written by Don Yacktman Copyright (c) 1993 by Don Yacktman.
  4. //                Version 1.95  All rights reserved.
  5. //        This notice may not be removed from this source code.
  6. //
  7. //    This object is included in the MiscKit by permission from the author
  8. //    and its use is governed by the MiscKit license, found in the file
  9. //    "LICENSE.rtf" in the MiscKit distribution.  Please refer to that file
  10. //    for a list of all applicable permissions and restrictions.
  11. //    
  12.  
  13. #import <misckit/MiscString.h>
  14. #import <mach/mach_error.h>
  15.  
  16. @implementation MiscString(NEXTSTEP)
  17.  
  18. // This category includes methods to support various NEXTSTEP features,
  19. // such as InterfaceBuilder (palettes), Distributed Objects (NXTransport),
  20. // and object archiving.
  21.  
  22. - read:(NXTypedStream *)stream
  23. {
  24.     int tempLength; char *tBuf;
  25.     [super read:stream];
  26.     
  27.     // find out how much space we will need
  28.     NXReadType(stream, "i", &tempLength);
  29.     
  30.     // note that -allocateBuffer:fromZone: will set _length for
  31.     // us -- it is important to not smash _length since it tracks
  32.     // the actual size of the current buffer!  (See the notes for
  33.     // -allocateBuffer:fromZone: for more info on why no smashing.)
  34.     // Note that -allocateBuffer:fromZone: increments the buffer
  35.     // size by one, so we must pass it decremented so that we unarchive
  36.     // with a buffer of the same length as the original object.
  37.     [self allocateBuffer:(tempLength-1) fromZone:[self zone]]; // make sure
  38.     // we have a buffer and it is big enough.
  39.     
  40.     // Get the string off the stream and into the buffer
  41.     NXReadType(stream, "*", &tBuf);
  42.     if (tBuf) strcpy(buffer, tBuf);
  43.     if (!tBuf && buffer) buffer[0] = '\0'; // NULL string?
  44.     
  45.     // now make sure that we have the right length for the string stored.
  46.     if (buffer) length = strlen(buffer);
  47.     else length = 0;
  48.     
  49.     // clean up and blow this joint
  50.     if (tBuf) free(tBuf);
  51.     return self;
  52. }
  53.  
  54. - write:(NXTypedStream *)stream
  55. {
  56.     [super write:stream];
  57.     // put the string and it's length on the stream.  The length is sort
  58.     // of redundant, but this assures that the string really is like the
  59.     // one that was archived -- if the stored instance had an oversize
  60.     // buffer, then read will restore an oversize buffer.  Of course,
  61.     // crud after the string won't be the same, so the new instance isn't
  62.     // quite the same, but...
  63.     NXWriteType(stream, "i", &_length);
  64.     NXWriteType(stream, "*", &buffer);
  65.     return self;
  66. }
  67.  
  68. // NXTransport protocol implementation (next three methods):
  69. - encodeUsing:(id <NXEncoding>)portal
  70. {
  71.     [portal encodeData:&_length ofType:"i"]; // no longer used, remains for compatability reasons
  72.     [portal encodeData:&length ofType:"i"];
  73.     [portal encodeData:&buffer ofType:"*"];
  74.     return self;
  75. }
  76.  
  77. - decodeUsing:(id <NXDecoding>)portal
  78. {
  79.     int newLen;
  80.     [self freeString];
  81.     [portal decodeData:&newLen ofType:"i"]; // no longer used, remains for compatability reasons
  82.     // This is not necessary and causes a leak:  [self allocateBuffer:newLen];
  83.     [portal decodeData:&length ofType:"i"];
  84.     [portal decodeData:&buffer ofType:"*"];
  85.     // Yes, I know I'm not supposed to fool with _length, but....
  86.     _length = length + 1; // it needs to be set properly and this ought to
  87.     // do the trick.  Note that the bycopy object doesn't necessarily have
  88.     // a buffer as large as the original.  We don't really care since they
  89.     // grow dynamically anyway and most strings passed through DO will end
  90.     // up being treated as constants anyway, so the trimming down is OK.
  91.     return self;
  92. }
  93.  
  94. - encodeRemotelyFor:(NXConnection *)connection
  95.     freeAfterEncoding:(BOOL *)flagp isBycopy:(BOOL)isByCopy
  96. {
  97.     if (isByCopy) {
  98.         *flagp = NO; // object will not be freed (ie, copy,  not move)
  99.         return self; //encode object (copy it)
  100.     }
  101.     *flagp = NO;
  102.     // super will encode the proxy otherwise
  103.     return [super encodeRemotelyFor:connection
  104.                 freeAfterEncoding:flagp isBycopy:isByCopy];
  105. }
  106.  
  107. // RTF support
  108. - (BOOL)isRTFText
  109. {
  110.     if (![self cmp:"{\\rtf0" n:6]) return YES; //} rtf header/magic num
  111.     if (![self cmp:"{\\\\rtf0" n:7]) return YES; //} happens in .address files
  112.     return NO;
  113. }
  114.  
  115. - (MiscString *)plainTextForRTF
  116. { // Donated by Darcy Brockbank and untested.  Let's hope it works!
  117.     id text = [[Text alloc] init];
  118.     // text may have to go into a dummy window, or we could
  119.     // make a major hack and ask for the [[NXApp mainMenu] getFieldEditor...
  120.     vm_size_t allocSize = (strlen(buffer)+1) * sizeof(char);
  121.     char * tmp;
  122.     NXStream * stream;
  123.     MiscString * other = nil;
  124.     kern_return_t ret;
  125.  
  126.     if (![self isRTFText]) return [self copy];
  127.     [text setText:""];
  128.     ret = (vm_allocate(task_self(), (vm_address_t *)&tmp, allocSize, TRUE));
  129.     if (ret != KERN_SUCCESS) {
  130.            mach_error("vm_allocate returned value of ", ret);
  131.         printf("Exiting with error.\n");
  132.             exit(-1);
  133.     }
  134.     strcpy(tmp,buffer);
  135.     stream = NXOpenMemory(tmp,allocSize,NX_READONLY);
  136.     [text readRichText:stream];
  137.     NXCloseMemory(stream,NX_TRUNCATEBUFFER);
  138.     ret = vm_deallocate(task_self(), (vm_address_t)tmp, allocSize);
  139.     if (ret != KERN_SUCCESS) {
  140.         mach_error("vm_deallocate returned value of ",ret);
  141.         printf("Exiting with error.\n");
  142.         exit(-1);
  143.     }
  144.     if ([text textLength]) {
  145.         int len = [text textLength];
  146.         char otherBuf[len + 1];
  147.         [text getSubstring:otherBuf start:0 length:len];
  148.         otherBuf[len]='\0';
  149.         other = [[MiscString alloc] initString:otherBuf];
  150.     }
  151.     [text free];
  152.     return other;    // nil if we were unsuccessful
  153. }
  154.  
  155. // Interface Builder support
  156. - (const char *)getInspectorClassName { return "MiscStringInspector"; }
  157. - (NXImage *)getIBImage { return [NXImage findImageNamed:"MiscStringObj"]; }
  158.  
  159. - writeToStream:(NXStream *)aStream
  160. {
  161.     NXPrintf(aStream,"%s", [self stringValue]);
  162.     return self;
  163. }
  164.  
  165. @end
  166.