From 24006db953ebc5e672e8b4d14db1d3102670c237 Mon Sep 17 00:00:00 2001
From: Iztok Jeras <iztok.jeras@gmail.com>
Date: 摹曛, 08 10月 2015 03:02:03 +0800
Subject: [PATCH] integer parser: remove trailing spaces

---
 libscpi/src/parser.c |  224 +++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 207 insertions(+), 17 deletions(-)

diff --git a/libscpi/src/parser.c b/libscpi/src/parser.c
index ffd8de1..033535b 100644
--- a/libscpi/src/parser.c
+++ b/libscpi/src/parser.c
@@ -424,7 +424,23 @@
 }
 
 /**
- * Write double value to the result
+ * Write float (32 bit) value to the result
+ * @param context
+ * @param val
+ * @return
+ */
+size_t SCPI_ResultFloat(scpi_t * context, float val) {
+    char buffer[32];
+    size_t result = 0;
+    size_t len = SCPI_FloatToStr(val, buffer, sizeof (buffer));
+    result += writeDelimiter(context);
+    result += writeData(context, buffer, len);
+    context->output_count++;
+    return result;
+}
+
+/**
+ * Write double (64bit) value to the result
  * @param context
  * @param val
  * @return
@@ -437,7 +453,6 @@
     result += writeData(context, buffer, len);
     context->output_count++;
     return result;
-
 }
 
 /**
@@ -591,7 +606,7 @@
  * @param sign
  * @return TRUE if succesful
  */
