+ * Parse a floating point text into an analog value.
+ *
+ * @param[in] column The input text, a floating point number.
+ * @param[in] inc The input module's context.
+ * @param[in] details The column processing details.
+ *
+ * @retval SR_OK Success.
+ * @retval SR_ERR Invalid input data (empty, or format error).
+ *
+ * This routine modifies the analog values in the current sample set,
+ * based on the text input and a user provided format spec.
+ */
+static int parse_analog(const char *column, struct context *inc,
+ const struct column_details *details)
+{
+ size_t length;
+ double dvalue; float fvalue;
+ csv_analog_t value;
+ int ret;
+
+ if (!format_is_analog(details->text_format))
+ return SR_ERR_BUG;
+
+ length = strlen(column);
+ if (!length) {
+ sr_err("Column %zu in line %zu is empty.", details->col_nr,
+ inc->line_number);
+ return SR_ERR;
+ }
+ if (sizeof(value) == sizeof(double)) {
+ ret = sr_atod_ascii(column, &dvalue);
+ value = dvalue;
+ } else if (sizeof(value) == sizeof(float)) {
+ ret = sr_atof_ascii(column, &fvalue);
+ value = fvalue;
+ } else {
+ ret = SR_ERR_BUG;
+ }
+ if (ret != SR_OK) {
+ sr_err("Cannot parse analog text %s in column %zu in line %zu.",
+ column, details->col_nr, inc->line_number);
+ return SR_ERR_DATA;
+ }
+ set_analog_value(inc, details->channel_offset, value);
+
+ return SR_OK;
+}
+
+/**
+ * Parse a timestamp text, auto-determine samplerate.
+ *
+ * @param[in] column The input text, a floating point number.
+ * @param[in] inc The input module's context.
+ * @param[in] details The column processing details.
+ *
+ * @retval SR_OK Success.
+ * @retval SR_ERR Invalid input data (empty, or format error).
+ *
+ * This routine attempts to automatically determine the input data's
+ * samplerate from text rows' timestamp values. Only simple formats are
+ * supported, user provided values always take precedence.
+ */
+static int parse_timestamp(const char *column, struct context *inc,
+ const struct column_details *details)
+{
+ double ts, rate;
+ int ret;
+
+ if (!format_is_timestamp(details->text_format))
+ return SR_ERR_BUG;
+
+ /*
+ * Implementor's notes on timestamp interpretation. Use a simple
+ * approach for improved maintainability which covers most cases
+ * of input data. There is not much gain in adding complexity,
+ * users can easily provide the rate when auto-detection fails.
+ * - Bail out if samplerate is known already.
+ * - Try to interpret the timestamp (simple float conversion).
+ * If conversion fails then clear all previous knowledge and
+ * bail out (non-fatal, perhaps warn). Silently ignore values
+ * of zero since those could be silent fails -- assume that
+ * genuine data contains at least two adjacent rows with useful
+ * timestamps for the feature to work reliably. Annoying users
+ * with "failed to detect" messages is acceptable here, since
+ * users expecting the feature to work should provide useful
+ * data, and there are easy ways to disable the detection or
+ * ignore the column.
+ * - If there is no previous timestamp, keep the current value
+ * for later reference and bail out.
+ * - If a previous timestamp was seen, determine the difference
+ * between them, and derive the samplerate. Update internal
+ * state (the value automatically gets sent to the datafeed),
+ * and clear previous knowledge. Subsequent calls will ignore
+ * following input data (see above, rate is known).
+ *
+ * TODO Potential future improvements:
+ * - Prefer rationals over floats for improved precision and
+ * reduced rounding errors which result in odd rates.
+ * - Support other formats ("2 ms" or similar)?
+ */
+ if (inc->calc_samplerate)
+ return SR_OK;
+ ret = sr_atod_ascii(column, &ts);
+ if (ret != SR_OK)
+ ts = 0.0;
+ if (!ts) {
+ sr_info("Cannot convert timestamp text %s in line %zu (or zero value).",
+ column, inc->line_number);
+ inc->prev_timestamp = 0.0;
+ return SR_OK;
+ }
+ if (!inc->prev_timestamp) {
+ sr_dbg("First timestamp value %g in line %zu.",
+ ts, inc->line_number);
+ inc->prev_timestamp = ts;
+ return SR_OK;
+ }
+ sr_dbg("Second timestamp value %g in line %zu.", ts, inc->line_number);
+ ts -= inc->prev_timestamp;
+ sr_dbg("Timestamp difference %g in line %zu.",
+ ts, inc->line_number);
+ if (!ts) {
+ sr_warn("Zero timestamp difference in line %zu.",
+ inc->line_number);
+ inc->prev_timestamp = ts;
+ return SR_OK;
+ }
+ rate = 1.0 / ts;
+ rate += 0.5;
+ rate = (uint64_t)rate;
+ sr_dbg("Rate from timestamp %g in line %zu.", rate, inc->line_number);
+ inc->calc_samplerate = rate;
+ inc->prev_timestamp = 0.0;
+
+ return SR_OK;
+}
+
+/**
+ * Parse routine which ignores the input text.