home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / info-service / gopher / Unix / gopher+1.2b4 / object / STRstring.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-24  |  3.5 KB  |  215 lines

  1. /********************************************************************
  2.  * lindner
  3.  * 3.2
  4.  * 1993/03/24 17:07:52
  5.  * /home/mudhoney/GopherSrc/CVS/gopher+/object/STRstring.c,v
  6.  * $Status: $
  7.  *
  8.  * Paul Lindner, University of Minnesota CIS.
  9.  *
  10.  * Copyright 1991, 1992 by the Regents of the University of Minnesota
  11.  * see the file "Copyright" in the distribution for conditions of use.
  12.  *********************************************************************
  13.  * MODULE: STRstring.c
  14.  * Implement dynamic string library functions
  15.  *********************************************************************
  16.  * Revision History:
  17.  * STRstring.c,v
  18.  * Revision 3.2  1993/03/24  17:07:52  lindner
  19.  * STRset with a NULL value will STRinit() the string
  20.  *
  21.  * Revision 3.1.1.1  1993/02/11  18:03:03  lindner
  22.  * Gopher+1.2beta release
  23.  *
  24.  * Revision 1.1  1992/12/10  23:27:52  lindner
  25.  * gopher 1.1 release
  26.  *
  27.  *
  28.  *********************************************************************/
  29.  
  30.  
  31. #include "STRstring.h"
  32. #include "String.h"
  33. #include "Malloc.h"
  34.  
  35. /*
  36.  * Make a new string, however use supplied parameter to set it.
  37.  */
  38. String *
  39. STRnewSet(in)
  40.   char *in;
  41. {
  42.      register String *temp;
  43.      register int len;
  44.  
  45.      temp = (String *) malloc(sizeof(String));
  46.      temp->data = NULL;
  47.  
  48.      if (in == NULL)
  49.       return(temp);
  50.  
  51.      len = strlen(in) + 1;
  52.  
  53.      temp->data = (char *) malloc(len * sizeof(char*));
  54.      strcpy(temp->data, in);
  55.           temp->len = len-1;
  56.  
  57.      return(temp);
  58. }
  59.      
  60. /*
  61.  * Make a new string, don't set anything for default yet.
  62.  */
  63.  
  64. String *
  65. STRnew()
  66. {
  67.      String *temp;
  68.  
  69.      temp = (String *) malloc(sizeof(String));
  70.      temp->data = NULL;
  71.  
  72.      return(temp);
  73. }
  74.  
  75. /*
  76.  * Destroy a string
  77.  */
  78.  
  79. void
  80. STRdestroy(st)
  81.   String *st;
  82. {
  83.      if (st != NULL) {
  84.       if (st->data != NULL)
  85.            free(st->data);
  86.       free(st);
  87.      } else
  88.       perror("STRdestroy: non existant string!\n");
  89.  
  90. }
  91.  
  92.  
  93. /*
  94.  * Clear out all the crud...
  95.  */
  96.  
  97. void 
  98. STRinit(st) 
  99.   String *st;
  100. {
  101.      if (st != NULL) {
  102.       
  103.       st->len  = 0;
  104.       if (st->data != NULL)
  105.            free(st->data);
  106.       st->data = NULL;
  107.      } else
  108.       perror("STRinit, non existant string!");
  109. }
  110.  
  111. /*
  112.  * Set a string value
  113.  */
  114.  
  115. void
  116. STRset(st, str)
  117.   String *st;
  118.   char   *str;
  119. {
  120.      register int len;
  121.  
  122.      if (str == NULL) {
  123.       STRinit(st);
  124.       return;
  125.      }
  126.  
  127.      if (*str == '\0')
  128.       len = 1;
  129.      else
  130.       len = strlen(str) + 1;
  131.  
  132.      /* Uninitialized data... */
  133.  
  134.      if (st->data == NULL) {
  135.       st->data = (char *) malloc(len);
  136.       strcpy(st->data, str);
  137.       st->len = len-1;
  138.      }
  139.  
  140.      /** Something's already there... **/
  141.  
  142.      else {
  143.       if (STRlen(st) > len)
  144.            strcpy(st->data, str);
  145.       else {
  146.            char *temp;
  147.  
  148.            temp = (char *) realloc(st->data, len);
  149.            /*** Should check for NULL ... ***/
  150.            st->data = temp;
  151.            strcpy(st->data, str);
  152.       }
  153.      }
  154. }
  155.  
  156. /*
  157.  * Add a string to the end of the string that's there already
  158.  */
  159.  
  160. String*
  161. STRcat(st, cp)
  162.   String *st;
  163.   char *cp;
  164. {
  165.      int len;
  166.      char *temp;
  167.  
  168.      if (cp == NULL)
  169.       return(NULL);
  170.      
  171.      if (STRlen(st) == 0) {
  172.       STRset(st, cp);
  173.       return(st);
  174.      }
  175.  
  176.      len = strlen(cp) + STRlen(st) + 1;
  177.  
  178.      temp = (char *) malloc(len);
  179.      strcpy(temp, STRget(st));
  180.      strcat(temp, cp);
  181.  
  182.      STRset(st, temp);
  183.      
  184.      free(temp);
  185.  
  186.      return(st);
  187. }
  188.  
  189.  
  190. int
  191. STRcmp(st1, st2)
  192.   String *st1;
  193.   String *st2;
  194. {
  195.      register char *cp1, *cp2;
  196.  
  197.      cp1 = STRget(st1);
  198.      cp2 = STRget(st2);
  199.  
  200.      if (cp1 == NULL) 
  201.       return(- !0);
  202.      else if (cp2 == NULL)
  203.       return( !0);
  204.      else
  205.       return(strcmp(cp1, cp2));
  206. }
  207.      
  208. String*
  209. STRcpy(s1, s2)
  210.   String *s1;
  211.   String *s2;
  212. {
  213.      STRset(s1, STRget(s2));
  214. }
  215.