SDDSlib
Loading...
Searching...
No Matches
cp_str.c File Reference

Implementation of string manipulation functions. More...

#include "mdb.h"

Go to the source code of this file.

Functions

char * cp_str (char **s, char *t)
 Copies a string, allocating memory for storage.
 
char * cpn_str (char **s, char *t, long n)
 Copies a specified number of characters from a string.
 

Detailed Description

Implementation of string manipulation functions.

License
This file is distributed under the terms of the Software License Agreement found in the file LICENSE included with this distribution.
Author
M. Borland, C. Saunders, R. Soliday

Definition in file cp_str.c.

Function Documentation

◆ cp_str()

char * cp_str ( char ** s,
char * t )

Copies a string, allocating memory for storage.

This function duplicates the input string t by allocating sufficient memory and copying its contents. If t is NULL, the destination pointer s is set to NULL.

Parameters
sPointer to the destination string pointer.
tSource string to be copied.
Returns
The copied string, or NULL if t is NULL.

Definition at line 28 of file cp_str.c.

28 {
29 if (t == NULL)
30 *s = NULL;
31 else {
32 *s = tmalloc(sizeof(*t) * (strlen(t) + 1));
33 strcpy_ss(*s, t);
34 }
35 return (*s);
36}
void * tmalloc(uint64_t size_of_block)
Allocates a memory block of the specified size with zero initialization.
Definition array.c:59
char * strcpy_ss(char *dest, const char *src)
Safely copies a string, handling memory overlap.
Definition str_copy.c:34

◆ cpn_str()

char * cpn_str ( char ** s,
char * t,
long n )

Copies a specified number of characters from a string.

This function duplicates the first n characters of the input string t by allocating sufficient memory and copying the specified number of characters. The copied string is null-terminated. If t is NULL, the destination pointer s is set to NULL.

Parameters
sPointer to the destination string pointer.
tSource string from which to copy characters.
nNumber of characters to copy.
Returns
The copied string with up to n characters, or NULL if t is NULL.

Definition at line 50 of file cp_str.c.

50 {
51 if (t == NULL)
52 *s = NULL;
53 else {
54 *s = tmalloc(sizeof(*t) * (n + 1));
55 strncpy(*s, t, n);
56 (*s)[n] = 0;
57 }
58 return (*s);
59}