]> sigrok.org Git - libsigrok.git/blame - strutil.c
gnuplot output: Optimize by only storing changes.
[libsigrok.git] / strutil.c
CommitLineData
25e7d9b1
UH
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
26/**
27 * Convert a numeric samplerate value to its "natural" string representation.
28 *
38ba2522 29 * E.g. a value of 3000000 would be converted to "3 MHz", 20000 to "20 kHz".
25e7d9b1
UH
30 *
31 * @param samplerate The samplerate in Hz.
32 * @return A malloc()ed string representation of the samplerate value,
33 * or NULL upon errors. The caller is responsible to free() the memory.
34 */
a00ba012 35char *sr_samplerate_string(uint64_t samplerate)
25e7d9b1
UH
36{
37 char *o;
38 int r;
39
40 o = malloc(30 + 1); /* Enough for a uint64_t as string + " GHz". */
484760d1 41 if (!o)
25e7d9b1
UH
42 return NULL;
43
59df0c77 44 if (samplerate >= SR_GHZ(1))
99c1fc59 45 r = snprintf(o, 30, "%" PRIu64 " GHz", samplerate / 1000000000);
59df0c77 46 else if (samplerate >= SR_MHZ(1))
99c1fc59 47 r = snprintf(o, 30, "%" PRIu64 " MHz", samplerate / 1000000);
59df0c77 48 else if (samplerate >= SR_KHZ(1))
38ba2522 49 r = snprintf(o, 30, "%" PRIu64 " kHz", samplerate / 1000);
25e7d9b1 50 else
99c1fc59 51 r = snprintf(o, 30, "%" PRIu64 " Hz", samplerate);
25e7d9b1
UH
52
53 if (r < 0) {
54 /* Something went wrong... */
55 free(o);
56 return NULL;
57 }
58
59 return o;
60}
2a3f9541 61
2a3f9541 62/**
dfcc0bf9 63 * Convert a numeric frequency value to the "natural" string representation
2a3f9541
BV
64 * of its period.
65 *
2507648e 66 * E.g. a value of 3000000 would be converted to "3 us", 20000 to "50 ms".
2a3f9541
BV
67 *
68 * @param frequency The frequency in Hz.
69 * @return A malloc()ed string representation of the frequency value,
70 * or NULL upon errors. The caller is responsible to free() the memory.
71 */
a00ba012 72char *sr_period_string(uint64_t frequency)
2a3f9541
BV
73{
74 char *o;
75 int r;
76
77 o = malloc(30 + 1); /* Enough for a uint64_t as string + " ms". */
484760d1 78 if (!o)
2a3f9541
BV
79 return NULL;
80
59df0c77 81 if (frequency >= SR_GHZ(1))
2a3f9541 82 r = snprintf(o, 30, "%" PRIu64 " ns", frequency / 1000000000);
59df0c77 83 else if (frequency >= SR_MHZ(1))
2507648e 84 r = snprintf(o, 30, "%" PRIu64 " us", frequency / 1000000);
59df0c77 85 else if (frequency >= SR_KHZ(1))
2a3f9541
BV
86 r = snprintf(o, 30, "%" PRIu64 " ms", frequency / 1000);
87 else
88 r = snprintf(o, 30, "%" PRIu64 " s", frequency);
89
90 if (r < 0) {
91 /* Something went wrong... */
92 free(o);
93 return NULL;
94 }
95
96 return o;
97}
40f5ddac 98
dfcc0bf9
UH
99/**
100 * TODO
101 *
102 * @param device TODO
103 * @param triggerstring TODO
104 * @return TODO
105 */
106char **sr_parse_triggerstring(struct sr_device *device,
107 const char *triggerstring)
40f5ddac
BV
108{
109 GSList *l;
1afe8989 110 struct sr_probe *probe;
40f5ddac
BV
111 int max_probes, probenum, i;
112 char **tokens, **triggerlist, *trigger, *tc, *trigger_types;
113 gboolean error;
114
115 max_probes = g_slist_length(device->probes);
116 error = FALSE;
117 triggerlist = g_malloc0(max_probes * sizeof(char *));
118 tokens = g_strsplit(triggerstring, ",", max_probes);
119 trigger_types = device->plugin->get_device_info(0, SR_DI_TRIGGER_TYPES);
120 if (trigger_types == NULL)
121 return NULL;
122
123 for (i = 0; tokens[i]; i++) {
124 if (tokens[i][0] < '0' || tokens[i][0] > '9') {
125 /* Named probe */
126 probenum = 0;
127 for (l = device->probes; l; l = l->next) {
1afe8989 128 probe = (struct sr_probe *)l->data;
40f5ddac
BV
129 if (probe->enabled
130 && !strncmp(probe->name, tokens[i],
131 strlen(probe->name))) {
132 probenum = probe->index;
133 break;
134 }
135 }
136 } else {
137 probenum = strtol(tokens[i], NULL, 10);
138 }
139
140 if (probenum < 1 || probenum > max_probes) {
141 printf("Invalid probe.\n");
142 error = TRUE;
143 break;
144 }
145
146 if ((trigger = strchr(tokens[i], '='))) {
147 for (tc = ++trigger; *tc; tc++) {
148 if (strchr(trigger_types, *tc) == NULL) {
149 printf("Unsupported trigger type "
150 "'%c'\n", *tc);
151 error = TRUE;
152 break;
153 }
154 }
155 if (!error)
156 triggerlist[probenum - 1] = g_strdup(trigger);
157 }
158 }
159 g_strfreev(tokens);
160
161 if (error) {
162 for (i = 0; i < max_probes; i++)
163 if (triggerlist[i])
164 g_free(triggerlist[i]);
165 g_free(triggerlist);
166 triggerlist = NULL;
167 }
168
169 return triggerlist;
170}
171
dfcc0bf9
UH
172/**
173 * Convert a "natural" string representation of a size value to uint64_t.
174 *
175 * E.g. a value of "3k" or "3 K" would be converted to 3000, a value
176 * of "15M" would be converted to 15000000.
177 *
178 * Value representations other than decimal (such as hex or octal) are not
179 * supported. Only 'k' (kilo), 'm' (mega), 'g' (giga) suffixes are supported.
180 * Spaces (but not other whitespace) between value and suffix are allowed.
181 *
182 * @param sizestring A string containing a (decimal) size value.
183 * @return The string's size value as uint64_t.
184 *
185 * TODO: Error handling.
186 */
40f5ddac
BV
187uint64_t sr_parse_sizestring(const char *sizestring)
188{
189 int multiplier;
190 uint64_t val;
191 char *s;
192
193 val = strtoull(sizestring, &s, 10);
194 multiplier = 0;
195 while (s && *s && multiplier == 0) {
196 switch (*s) {
197 case ' ':
198 break;
199 case 'k':
200 case 'K':
59df0c77 201 multiplier = SR_KHZ(1);
40f5ddac
BV
202 break;
203 case 'm':
204 case 'M':
59df0c77 205 multiplier = SR_MHZ(1);
40f5ddac
BV
206 break;
207 case 'g':
208 case 'G':
59df0c77 209 multiplier = SR_GHZ(1);
40f5ddac
BV
210 break;
211 default:
212 val = 0;
213 multiplier = -1;
214 }
215 s++;
216 }
217 if (multiplier > 0)
218 val *= multiplier;
219
220 return val;
221}
222
dfcc0bf9
UH
223/**
224 * Convert a "natural" string representation of a time value to an
225 * uint64_t value in milliseconds.
226 *
227 * E.g. a value of "3s" or "3 s" would be converted to 3000, a value
228 * of "15ms" would be converted to 15.
229 *
230 * Value representations other than decimal (such as hex or octal) are not
231 * supported. Only lower-case "s" and "ms" time suffixes are supported.
232 * Spaces (but not other whitespace) between value and suffix are allowed.
233 *
234 * @param timestring A string containing a (decimal) time value.
235 * @return The string's time value as uint64_t, in milliseconds.
236 *
237 * TODO: Error handling.
238 * TODO: Add support for "m" (minutes) and others.
239 * TODO: picoseconds?
240 * TODO: Allow both lower-case and upper-case.
241 */
40f5ddac
BV
242uint64_t sr_parse_timestring(const char *timestring)
243{
244 uint64_t time_msec;
245 char *s;
246
247 time_msec = strtoull(timestring, &s, 10);
248 if (time_msec == 0 && s == timestring)
249 return 0;
250
251 if (s && *s) {
252 while (*s == ' ')
253 s++;
254 if (!strcmp(s, "s"))
255 time_msec *= 1000;
256 else if (!strcmp(s, "ms"))
257 ; /* redundant */
258 else
259 return 0;
260 }
261
262 return time_msec;
263}