Browse Source

adding string handling

doc_update_sequoia
Volker Birk 5 years ago
parent
commit
b88730691b
2 changed files with 104 additions and 0 deletions
  1. +52
    -0
      src/pEp_string.c
  2. +52
    -0
      src/pEp_string.h

+ 52
- 0
src/pEp_string.c View File

@ -0,0 +1,52 @@
// This file is under GNU General Public License 3.0
// see LICENSE.txt
#include "pEp_string.h"
#include <stdlib.h>
#include <assert.h>
DYNAMIC_API char * new_string(const char *src, size_t len)
{
assert(src || len);
if (!(src || len))
return NULL;
char *s = NULL;
if (src) {
if (len)
s = strndup(src, len);
else
s = strdup(src);
assert(s);
}
else {
s = calloc(1, len);
assert(s);
}
return s;
}
DYNAMIC_API void free_string(char *s)
{
free(s);
}
DYNAMIC_API char * string_dup(const char *src, size_t len)
{
assert(src);
if (!src)
return NULL;
char *s = NULL;
if (len)
s = strndup(src, len);
else
s = strdup(src);
assert(s);
return s;
}

+ 52
- 0
src/pEp_string.h View File

@ -0,0 +1,52 @@
// This file is under GNU General Public License 3.0
// see LICENSE.txt
#pragma once
#include <string.h>
#include "dynamic_api.h"
#ifdef __cplusplus
extern "C" {
#endif
// new_string() - allocate a new string
//
// parameters:
// src (in) string to copy or NULL
// len (in) length of newly created string or 0 for default
//
// return value:
// pointer to string object or NULL if out of memory
//
// caveat:
// one of the two parameters has to be set at least
DYNAMIC_API char * new_string(const char *src, size_t len);
// free_string() - free memory occupied by string
//
// parameters:
// ts (in) pointer to string to free
DYNAMIC_API void free_string(char *s);
// string_dup() - duplicate a string
//
// parameters:
// src (in) string to duplicate
// len (in) length of newly created string or 0 for default
//
// return value:
// pointer to copy or NULL if out of memory
DYNAMIC_API char * string_dup(const char *src, size_t len);
#ifdef __cplusplus
}
#endif

Loading…
Cancel
Save