1 /*****************************************************************************\
3 * | || | ___ | |_ _ __ | | _ _ __ _ |_ ) *
4 * | __ |/ _ \| _|| '_ \| || || |/ _` | / / *
5 * |_||_|\___/ \__|| .__/|_| \_,_|\__, |/___| *
7 \*****************************************************************************/
12 #include "mem_utils.h"
13 #include "parser_utils.h"
16 * Creates a newly allocated null-terminated string representing line
17 * starting at a given pointer and ending at the closest newline. If
18 * no newline present, returns NULL. TODO, use dup_token
21 * @2 Pointer where the end position is returned
23 * Returns: Newly allocated string containing the line or NULL
25 char *dup_line(char *start
, char **nptr
) {
28 ptr
= strchr(start
, '\n');
32 rv
= xmalloc(ptr
- start
+ 1);
33 memcpy(rv
, start
, ptr
- start
);
43 * Returns a token delimited by the given function.
46 * @2 Pointer where the end position is returned
47 * @3 Function that identifies the delimiter characters
49 * Returns: Newly allocated string containing the token or NULL
51 char *dup_token(char *start
, char **nptr
, int (*isdelimiter
)(int)) {
54 while (isdelimiter(*start
) && *start
)
59 while (!isdelimiter(*ptr
) && *ptr
)
65 rv
= xmalloc(ptr
- start
+ 1);
66 memcpy(rv
, start
, ptr
- start
);
70 while (isdelimiter(*ptr
))
79 * Returns the last token delimited by the given function.
81 * @1 Starting pointer of the whole string
82 * @2 Starting position
83 * @3 Pointer where the end position is returned
84 * @4 Function that identifies the delimiter characters
86 * Returns: Newly allocated string containing the token or NULL
88 char *dup_token_r(char *start
, char *start_string
, char **nptr
, int (*isdelimiter
)(int)) {
91 if (start
<= start_string
)
94 while (isdelimiter(*start
) && (start
> start_string
))
97 if (start
< start_string
)
102 while (!isdelimiter(*ptr
) && (ptr
> start_string
))
105 if (ptr
<= start_string
)
110 rv
= xmalloc(start
- ptr
+ 2);
111 memcpy(rv
, ptr
, start
- ptr
+ 1);
112 rv
[start
- ptr
+ 1] = '\0';
116 while ((ptr
> start_string
) && isdelimiter(*ptr
))
119 if (ptr
< start_string
)