SDDSlib
Loading...
Searching...
No Matches
cp_str.c
Go to the documentation of this file.
1/**
2 * @file cp_str.c
3 * @brief Implementation of string manipulation functions.
4 *
5 * @copyright
6 * - (c) 2002 The University of Chicago, as Operator of Argonne National Laboratory.
7 * - (c) 2002 The Regents of the University of California, as Operator of Los Alamos National Laboratory.
8 *
9 * @license
10 * This file is distributed under the terms of the Software License Agreement
11 * found in the file LICENSE included with this distribution.
12 *
13 * @author M. Borland, C. Saunders, R. Soliday
14 */
15
16#include "mdb.h"
17
18/**
19 * @brief Copies a string, allocating memory for storage.
20 *
21 * This function duplicates the input string `t` by allocating sufficient memory
22 * and copying its contents. If `t` is `NULL`, the destination pointer `s` is set to `NULL`.
23 *
24 * @param s Pointer to the destination string pointer.
25 * @param t Source string to be copied.
26 * @return The copied string, or `NULL` if `t` is `NULL`.
27 */
28char *cp_str(char **s, char *t) {
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}
37
38/**
39 * @brief Copies a specified number of characters from a string.
40 *
41 * This function duplicates the first `n` characters of the input string `t` by
42 * allocating sufficient memory and copying the specified number of characters.
43 * The copied string is null-terminated. If `t` is `NULL`, the destination pointer `s` is set to `NULL`.
44 *
45 * @param s Pointer to the destination string pointer.
46 * @param t Source string from which to copy characters.
47 * @param n Number of characters to copy.
48 * @return The copied string with up to `n` characters, or `NULL` if `t` is `NULL`.
49 */
50char *cpn_str(char **s, char *t, long n) {
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}
void * tmalloc(uint64_t size_of_block)
Allocates a memory block of the specified size with zero initialization.
Definition array.c:59
char * cpn_str(char **s, char *t, long n)
Copies a specified number of characters from a string.
Definition cp_str.c:50
char * cp_str(char **s, char *t)
Copies a string, allocating memory for storage.
Definition cp_str.c:28
char * strcpy_ss(char *dest, const char *src)
Safely copies a string, handling memory overlap.
Definition str_copy.c:34