From 1f3aae322221514a8df3e06c0adf6844a597b025 Mon Sep 17 00:00:00 2001 From: Jan Breuer <jan.breuer@jaybee.cz> Date: 周一, 10 4月 2017 19:03:01 +0800 Subject: [PATCH] Merge pull request #85 from richardbarlow/master --- libscpi/src/units.c | 42 ++++++++++++++++++++++++------------------ 1 files changed, 24 insertions(+), 18 deletions(-) diff --git a/libscpi/src/units.c b/libscpi/src/units.c index 47c7f9e..3a7b8c2 100644 --- a/libscpi/src/units.c +++ b/libscpi/src/units.c @@ -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, ¶m, &(value->value)); + SCPI_ParamToDouble(context, ¶m, &(value->content.value)); break; case SCPI_TOKEN_HEXNUM: - SCPI_ParamToDouble(context, ¶m, &(value->value)); + SCPI_ParamToDouble(context, ¶m, &(value->content.value)); break; case SCPI_TOKEN_OCTNUM: - SCPI_ParamToDouble(context, ¶m, &(value->value)); + SCPI_ParamToDouble(context, ¶m, &(value->content.value)); break; case SCPI_TOKEN_BINNUM: - SCPI_ParamToDouble(context, ¶m, &(value->value)); + SCPI_ParamToDouble(context, ¶m, &(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, ¶m, &(value->value)); + SCPI_ParamToDouble(context, ¶m, &(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; -- Gitblit v1.9.1