-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpwb_utilities.c
53 lines (49 loc) · 1.46 KB
/
pwb_utilities.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <string.h>
#include "pwb_utilities.h"
/**
* @brief Returns memory required to save a string, 0 if NULL
*
* Function checks for NULL (returns 0 length in that case),
* and includes room for a terminating \0.
*/
int get_string_saved_len(const char *str)
{
int slen = 0;
if (str && (slen = strlen(str))>0)
++slen;
return slen;
}
/**
* @brief Packs a string with other strings into a block of memory.
*
* Tracks progress packing block through pointer-to-pointer arguments.
* No copy is made if the string is NULL or of zero-length. @p string
* will be set to NULL.
*
* @param "string" pointer to copy of @p value in the block
* @param "buff_pointer" pointer to next available address in the block
* @param "buff_limit" pointer to end of block to prevent overruns
* @param "value" string value to copy into the block
*
* @return true for success, false for failure due to insufficient memory
*/
bool pack_string_in_block(const char **string,
char **buff_pointer,
char *buff_limit,
const char *value)
{
*string = NULL;
int slen;
if (value && (slen = strlen(value)) > 0)
{
// check for enough space
if (buff_limit - *buff_pointer <= slen)
return false;
*string = *buff_pointer;
memcpy(*buff_pointer, value, slen);
(*buff_pointer) += slen;
**buff_pointer = '\0';
(*buff_pointer)++;
}
return true;
}