-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.c
109 lines (90 loc) · 2.62 KB
/
util.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "driver.h"
#include "util.h"
/**
* Copy the first @len - 1 bytes of @src (will also terminate at '\0') to @dst.
* @dst will be null-terminated anyway.
* Notice that @len refers to the length of @dst. So this method is usually used for returning information in application's buffer.
* The address of @dst will be returned.
*/
char *my_strncpy_out_fn(char *dst, const char *src, int len)
{
my_strncpy_out(dst, src, len);
return dst;
}
/**
* Copy the first @len bytes of @src (will also terminate at '\0') to @dst.
* @dst will be null-terminated anyway.
* Notice that @len refers to the length of @src. So this method is usually used for copying application's buffer to driver-internal buffer.
* The address of @dst will be returned.
*/
char *my_strncpy_in_fn(char *dst, int len_dst, const char *src, int len_src)
{
my_strncpy_in(dst, len_dst, src, len_src);
return dst;
}
/**
* copy @s to @buf.
* Notice: If any memory allocation failure, @buf will not be eliminated.
* The caller will decide if @buf should be discarded.
*
* @s - source
* @len - length of @s (or SQL_NTS, in this case, @len is computed as strlen(@s))
* @buf - destination. If @buf is null, allocate it and return the addr of the newly allocated memory.
*/
char *make_string(const char *s, int len, char *buf)
{
char *str;
if(!s) return NULL;
if((len < 0 && len != SQL_NTS) ||
(len == SQL_NTS && (len = (int)strlen(s)) <= 0))
return NULL;
++len; /* an extra char for \0. */
if(buf) {
if((str = realloc(buf, strlen(buf) + len + 1)) == NULL)
return NULL;
/* append @s to @buf */
strcat(str, s);
return str;
}
if((str = my_malloc(char, len)) == NULL)
return NULL;
my_strncpy_out(str, s, len);
return str;
}
/**
* Split a string by @delim.
* The usage is the same as strtok. Except it accepts a char instead of a char *.
*/
char *chrtok(char *str, const char delim)
{
static char *src = NULL;
char *p, *ret = 0;
if (str != NULL)
src = str;
if (src == NULL)
return NULL;
if ((p = strchr(src, delim)) != NULL) {
*p = 0;
ret = src;
src = ++p;
}
return ret;
}
/**
* Thread-safe version of chrtok.
* The usage is the same as strtok. Except it accepts a char instead of a char *.
*/
char *chrtok_r(char *str, const char delim, char **last)
{
char *src = NULL;
char *p, *ret = 0;
if(str != NULL) src = str;
else src = *last;
if(src == NULL) return NULL;
if ((p = strchr(src, delim)) != NULL) {
*p = 0;
ret = src;
*last = ++p;
}
return ret;
}