* Convert a numeric frequency value to the "natural" string representation * of its period. * * E.g. a value of 3000000 would be converted to "3 us", 20000 to "50 ms". * * @param frequency The frequency in Hz. * * @return A malloc()ed string representation of the frequency value, * or NULL upon errors. The caller is responsible to g_free() the * memory. */
| 177 | * memory. |
| 178 | */ |
| 179 | SR_API char *sr_period_string(uint64_t frequency) |
| 180 | { |
| 181 | char *o; |
| 182 | int r; |
| 183 | |
| 184 | /* Allocate enough for a uint64_t as string + " ms". */ |
| 185 | if (!(o = malloc(30 + 1))) { |
| 186 | sr_err("%s: o malloc failed", __func__); |
| 187 | return NULL; |
| 188 | } |
| 189 | |
| 190 | if (frequency >= SR_GHZ(1)) |
| 191 | r = snprintf(o, 30, "%llu ns", (u64_t)(frequency / 1000000000)); |
| 192 | else if (frequency >= SR_MHZ(1)) |
| 193 | r = snprintf(o, 30, "%llu us", (u64_t)(frequency / 1000000)); |
| 194 | else if (frequency >= SR_KHZ(1)) |
| 195 | r = snprintf(o, 30, "%llu ms", (u64_t)(frequency / 1000)); |
| 196 | else |
| 197 | r = snprintf(o, 30, "%llu s", (u64_t)frequency); |
| 198 | |
| 199 | if (r < 0) { |
| 200 | /* Something went wrong... */ |
| 201 | g_free(o); |
| 202 | return NULL; |
| 203 | } |
| 204 | |
| 205 | return o; |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Convert a numeric time(ns) value to the "natural" string representation |
no outgoing calls
no test coverage detected