annotate src/dmstring.c @ 1315:7687412f9aef

Fix jssmod sample conversion flags storing .. urgh.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 20 Aug 2017 01:54:54 +0300
parents e06abfde6c39
children 47fe47f01fea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 #include "dmlib.h"
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 #include <stdarg.h>
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3
813
b0cd28b6c9f3 Add new utility functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
4
b0cd28b6c9f3 Add new utility functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
5 /* Returns the filename and path without the last filename extension,
b0cd28b6c9f3 Add new utility functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
6 * E.g. everything before the last '.', if any.
b0cd28b6c9f3 Add new utility functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
7 */
b0cd28b6c9f3 Add new utility functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
8 char *dm_basefilename(const char *filename)
b0cd28b6c9f3 Add new utility functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
9 {
b0cd28b6c9f3 Add new utility functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
10 char *tmp, *fext;
b0cd28b6c9f3 Add new utility functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
11
b0cd28b6c9f3 Add new utility functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
12 if (filename == NULL ||
b0cd28b6c9f3 Add new utility functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
13 (tmp = dm_strdup(filename)) == NULL)
b0cd28b6c9f3 Add new utility functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
14 return NULL;
b0cd28b6c9f3 Add new utility functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
15
b0cd28b6c9f3 Add new utility functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
16 if ((fext = strrchr(tmp, '.')) != NULL)
b0cd28b6c9f3 Add new utility functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
17 {
b0cd28b6c9f3 Add new utility functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
18 char *fpath = strrchr(tmp, DM_DIR_SEPARATOR);
b0cd28b6c9f3 Add new utility functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
19 if (fpath == NULL || (fpath != NULL && fext > fpath))
b0cd28b6c9f3 Add new utility functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
20 *fext = 0;
b0cd28b6c9f3 Add new utility functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
21 }
b0cd28b6c9f3 Add new utility functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
22
b0cd28b6c9f3 Add new utility functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
23 return tmp;
b0cd28b6c9f3 Add new utility functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
24 }
b0cd28b6c9f3 Add new utility functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
25
b0cd28b6c9f3 Add new utility functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
26
b0cd28b6c9f3 Add new utility functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
27 /* Replace filename extension based on format pattern.
b0cd28b6c9f3 Add new utility functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
28 * Usage: res = dm_strdup_fext(orig_filename, "foo_%s.cmp");
b0cd28b6c9f3 Add new utility functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
29 */
b0cd28b6c9f3 Add new utility functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
30 char *dm_strdup_fext(const char *filename, const char *fmt)
b0cd28b6c9f3 Add new utility functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
31 {
b0cd28b6c9f3 Add new utility functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
32 char *result, *tmp;
b0cd28b6c9f3 Add new utility functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
33
b0cd28b6c9f3 Add new utility functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
34 if ((tmp = dm_basefilename(filename)) == NULL)
b0cd28b6c9f3 Add new utility functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
35 return NULL;
b0cd28b6c9f3 Add new utility functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
36
b0cd28b6c9f3 Add new utility functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
37 result = dm_strdup_printf(fmt, tmp);
b0cd28b6c9f3 Add new utility functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
38
b0cd28b6c9f3 Add new utility functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
39 dmFree(tmp);
b0cd28b6c9f3 Add new utility functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
40
b0cd28b6c9f3 Add new utility functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
41 return result;
b0cd28b6c9f3 Add new utility functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
42 }
b0cd28b6c9f3 Add new utility functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
43
b0cd28b6c9f3 Add new utility functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 812
diff changeset
44
817
e574764ae065 Add comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 816
diff changeset
45 /* Check if end of the given string str matches needle
e574764ae065 Add comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 816
diff changeset
46 * case-insensitively, return pointer to start of the match,
e574764ae065 Add comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 816
diff changeset
47 * if found, NULL otherwise.
e574764ae065 Add comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 816
diff changeset
48 */
816
091461e0213f Add new utility function.
Matti Hamalainen <ccr@tnsp.org>
parents: 813
diff changeset
49 char *dm_strrcasecmp(char *str, const char *needle)
091461e0213f Add new utility function.
Matti Hamalainen <ccr@tnsp.org>
parents: 813
diff changeset
50 {
091461e0213f Add new utility function.
Matti Hamalainen <ccr@tnsp.org>
parents: 813
diff changeset
51 if (str == NULL || needle == NULL)
091461e0213f Add new utility function.
Matti Hamalainen <ccr@tnsp.org>
parents: 813
diff changeset
52 return NULL;
091461e0213f Add new utility function.
Matti Hamalainen <ccr@tnsp.org>
parents: 813
diff changeset
53
091461e0213f Add new utility function.
Matti Hamalainen <ccr@tnsp.org>
parents: 813
diff changeset
54 const size_t
091461e0213f Add new utility function.
Matti Hamalainen <ccr@tnsp.org>
parents: 813
diff changeset
55 slen = strlen(str),
091461e0213f Add new utility function.
Matti Hamalainen <ccr@tnsp.org>
parents: 813
diff changeset
56 nlen = strlen(needle);
1102
e06abfde6c39 Cosmetics pass: Remove excess whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 836
diff changeset
57
816
091461e0213f Add new utility function.
Matti Hamalainen <ccr@tnsp.org>
parents: 813
diff changeset
58 if (slen < nlen)
091461e0213f Add new utility function.
Matti Hamalainen <ccr@tnsp.org>
parents: 813
diff changeset
59 return NULL;
091461e0213f Add new utility function.
Matti Hamalainen <ccr@tnsp.org>
parents: 813
diff changeset
60
091461e0213f Add new utility function.
Matti Hamalainen <ccr@tnsp.org>
parents: 813
diff changeset
61 if (strcasecmp(str - nlen - 1, needle) == 0)
091461e0213f Add new utility function.
Matti Hamalainen <ccr@tnsp.org>
parents: 813
diff changeset
62 return str - nlen - 1;
091461e0213f Add new utility function.
Matti Hamalainen <ccr@tnsp.org>
parents: 813
diff changeset
63 else
091461e0213f Add new utility function.
Matti Hamalainen <ccr@tnsp.org>
parents: 813
diff changeset
64 return NULL;
091461e0213f Add new utility function.
Matti Hamalainen <ccr@tnsp.org>
parents: 813
diff changeset
65 }
091461e0213f Add new utility function.
Matti Hamalainen <ccr@tnsp.org>
parents: 813
diff changeset
66
091461e0213f Add new utility function.
Matti Hamalainen <ccr@tnsp.org>
parents: 813
diff changeset
67
744
2726d91e3409 Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
68 /* Implementation of strdup() with a NULL check
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
69 */
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70 char *dm_strdup(const char *s)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
71 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
72 char *res;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
73 if (s == NULL)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74 return NULL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
75
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
76 if ((res = dmMalloc(strlen(s) + 1)) == NULL)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
77 return NULL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
78
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
79 strcpy(res, s);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
80 return res;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
83
744
2726d91e3409 Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
84 /* Implementation of strndup() with NULL check
2726d91e3409 Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
85 */
2726d91e3409 Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
86 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
87 {
2726d91e3409 Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
88 char *res;
2726d91e3409 Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
89 if (s == NULL)
2726d91e3409 Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
90 return NULL;
2726d91e3409 Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
91
2726d91e3409 Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
92 size_t len = strlen(s);
2726d91e3409 Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
93 if (len > n)
2726d91e3409 Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
94 len = n;
2726d91e3409 Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
95
2726d91e3409 Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
96 if ((res = dmMalloc(len + 1)) == NULL)
2726d91e3409 Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
97 return NULL;
2726d91e3409 Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
98
2726d91e3409 Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
99 memcpy(res, s, len);
2726d91e3409 Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
100 res[len] = 0;
2726d91e3409 Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
101
2726d91e3409 Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
102 return res;
2726d91e3409 Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
103 }
2726d91e3409 Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
104
2726d91e3409 Add implementation of dm_strndup().
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
105
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
106 /* Simulate a sprintf() that allocates memory
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
107 */
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
108 char *dm_strdup_vprintf(const char *fmt, va_list args)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
109 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
110 int size = 64;
836
85442780089f Fix reallocation.
Matti Hamalainen <ccr@tnsp.org>
parents: 817
diff changeset
111 char *buf, *tmp;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
112
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
113 if ((buf = dmMalloc(size)) == NULL)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
114 return NULL;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
115
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
116 while (1)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
117 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
118 int n;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
119 va_list ap;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
120 va_copy(ap, args);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
121 n = vsnprintf(buf, size, fmt, ap);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
122 va_end(ap);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
123
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
124 if (n > -1 && n < size)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
125 return buf;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
126 if (n > -1)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
127 size = n + 1;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
128 else
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
129 size *= 2;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
130
836
85442780089f Fix reallocation.
Matti Hamalainen <ccr@tnsp.org>
parents: 817
diff changeset
131 if ((tmp = dmRealloc(buf, size)) == NULL)
85442780089f Fix reallocation.
Matti Hamalainen <ccr@tnsp.org>
parents: 817
diff changeset
132 {
85442780089f Fix reallocation.
Matti Hamalainen <ccr@tnsp.org>
parents: 817
diff changeset
133 dmFree(buf);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
134 return NULL;
836
85442780089f Fix reallocation.
Matti Hamalainen <ccr@tnsp.org>
parents: 817
diff changeset
135 }
85442780089f Fix reallocation.
Matti Hamalainen <ccr@tnsp.org>
parents: 817
diff changeset
136 else
85442780089f Fix reallocation.
Matti Hamalainen <ccr@tnsp.org>
parents: 817
diff changeset
137 buf = tmp;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
138 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
139 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
140
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
141
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
142 char *dm_strdup_printf(const char *fmt, ...)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
143 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
144 char *res;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
145 va_list ap;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
146
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
147 va_start(ap, fmt);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
148 res = dm_strdup_vprintf(fmt, ap);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
149 va_end(ap);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
150
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
151 return res;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
152 }