-static scpi_bool_t ParamToInt32(scpi_t * context, scpi_parameter_t * parameter, int32_t * value, scpi_bool_t sign) {
+static scpi_bool_t ParamSignToUInt32(scpi_t * context, scpi_parameter_t * parameter, uint32_t * value, scpi_bool_t sign) {
 
     if (!value) {
         SCPI_ErrorPush(context, SCPI_ERROR_SYSTEM_ERROR);
@@ -600,17 +615,51 @@
 
     switch (parameter->type) {
         case SCPI_TOKEN_HEXNUM:
-            return strToUInt32(parameter->ptr, (uint32_t *)value, 16) > 0 ? TRUE : FALSE;
+            return strBaseToUInt32(parameter->ptr, value, 16) > 0 ? TRUE : FALSE;
         case SCPI_TOKEN_OCTNUM:
-            return strToUInt32(parameter->ptr, (uint32_t *)value, 8) > 0 ? TRUE : FALSE;
+            return strBaseToUInt32(parameter->ptr, value, 8) > 0 ? TRUE : FALSE;
         case SCPI_TOKEN_BINNUM:
-            return strToUInt32(parameter->ptr, (uint32_t *)value, 2) > 0 ? TRUE : FALSE;
+            return strBaseToUInt32(parameter->ptr, value, 2) > 0 ? TRUE : FALSE;
         case SCPI_TOKEN_DECIMAL_NUMERIC_PROGRAM_DATA:
         case SCPI_TOKEN_DECIMAL_NUMERIC_PROGRAM_DATA_WITH_SUFFIX:
             if (sign) {
-                return strToInt32(parameter->ptr, value, 10) > 0 ? TRUE : FALSE;
+                return strBaseToInt32(parameter->ptr, (int32_t *)value, 10) > 0 ? TRUE : FALSE;
             } else {
-                return strToUInt32(parameter->ptr, (uint32_t *)value, 10) > 0 ? TRUE : FALSE;
+                return strBaseToUInt32(parameter->ptr, value, 10) > 0 ? TRUE : FALSE;
+            }
+        default:
+            return FALSE;
+    }
+}
+
+/**
+ * Convert parameter to signed/unsigned 64 bit integer
+ * @param context
+ * @param parameter
+ * @param value result
+ * @param sign
+ * @return TRUE if succesful
+ */
+static scpi_bool_t ParamSignToUInt64(scpi_t * context, scpi_parameter_t * parameter, uint64_t * value, scpi_bool_t sign) {
+
+    if (!value) {
+        SCPI_ErrorPush(context, SCPI_ERROR_SYSTEM_ERROR);
+        return FALSE;
+    }
+
+    switch (parameter->type) {
+        case SCPI_TOKEN_HEXNUM:
+            return strBaseToUInt64(parameter->ptr, value, 16) > 0 ? TRUE : FALSE;
+        case SCPI_TOKEN_OCTNUM:
+            return strBaseToUInt64(parameter->ptr, value, 8) > 0 ? TRUE : FALSE;
+        case SCPI_TOKEN_BINNUM:
+            return strBaseToUInt64(parameter->ptr, value, 2) > 0 ? TRUE : FALSE;
+        case SCPI_TOKEN_DECIMAL_NUMERIC_PROGRAM_DATA:
+        case SCPI_TOKEN_DECIMAL_NUMERIC_PROGRAM_DATA_WITH_SUFFIX:
+            if (sign) {
+                return strBaseToInt64(parameter->ptr, (int64_t *)value, 10) > 0 ? TRUE : FALSE;
+            } else {
+                return strBaseToUInt64(parameter->ptr, value, 10) > 0 ? TRUE : FALSE;
             }
         default:
             return FALSE;
@@ -625,7 +674,7 @@
  * @return TRUE if succesful
  */
 scpi_bool_t SCPI_ParamToInt32(scpi_t * context, scpi_parameter_t * parameter, int32_t * value) {
-    return ParamToInt32(context, parameter, value, TRUE);
+    return ParamSignToUInt32(context, parameter, (uint32_t *)value, TRUE);
 }
 
 /**
@@ -636,18 +685,40 @@
  * @return TRUE if succesful
  */
 scpi_bool_t SCPI_ParamToUInt32(scpi_t * context,  scpi_parameter_t * parameter, uint32_t * value) {
-    return ParamToInt32(context, parameter, (int32_t *)value, FALSE);
+    return ParamSignToUInt32(context, parameter, value, FALSE);
 }
 
 /**
- * Convert parameter to double
+ * Convert parameter to signed 64 bit integer
+ * @param context
+ * @param parameter
+ * @param value result
+ * @return TRUE if succesful
+ */
+scpi_bool_t SCPI_ParamToInt64(scpi_t * context, scpi_parameter_t * parameter, int64_t * value) {
+    return ParamSignToUInt64(context, parameter, (uint64_t *)value, TRUE);
+}
+
+/**
+ * Convert parameter to unsigned 32 bit integer
+ * @param context
+ * @param parameter
+ * @param value result
+ * @return TRUE if succesful
+ */
+scpi_bool_t SCPI_ParamToUInt64(scpi_t * context,  scpi_parameter_t * parameter, uint64_t * value) {
+    return ParamSignToUInt64(context, parameter, value, FALSE);
+}
+
+/**
+ * Convert parameter to float (32 bit)
  * @param context
  * @param parameter
  * @param value result
  * @return TRUE if succesful
  */
 #include "stdio.h"
-scpi_bool_t SCPI_ParamToDouble(scpi_t * context, scpi_parameter_t * parameter, double * value) {
+scpi_bool_t SCPI_ParamToFloat(scpi_t * context, scpi_parameter_t * parameter, float * value) {
     scpi_bool_t result;
     uint32_t valint;
 
@@ -665,6 +736,40 @@
             break;
         case SCPI_TOKEN_DECIMAL_NUMERIC_PROGRAM_DATA:
         case SCPI_TOKEN_DECIMAL_NUMERIC_PROGRAM_DATA_WITH_SUFFIX:
+            result = strToFloat(parameter->ptr, value) > 0 ? TRUE : FALSE;
+            break;
+        default:
+            result = FALSE;
+    }
+    return result;
+}
+
+/**
+ * Convert parameter to double (64 bit)
+ * @param context
+ * @param parameter
+ * @param value result
+ * @return TRUE if succesful
+ */
+#include "stdio.h"
+scpi_bool_t SCPI_ParamToDouble(scpi_t * context, scpi_parameter_t * parameter, double * value) {
+    scpi_bool_t result;
+    uint64_t valint;
+
+    if (!value) {
+        SCPI_ErrorPush(context, SCPI_ERROR_SYSTEM_ERROR);
+        return FALSE;
+    }
+
+    switch (parameter->type) {
+        case SCPI_TOKEN_HEXNUM:
+        case SCPI_TOKEN_OCTNUM:
+        case SCPI_TOKEN_BINNUM:
+            result = SCPI_ParamToUInt64(context, parameter, &valint);
+            *value = valint;
+            break;
+        case SCPI_TOKEN_DECIMAL_NUMERIC_PROGRAM_DATA:
+        case SCPI_TOKEN_DECIMAL_NUMERIC_PROGRAM_DATA_WITH_SUFFIX:
             result = strToDouble(parameter->ptr, value) > 0 ? TRUE : FALSE;
             break;
         default:
@@ -674,7 +779,38 @@
 }
 
 /**
- * Read floating point parameter
+ * Read floating point float (32 bit) parameter
+ * @param context
+ * @param value
+ * @param mandatory
+ * @return
+ */
+scpi_bool_t SCPI_ParamFloat(scpi_t * context, float * value, scpi_bool_t mandatory) {
+    scpi_bool_t result;
+    scpi_parameter_t param;
+
+    if (!value) {
+        SCPI_ErrorPush(context, SCPI_ERROR_SYSTEM_ERROR);
+        return FALSE;
+    }
+
+    result = SCPI_Parameter(context, &param, mandatory);
+    if (result) {
+        if (SCPI_ParamIsNumber(&param, FALSE)) {
+            SCPI_ParamToFloat(context, &param, value);
+        } else if (SCPI_ParamIsNumber(&param, TRUE)) {
+            SCPI_ErrorPush(context, SCPI_ERROR_SUFFIX_NOT_ALLOWED);
+            result = FALSE;
+        } else {
+            SCPI_ErrorPush(context, SCPI_ERROR_DATA_TYPE_ERROR);
+            result = FALSE;
+        }
+    }
+    return result;
+}
+
+/**
+ * Read floating point double (64 bit) parameter
  * @param context
  * @param value
  * @param mandatory
@@ -712,7 +848,7 @@
  * @param sign
  * @return
  */
-static scpi_bool_t ParamInt32(scpi_t * context, int32_t * value, scpi_bool_t mandatory, scpi_bool_t sign) {
+static scpi_bool_t ParamSignUInt32(scpi_t * context, uint32_t * value, scpi_bool_t mandatory, scpi_bool_t sign) {
     scpi_bool_t result;
     scpi_parameter_t param;
 
@@ -724,7 +860,39 @@
     result = SCPI_Parameter(context, &param, mandatory);
     if (result) {
         if (SCPI_ParamIsNumber(&param, FALSE)) {
-            result = ParamToInt32(context, &param, value, sign);
+            result = ParamSignToUInt32(context, &param, value, sign);
+        } else if (SCPI_ParamIsNumber(&param, TRUE)) {
+            SCPI_ErrorPush(context, SCPI_ERROR_SUFFIX_NOT_ALLOWED);
+            result = FALSE;
+        } else {
+            SCPI_ErrorPush(context, SCPI_ERROR_DATA_TYPE_ERROR);
+            result = FALSE;
+        }
+    }
+    return result;
+}
+
+/**
+ * Read signed/unsigned 64 bit integer parameter
+ * @param context
+ * @param value
+ * @param mandatory
+ * @param sign
+ * @return
+ */
+static scpi_bool_t ParamSignUInt64(scpi_t * context, uint64_t * value, scpi_bool_t mandatory, scpi_bool_t sign) {
+    scpi_bool_t result;
+    scpi_parameter_t param;
+
+    if (!value) {
+        SCPI_ErrorPush(context, SCPI_ERROR_SYSTEM_ERROR);
+        return FALSE;
+    }
+
+    result = SCPI_Parameter(context, &param, mandatory);
+    if (result) {
+        if (SCPI_ParamIsNumber(&param, FALSE)) {
+            result = ParamSignToUInt64(context, &param, value, sign);
         } else if (SCPI_ParamIsNumber(&param, TRUE)) {
             SCPI_ErrorPush(context, SCPI_ERROR_SUFFIX_NOT_ALLOWED);
             result = FALSE;
@@ -744,7 +912,7 @@
  * @return
  */
 scpi_bool_t SCPI_ParamInt32(scpi_t * context, int32_t * value, scpi_bool_t mandatory) {
-    return ParamInt32(context, value, mandatory, TRUE);
+    return ParamSignUInt32(context, (uint32_t *)value, mandatory, TRUE);
 }
 
 /**
@@ -755,7 +923,29 @@
  * @return
  */
 scpi_bool_t SCPI_ParamUInt32(scpi_t * context, uint32_t * value, scpi_bool_t mandatory) {
-    return ParamInt32(context, (int32_t *)value, mandatory, FALSE);
+    return ParamSignUInt32(context, value, mandatory, FALSE);
+}
+
+/**
+ * Read signed 64 bit integer parameter
+ * @param context
+ * @param value
+ * @param mandatory
+ * @return
+ */
+scpi_bool_t SCPI_ParamInt64(scpi_t * context, int64_t * value, scpi_bool_t mandatory) {
+    return ParamSignUInt64(context, (uint64_t *)value, mandatory, TRUE);
+}
+
+/**
+ * Read unsigned 64 bit integer parameter
+ * @param context
+ * @param value
+ * @param mandatory
+ * @return
+ */
+scpi_bool_t SCPI_ParamUInt64(scpi_t * context, uint64_t * value, scpi_bool_t mandatory) {
+    return ParamSignUInt64(context, value, mandatory, FALSE);
 }
 
 /**

--
Gitblit v1.9.1