Bert Vermeulen | 40f5dda | 2011-01-31 22:29:40 +0100 | [diff] [blame] | 1 | /* |
Uwe Hermann | 50985c2 | 2013-04-23 22:24:30 +0200 | [diff] [blame] | 2 | * This file is part of the libsigrok project. |
Bert Vermeulen | 40f5dda | 2011-01-31 22:29:40 +0100 | [diff] [blame] | 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> |
Bert Vermeulen | 45c59c8 | 2012-07-05 00:55:07 +0200 | [diff] [blame] | 24 | #include "libsigrok.h" |
| 25 | #include "libsigrok-internal.h" |
Bert Vermeulen | 40f5dda | 2011-01-31 22:29:40 +0100 | [diff] [blame] | 26 | |
Uwe Hermann | 29a2719 | 2013-05-03 21:59:32 +0200 | [diff] [blame] | 27 | /* Message logging helpers with subsystem-specific prefix string. */ |
| 28 | #define LOG_PREFIX "strutil: " |
| 29 | #define sr_log(l, s, args...) sr_log(l, LOG_PREFIX s, ## args) |
| 30 | #define sr_spew(s, args...) sr_spew(LOG_PREFIX s, ## args) |
| 31 | #define sr_dbg(s, args...) sr_dbg(LOG_PREFIX s, ## args) |
| 32 | #define sr_info(s, args...) sr_info(LOG_PREFIX s, ## args) |
| 33 | #define sr_warn(s, args...) sr_warn(LOG_PREFIX s, ## args) |
| 34 | #define sr_err(s, args...) sr_err(LOG_PREFIX s, ## args) |
Uwe Hermann | a885ce3 | 2012-11-11 12:44:16 +0100 | [diff] [blame] | 35 | |
Bert Vermeulen | 40f5dda | 2011-01-31 22:29:40 +0100 | [diff] [blame] | 36 | /** |
Uwe Hermann | 393fb9c | 2012-10-22 00:30:12 +0200 | [diff] [blame] | 37 | * @file |
| 38 | * |
| 39 | * Helper functions for handling or converting libsigrok-related strings. |
| 40 | */ |
| 41 | |
| 42 | /** |
Uwe Hermann | 7b870c3 | 2012-10-21 16:13:36 +0200 | [diff] [blame] | 43 | * @defgroup grp_strutil String utilities |
| 44 | * |
| 45 | * Helper functions for handling or converting libsigrok-related strings. |
| 46 | * |
| 47 | * @{ |
| 48 | */ |
| 49 | |
| 50 | /** |
Joel Holdsworth | 4cc9aea | 2012-07-07 09:32:00 +0100 | [diff] [blame] | 51 | * Convert a numeric value value to its "natural" string representation. |
| 52 | * in SI units |
| 53 | * |
| 54 | * E.g. a value of 3000000, with units set to "W", would be converted |
| 55 | * to "3 MW", 20000 to "20 kW", 31500 would become "31.5 kW". |
| 56 | * |
| 57 | * @param x The value to convert. |
| 58 | * @param unit The unit to append to the string, or NULL if the string |
| 59 | * has no units. |
| 60 | * |
| 61 | * @return A g_try_malloc()ed string representation of the samplerate value, |
| 62 | * or NULL upon errors. The caller is responsible to g_free() the |
| 63 | * memory. |
| 64 | */ |
| 65 | SR_API char *sr_si_string_u64(uint64_t x, const char *unit) |
| 66 | { |
Uwe Hermann | a885ce3 | 2012-11-11 12:44:16 +0100 | [diff] [blame] | 67 | if (unit == NULL) |
Joel Holdsworth | 4cc9aea | 2012-07-07 09:32:00 +0100 | [diff] [blame] | 68 | unit = ""; |
| 69 | |
| 70 | if ((x >= SR_GHZ(1)) && (x % SR_GHZ(1) == 0)) { |
| 71 | return g_strdup_printf("%" PRIu64 " G%s", x / SR_GHZ(1), unit); |
| 72 | } else if ((x >= SR_GHZ(1)) && (x % SR_GHZ(1) != 0)) { |
| 73 | return g_strdup_printf("%" PRIu64 ".%" PRIu64 " G%s", |
| 74 | x / SR_GHZ(1), x % SR_GHZ(1), unit); |
| 75 | } else if ((x >= SR_MHZ(1)) && (x % SR_MHZ(1) == 0)) { |
| 76 | return g_strdup_printf("%" PRIu64 " M%s", |
| 77 | x / SR_MHZ(1), unit); |
| 78 | } else if ((x >= SR_MHZ(1)) && (x % SR_MHZ(1) != 0)) { |
| 79 | return g_strdup_printf("%" PRIu64 ".%" PRIu64 " M%s", |
| 80 | x / SR_MHZ(1), x % SR_MHZ(1), unit); |
| 81 | } else if ((x >= SR_KHZ(1)) && (x % SR_KHZ(1) == 0)) { |
| 82 | return g_strdup_printf("%" PRIu64 " k%s", |
| 83 | x / SR_KHZ(1), unit); |
| 84 | } else if ((x >= SR_KHZ(1)) && (x % SR_KHZ(1) != 0)) { |
| 85 | return g_strdup_printf("%" PRIu64 ".%" PRIu64 " k%s", |
| 86 | x / SR_KHZ(1), x % SR_KHZ(1), unit); |
| 87 | } else { |
| 88 | return g_strdup_printf("%" PRIu64 " %s", x, unit); |
| 89 | } |
| 90 | |
Uwe Hermann | a885ce3 | 2012-11-11 12:44:16 +0100 | [diff] [blame] | 91 | sr_err("%s: Error creating SI units string.", __func__); |
Joel Holdsworth | 4cc9aea | 2012-07-07 09:32:00 +0100 | [diff] [blame] | 92 | return NULL; |
| 93 | } |
| 94 | |
| 95 | /** |
Bert Vermeulen | 40f5dda | 2011-01-31 22:29:40 +0100 | [diff] [blame] | 96 | * Convert a numeric samplerate value to its "natural" string representation. |
| 97 | * |
Uwe Hermann | c69e35a | 2012-02-27 22:27:33 +0100 | [diff] [blame] | 98 | * E.g. a value of 3000000 would be converted to "3 MHz", 20000 to "20 kHz", |
| 99 | * 31500 would become "31.5 kHz". |
Bert Vermeulen | 40f5dda | 2011-01-31 22:29:40 +0100 | [diff] [blame] | 100 | * |
| 101 | * @param samplerate The samplerate in Hz. |
Uwe Hermann | 44dae53 | 2012-02-17 20:44:19 +0100 | [diff] [blame] | 102 | * |
Uwe Hermann | 133a37b | 2012-02-11 20:06:46 +0100 | [diff] [blame] | 103 | * @return A g_try_malloc()ed string representation of the samplerate value, |
| 104 | * or NULL upon errors. The caller is responsible to g_free() the |
| 105 | * memory. |
Bert Vermeulen | 40f5dda | 2011-01-31 22:29:40 +0100 | [diff] [blame] | 106 | */ |
Uwe Hermann | 1a081ca | 2012-02-01 23:40:35 +0100 | [diff] [blame] | 107 | SR_API char *sr_samplerate_string(uint64_t samplerate) |
Bert Vermeulen | 40f5dda | 2011-01-31 22:29:40 +0100 | [diff] [blame] | 108 | { |
Joel Holdsworth | 4cc9aea | 2012-07-07 09:32:00 +0100 | [diff] [blame] | 109 | return sr_si_string_u64(samplerate, "Hz"); |
Bert Vermeulen | 40f5dda | 2011-01-31 22:29:40 +0100 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /** |
Uwe Hermann | dfcc0bf | 2011-02-08 21:47:50 +0100 | [diff] [blame] | 113 | * Convert a numeric frequency value to the "natural" string representation |
Bert Vermeulen | 40f5dda | 2011-01-31 22:29:40 +0100 | [diff] [blame] | 114 | * of its period. |
| 115 | * |
| 116 | * E.g. a value of 3000000 would be converted to "3 us", 20000 to "50 ms". |
| 117 | * |
| 118 | * @param frequency The frequency in Hz. |
Uwe Hermann | 44dae53 | 2012-02-17 20:44:19 +0100 | [diff] [blame] | 119 | * |
Uwe Hermann | 133a37b | 2012-02-11 20:06:46 +0100 | [diff] [blame] | 120 | * @return A g_try_malloc()ed string representation of the frequency value, |
| 121 | * or NULL upon errors. The caller is responsible to g_free() the |
| 122 | * memory. |
Bert Vermeulen | 40f5dda | 2011-01-31 22:29:40 +0100 | [diff] [blame] | 123 | */ |
Uwe Hermann | 1a081ca | 2012-02-01 23:40:35 +0100 | [diff] [blame] | 124 | SR_API char *sr_period_string(uint64_t frequency) |
Bert Vermeulen | 40f5dda | 2011-01-31 22:29:40 +0100 | [diff] [blame] | 125 | { |
| 126 | char *o; |
| 127 | int r; |
| 128 | |
Uwe Hermann | 133a37b | 2012-02-11 20:06:46 +0100 | [diff] [blame] | 129 | /* Allocate enough for a uint64_t as string + " ms". */ |
| 130 | if (!(o = g_try_malloc0(30 + 1))) { |
Uwe Hermann | a885ce3 | 2012-11-11 12:44:16 +0100 | [diff] [blame] | 131 | sr_err("%s: o malloc failed", __func__); |
Bert Vermeulen | 40f5dda | 2011-01-31 22:29:40 +0100 | [diff] [blame] | 132 | return NULL; |
Uwe Hermann | 133a37b | 2012-02-11 20:06:46 +0100 | [diff] [blame] | 133 | } |
Bert Vermeulen | 40f5dda | 2011-01-31 22:29:40 +0100 | [diff] [blame] | 134 | |
Uwe Hermann | 59df0c7 | 2011-02-22 17:57:03 +0100 | [diff] [blame] | 135 | if (frequency >= SR_GHZ(1)) |
Bert Vermeulen | 40f5dda | 2011-01-31 22:29:40 +0100 | [diff] [blame] | 136 | r = snprintf(o, 30, "%" PRIu64 " ns", frequency / 1000000000); |
Uwe Hermann | 59df0c7 | 2011-02-22 17:57:03 +0100 | [diff] [blame] | 137 | else if (frequency >= SR_MHZ(1)) |
Bert Vermeulen | 40f5dda | 2011-01-31 22:29:40 +0100 | [diff] [blame] | 138 | r = snprintf(o, 30, "%" PRIu64 " us", frequency / 1000000); |
Uwe Hermann | 59df0c7 | 2011-02-22 17:57:03 +0100 | [diff] [blame] | 139 | else if (frequency >= SR_KHZ(1)) |
Bert Vermeulen | 40f5dda | 2011-01-31 22:29:40 +0100 | [diff] [blame] | 140 | r = snprintf(o, 30, "%" PRIu64 " ms", frequency / 1000); |
| 141 | else |
| 142 | r = snprintf(o, 30, "%" PRIu64 " s", frequency); |
| 143 | |
| 144 | if (r < 0) { |
| 145 | /* Something went wrong... */ |
Uwe Hermann | 133a37b | 2012-02-11 20:06:46 +0100 | [diff] [blame] | 146 | g_free(o); |
Bert Vermeulen | 40f5dda | 2011-01-31 22:29:40 +0100 | [diff] [blame] | 147 | return NULL; |
| 148 | } |
| 149 | |
| 150 | return o; |
| 151 | } |
| 152 | |
Uwe Hermann | dfcc0bf | 2011-02-08 21:47:50 +0100 | [diff] [blame] | 153 | /** |
Bert Vermeulen | e0e1506 | 2013-03-31 10:27:15 +0200 | [diff] [blame] | 154 | * Convert a numeric voltage value to the "natural" string representation |
| 155 | * of its voltage value. The voltage is specified as a rational number's |
| 156 | * numerator and denominator. |
Bert Vermeulen | 79afc8c | 2012-05-17 01:54:57 +0200 | [diff] [blame] | 157 | * |
| 158 | * E.g. a value of 300000 would be converted to "300mV", 2 to "2V". |
| 159 | * |
Bert Vermeulen | e0e1506 | 2013-03-31 10:27:15 +0200 | [diff] [blame] | 160 | * @param v_p The voltage numerator. |
| 161 | * @param v_q The voltage denominator. |
Bert Vermeulen | 79afc8c | 2012-05-17 01:54:57 +0200 | [diff] [blame] | 162 | * |
| 163 | * @return A g_try_malloc()ed string representation of the voltage value, |
| 164 | * or NULL upon errors. The caller is responsible to g_free() the |
| 165 | * memory. |
| 166 | */ |
Bert Vermeulen | e0e1506 | 2013-03-31 10:27:15 +0200 | [diff] [blame] | 167 | SR_API char *sr_voltage_string(uint64_t v_p, uint64_t v_q) |
Bert Vermeulen | 79afc8c | 2012-05-17 01:54:57 +0200 | [diff] [blame] | 168 | { |
Bert Vermeulen | 79afc8c | 2012-05-17 01:54:57 +0200 | [diff] [blame] | 169 | int r; |
Bert Vermeulen | d5a669a | 2013-02-04 13:36:23 +0100 | [diff] [blame] | 170 | char *o; |
Bert Vermeulen | 79afc8c | 2012-05-17 01:54:57 +0200 | [diff] [blame] | 171 | |
| 172 | if (!(o = g_try_malloc0(30 + 1))) { |
Uwe Hermann | a885ce3 | 2012-11-11 12:44:16 +0100 | [diff] [blame] | 173 | sr_err("%s: o malloc failed", __func__); |
Bert Vermeulen | 79afc8c | 2012-05-17 01:54:57 +0200 | [diff] [blame] | 174 | return NULL; |
| 175 | } |
| 176 | |
Bert Vermeulen | e0e1506 | 2013-03-31 10:27:15 +0200 | [diff] [blame] | 177 | if (v_q == 1000) |
| 178 | r = snprintf(o, 30, "%" PRIu64 "mV", v_p); |
| 179 | else if (v_q == 1) |
| 180 | r = snprintf(o, 30, "%" PRIu64 "V", v_p); |
Bert Vermeulen | 79afc8c | 2012-05-17 01:54:57 +0200 | [diff] [blame] | 181 | else |
Bert Vermeulen | e0e1506 | 2013-03-31 10:27:15 +0200 | [diff] [blame] | 182 | r = snprintf(o, 30, "%gV", (float)v_p / (float)v_q); |
Bert Vermeulen | 79afc8c | 2012-05-17 01:54:57 +0200 | [diff] [blame] | 183 | |
| 184 | if (r < 0) { |
| 185 | /* Something went wrong... */ |
| 186 | g_free(o); |
| 187 | return NULL; |
| 188 | } |
| 189 | |
| 190 | return o; |
| 191 | } |
| 192 | |
| 193 | /** |
Uwe Hermann | bf978d3 | 2012-04-07 17:35:14 +0200 | [diff] [blame] | 194 | * Parse a trigger specification string. |
Uwe Hermann | dfcc0bf | 2011-02-08 21:47:50 +0100 | [diff] [blame] | 195 | * |
Uwe Hermann | 9c5332d | 2012-10-21 16:52:56 +0200 | [diff] [blame] | 196 | * @param sdi The device instance for which the trigger specification is |
| 197 | * intended. Must not be NULL. Also, sdi->driver and |
| 198 | * sdi->driver->info_get must not be NULL. |
Uwe Hermann | bf978d3 | 2012-04-07 17:35:14 +0200 | [diff] [blame] | 199 | * @param triggerstring The string containing the trigger specification for |
| 200 | * one or more probes of this device. Entries for multiple probes are |
| 201 | * comma-separated. Triggers are specified in the form key=value, |
| 202 | * where the key is a probe number (or probe name) and the value is |
| 203 | * the requested trigger type. Valid trigger types currently |
| 204 | * include 'r' (rising edge), 'f' (falling edge), 'c' (any pin value |
| 205 | * change), '0' (low value), or '1' (high value). |
| 206 | * Example: "1=r,sck=f,miso=0,7=c" |
Uwe Hermann | 44dae53 | 2012-02-17 20:44:19 +0100 | [diff] [blame] | 207 | * |
Uwe Hermann | bf978d3 | 2012-04-07 17:35:14 +0200 | [diff] [blame] | 208 | * @return Pointer to a list of trigger types (strings), or NULL upon errors. |
| 209 | * The pointer list (if non-NULL) has as many entries as the |
| 210 | * respective device has probes (all physically available probes, |
| 211 | * not just enabled ones). Entries of the list which don't have |
| 212 | * a trigger value set in 'triggerstring' are NULL, the other entries |
| 213 | * contain the respective trigger type which is requested for the |
| 214 | * respective probe (e.g. "r", "c", and so on). |
Uwe Hermann | dfcc0bf | 2011-02-08 21:47:50 +0100 | [diff] [blame] | 215 | */ |
Bert Vermeulen | 92ae798 | 2012-07-22 14:28:40 +0200 | [diff] [blame] | 216 | SR_API char **sr_parse_triggerstring(const struct sr_dev_inst *sdi, |
Uwe Hermann | 1a081ca | 2012-02-01 23:40:35 +0100 | [diff] [blame] | 217 | const char *triggerstring) |
Bert Vermeulen | 40f5dda | 2011-01-31 22:29:40 +0100 | [diff] [blame] | 218 | { |
| 219 | GSList *l; |
Bert Vermeulen | 003595a | 2013-03-25 20:21:10 +0100 | [diff] [blame] | 220 | GVariant *gvar; |
Uwe Hermann | 1afe898 | 2011-02-08 17:47:38 +0100 | [diff] [blame] | 221 | struct sr_probe *probe; |
Bert Vermeulen | 40f5dda | 2011-01-31 22:29:40 +0100 | [diff] [blame] | 222 | int max_probes, probenum, i; |
Joel Holdsworth | b7f578b | 2012-05-07 13:35:56 +0100 | [diff] [blame] | 223 | char **tokens, **triggerlist, *trigger, *tc; |
| 224 | const char *trigger_types; |
Bert Vermeulen | 40f5dda | 2011-01-31 22:29:40 +0100 | [diff] [blame] | 225 | gboolean error; |
| 226 | |
Bert Vermeulen | 92ae798 | 2012-07-22 14:28:40 +0200 | [diff] [blame] | 227 | max_probes = g_slist_length(sdi->probes); |
Bert Vermeulen | 40f5dda | 2011-01-31 22:29:40 +0100 | [diff] [blame] | 228 | error = FALSE; |
Uwe Hermann | b53738b | 2011-04-16 14:17:51 +0200 | [diff] [blame] | 229 | |
| 230 | if (!(triggerlist = g_try_malloc0(max_probes * sizeof(char *)))) { |
Uwe Hermann | a885ce3 | 2012-11-11 12:44:16 +0100 | [diff] [blame] | 231 | sr_err("%s: triggerlist malloc failed", __func__); |
Uwe Hermann | b53738b | 2011-04-16 14:17:51 +0200 | [diff] [blame] | 232 | return NULL; |
| 233 | } |
| 234 | |
Bert Vermeulen | 003595a | 2013-03-25 20:21:10 +0100 | [diff] [blame] | 235 | if (sdi->driver->config_list(SR_CONF_TRIGGER_TYPE, &gvar, sdi) != SR_OK) { |
Uwe Hermann | a885ce3 | 2012-11-11 12:44:16 +0100 | [diff] [blame] | 236 | sr_err("%s: Device doesn't support any triggers.", __func__); |
Bert Vermeulen | 40f5dda | 2011-01-31 22:29:40 +0100 | [diff] [blame] | 237 | return NULL; |
Uwe Hermann | bf978d3 | 2012-04-07 17:35:14 +0200 | [diff] [blame] | 238 | } |
Bert Vermeulen | 003595a | 2013-03-25 20:21:10 +0100 | [diff] [blame] | 239 | trigger_types = g_variant_get_string(gvar, NULL); |
Bert Vermeulen | 40f5dda | 2011-01-31 22:29:40 +0100 | [diff] [blame] | 240 | |
Bert Vermeulen | 31fc1fb | 2012-07-23 02:58:56 +0200 | [diff] [blame] | 241 | tokens = g_strsplit(triggerstring, ",", max_probes); |
Bert Vermeulen | 40f5dda | 2011-01-31 22:29:40 +0100 | [diff] [blame] | 242 | for (i = 0; tokens[i]; i++) { |
Bert Vermeulen | 17ff112 | 2012-10-08 23:56:06 +0200 | [diff] [blame] | 243 | probenum = -1; |
| 244 | for (l = sdi->probes; l; l = l->next) { |
| 245 | probe = (struct sr_probe *)l->data; |
| 246 | if (probe->enabled |
| 247 | && !strncmp(probe->name, tokens[i], |
| 248 | strlen(probe->name))) { |
| 249 | probenum = probe->index; |
| 250 | break; |
Bert Vermeulen | 40f5dda | 2011-01-31 22:29:40 +0100 | [diff] [blame] | 251 | } |
Bert Vermeulen | 40f5dda | 2011-01-31 22:29:40 +0100 | [diff] [blame] | 252 | } |
| 253 | |
Bert Vermeulen | 31fc1fb | 2012-07-23 02:58:56 +0200 | [diff] [blame] | 254 | if (probenum < 0 || probenum >= max_probes) { |
Uwe Hermann | a885ce3 | 2012-11-11 12:44:16 +0100 | [diff] [blame] | 255 | sr_err("Invalid probe."); |
Bert Vermeulen | 40f5dda | 2011-01-31 22:29:40 +0100 | [diff] [blame] | 256 | error = TRUE; |
| 257 | break; |
| 258 | } |
| 259 | |
| 260 | if ((trigger = strchr(tokens[i], '='))) { |
| 261 | for (tc = ++trigger; *tc; tc++) { |
| 262 | if (strchr(trigger_types, *tc) == NULL) { |
Uwe Hermann | a885ce3 | 2012-11-11 12:44:16 +0100 | [diff] [blame] | 263 | sr_err("Unsupported trigger " |
Uwe Hermann | 0aeb0cc | 2012-04-07 17:40:52 +0200 | [diff] [blame] | 264 | "type '%c'.", *tc); |
Bert Vermeulen | 40f5dda | 2011-01-31 22:29:40 +0100 | [diff] [blame] | 265 | error = TRUE; |
| 266 | break; |
| 267 | } |
| 268 | } |
| 269 | if (!error) |
Bert Vermeulen | 31fc1fb | 2012-07-23 02:58:56 +0200 | [diff] [blame] | 270 | triggerlist[probenum] = g_strdup(trigger); |
Bert Vermeulen | 40f5dda | 2011-01-31 22:29:40 +0100 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | g_strfreev(tokens); |
Bert Vermeulen | 003595a | 2013-03-25 20:21:10 +0100 | [diff] [blame] | 274 | g_variant_unref(gvar); |
Bert Vermeulen | 40f5dda | 2011-01-31 22:29:40 +0100 | [diff] [blame] | 275 | |
| 276 | if (error) { |
| 277 | for (i = 0; i < max_probes; i++) |
Uwe Hermann | 66410a8 | 2012-01-19 00:32:02 +0100 | [diff] [blame] | 278 | g_free(triggerlist[i]); |
Bert Vermeulen | 40f5dda | 2011-01-31 22:29:40 +0100 | [diff] [blame] | 279 | g_free(triggerlist); |
| 280 | triggerlist = NULL; |
| 281 | } |
| 282 | |
| 283 | return triggerlist; |
| 284 | } |
| 285 | |
Uwe Hermann | dfcc0bf | 2011-02-08 21:47:50 +0100 | [diff] [blame] | 286 | /** |
| 287 | * Convert a "natural" string representation of a size value to uint64_t. |
| 288 | * |
| 289 | * E.g. a value of "3k" or "3 K" would be converted to 3000, a value |
| 290 | * of "15M" would be converted to 15000000. |
| 291 | * |
| 292 | * Value representations other than decimal (such as hex or octal) are not |
| 293 | * supported. Only 'k' (kilo), 'm' (mega), 'g' (giga) suffixes are supported. |
| 294 | * Spaces (but not other whitespace) between value and suffix are allowed. |
| 295 | * |
| 296 | * @param sizestring A string containing a (decimal) size value. |
Bert Vermeulen | f64c141 | 2011-11-27 19:31:25 +0100 | [diff] [blame] | 297 | * @param size Pointer to uint64_t which will contain the string's size value. |
Uwe Hermann | dfcc0bf | 2011-02-08 21:47:50 +0100 | [diff] [blame] | 298 | * |
Uwe Hermann | 44dae53 | 2012-02-17 20:44:19 +0100 | [diff] [blame] | 299 | * @return SR_OK upon success, SR_ERR upon errors. |
Uwe Hermann | dfcc0bf | 2011-02-08 21:47:50 +0100 | [diff] [blame] | 300 | */ |
Uwe Hermann | 1a081ca | 2012-02-01 23:40:35 +0100 | [diff] [blame] | 301 | SR_API int sr_parse_sizestring(const char *sizestring, uint64_t *size) |
Bert Vermeulen | 40f5dda | 2011-01-31 22:29:40 +0100 | [diff] [blame] | 302 | { |
Bert Vermeulen | f64c141 | 2011-11-27 19:31:25 +0100 | [diff] [blame] | 303 | int multiplier, done; |
Bert Vermeulen | 40f5dda | 2011-01-31 22:29:40 +0100 | [diff] [blame] | 304 | char *s; |
| 305 | |
Bert Vermeulen | f64c141 | 2011-11-27 19:31:25 +0100 | [diff] [blame] | 306 | *size = strtoull(sizestring, &s, 10); |
Bert Vermeulen | 40f5dda | 2011-01-31 22:29:40 +0100 | [diff] [blame] | 307 | multiplier = 0; |
Bert Vermeulen | f64c141 | 2011-11-27 19:31:25 +0100 | [diff] [blame] | 308 | done = FALSE; |
| 309 | while (s && *s && multiplier == 0 && !done) { |
Bert Vermeulen | 40f5dda | 2011-01-31 22:29:40 +0100 | [diff] [blame] | 310 | switch (*s) { |
| 311 | case ' ': |
| 312 | break; |
| 313 | case 'k': |
| 314 | case 'K': |
Uwe Hermann | 59df0c7 | 2011-02-22 17:57:03 +0100 | [diff] [blame] | 315 | multiplier = SR_KHZ(1); |
Bert Vermeulen | 40f5dda | 2011-01-31 22:29:40 +0100 | [diff] [blame] | 316 | break; |
| 317 | case 'm': |
| 318 | case 'M': |
Uwe Hermann | 59df0c7 | 2011-02-22 17:57:03 +0100 | [diff] [blame] | 319 | multiplier = SR_MHZ(1); |
Bert Vermeulen | 40f5dda | 2011-01-31 22:29:40 +0100 | [diff] [blame] | 320 | break; |
| 321 | case 'g': |
| 322 | case 'G': |
Uwe Hermann | 59df0c7 | 2011-02-22 17:57:03 +0100 | [diff] [blame] | 323 | multiplier = SR_GHZ(1); |
Bert Vermeulen | 40f5dda | 2011-01-31 22:29:40 +0100 | [diff] [blame] | 324 | break; |
| 325 | default: |
Bert Vermeulen | f64c141 | 2011-11-27 19:31:25 +0100 | [diff] [blame] | 326 | done = TRUE; |
| 327 | s--; |
Bert Vermeulen | 40f5dda | 2011-01-31 22:29:40 +0100 | [diff] [blame] | 328 | } |
| 329 | s++; |
| 330 | } |
| 331 | if (multiplier > 0) |
Bert Vermeulen | f64c141 | 2011-11-27 19:31:25 +0100 | [diff] [blame] | 332 | *size *= multiplier; |
Bert Vermeulen | 40f5dda | 2011-01-31 22:29:40 +0100 | [diff] [blame] | 333 | |
Bert Vermeulen | f64c141 | 2011-11-27 19:31:25 +0100 | [diff] [blame] | 334 | if (*s && strcasecmp(s, "Hz")) |
| 335 | return SR_ERR; |
| 336 | |
| 337 | return SR_OK; |
Bert Vermeulen | 40f5dda | 2011-01-31 22:29:40 +0100 | [diff] [blame] | 338 | } |
| 339 | |
Uwe Hermann | dfcc0bf | 2011-02-08 21:47:50 +0100 | [diff] [blame] | 340 | /** |
| 341 | * Convert a "natural" string representation of a time value to an |
| 342 | * uint64_t value in milliseconds. |
| 343 | * |
| 344 | * E.g. a value of "3s" or "3 s" would be converted to 3000, a value |
| 345 | * of "15ms" would be converted to 15. |
| 346 | * |
| 347 | * Value representations other than decimal (such as hex or octal) are not |
| 348 | * supported. Only lower-case "s" and "ms" time suffixes are supported. |
| 349 | * Spaces (but not other whitespace) between value and suffix are allowed. |
| 350 | * |
| 351 | * @param timestring A string containing a (decimal) time value. |
| 352 | * @return The string's time value as uint64_t, in milliseconds. |
| 353 | * |
Uwe Hermann | 6b2d8d3 | 2012-10-21 23:24:42 +0200 | [diff] [blame] | 354 | * @todo Add support for "m" (minutes) and others. |
| 355 | * @todo Add support for picoseconds? |
| 356 | * @todo Allow both lower-case and upper-case? If no, document it. |
Uwe Hermann | dfcc0bf | 2011-02-08 21:47:50 +0100 | [diff] [blame] | 357 | */ |
Uwe Hermann | 1a081ca | 2012-02-01 23:40:35 +0100 | [diff] [blame] | 358 | SR_API uint64_t sr_parse_timestring(const char *timestring) |
Bert Vermeulen | 40f5dda | 2011-01-31 22:29:40 +0100 | [diff] [blame] | 359 | { |
| 360 | uint64_t time_msec; |
| 361 | char *s; |
| 362 | |
Uwe Hermann | 6b2d8d3 | 2012-10-21 23:24:42 +0200 | [diff] [blame] | 363 | /* TODO: Error handling, logging. */ |
| 364 | |
Bert Vermeulen | 40f5dda | 2011-01-31 22:29:40 +0100 | [diff] [blame] | 365 | time_msec = strtoull(timestring, &s, 10); |
| 366 | if (time_msec == 0 && s == timestring) |
| 367 | return 0; |
| 368 | |
| 369 | if (s && *s) { |
| 370 | while (*s == ' ') |
| 371 | s++; |
| 372 | if (!strcmp(s, "s")) |
| 373 | time_msec *= 1000; |
| 374 | else if (!strcmp(s, "ms")) |
| 375 | ; /* redundant */ |
| 376 | else |
| 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | return time_msec; |
| 381 | } |
Gareth McMullin | 4d436e7 | 2011-11-19 13:41:41 +1300 | [diff] [blame] | 382 | |
Uwe Hermann | 1a081ca | 2012-02-01 23:40:35 +0100 | [diff] [blame] | 383 | SR_API gboolean sr_parse_boolstring(const char *boolstr) |
Gareth McMullin | 4d436e7 | 2011-11-19 13:41:41 +1300 | [diff] [blame] | 384 | { |
| 385 | if (!boolstr) |
| 386 | return FALSE; |
| 387 | |
Bert Vermeulen | 993526f | 2012-04-23 15:31:41 +0200 | [diff] [blame] | 388 | if (!g_ascii_strncasecmp(boolstr, "true", 4) || |
| 389 | !g_ascii_strncasecmp(boolstr, "yes", 3) || |
| 390 | !g_ascii_strncasecmp(boolstr, "on", 2) || |
| 391 | !g_ascii_strncasecmp(boolstr, "1", 1)) |
Gareth McMullin | 4d436e7 | 2011-11-19 13:41:41 +1300 | [diff] [blame] | 392 | return TRUE; |
| 393 | |
| 394 | return FALSE; |
| 395 | } |
Bert Vermeulen | 76f4c61 | 2012-05-15 20:46:14 +0200 | [diff] [blame] | 396 | |
Bert Vermeulen | 76e107d | 2013-03-30 14:41:01 +0100 | [diff] [blame] | 397 | SR_API int sr_parse_period(const char *periodstr, uint64_t *p, uint64_t *q) |
Bert Vermeulen | 76f4c61 | 2012-05-15 20:46:14 +0200 | [diff] [blame] | 398 | { |
| 399 | char *s; |
| 400 | |
Bert Vermeulen | 76e107d | 2013-03-30 14:41:01 +0100 | [diff] [blame] | 401 | *p = strtoull(periodstr, &s, 10); |
| 402 | if (*p == 0 && s == periodstr) |
Bert Vermeulen | 76f4c61 | 2012-05-15 20:46:14 +0200 | [diff] [blame] | 403 | /* No digits found. */ |
| 404 | return SR_ERR_ARG; |
| 405 | |
| 406 | if (s && *s) { |
| 407 | while (*s == ' ') |
| 408 | s++; |
Petteri Aimonen | 8c012ad | 2012-11-20 21:02:14 +0200 | [diff] [blame] | 409 | if (!strcmp(s, "fs")) |
Bert Vermeulen | 76e107d | 2013-03-30 14:41:01 +0100 | [diff] [blame] | 410 | *q = 1000000000000000ULL; |
Petteri Aimonen | 8c012ad | 2012-11-20 21:02:14 +0200 | [diff] [blame] | 411 | else if (!strcmp(s, "ps")) |
Bert Vermeulen | 76e107d | 2013-03-30 14:41:01 +0100 | [diff] [blame] | 412 | *q = 1000000000000ULL; |
Petteri Aimonen | 8c012ad | 2012-11-20 21:02:14 +0200 | [diff] [blame] | 413 | else if (!strcmp(s, "ns")) |
Bert Vermeulen | 76e107d | 2013-03-30 14:41:01 +0100 | [diff] [blame] | 414 | *q = 1000000000ULL; |
Bert Vermeulen | 76f4c61 | 2012-05-15 20:46:14 +0200 | [diff] [blame] | 415 | else if (!strcmp(s, "us")) |
Bert Vermeulen | 76e107d | 2013-03-30 14:41:01 +0100 | [diff] [blame] | 416 | *q = 1000000; |
Bert Vermeulen | 76f4c61 | 2012-05-15 20:46:14 +0200 | [diff] [blame] | 417 | else if (!strcmp(s, "ms")) |
Bert Vermeulen | 76e107d | 2013-03-30 14:41:01 +0100 | [diff] [blame] | 418 | *q = 1000; |
Bert Vermeulen | 76f4c61 | 2012-05-15 20:46:14 +0200 | [diff] [blame] | 419 | else if (!strcmp(s, "s")) |
Bert Vermeulen | 76e107d | 2013-03-30 14:41:01 +0100 | [diff] [blame] | 420 | *q = 1; |
Bert Vermeulen | 76f4c61 | 2012-05-15 20:46:14 +0200 | [diff] [blame] | 421 | else |
| 422 | /* Must have a time suffix. */ |
| 423 | return SR_ERR_ARG; |
| 424 | } |
| 425 | |
| 426 | return SR_OK; |
| 427 | } |
| 428 | |
| 429 | |
Bert Vermeulen | 76e107d | 2013-03-30 14:41:01 +0100 | [diff] [blame] | 430 | SR_API int sr_parse_voltage(const char *voltstr, uint64_t *p, uint64_t *q) |
Bert Vermeulen | 79afc8c | 2012-05-17 01:54:57 +0200 | [diff] [blame] | 431 | { |
| 432 | char *s; |
| 433 | |
Bert Vermeulen | 76e107d | 2013-03-30 14:41:01 +0100 | [diff] [blame] | 434 | *p = strtoull(voltstr, &s, 10); |
| 435 | if (*p == 0 && s == voltstr) |
Bert Vermeulen | 79afc8c | 2012-05-17 01:54:57 +0200 | [diff] [blame] | 436 | /* No digits found. */ |
| 437 | return SR_ERR_ARG; |
| 438 | |
| 439 | if (s && *s) { |
| 440 | while (*s == ' ') |
| 441 | s++; |
| 442 | if (!strcasecmp(s, "mv")) |
Bert Vermeulen | 76e107d | 2013-03-30 14:41:01 +0100 | [diff] [blame] | 443 | *q = 1000L; |
Bert Vermeulen | 79afc8c | 2012-05-17 01:54:57 +0200 | [diff] [blame] | 444 | else if (!strcasecmp(s, "v")) |
Bert Vermeulen | 76e107d | 2013-03-30 14:41:01 +0100 | [diff] [blame] | 445 | *q = 1; |
Bert Vermeulen | 79afc8c | 2012-05-17 01:54:57 +0200 | [diff] [blame] | 446 | else |
| 447 | /* Must have a base suffix. */ |
| 448 | return SR_ERR_ARG; |
| 449 | } |
| 450 | |
| 451 | return SR_OK; |
| 452 | } |
| 453 | |
Uwe Hermann | 7b870c3 | 2012-10-21 16:13:36 +0200 | [diff] [blame] | 454 | /** @} */ |