Defines | Functions

dstring_sprintf.c File Reference

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include "utility.h"
#include "xnprintf.h"
Include dependency graph for dstring_sprintf.c:

Go to the source code of this file.

Defines

#define DO_FREE   1
#define DO_NOT_FREE   0

Functions

char * dstring_sprintf (const char *fmt,...)
char * dstring_append_common (char *s1, char *s2, int do_free)
char * dstring_append (char *s1, char *s2)
char * dstring_append_free (char *s1, char *s2)

Detailed Description

This file contains dynamic string handling functions.

Definition in file dstring_sprintf.c.


Define Documentation

#define DO_FREE   1

Definition at line 43 of file dstring_sprintf.c.

#define DO_NOT_FREE   0

Definition at line 44 of file dstring_sprintf.c.


Function Documentation

char* dstring_append ( char *  s1,
char *  s2 
)

Appends s2 to s1 without freeing s2.

Parameters:
s1 -- the string to which we will append
s2 -- the string to append to s1
Returns:
pointer to the new appended string

Definition at line 76 of file dstring_sprintf.c.

{
  return(dstring_append_common(s1,s2,DO_NOT_FREE));
}

Here is the call graph for this function:

Here is the caller graph for this function:

char* dstring_append_common ( char *  s1,
char *  s2,
int  do_free 
)

Common routine for the dstring_append functions. Appends s2 to s1.

Parameters:
s1 -- the string to which we will append
s2 -- the string to append to s1
do_free -- if TRUE, free s2 after appending. otherwise, do not free s2.
Returns:
pointer to the new appended string

Definition at line 58 of file dstring_sprintf.c.

{
  if ((s1 = realloc(s1, strlen(s1)+strlen(s2)+1)) == NULL) 
    return NULL;
  s1 = strcat(s1,s2);
  if (do_free && s2!=NULL) free(s2);
  return(s1);
}

Here is the caller graph for this function:

char* dstring_append_free ( char *  s1,
char *  s2 
)

Appends s2 to s1 and frees s2.

Parameters:
s1 -- the string to which we will append
s2 -- the string to append to s1
Returns:
pointer to the new appended string

Definition at line 90 of file dstring_sprintf.c.

{
  return(dstring_append_common(s1,s2,DO_FREE));
}

Here is the call graph for this function:

Here is the caller graph for this function:

char* dstring_sprintf ( const char *  fmt,
  ... 
)

Taken from the Linux man page of snprintf. To allocate a sufficiently large string and print into it (code correct for both glibc 2.0 and glibc 2.1). This string needs to be deallocated manually.

Parameters:
fmt -- the format string (same as printf)
... -- the arguments to be printed
Returns:
a dynamically allocated string containing the output of the sprintf.

Definition at line 31 of file dstring_sprintf.c.

                                            {
  char *buf;
  int status;
  va_list ap;

  va_start (ap, fmt);
  status = vasprintf (&buf, fmt, ap);
  va_end (ap);
  
  return(buf);
}