SR_API int sr_analog_to_float(const struct sr_datafeed_analog *analog,
float *buf);
-SR_API int sr_analog_float_to_string(float value, unsigned int digits,
- char **result);
SR_API int sr_analog_unit_to_string(const struct sr_datafeed_analog *analog,
char **result);
SR_API void sr_rational_set(struct sr_rational *r, int64_t p, uint64_t q);
return SR_OK;
}
-/**
- * Convert a floating point value to a string, limited to the given
- * number of decimal digits.
- *
- * @param[in] value The value to convert.
- * @param[in] digits Number of digits after the decimal point to print.
- * Must be >= 0.
- * @param[out] result Pointer to store result. Must not be NULL.
- *
- * The string is allocated by the function and must be freed by the caller
- * after use by calling g_free().
- *
- * @retval SR_OK Success.
- * @retval SR_ERR_ARG Invalid argument.
- *
- * @since 0.4.0
- */
-SR_API int sr_analog_float_to_string(float value, unsigned int digits, char **result)
-{
- unsigned int cnt, i;
-
- if (!result)
- return SR_ERR_ARG;
-
- /* This produces at least one too many digits. */
- *result = g_strdup_printf("%.*f", digits, value);
- for (i = 0, cnt = 0; (*result)[i]; i++) {
- if (isdigit((*result)[i++]))
- cnt++;
- if (cnt == digits) {
- (*result)[i] = 0;
- break;
- }
- }
-
- return SR_OK;
-}
-
/**
* Convert the unit/MQ/MQ flags in the analog struct to a string.
*
for (l = analog->meaning->channels, c = 0; l; l = l->next, c++) {
ch = l->data;
g_string_append_printf(*out, "%s: ", ch->name);
- sr_analog_float_to_string(fdata[i * num_channels + c],
- digits, &number);
+ number = g_strdup_printf("%.*f", digits,
+ fdata[i * num_channels + c]);
g_string_append(*out, number);
g_free(number);
g_string_append(*out, " ");
}
END_TEST
-#if 0
-START_TEST(test_analog_float_to_string)
-{
- int ret;
- unsigned int i;
- char *result;
- const char *r[] = {"3", "3.1", "3.14", "3.145", "3.1415", "3.15159"};
-
- for (i = 0; i < ARRAY_SIZE(r); i++) {
- ret = sr_analog_float_to_string(G_PI, i, &result);
- fail_unless(ret == SR_OK);
- fail_unless(result != NULL);
- fail_unless(!strcmp(result, r[i]), "%s != %s", result, r[i]);
- g_free(result);
- }
-}
-END_TEST
-#endif
-
START_TEST(test_analog_float_to_string_null)
{
int ret;
tc = tcase_create("analog_to_float");
tcase_add_test(tc, test_analog_to_float);
tcase_add_test(tc, test_analog_to_float_null);
-#if 0
- tcase_add_test(tc, test_analog_float_to_string);
-#endif
tcase_add_test(tc, test_analog_float_to_string_null);
tcase_add_test(tc, test_analog_unit_to_string);
tcase_add_test(tc, test_analog_unit_to_string_null);