home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume26 / mytinfo / part01 / addstr.c next >
C/C++ Source or Header  |  1992-12-26  |  2KB  |  89 lines

  1. /*
  2.  * addstr.c
  3.  *
  4.  * By Ross Ridge
  5.  * Public Domain
  6.  * 92/02/01 07:34:01
  7.  *
  8.  */
  9.  
  10. #include "defs.h"
  11. #include "term.h"
  12.  
  13. #include <ctype.h>
  14.  
  15. #ifdef USE_SCCS_IDS
  16. static char const SCCSid[] = "@(#) mytinfo addstr.c 3.2 92/02/01 public domain, By Ross Ridge";
  17. #endif
  18. /*
  19.  * I think this routine could be improved, as it is now it searches a
  20.  * linked list of strbufs for one that has enough room left for the
  21.  * string. The only thing else I can think of doing would be to 
  22.  * expand a buffer by realloc and then fix the string pointers if it
  23.  * moves.
  24.  */
  25.  
  26. static struct strbuf *strbuf = NULL;
  27.    
  28. struct strbuf *
  29. _endstr() {
  30.     register struct strbuf *p;
  31.  
  32.     p = strbuf;
  33.     strbuf = NULL;
  34.     return p;
  35. }
  36.  
  37. char *
  38. _addstr(s)
  39. register char *s; {
  40.     register struct strbuf *p;
  41.     register int l;
  42.  
  43.     if (s == NULL) {
  44.         strbuf = NULL;
  45.         return NULL;
  46.     }
  47.  
  48.     if (strbuf == NULL) {
  49.         strbuf = (struct strbuf *) malloc(sizeof(struct strbuf));
  50.         if (strbuf == NULL)
  51.             return NULL;
  52.         strbuf->len = 0;
  53.         strbuf->next = NULL;
  54.     }
  55.     l = strlen(s) + 1;
  56.     if (l > MAX_CHUNK)
  57.         return NULL;
  58.     p = strbuf;
  59.     while (l + p->len > MAX_CHUNK) {
  60.         if (p->next == NULL) {
  61.             p->next = (struct strbuf *)
  62.                     malloc(sizeof(struct strbuf));
  63.             p = p->next;
  64.             if (p == NULL)
  65.                 return NULL;
  66.             p->len = 0;
  67.             p->next = NULL;
  68.             break;
  69.         }
  70.         p = p->next;
  71.     }
  72.     s = strcpy(p->buf + p->len, s);
  73.     p->len += l;
  74.     return s;
  75. }
  76.  
  77. void
  78. _del_strs(p)
  79. TERMINAL *p; {
  80.     struct strbuf *q;
  81.  
  82.     q = p->strbuf;
  83.     while(q != NULL) {
  84.         p->strbuf = q->next; 
  85.         free((anyptr) q);
  86.         q = p->strbuf;
  87.     }
  88. }
  89.