Example #1
0
/**
 * Convert scpi_number_t to string
 * @param context
 * @param value number value
 * @param str target string
 * @param len max length of string
 * @return number of chars written to string
 */
size_t SCPI_NumberToStr(scpi_t * context, const scpi_choice_def_t * special, scpi_number_t * value, char * str, size_t len) {
    const char * type;
    const char * unit;
    size_t result;

    if (!value || !str) {
        return 0;
    }

    if (value->special) {
        if (SCPI_ChoiceToName(special, value->tag, &type)) {
            strncpy(str, type, len);
            return min(strlen(type), len);
        } else {
            str[0] = 0;
            return 0;
        }
    }

    result = SCPI_DoubleToStr(value->value, str, len);

    unit = translateUnitInverse(context->units, value->unit);

    if (unit) {
        strncat(str, " ", len);
        strncat(str, unit, len);
        result += strlen(unit) + 1;
    }

    return result;
}
Example #2
0
/**
 * Write double (64bit) value to the result
 * @param context
 * @param val
 * @return
 */
size_t SCPI_ResultDouble(scpi_t * context, double val) {
    char buffer[32];
    size_t result = 0;
    size_t len = SCPI_DoubleToStr(val, buffer, sizeof (buffer));
    result += writeDelimiter(context);
    result += writeData(context, buffer, len);
    context->output_count++;
    return result;
}
Example #3
0
static void test_doubleToStr() {
    const size_t max = 49 + 1;
    double val[] = {1, -1, 1.1, -1.1, 1e3, 1e30, -1.3e30, -1.3e-30};
    int N = sizeof (val) / sizeof (*val);
    int i;
    char str[max];
    char ref[max];
    size_t len;

    for (i = 0; i < N; i++) {
        len = SCPI_DoubleToStr(val[i], str, max);
        snprintf(ref, max, "%.15lg", val[i]);
        CU_ASSERT(len == strlen(ref));
        CU_ASSERT_STRING_EQUAL(str, ref);
    }
}