Jan Breuer
2017-08-15 3192f8a0084f8a6f82ac18e187d9db30e524ff9b
libscpi/src/units.c
@@ -63,7 +63,7 @@
 * units definition IEEE 488.2-1992 tab 7-1
 */
const scpi_unit_def_t scpi_units_def[] = {
#if USE_UNITS_PARICLES
#if USE_UNITS_PARTICLES
    /* Absorbet dose */
    {/* name */ "GY", /* unit */ SCPI_UNIT_GRAY, /* mult */ 1},
@@ -90,7 +90,7 @@
    /* Mass */
    {/* name */ "U", /* unit */ SCPI_UNIT_ATOMIC_MASS, /* mult */ 1},
#endif /* USE_UNITS_PARICLES */
#endif /* USE_UNITS_PARTICLES */
#if USE_UNITS_ANGLE
    /* Angle */
@@ -248,7 +248,7 @@
    /* Temperature */
    {/* name */ "CEL", /* unit */ SCPI_UNIT_CELSIUS, /* mult */ 1},
#if USE_UNITS_IMPERIAL
    {/* name */ "FAR", /* unit */ SCPI_UNIT_FAGRENHEIT, /* mult */ 1},
    {/* name */ "FAR", /* unit */ SCPI_UNIT_FAHRENHEIT, /* mult */ 1},
#endif /* USE_UNITS_IMPERIAL */
    {/* name */ "K", /* unit */ SCPI_UNIT_KELVIN, /* mult */ 1},
#endif /* USE_UNITS_TEMPERATURE */
@@ -355,7 +355,7 @@
        return FALSE;
    }
    value->value *= unitDef->mult;
    value->content.value *= unitDef->mult;
    value->unit = unitDef->unit;
    return TRUE;
@@ -426,23 +426,23 @@
    switch (param.type) {
        case SCPI_TOKEN_DECIMAL_NUMERIC_PROGRAM_DATA:
            SCPI_ParamToDouble(context, &param, &(value->value));
            SCPI_ParamToDouble(context, &param, &(value->content.value));
            break;
        case SCPI_TOKEN_HEXNUM:
            SCPI_ParamToDouble(context, &param, &(value->value));
            SCPI_ParamToDouble(context, &param, &(value->content.value));
            break;
        case SCPI_TOKEN_OCTNUM:
            SCPI_ParamToDouble(context, &param, &(value->value));
            SCPI_ParamToDouble(context, &param, &(value->content.value));
            break;
        case SCPI_TOKEN_BINNUM:
            SCPI_ParamToDouble(context, &param, &(value->value));
            SCPI_ParamToDouble(context, &param, &(value->content.value));
            break;
        case SCPI_TOKEN_DECIMAL_NUMERIC_PROGRAM_DATA_WITH_SUFFIX:
            scpiLex_DecimalNumericProgramData(&state, &token);
            scpiLex_WhiteSpace(&state, &token);
            scpiLex_SuffixProgramData(&state, &token);
            SCPI_ParamToDouble(context, &param, &(value->value));
            SCPI_ParamToDouble(context, &param, &(value->content.value));
            result = transformNumber(context, token.ptr, token.len, value);
            break;
@@ -454,7 +454,7 @@
            result = SCPI_ParamToChoice(context, &token, special, &tag);
            value->special = TRUE;
            value->tag = tag;
            value->content.tag = tag;
            break;
        default:
@@ -469,7 +469,7 @@
 * @param context
 * @param value number value
 * @param str target string
 * @param len max length of string
 * @param len max length of string including null-character termination
 * @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) {
@@ -477,28 +477,34 @@
    const char * unit;
    size_t result;
    if (!value || !str) {
    if (!value || !str || len==0) {
        return 0;
    }
    if (value->special) {
        if (SCPI_ChoiceToName(special, value->tag, &type)) {
        if (SCPI_ChoiceToName(special, value->content.tag, &type)) {
            strncpy(str, type, len);
            return min(strlen(type), len);
            result = SCPIDEFINE_strnlen(str, len - 1);
            str[result] = '\0';
            return result;
        } else {
            str[0] = 0;
            str[0] = '\0';
            return 0;
        }
    }
    result = SCPI_DoubleToStr(value->value, str, len);
    result = SCPI_DoubleToStr(value->content.value, str, len);
    unit = translateUnitInverse(context->units, value->unit);
    if (result + 1 < len) {
        unit = translateUnitInverse(context->units, value->unit);
    if (unit) {
        strncat(str, " ", len);
        strncat(str, unit, len);
        result += strlen(unit) + 1;
        if (unit) {
            strncat(str, " ", len - result);
            if (result + 2 < len) {
                strncat(str, unit, len - result - 1);
            }
            result = strlen(str);
        }
    }
    return result;