-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsplit.c
More file actions
68 lines (55 loc) · 2.32 KB
/
Copy pathsplit.c
File metadata and controls
68 lines (55 loc) · 2.32 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*******************************************************************************
* File: split.c
* Created: 2024-04-03
*
* Authors:
* Tyler Matijevich
*
* License:
* This file split.c is part of the IecString project
* released under the MIT license agreement.
******************************************************************************/
#include "main.h"
/* Split source into tokens */
int32_t IecStringSplit(char *destination, uint32_t size, char *source,
char *delimeters, uint32_t *address)
{
/* Gaurd null pointers */
if (!destination || !source || !delimeters)
return IECSTRING_ERROR_NULL;
/* Check for zero size */
if (!size)
return IECSTRING_ERROR_SIZE_ZERO;
/* Check if source overlaps destination size */
if (destination <= source && source < destination + size)
return IECSTRING_ERROR_OVERLAP;
/* Check if destination overlaps source length */
size_t length = strlen(source);
if (source <= destination && destination <= source + length)
return IECSTRING_ERROR_OVERLAP;
/* Check if delimeters overlaps destination size */
if (destination <= delimeters && delimeters < destination + size)
return IECSTRING_ERROR_OVERLAP;
/* Check if destination overlaps delimeters length */
length = strlen(delimeters);
if (delimeters <= destination && destination <= delimeters + length)
return IECSTRING_ERROR_OVERLAP;
/* Return the number of characters in the initial portion containing
only delimeters */
size_t length_before = strspn(source, delimeters);
/* Return the number of characters before the first occurrence
of any delimeter */
size_t token_length = strcspn(source + length_before, delimeters);
/* Copy the safe and optimal number of characters */
size_t minimum = MIN(size - 1, token_length);
strncpy(destination, source + length_before, minimum);
/* Add null terminator */
destination[minimum] = '\0';
/* Determine the delimeter length after and set the next read address */
char* read = source + length_before + token_length;
read += strspn(read, delimeters);
if (address)
*address = *read ? (uint32_t)read : 0;
/* Truncated if token length exceeds size */
return IECSTRING_WARNING_TRUNCATE * (token_length > size - 1);
}