NMD
Data Structures | Functions
nmdutil.c File Reference
#include <stdlib.h>
#include "nmd.h"
#include "nmd_impl.h"
#include "string.h"
Include dependency graph for nmdutil.c:

Go to the source code of this file.

Data Structures

struct  NMD_string_

Functions

static NMDErrorCode NMDStringCreateOfSize (int n, NMD_string *r_str)
NMDErrorCode NMDStringCreate (const char *txt, NMD_string *r_str)
NMDErrorCode NMDStringDestroy (NMD_string str)
NMDErrorCode NMDStringGetString (NMD_string str, const char **t)
NMDErrorCode NMDStringConcat (char s1, NMD_string str1, char s2, NMD_string str2, char s3, NMD_string *r_str)
NMDErrorCode NMDStringAppend (char s1, NMD_string *str1, char s2, NMD_string str2, char s3)

Function Documentation

NMDErrorCode NMDStringAppend ( char  s1,
NMD_string str1,
char  s2,
NMD_string  str2,
char  s3 
)

A version of NMDStringConcat() that appends to string 1, rather than creating a new string.

Definition at line 121 of file nmdutil.c.

References CHECKHASNMDCOOKIE, NMDStringConcat(), and NMDStringDestroy().

Referenced by NMDObjectDumpToMySQL().

{
  NMD_string nnew; NMDErrorCode ierr;
  CHECKHASNMDCOOKIE(*str1);
  CHECKHASNMDCOOKIE(str2);
  ierr = NMDStringConcat(s1,*str1,s2,str2,s3,&nnew); NMD_ERR_RETURN(ierr);
  ierr = NMDStringDestroy(*str1); NMD_ERR_RETURN(ierr);
  *str1 = nnew;
  return 0;
}

Here is the call graph for this function:

NMDErrorCode NMDStringConcat ( char  s1,
NMD_string  str1,
char  s2,
NMD_string  str2,
char  s3,
NMD_string r_str 
)

Concatenate string objects, with delimiter characters before, after, in between. All delimiters, and the second string, can be null.

Definition at line 74 of file nmdutil.c.

References CHECKHASNMDCOOKIE, NMD_string_::n, NMDStringCreateOfSize(), and NMD_string_::t.

Referenced by NMDObjectDumpToMySQL(), and NMDStringAppend().

{
  NMD_string str; NMDErrorCode ierr; int l;
  CHECKHASNMDCOOKIE(str1);

  l = str1->n + ( str2 ? str2->n : 0 ) + (s1!=0) + (s2!=0) + (s3!=0);
  ierr = NMDStringCreateOfSize(l,&str); NMD_ERR_RETURN(ierr);

  l = 0;
  if (s1) {
    if (l+1>str->n) NMD_ERR_REPORT("string overflow");
    memcpy(str->t+l,&s1,1);
  }

  l = strlen(str->t);
  if (l+str1->n>str->n) NMD_ERR_REPORT("string overflow");
  memcpy(str->t+l,str1->t,str1->n);

  if (s2) {
    CHECKHASNMDCOOKIE(str2);
    l = strlen(str->t);
    if (l+1>str->n) NMD_ERR_REPORT("string overflow");
    memcpy(str->t+l,&s2,1);
  }

  if (str2) {
    l = strlen(str->t);
    if (l+str2->n>str->n) NMD_ERR_REPORT("string overflow");
    memcpy(str->t+l,str2->t,str2->n);
  }

  if (s3) {
    l = strlen(str->t);
    if (l+1>str->n) NMD_ERR_REPORT("string overflow");
    memcpy(str->t+l,&s3,1);
  }

  *r_str = str;
  return 0;
}

Here is the call graph for this function:

NMDErrorCode NMDStringCreate ( const char *  txt,
NMD_string r_str 
)

Create a string object around a C string; the C string is copied, so it can be freed by the calling environment.

Definition at line 36 of file nmdutil.c.

References NMDStringCreateOfSize(), and NMD_string_::t.

Referenced by NMDObjectDumpToMySQL().

{
  NMD_string str; NMDErrorCode ierr; int l;
  l = strlen(txt);
  ierr = NMDStringCreateOfSize(l,&str); NMD_ERR_RETURN(ierr);
  if (l) memcpy(str->t,txt,l);
  *r_str = str;
  return 0;
}

Here is the call graph for this function:

static NMDErrorCode NMDStringCreateOfSize ( int  n,
NMD_string r_str 
) [static]

Internal auxiliary function for creating a string object of a given length. Zero length is allowed.

Definition at line 21 of file nmdutil.c.

References NMD_string_::cookie, NMD_string_::n, NMD_MALLOC, NMDCOOKIE, and NMD_string_::t.

Referenced by NMDStringConcat(), and NMDStringCreate().

{
  NMD_string str; char *t;
  NMD_MALLOC(t,n+1,char,"nmd string");
  NMD_MALLOC(str,1,struct NMD_string_,"string object");
  str->n = n; str->t = t; str->cookie = NMDCOOKIE;
  *r_str = str;
  return 0;
}
NMDErrorCode NMDStringDestroy ( NMD_string  str)

Destroy a string object, and free the stored string.

Definition at line 49 of file nmdutil.c.

References CHECKHASNMDCOOKIE, NMD_FREE, and NMD_string_::t.

Referenced by NMDObjectDumpToMySQL(), and NMDStringAppend().

{
  CHECKHASNMDCOOKIE(str);
  NMD_FREE(str->t); 
  NMD_FREE(str);
  return 0;
}
NMDErrorCode NMDStringGetString ( NMD_string  str,
const char **  t 
)

Return a pointer to the string in a string object

Definition at line 60 of file nmdutil.c.

References CHECKHASNMDCOOKIE, and NMD_string_::t.

Referenced by NMDObjectDumpToMySQL().

{
  CHECKHASNMDCOOKIE(str);
  *t = str->t;
  return 0;
}