]> sigrok.org Git - libsigrok.git/blob - strutil.c
Use g_try_malloc/g_free/g_strdup consistently.
[libsigrok.git] / strutil.c
1 /*
2  * This file is part of the sigrok project.
3  *
4  * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20
21 #include <stdint.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include "sigrok.h"
25 #include "sigrok-internal.h"
26
27 /**
28  * Convert a numeric samplerate value to its "natural" string representation.
29  *
30  * E.g. a value of 3000000 would be converted to "3 MHz", 20000 to "20 kHz".
31  *
32  * @param samplerate The samplerate in Hz.
33  * @return A g_try_malloc()ed string representation of the samplerate value,
34  *         or NULL upon errors. The caller is responsible to g_free() the
35  *         memory.
36  */
37 SR_API char *sr_samplerate_string(uint64_t samplerate)
38 {
39         char *o;
40         int r;
41
42         /* Allocate enough for a uint64_t as string + " GHz". */
43         if (!(o = g_try_malloc0(30 + 1))) {
44                 sr_err("strutil: %s: o malloc failed", __func__);
45                 return NULL;
46         }
47
48         if (samplerate >= SR_GHZ(1))
49                 r = snprintf(o, 30, "%" PRIu64 " GHz", samplerate / 1000000000);
50         else if (samplerate >= SR_MHZ(1))
51                 r = snprintf(o, 30, "%" PRIu64 " MHz", samplerate / 1000000);
52         else if (samplerate >= SR_KHZ(1))
53                 r = snprintf(o, 30, "%" PRIu64 " kHz", samplerate / 1000);
54         else
55                 r = snprintf(o, 30, "%" PRIu64 " Hz", samplerate);
56
57         if (r < 0) {
58                 /* Something went wrong... */
59                 g_free(o);
60                 return NULL;
61         }
62
63         return o;
64 }
65
66 /**
67  * Convert a numeric frequency value to the "natural" string representation
68  * of its period.
69  *
70  * E.g. a value of 3000000 would be converted to "3 us", 20000 to "50 ms".
71  *
72  * @param frequency The frequency in Hz.
73  * @return A g_try_malloc()ed string representation of the frequency value,
74  *         or NULL upon errors. The caller is responsible to g_free() the
75  *         memory.
76  */
77 SR_API char *sr_period_string(uint64_t frequency)
78 {
79         char *o;
80         int r;
81
82         /* Allocate enough for a uint64_t as string + " ms". */
83         if (!(o = g_try_malloc0(30 + 1))) {
84                 sr_err("strutil: %s: o malloc failed", __func__);
85                 return NULL;
86         }
87
88         if (frequency >= SR_GHZ(1))
89                 r = snprintf(o, 30, "%" PRIu64 " ns", frequency / 1000000000);
90         else if (frequency >= SR_MHZ(1))
91                 r = snprintf(o, 30, "%" PRIu64 " us", frequency / 1000000);
92         else if (frequency >= SR_KHZ(1))
93                 r = snprintf(o, 30, "%" PRIu64 " ms", frequency / 1000);
94         else
95                 r = snprintf(o, 30, "%" PRIu64 " s", frequency);
96
97         if (r < 0) {
98                 /* Something went wrong... */
99                 g_free(o);
100                 return NULL;
101         }
102
103         return o;
104 }
105
106 /**
107  * TODO
108  *
109  * @param device TODO
110  * @param triggerstring TODO
111  * @return TODO
112  */
113 SR_API char **sr_parse_triggerstring(struct sr_device *device,
114                                      const char *triggerstring)
115 {
116         GSList *l;
117         struct sr_probe *probe;
118         int max_probes, probenum, i;
119         char **tokens, **triggerlist, *trigger, *tc, *trigger_types;
120         gboolean error;
121
122         max_probes = g_slist_length(device->probes);
123         error = FALSE;
124
125         if (!(triggerlist = g_try_malloc0(max_probes * sizeof(char *)))) {
126                 sr_err("session file: %s: metafile malloc failed", __func__);
127                 return NULL;
128         }
129
130         tokens = g_strsplit(triggerstring, ",", max_probes);
131         trigger_types = device->plugin->get_device_info(0, SR_DI_TRIGGER_TYPES);
132         if (trigger_types == NULL)
133                 return NULL;
134
135         for (i = 0; tokens[i]; i++) {
136                 if (tokens[i][0] < '0' || tokens[i][0] > '9') {
137                         /* Named probe */
138                         probenum = 0;
139                         for (l = device->probes; l; l = l->next) {
140                                 probe = (struct sr_probe *)l->data;
141                                 if (probe->enabled
142                                     && !strncmp(probe->name, tokens[i],
143                                                 strlen(probe->name))) {
144                                         probenum = probe->index;
145                                         break;
146                                 }
147                         }
148                 } else {
149                         probenum = strtol(tokens[i], NULL, 10);
150                 }
151
152                 if (probenum < 1 || probenum > max_probes) {
153                         sr_err("Invalid probe.\n");
154                         error = TRUE;
155                         break;
156                 }
157
158                 if ((trigger = strchr(tokens[i], '='))) {
159                         for (tc = ++trigger; *tc; tc++) {
160                                 if (strchr(trigger_types, *tc) == NULL) {
161                                         sr_err("Unsupported trigger type "
162                                                "'%c'\n", *tc);
163                                         error = TRUE;
164                                         break;
165                                 }
166                         }
167                         if (!error)
168                                 triggerlist[probenum - 1] = g_strdup(trigger);
169                 }
170         }
171         g_strfreev(tokens);
172
173         if (error) {
174                 for (i = 0; i < max_probes; i++)
175                         g_free(triggerlist[i]);
176                 g_free(triggerlist);
177                 triggerlist = NULL;
178         }
179
180         return triggerlist;
181 }
182
183 /**
184  * Convert a "natural" string representation of a size value to uint64_t.
185  *
186  * E.g. a value of "3k" or "3 K" would be converted to 3000, a value
187  * of "15M" would be converted to 15000000.
188  *
189  * Value representations other than decimal (such as hex or octal) are not
190  * supported. Only 'k' (kilo), 'm' (mega), 'g' (giga) suffixes are supported.
191  * Spaces (but not other whitespace) between value and suffix are allowed.
192  *
193  * @param sizestring A string containing a (decimal) size value.
194  * @param size Pointer to uint64_t which will contain the string's size value.
195  * @return SR_OK or error code
196  *
197  */
198 SR_API int sr_parse_sizestring(const char *sizestring, uint64_t *size)
199 {
200         int multiplier, done;
201         char *s;
202
203         *size = strtoull(sizestring, &s, 10);
204         multiplier = 0;
205         done = FALSE;
206         while (s && *s && multiplier == 0 && !done) {
207                 switch (*s) {
208                 case ' ':
209                         break;
210                 case 'k':
211                 case 'K':
212                         multiplier = SR_KHZ(1);
213                         break;
214                 case 'm':
215                 case 'M':
216                         multiplier = SR_MHZ(1);
217                         break;
218                 case 'g':
219                 case 'G':
220                         multiplier = SR_GHZ(1);
221                         break;
222                 default:
223                         done = TRUE;
224                         s--;
225                 }
226                 s++;
227         }
228         if (multiplier > 0)
229                 *size *= multiplier;
230
231         if (*s && strcasecmp(s, "Hz"))
232                 return SR_ERR;
233
234         return SR_OK;
235 }
236
237 /**
238  * Convert a "natural" string representation of a time value to an
239  * uint64_t value in milliseconds.
240  *
241  * E.g. a value of "3s" or "3 s" would be converted to 3000, a value
242  * of "15ms" would be converted to 15.
243  *
244  * Value representations other than decimal (such as hex or octal) are not
245  * supported. Only lower-case "s" and "ms" time suffixes are supported.
246  * Spaces (but not other whitespace) between value and suffix are allowed.
247  *
248  * @param timestring A string containing a (decimal) time value.
249  * @return The string's time value as uint64_t, in milliseconds.
250  *
251  * TODO: Error handling.
252  * TODO: Add support for "m" (minutes) and others.
253  * TODO: picoseconds?
254  * TODO: Allow both lower-case and upper-case.
255  */
256 SR_API uint64_t sr_parse_timestring(const char *timestring)
257 {
258         uint64_t time_msec;
259         char *s;
260
261         time_msec = strtoull(timestring, &s, 10);
262         if (time_msec == 0 && s == timestring)
263                 return 0;
264
265         if (s && *s) {
266                 while (*s == ' ')
267                         s++;
268                 if (!strcmp(s, "s"))
269                         time_msec *= 1000;
270                 else if (!strcmp(s, "ms"))
271                         ; /* redundant */
272                 else
273                         return 0;
274         }
275
276         return time_msec;
277 }
278
279 SR_API gboolean sr_parse_boolstring(const char *boolstr)
280 {
281         if (!boolstr)
282                 return FALSE;
283
284         if (!g_strcasecmp(boolstr, "true") ||
285             !g_strcasecmp(boolstr, "yes") ||
286             !g_strcasecmp(boolstr, "on") ||
287             !g_strcasecmp(boolstr, "1"))
288                 return TRUE;
289
290         return FALSE;
291 }