home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / Mail / Bundles / EnhanceMail-2.1-MIHS / SimpleString.m < prev    next >
Encoding:
Text File  |  1997-11-15  |  3.6 KB  |  206 lines

  1. /* -*-C-*-
  2. *******************************************************************************
  3. *
  4. * File:         SimpleString.m
  5. * RCS:          /usr/local/sources/CVS/EnhanceMail/SimpleString.m,v 1.6 1997/11/15 16:56:40 tom Exp
  6. * Description:  
  7. * Author:       Carl Edman
  8. * Created:      Tue Oct 17 23:45:45 1995
  9. * Modified:     Sat Jun 22 19:10:45 1996 (Carl Edman) cedman@capitalist.princeton.edu
  10. * Language:     C
  11. * Package:      N/A
  12. * Status:       Experimental (Do Not Distribute)
  13. *
  14. * (C) Copyright 1995, but otherwise this file is perfect freeware.
  15. *
  16. *******************************************************************************
  17. */
  18.  
  19. #import "SimpleString.h"
  20. #import "EnhanceMail.h"
  21.  
  22. #define START 64
  23.  
  24. #define BYTE_LENGTH(self)  (self->cur - self->data)
  25. #define MUSTGROW(self,len) (self->cur+(len)>=self->data+malloc_size(self->data))
  26.  
  27.  
  28. @implementation SimpleString
  29.  
  30. - init
  31. {
  32.    [super init];
  33.    cur=data=NXZoneMalloc([self zone],START);
  34.    *cur='\0';
  35.    return self;
  36. }
  37.  
  38. - free
  39. {
  40.    free(data);
  41.    return [super free];
  42. }
  43.  
  44. - grow:(int)by
  45. {
  46.    int pos = cur-data;
  47.    int size = malloc_size(data);
  48.  
  49.    do { size *= 2; } while (pos+by >= size);
  50.    data=NXZoneRealloc([self zone],data,size);
  51.    cur=data+pos;
  52.    return self;
  53. }
  54.  
  55. - grow
  56. {
  57.    return [self grow:0];
  58. }
  59.  
  60. - (char *)string
  61. {
  62.    return data;
  63. }
  64.  
  65. - (int)length
  66. {
  67.    return BYTE_LENGTH(self);
  68. }
  69.  
  70. #define iskanji(c) (c & 0x80)
  71.  
  72. - (int)textLength
  73. {
  74.    if (EnhanceUseKanji())
  75.    {
  76.       char *c=data;
  77.       int length=0;
  78.       const int byte_per_mbchar=2;
  79.  
  80.       while (c<cur)
  81.       {
  82.         if (iskanji(*c)) c+=byte_per_mbchar; else c++;
  83.         length++;
  84.       }
  85.       return length;
  86.    }
  87.    return BYTE_LENGTH(self);
  88. }
  89.  
  90. - empty
  91. {
  92.    cur=data;
  93.    *cur='\0';
  94.    return self;
  95. }
  96.  
  97. - (int)appendChar:(char)c
  98. {
  99.    if (MUSTGROW(self,1)) [self grow:1];
  100.    *cur++=c;
  101.    *cur='\0';
  102.    return cur-data;
  103. }
  104.  
  105. - (int)appendString:(const char *)str
  106. {
  107.    return [self appendString:str length:strlen(str)];
  108. }
  109.  
  110. - (int)appendString:(const char *)str length:(int)len
  111. {
  112.    if (MUSTGROW(self,len)) [self grow:len];
  113.    while(len>0) { *cur++=*str++; len--; }
  114.    *cur='\0';
  115.    return cur-data;
  116. }
  117.  
  118. - (int)appendStream:(NXStream *)s
  119. {
  120.    int c;
  121.    
  122.    while ((c=NXRead(s,cur,data+malloc_size(data)-cur))>0)
  123.    {
  124.       cur+=c;
  125.       if (MUSTGROW(self,0)) [self grow];
  126.    }
  127.    
  128.    *cur='\0';
  129.    return cur-data;
  130. }
  131.  
  132. - (int)appendFile:(int)fd
  133. {
  134.    int c;
  135.    
  136.    while ((c=read(fd,cur,data+malloc_size(data)-cur))>0)
  137.    {
  138.       cur+=c;
  139.       if (MUSTGROW(self,0)) [self grow];
  140.    }
  141.    
  142.    *cur='\0';
  143.    return cur-data;
  144. }
  145.  
  146. - (int)writeFile:(int)fd
  147. {
  148.    char *pos;
  149.    int c;
  150.  
  151.    for(pos=data;pos<cur;)
  152.    {
  153.       c=write(fd,pos,cur-pos);
  154.       if (c<0) return c;
  155.       pos+=c;
  156.    }
  157.    
  158.    return cur-data;
  159. }
  160.  
  161. - (int)appendSimpleString:(id)sstr
  162. {
  163.    return [self appendString:[sstr string] length:[sstr length]];
  164. }
  165.  
  166. - (int)appendAndFreeSimpleString:(id)sstr
  167. {
  168.    int ret = [self appendSimpleString:sstr];
  169.    [sstr free];
  170.    return ret;
  171. }
  172.  
  173. - (int)insertString:(const char *)str
  174. {
  175.    return [self insertString:str length:strlen(str) at:0];
  176. }
  177.  
  178. - (int)insertString:(const char *)str length:(int)len
  179. {
  180.    return [self insertString:str length:len at:0];
  181. }
  182.  
  183. - (int)insertString:(const char *)str length:(int)len at:(int)pos
  184. {
  185.    char *p, *d;
  186.    const char *s;
  187.  
  188.    if (MUSTGROW(self,len)) [self grow:len];
  189.    if (pos < 0) p = data;
  190.    else if (pos > cur-data) p = cur;
  191.    else p = data + pos;
  192.    cur += len;
  193.    // shift tail (including terminating '\0'):
  194.    for (d = cur + 1, s = d - len;  s > p; ) *--d = *--s;
  195.    // insert string:
  196.    for (s = str, d = p;  --len >= 0; ) *d++ = *s++;
  197.    return cur-data;
  198. }
  199.  
  200. - (int)lastChar
  201. {
  202.    return (cur>data) ? *(cur-1) : -1;
  203. }
  204.  
  205. @end // SimpleString
  206.