Back to index

cell-binutils  2.17cvs20070401
Functions
dyn-string.c File Reference
#include <stdio.h>
#include "libiberty.h"
#include "dyn-string.h"

Go to the source code of this file.

Functions

int dyn_string_init (struct dyn_string *ds_struct_ptr, int space)
dyn_string_t dyn_string_new (int space)
void dyn_string_delete (dyn_string_t ds)
char * dyn_string_release (dyn_string_t ds)
dyn_string_t dyn_string_resize (dyn_string_t ds, int space)
void dyn_string_clear (dyn_string_t ds)
int dyn_string_copy (dyn_string_t dest, dyn_string_t src)
int dyn_string_copy_cstr (dyn_string_t dest, const char *src)
int dyn_string_prepend (dyn_string_t dest, dyn_string_t src)
int dyn_string_prepend_cstr (dyn_string_t dest, const char *src)
int dyn_string_insert (dyn_string_t dest, int pos, dyn_string_t src)
int dyn_string_insert_cstr (dyn_string_t dest, int pos, const char *src)
int dyn_string_insert_char (dyn_string_t dest, int pos, int c)
int dyn_string_append (dyn_string_t dest, dyn_string_t s)
int dyn_string_append_cstr (dyn_string_t dest, const char *s)
int dyn_string_append_char (dyn_string_t dest, int c)
int dyn_string_substring (dyn_string_t dest, dyn_string_t src, int start, int end)
int dyn_string_eq (dyn_string_t ds1, dyn_string_t ds2)

Function Documentation

Definition at line 312 of file dyn-string.c.

{
  if (dyn_string_resize (dest, dest->length + s->length) == 0)
    return 0;
  strcpy (dest->s + dest->length, s->s);
  dest->length += s->length;
  return 1;
}

Here is the call graph for this function:

Definition at line 343 of file dyn-string.c.

{
  /* Make room for the extra character.  */
  if (dyn_string_resize (dest, dest->length + 1) == NULL)
    return 0;
  /* Append the character; it will overwrite the old NUL.  */
  dest->s[dest->length] = c;
  /* Add a new NUL at the end.  */
  dest->s[dest->length + 1] = '\0';
  /* Update the length.  */
  ++(dest->length);
  return 1;
}

Here is the call graph for this function:

int dyn_string_append_cstr ( dyn_string_t  dest,
const char *  s 
)

Definition at line 326 of file dyn-string.c.

{
  int len = strlen (s);

  /* The new length is the old length plus the size of our string, plus
     one for the null at the end.  */
  if (dyn_string_resize (dest, dest->length + len) == NULL)
    return 0;
  strcpy (dest->s + dest->length, s);
  dest->length += len;
  return 1;
}

Here is the call graph for this function:

Definition at line 172 of file dyn-string.c.

{
  /* A dyn_string always has room for at least the NUL terminator.  */
  ds->s[0] = '\0';
  ds->length = 0;
}

Definition at line 184 of file dyn-string.c.

{
  if (dest == src)
    abort ();

  /* Make room in DEST.  */
  if (dyn_string_resize (dest, src->length) == NULL)
    return 0;
  /* Copy DEST into SRC.  */
  strcpy (dest->s, src->s);
  /* Update the size of DEST.  */
  dest->length = src->length;
  return 1;
}

Here is the call graph for this function:

int dyn_string_copy_cstr ( dyn_string_t  dest,
const char *  src 
)

Definition at line 204 of file dyn-string.c.

{
  int length = strlen (src);
  /* Make room in DEST.  */
  if (dyn_string_resize (dest, length) == NULL)
    return 0;
  /* Copy DEST into SRC.  */
  strcpy (dest->s, src);
  /* Update the size of DEST.  */
  dest->length = length;
  return 1;
}

Here is the call graph for this function:

Definition at line 108 of file dyn-string.c.

{
  free (ds->s);
  free (ds);
}

Here is the call graph for this function:

Definition at line 390 of file dyn-string.c.

{
  /* If DS1 and DS2 have different lengths, they must not be the same.  */
  if (ds1->length != ds2->length)
    return 0;
  else
    return !strcmp (ds1->s, ds2->s);
}

Here is the call graph for this function:

int dyn_string_init ( struct dyn_string ds_struct_ptr,
int  space 
)

Definition at line 59 of file dyn-string.c.

