home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / me34src.zip / me3 / util / dstring.c < prev    next >
C/C++ Source or Header  |  1995-01-14  |  3KB  |  132 lines

  1. /* dstring.c: dynamic strings.
  2.  * C Durland    Public Domain
  3.  */
  4.  
  5. #include "const.h"
  6. #include "dstring.h"
  7.  
  8. extern char *malloc(), *realloc(), *strcpy();
  9.  
  10. static char empty_string[1] = { '\0' };
  11.  
  12.     /* Note:  You must initialize a dString.  If it is declared as a
  13.      *   global var, it is initialized enough if you set it before you
  14.      *   get it.
  15.      */
  16. void init_dString(ds) register dString *ds;
  17. {
  18.   ds->size = 0; ds->string = empty_string;
  19. }
  20.  
  21.     /* Returns: TRUE is ok, FALSE if could not malloc (string set to
  22.      *   empty_string).
  23.      */
  24. set_dString(ds,text) register dString *ds; char *text;
  25. {
  26.   if (!pad_dString(ds,strlen(text))) return FALSE;
  27.  
  28.   strcpy(ds->string,text);
  29.  
  30.   return TRUE;
  31. }
  32.  
  33. void free_dString(ds) register dString *ds;
  34. {
  35.   if (ds->size) free(ds->string);
  36.   init_dString(ds);
  37. }
  38.  
  39.     /* Make sure dString has enough space in it to hold a string of length
  40.      *   len.  Also leave room for the \0.
  41.      * Notes:
  42.      *   dString contents are sometimes trashed by this routine.
  43.      *   The location of the text might be moved so don't say 
  44.      *     ptr = ds->string, pad_dString(ds) and expect ptr to be valid!
  45.      *   Some routines will want the orginal contents to be intact after the
  46.      *     pad so that can do things like strcat().
  47.      *   !!!Under Ansi C, I'll need to free the dstring if the realloc()
  48.      *     fails.  Of course this doesn't work everywhere so I'll blow it
  49.      *     off.
  50.      * Input:
  51.      *   ds  : Pointer to dString that needs some room
  52.      *   len : Strlen of string that ds needs to be able to hold.
  53.      * Output:
  54.      *   ds : Expanded dString.  Freed and initialized if no memory (ie old
  55.      *     contents are gone).
  56.      * Returns:
  57.      *   TRUE:   Everything went as expected.
  58.      *   FALSE:  Out of memory.
  59.      */
  60. pad_dString(ds,len) register dString *ds; int len;
  61. {
  62.   char *ptr;
  63.   int size = ds->size;
  64.  
  65.   len++;
  66.   if (len <= size) return TRUE;
  67.  
  68.   if (size)        /* dString has already been malloc()ed */
  69.     ptr = realloc(ds->string, len);
  70.   else  ptr = malloc(len);
  71.  
  72.   if (!ptr) { init_dString(ds); return FALSE; }
  73.  
  74.   ds->size = len;
  75.   ds->string = ptr;
  76.  
  77.   return TRUE;
  78. }
  79.  
  80. #if 0
  81. char *malloc(), *realloc(), *strcat();
  82. cat_dString(ds,string) register dString *ds; char *string;
  83. {
  84.   register char *ptr = ds->string;
  85.   register int n = strlen(string) +1, size = ds->size;
  86.  
  87.   if (size<n)    /* expand string */
  88.   {
  89.     ptr = size ? realloc(ptr,n) : malloc(n);
  90.     if (ptr == NULL) { init_dString(ds); return 0; }
  91.     if (!size) *ptr = '\0';
  92.     ds->size = n;
  93.   }
  94.   ds->string = strcat(ptr,string);
  95.   return 1;
  96. }
  97. #endif
  98.  
  99.  
  100. #ifdef TEST
  101. /* ******************************************************************** */
  102. /* ***********  TESTING *********************************************** */
  103. /* ******************************************************************** */
  104.  
  105. #include <stdio.h>
  106.  
  107. main()
  108. {
  109.   char buf[200];
  110.   dString ds;
  111.  
  112.   init_dString(&ds);
  113.   while (1)
  114.   {
  115.     printf(">>>>s(set string), p(print string), q(quit): ");
  116.     gets(buf); puts("");
  117.     switch (*buf)
  118.     {
  119.       case 's':    /* set string */
  120.     printf("string: "); gets(buf); puts("");
  121.     set_dString(&ds,buf);
  122.     break;
  123.       case 'p':    /* print string */
  124.     printf(">%s<\n",ds.string);
  125.     break;
  126.       case 'q': goto done;
  127.     }
  128.   }
  129.   done: ;
  130. }
  131. #endif
  132.