home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Audio-DSP / NU / Source / NuString.m < prev    next >
Encoding:
Text File  |  1993-01-04  |  2.9 KB  |  129 lines

  1. /**
  2.  ** NuString
  3.  ** Provides a set of useful string manipulation
  4.  ** procedures.
  5.  **/
  6.  
  7. #import "NuString.h"
  8. #import <strings.h>
  9. #import <stdlib.h>
  10.  
  11. // here are the "good" malloc lengths.  Anything larger than
  12. // 4088 is best is a multiple of 8192
  13. int goodLens[] = {16, 32, 64, 128, 176,
  14.      252, 340, 508, 680, 1020, 1360, 2044, 2724, 4088} ;
  15.  
  16.  
  17. int bestLength_(int len)
  18. { // return the "best" malloc size, given length
  19.   // of a string (len should include space for
  20.   // the null terminator).
  21.   int theLen, i ;
  22.   if(len <= 4088)
  23.   { i = 0 ;
  24.     while(goodLens[i] < len)
  25.     i++ ;
  26.     return goodLens[i] ;
  27.   }
  28.   else
  29.   { i = 1 ;
  30.     while((theLen = (i * 8192)) < len)
  31.       i++ ;
  32.     return theLen ;
  33.   }
  34. }
  35.  
  36. @implementation NuString: Object
  37. { char *string ;
  38.   unsigned capacity ;
  39. }
  40.  
  41.  
  42. + new: (char *) aString ;
  43. { // as a convenience, alloc and init from a cstring
  44.   self = [[self alloc] initFromString: aString] ;
  45.   return self ;
  46. }
  47.  
  48.  
  49. - catStream: (NXStream *) aStream ;
  50. { // concatenate aStream onto myself
  51.   int neededLength, currentLength, streamLength, maxLength ;
  52.   char *streamBuffer ;
  53.   NXGetMemoryBuffer(aStream, &streamBuffer, &streamLength, &maxLength);
  54.   currentLength = strlen(string) ;
  55.   neededLength =  currentLength + streamLength + 1 ;
  56.   if(neededLength > capacity)
  57.   { char *newString ;
  58.     capacity = bestLength_(neededLength) ;
  59.     newString = (char *) malloc(capacity) ;
  60.     strcpy(newString,string);
  61.     free(string) ;
  62.     string = newString ;
  63.   }
  64.   bcopy(streamBuffer,&string[currentLength],streamLength) ;
  65.   string[neededLength -1] = '\0' ;
  66.   return self ;
  67. }    
  68.   
  69. - catString: (char *) aString ;
  70. { // concatenate aString onto myself
  71.   int neededLength ;
  72.   neededLength =  strlen(string) + strlen(aString) + 1 ;
  73.   if(neededLength > capacity)
  74.   { char *newString ;
  75.     capacity = bestLength_(neededLength) ;
  76.     newString = (char *) malloc(capacity) ;
  77.     strcpy(newString,string);
  78.     free(string) ;
  79.     string = newString ;
  80.   }
  81.   strcat(string, aString) ;
  82.   return self ;
  83. }   
  84.  
  85. - (const char *) cString ;
  86. { // returns the "address" of the string
  87.   return (const char *) string ;
  88. }
  89.  
  90.   
  91. - free ;
  92. {  if(string)
  93.      free(string) ;
  94.   return [super free] ;
  95. }
  96.  
  97.  
  98. - initFromStream: (NXStream *) aStream ;
  99. { char *buffer ;
  100.   int length, maxLength ;
  101.   NXGetMemoryBuffer(aStream, &buffer, &length, &maxLength);
  102.   capacity = bestLength_(length + 1) ;
  103.   string = (char *) malloc(capacity) ;
  104.   bcopy(buffer, string, length);
  105.   string[length] = '\0' ; // make sure null-terminated!
  106.   return self ;
  107. }
  108.  
  109. - initFromString: (char *) aString ;
  110. { capacity = bestLength_(strlen(aString) + 1) ;
  111.   string = (char *) malloc(capacity) ;
  112.   strcpy(string,aString) ;
  113.   return self ;
  114.  
  115. -read: (NXTypedStream *) stream ;
  116. { [super read: stream] ;
  117.   NXReadTypes(stream, "*i", &string, &capacity) ;
  118.   return self ;
  119. }
  120.  
  121. -write: (NXTypedStream *) stream ;
  122. { [super write: stream] ;
  123.   NXWriteTypes(stream, "*i", &string, &capacity)  ;
  124.   return self ;
  125. }
  126.  
  127. @end
  128.