Mercurial > hg > dmlib
annotate dmstring.c @ 744:2726d91e3409
Add implementation of dm_strndup().
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sat, 04 May 2013 03:25:29 +0300 |
parents | 32250b436bca |
children |
rev | line source |
---|---|
0 | 1 #include "dmlib.h" |
2 #include <stdarg.h> | |
3 | |
744
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
4 /* Implementation of strdup() with a NULL check |
0 | 5 */ |
6 char *dm_strdup(const char *s) | |
7 { | |
8 char *res; | |
9 if (s == NULL) | |
10 return NULL; | |
11 | |
12 if ((res = dmMalloc(strlen(s) + 1)) == NULL) | |
13 return NULL; | |
14 | |
15 strcpy(res, s); | |
16 return res; | |
17 } | |
18 | |
19 | |
744
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
20 /* Implementation of strndup() with NULL check |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
21 */ |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
22 char *dm_strndup(const char *s, const size_t n) |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
23 { |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
24 char *res; |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
25 if (s == NULL) |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
26 return NULL; |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
27 |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
28 size_t len = strlen(s); |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
29 if (len > n) |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
30 len = n; |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
31 |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
32 if ((res = dmMalloc(len + 1)) == NULL) |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
33 return NULL; |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
34 |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
35 memcpy(res, s, len); |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
36 res[len] = 0; |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
37 |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
38 return res; |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
39 } |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
40 |
2726d91e3409
Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
41 |
0 | 42 /* Simulate a sprintf() that allocates memory |
43 */ | |
44 char *dm_strdup_vprintf(const char *fmt, va_list args) | |
45 { | |
46 int size = 64; | |
47 char *buf; | |
48 | |
49 if ((buf = dmMalloc(size)) == NULL) | |
50 return NULL; | |
51 | |
52 while (1) | |
53 { | |
54 int n; | |
55 va_list ap; | |
56 va_copy(ap, args); | |
57 n = vsnprintf(buf, size, fmt, ap); | |
58 va_end(ap); | |
59 | |
60 if (n > -1 && n < size) | |
61 return buf; | |
62 if (n > -1) | |
63 size = n + 1; | |
64 else | |
65 size *= 2; | |
66 | |
67 if ((buf = dmRealloc(buf, size)) == NULL) | |
68 return NULL; | |
69 } | |
70 } | |
71 | |
72 | |
73 char *dm_strdup_printf(const char *fmt, ...) | |
74 { | |
75 char *res; | |
76 va_list ap; | |
77 | |
78 va_start(ap, fmt); | |
79 res = dm_strdup_vprintf(fmt, ap); | |
80 va_end(ap); | |
81 | |
82 return res; | |
83 } |