home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1994 June / NEBULA_SE.ISO / SourceCode / MiscKit / Source / MiscStringNEXTSTEP.m < prev    next >
Encoding:
Text File  |  1994-03-04  |  3.1 KB  |  102 lines

  1. //
  2. //    MiscStringNEXTSTEP.m
  3. //        Written by Don Yacktman (c) 1993 by Don Yacktman.
  4. //                Version 1.7  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.  
  15. @implementation MiscString(NEXTSTEP)
  16.  
  17. // This category includes methods to support various NEXTSTEP features,
  18. // such as InterfaceBuilder (palettes), Distributed Objects (NXTransport),
  19. // and object archiving.
  20.  
  21. - read:(NXTypedStream *)stream
  22. {
  23.     int tempLength; char *tBuf;
  24.     [super read:stream];
  25.     
  26.     // find out how much space we will need
  27.     NXReadType(stream, "i", &tempLength);
  28.     
  29.     // note that -allocateBuffer:fromZone: will set _length for
  30.     // us -- it is important to not smash _length since it tracks
  31.     // the actual size of the current buffer!  (See the notes for
  32.     // -allocateBuffer:fromZone: for more info on why no smashing.)
  33.     [self allocateBuffer:tempLength fromZone:[self zone]]; // make sure
  34.     // we have a buffer and it is big enough.
  35.     
  36.     // Get the string off the stream and into the buffer
  37.     NXReadType(stream, "*", &tBuf);
  38.     if (tBuf) strcpy(buffer, tBuf);
  39.     if (!tBuf && buffer) buffer[0] = '\0'; // NULL string?
  40.     
  41.     // now make sure that we have the right length for the string stored.
  42.     if (buffer) length = strlen(buffer);
  43.     else length = 0;
  44.     
  45.     // clean up and blow this joint
  46.     if (tBuf) free(tBuf);
  47.     return self;
  48. }
  49.  
  50. - write:(NXTypedStream *)stream
  51. {
  52.     [super write:stream];
  53.     // put the string and it's length on the stream.  The length is sort
  54.     // of redundant, but this assures that the string really is like the
  55.     // one that was archived -- if the stored instance had an oversize
  56.     // buffer, then read will restore an oversize buffer.  Of course,
  57.     // crud after the string won't be the same, so the new instance isn't
  58.     // quite the same, but...
  59.     NXWriteType(stream, "i", &_length);
  60.     NXWriteType(stream, "*", &buffer);
  61.     return self;
  62. }
  63.  
  64. // NXTransport protocol implementation (next three methods):
  65. - encodeUsing:(id <NXEncoding>)portal
  66. {
  67.     [portal encodeData:&_length ofType:"i"];
  68.     [portal encodeData:&length ofType:"i"];
  69.     [portal encodeData:&buffer ofType:"*"];
  70.     return self;
  71. }
  72.  
  73. - decodeUsing:(id <NXDecoding>)portal
  74. {
  75.     int newLen;
  76.     [self freeString];
  77.     [portal decodeData:&newLen ofType:"i"];
  78.     [self allocateBuffer:newLen];
  79.     [portal decodeData:&length ofType:"i"];
  80.     [portal decodeData:&buffer ofType:"*"];
  81.     return self;
  82. }
  83.  
  84. - encodeRemotelyFor:(NXConnection *)connection
  85.     freeAfterEncoding:(BOOL *)flagp isBycopy:(BOOL)isByCopy
  86. {
  87.     if (isByCopy) {
  88.         *flagp = NO; // object will not be freed (ie, copy,  not move)
  89.         return self; //encode object (copy it)
  90.     }
  91.     *flagp = NO;
  92.     // super will encode the proxy otherwise
  93.     return [super encodeRemotelyFor:connection
  94.                 freeAfterEncoding:flagp isBycopy:isByCopy];
  95. }
  96.  
  97. // Interface Builder support
  98. - (const char *)getInspectorClassName { return "MiscStringInspector"; }
  99. - (NXImage *)getIBImage { return [NXImage findImageNamed:"MiscStringObj"]; }
  100.  
  101. @end
  102.