{
  /* We need at least one byte in which to store the terminating NUL.  */
  if (space == 0)
    space = 1;

#ifdef RETURN_ON_ALLOCATION_FAILURE
  ds_struct_ptr->s = (char *) malloc (space);
  if (ds_struct_ptr->s == NULL)
    return 0;
#else
  ds_struct_ptr->s = XNEWVEC (char, space);
#endif
  ds_struct_ptr->allocated = space;
  ds_struct_ptr->length = 0;
  ds_struct_ptr->s[0] = '\0';

  return 1;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int dyn_string_insert ( dyn_string_t  dest,
int  pos,
dyn_string_t  src 
)

Definition at line 244 of file dyn-string.c.

{
  int i;

  if (src == dest)
    abort ();

  if (dyn_string_resize (dest, dest->length + src->length) == NULL)
    return 0;
  /* Make room for the insertion.  Be sure to copy the NUL.  */
  for (i = dest->length; i >= pos; --i)
    dest->s[i + src->length] = dest->s[i];
  /* Splice in the new stuff.  */
  strncpy (dest->s + pos, src->s, src->length);
  /* Compute the new length.  */
  dest->length += src->length;
  return 1;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int dyn_string_insert_char ( dyn_string_t  dest,
int  pos,
int  c 
)

Definition at line 291 of file dyn-string.c.

{
  int i;

  if (dyn_string_resize (dest, dest->length + 1) == NULL)
    return 0;
  /* Make room for the insertion.  Be sure to copy the NUL.  */
  for (i = dest->length; i >= pos; --i)
    dest->s[i + 1] = dest->s[i];
  /* Add the new character.  */
  dest->s[pos] = c;
  /* Compute the new length.  */
  ++dest->length;
  return 1;
}

Here is the call graph for this function:

int dyn_string_insert_cstr ( dyn_string_t  dest,
int  pos,
const char *  src 
)

Definition at line 269 of file dyn-string.c.

{
  int i;
  int length = strlen (src);

  if (dyn_string_resize (dest, dest->length + length) == NULL)
    return 0;
  /* Make room for the insertion.  Be sure to copy the NUL.  */
  for (i = dest->length; i >= pos; --i)
    dest->s[i + length] = dest->s[i];
  /* Splice in the new stuff.  */
  strncpy (dest->s + pos, src, length);
  /* Compute the new length.  */
  dest->length += length;
  return 1;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 86 of file dyn-string.c.

{
  dyn_string_t result;
#ifdef RETURN_ON_ALLOCATION_FAILURE
  result = (dyn_string_t) malloc (sizeof (struct dyn_string));
  if (result == NULL)
    return NULL;
  if (!dyn_string_init (result, space))
    {
      free (result);
      return NULL;
    }
#else
  result = XNEW (struct dyn_string);
  dyn_string_init (result, space);
#endif
  return result;
}

Here is the call graph for this function:

Definition at line 223 of file dyn-string.c.

{
  return dyn_string_insert (dest, 0, src);
}

Here is the call graph for this function:

int dyn_string_prepend_cstr ( dyn_string_t  dest,
const char *  src 
)

Definition at line 233 of file dyn-string.c.

{
  return dyn_string_insert_cstr (dest, 0, src);
}

Here is the call graph for this function:

Definition at line 119 of file dyn-string.c.

{
  /* Store the old buffer.  */
  char* result = ds->s;
  /* The buffer is no longer owned by DS.  */
  ds->s = NULL;
  /* Delete DS.  */
  free (ds);
  /* Return the old buffer.  */
  return result;
}

Here is the call graph for this function:

Definition at line 139 of file dyn-string.c.

{
  int new_allocated = ds->allocated;

  /* Increase SPACE to hold the NUL termination.  */
  ++space;

  /* Increase allocation by factors of two.  */
  while (space > new_allocated)
    new_allocated *= 2;
    
  if (new_allocated != ds->allocated)
    {
      ds->allocated = new_allocated;
      /* We actually need more space.  */
#ifdef RETURN_ON_ALLOCATION_FAILURE
      ds->s = (char *) realloc (ds->s, ds->allocated);
      if (ds->s == NULL)
       {
         free (ds);
         return NULL;
       }
#else
      ds->s = XRESIZEVEC (char, ds->s, ds->allocated);
#endif
    }

  return ds;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int dyn_string_substring ( dyn_string_t  dest,
dyn_string_t  src,
int  start,
int  end 
)

Definition at line 364 of file dyn-string.c.

{
  int i;
  int length = end - start;

  if (start > end || start > src->length || end > src->length)
    abort ();

  /* Make room for the substring.  */
  if (dyn_string_resize (dest, length) == NULL)
    return 0;
  /* Copy the characters in the substring,  */
  for (i = length; --i >= 0; )
    dest->s[i] = src->s[start + i];
  /* NUL-terimate the result.  */
  dest->s[length] = '\0';
  /* Record the length of the substring.  */
  dest->length = length;

  return 1;
}

Here is the call graph for this function: