From 811e11d96f5ae2fa6972ac1bf095eeebd9486f9a Mon Sep 17 00:00:00 2001
From: Andrey Nakin <andrey.nakin@gmail.com>
Date: 周一, 21 10月 2013 03:48:06 +0800
Subject: [PATCH] add function SCPI_ParamChoice

---
 libscpi/src/parser.c |   82 +++++++++++++++++++++++++++++++++++++++++
 1 files changed, 82 insertions(+), 0 deletions(-)

diff --git a/libscpi/src/parser.c b/libscpi/src/parser.c
index a6acd6e..fb6396f 100644
--- a/libscpi/src/parser.c
+++ b/libscpi/src/parser.c
@@ -364,6 +364,16 @@
 }
 
 /**
+ * Write boolean value to the result
+ * @param context
+ * @param val
+ * @return 
+ */
+size_t SCPI_ResultBool(scpi_t * context, bool_t val) {
+	return SCPI_ResultInt(context, val ? 1 : 0);
+}
+
+/**
  * Write double walue to the result
  * @param context
  * @param val
@@ -567,3 +577,75 @@
 
     return FALSE;
 }
+
+/**
+ * Parse boolean parameter
+ * @param context
+ * @param value
+ * @param mandatory
+ * @return 
+ */
+bool_t SCPI_ParamBool(scpi_t * context, bool_t * value, bool_t mandatory) {
+    const char * param;
+    size_t param_len;
+    size_t num_len;
+    int32_t i;
+
+    if (!value) {
+        return FALSE;
+    }
+
+    if (!SCPI_ParamString(context, &param, &param_len, mandatory)) {
+        return FALSE;
+    }
+
+    if (matchPattern("ON", 2, param, param_len)) {
+        *value = TRUE;
+    } else if (matchPattern("OFF", 3, param, param_len)) {
+        *value = FALSE;
+    } else {
+        num_len = strToLong(param, &i);
+
+        if (num_len != param_len) {
+            SCPI_ErrorPush(context, SCPI_ERROR_SUFFIX_NOT_ALLOWED);
+            return FALSE;
+        }
+
+        *value = i ? TRUE : FALSE;
+    }
+
+    return TRUE;
+}
+
+/**
+ * Parse choice parameter
+ * @param context
+ * @param options
+ * @param value
+ * @param mandatory
+ * @return 
+ */
+bool_t SCPI_ParamChoice(scpi_t * context, const char * options[], size_t * value, bool_t mandatory) {
+    const char * param;
+    size_t param_len;
+    size_t res;
+
+    if (!options || !value) {
+        return FALSE;
+    }
+
+    if (!SCPI_ParamString(context, &param, &param_len, mandatory)) {
+        return FALSE;
+    }
+
+	for (res = 0; options[res]; ++res) {
+	    if (matchPattern(options[res], strlen(options[res]), param, param_len)) {
+			*value = res;
+			return TRUE;
+		}
+    }
+
+	SCPI_ErrorPush(context, SCPI_ERROR_ILLEGAL_PARAMETER_VALUE);
+    return FALSE;
+}
+

--
Gitblit v1.9.1