From b5e8e4dc2bdeca39195a78d8d7f01bd7f099923c Mon Sep 17 00:00:00 2001
From: Jan Breuer <jan.breuer@jaybee.cz>
Date: 周五, 13 11月 2015 18:54:29 +0800
Subject: [PATCH] Resolve #65: Cast char to unsigned in isctype functions

---
 libscpi/src/units.c |  195 +++++++++++++++++++++++++++---------------------
 1 files changed, 108 insertions(+), 87 deletions(-)

diff --git a/libscpi/src/units.c b/libscpi/src/units.c
index 3583ef5..f59fe86 100644
--- a/libscpi/src/units.c
+++ b/libscpi/src/units.c
@@ -37,9 +37,10 @@
 #include <string.h>
 #include "scpi/parser.h"
 #include "scpi/units.h"
-#include "utils.h"
+#include "utils_private.h"
+#include "scpi/utils.h"
 #include "scpi/error.h"
-#include "scpi/lexer.h"
+#include "lexer_private.h"
 
 
 /*
@@ -103,68 +104,18 @@
 /*
  * Special number values definition
  */
-const scpi_special_number_def_t scpi_special_numbers_def[] = {
+const scpi_choice_def_t scpi_special_numbers_def[] = {
     {/* name */ "MINimum", /* type */ SCPI_NUM_MIN},
     {/* name */ "MAXimum", /* type */ SCPI_NUM_MAX},
     {/* name */ "DEFault", /* type */ SCPI_NUM_DEF},
     {/* name */ "UP", /* type */ SCPI_NUM_UP},
     {/* name */ "DOWN", /* type */ SCPI_NUM_DOWN},
     {/* name */ "NAN", /* type */ SCPI_NUM_NAN},
-    {/* name */ "INF", /* type */ SCPI_NUM_INF},
+    {/* name */ "INFinity", /* type */ SCPI_NUM_INF},
     {/* name */ "NINF", /* type */ SCPI_NUM_NINF},
-    SCPI_SPECIAL_NUMBERS_LIST_END,
+    {/* name */ "AUTO", /* type */ SCPI_NUM_AUTO},
+    SCPI_CHOICE_LIST_END,
 };
-
-/**
- * Match string constant to one of special number values
- * @param specs specifications of special numbers (patterns)
- * @param str string to be recognised
- * @param len length of string
- * @param value resultin value
- * @return TRUE if str matches one of specs patterns
- */
-static bool_t translateSpecialNumber(const scpi_special_number_def_t * specs, const char * str, size_t len, scpi_number_parameter_t * value) {
-    int i;
-
-    value->value = 0.0;
-    value->unit = SCPI_UNIT_NONE;
-    value->type = SCPI_NUM_NUMBER;
-
-    if (specs == NULL) {
-        return FALSE;
-    }
-
-    for (i = 0; specs[i].name != NULL; i++) {
-        if (matchPattern(specs[i].name, strlen(specs[i].name), str, len)) {
-            value->type = specs[i].type;
-            return TRUE;
-        }
-    }
-
-    return FALSE;
-}
-
-/**
- * Convert special number type to its string representation
- * @param specs specifications of special numbers (patterns)
- * @param type type of special number
- * @return String representing special number or NULL
- */
-static const char * translateSpecialNumberInverse(const scpi_special_number_def_t * specs, scpi_special_number_t type) {
-    int i;
-
-    if (specs == NULL) {
-        return NULL;
-    }
-
-    for (i = 0; specs[i].name != NULL; i++) {
-        if (specs[i].type == type) {
-            return specs[i].name;
-        }
-    }
-
-    return NULL;
-}
 
 /**
  * Convert string describing unit to its representation
@@ -219,7 +170,7 @@
  * @param value preparsed numeric value
  * @return TRUE if value parameter was converted to base units
  */
-static bool_t transformNumber(scpi_t * context, const char * unit, size_t len, scpi_number_parameter_t * value) {
+static scpi_bool_t transformNumber(scpi_t * context, const char * unit, size_t len, scpi_number_t * value) {
     size_t s;
     const scpi_unit_def_t * unitDef;
     s = skipWhitespace(unit, len);
@@ -249,33 +200,100 @@
  * @param mandatory if the parameter is mandatory
  * @return 
  */
-bool_t SCPI_ParamTranslateNumberVal(scpi_t * context, scpi_parameter_t * parameter) {
-    token_t token;
+scpi_bool_t SCPI_ParamNumber(scpi_t * context, const scpi_choice_def_t * special, scpi_number_t * value, scpi_bool_t mandatory) {
+    scpi_token_t token;
     lex_state_t state;
+    scpi_parameter_t param;
+    scpi_bool_t result;
+    int32_t tag;
 
-    state.buffer = parameter->data.ptr;
-    state.pos = state.buffer;
-    state.len = parameter->data.len;
-
-    if (parameter->type == TokDecimalNumericProgramDataWithSuffix) {
-        SCPI_LexDecimalNumericProgramData(&state, &token);
-        SCPI_LexWhiteSpace(&state, &token);
-        SCPI_LexSuffixProgramData(&state, &token);
-
-        return transformNumber(context, token.ptr, token.len, &parameter->number);
-
-    } else if (parameter->type == TokProgramMnemonic) {
-        SCPI_LexWhiteSpace(&state, &token);
-        SCPI_LexCharacterProgramData(&state, &token);
-
-        /* convert string to special number type */
-        if (translateSpecialNumber(context->special_numbers, token.ptr, token.len, &parameter->number)) {
-            /* found special type */
-            return TRUE;
-        }
+    if (!value) {
+        SCPI_ErrorPush(context, SCPI_ERROR_SYSTEM_ERROR);
+        return FALSE;
     }
 
-    return FALSE;
+    result = SCPI_Parameter(context, &param, mandatory);
+
+    if (!result) {
+        return result;
+    }
+
+    state.buffer = param.ptr;
+    state.pos = state.buffer;
+    state.len = param.len;
+
+    switch (param.type) {
+        case SCPI_TOKEN_DECIMAL_NUMERIC_PROGRAM_DATA:
+        case SCPI_TOKEN_HEXNUM:
+        case SCPI_TOKEN_OCTNUM:
+        case SCPI_TOKEN_BINNUM:
+        case SCPI_TOKEN_DECIMAL_NUMERIC_PROGRAM_DATA_WITH_SUFFIX:
+        case SCPI_TOKEN_PROGRAM_MNEMONIC:
+            value->unit = SCPI_UNIT_NONE;
+            value->special = FALSE;
+            result = TRUE;
+            break;
+        default:
+            break;
+    }
+
+    switch (param.type) {
+        case SCPI_TOKEN_DECIMAL_NUMERIC_PROGRAM_DATA:
+        case SCPI_TOKEN_DECIMAL_NUMERIC_PROGRAM_DATA_WITH_SUFFIX:
+        case SCPI_TOKEN_PROGRAM_MNEMONIC:
+            value->base = 10;
+            break;
+        case SCPI_TOKEN_BINNUM:
+            value->base = 2;
+            break;
+        case SCPI_TOKEN_HEXNUM:
+            value->base = 16;
+            break;
+        case SCPI_TOKEN_OCTNUM:
+            value->base = 8;
+            break;
+        default:
+            break;
+    }
+
+    switch (param.type) {
+        case SCPI_TOKEN_DECIMAL_NUMERIC_PROGRAM_DATA:
+            SCPI_ParamToDouble(context, &param, &(value->value));
+            break;
+        case SCPI_TOKEN_HEXNUM:
+            SCPI_ParamToDouble(context, &param, &(value->value));
+            break;
+        case SCPI_TOKEN_OCTNUM:
+            SCPI_ParamToDouble(context, &param, &(value->value));
+            break;
+        case SCPI_TOKEN_BINNUM:
+            SCPI_ParamToDouble(context, &param, &(value->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));
+
+            result = transformNumber(context, token.ptr, token.len, value);
+            break;
+        case SCPI_TOKEN_PROGRAM_MNEMONIC:
+            scpiLex_WhiteSpace(&state, &token);
+            scpiLex_CharacterProgramData(&state, &token);
+
+            /* convert string to special number type */
+            result = SCPI_ParamToChoice(context, &token, special, &tag);
+
+            value->special = TRUE;
+            value->tag = tag;
+
+            break;
+        default:
+            result = FALSE;
+    }
+
+    return result;
 }
 
 /**
@@ -286,7 +304,7 @@
  * @param len max length of string
  * @return number of chars written to string
  */
-size_t SCPI_NumberToStr(scpi_t * context, scpi_number_parameter_t * value, char * str, size_t len) {
+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;
@@ -295,14 +313,17 @@
         return 0;
     }
 
-    type = translateSpecialNumberInverse(context->special_numbers, value->type);
-
-    if (type) {
-        strncpy(str, type, len);
-        return min(strlen(type), len);
+    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 = doubleToStr(value->value, str, len);
+    result = SCPI_DoubleToStr(value->value, str, len);
 
     unit = translateUnitInverse(context->units, value->unit);
 

--
Gitblit v1.9.1