/*
 * Strings library.
 *
 * Copyright 1990 by the Massachusetts Institute of Technology.
 * 
 * For copying and distribution information, please see the file
 * <mit-copyright.h>.
 *
 * Tom Coppeto
 * MIT Network Group
 * 15 April 1990
 *
 *    $Source: /afs/net.mit.edu/tools/src/simon/RCS/str_utils.c,v $
 *    $Author: tom $
 *    $Locker:  $
 */

#include <rope.h>


Rope 
str_saveString(s, len)
     char *s;
     unsigned int len;
{
  static Rope str;

  bzero(&str, sizeof(str));
  str.s = (char *) malloc(len * sizeof(char));
  if(!str.s)
    return(str);
  str.allocated = str.length = len;
  bcopy(s, str.s, len);
  return(str);
}


Rope
str_addString(s1, s2)
     Rope s1;
     Rope s2;
{
  if(!s1.allocated)
    s1.s = (char *) malloc(s2.length);
  else
    if(s1.allocated < s2.length)
      s1.s = (char *) realloc(s1.s, s2.length);
  
  s1.allocated = s2.allocated;
  s1.length    = s2.length;
  bcopy(s2.s, s1.s, s2.length);
  return(s1);
}


Rope
str_makeString(s)
     char *s;
{
  static Rope str;

  bzero(&str, sizeof(str));
  str.s = s;
  if(s)
    str.length = strlen(s) + 1;
  return(str);
}

Rope
str_makeData(s, length)
     char *s;
     int length;
{
  static Rope str;

  bzero(&str, sizeof(str));
  str.s = s;
  if(s)
    str.length = length;
  return(str);
}


Rope
str_clearString(str)
     Rope str;
{
  str.length = 0;
  if(str.allocated)
    bzero(str.s, str.allocated);
  return(str);
}

void 
str_freeString(str)
     Rope str;
{
  if(str.allocated)
    (void) free(str.s);
  return;
}


char *
str_printString(str)
     Rope str;
{
  if(!str.s || !str.length)
    return("");
  else
    return(str.s);
}
