From 9b9bc8a81ad303ffb1e5eef12f34702b2aceef60 Mon Sep 17 00:00:00 2001
From: Jan Breuer <jan.breuer@jaybee.cz>
Date: 周三, 22 4月 2015 03:52:29 +0800
Subject: [PATCH] Support RESPONSE MESSAGE UNIT SEPARATOR, issue #21

---
 libscpi/src/parser.c |   80 +++++++++++++++++++++++++++++----------
 1 files changed, 59 insertions(+), 21 deletions(-)

diff --git a/libscpi/src/parser.c b/libscpi/src/parser.c
index d5728af..f437018 100644
--- a/libscpi/src/parser.c
+++ b/libscpi/src/parser.c
@@ -39,7 +39,8 @@
 
 #include "scpi/config.h"
 #include "scpi/parser.h"
-#include "utils.h"
+#include "scpi/utils_private.h"
+#include "scpi/utils.h"
 #include "scpi/error.h"
 #include "scpi/constants.h"
 
@@ -52,7 +53,7 @@
 
 static void paramSkipBytes(scpi_t * context, size_t num);
 static void paramSkipWhitespace(scpi_t * context);
-static bool_t paramNext(scpi_t * context, bool_t mandatory);
+static scpi_bool_t paramNext(scpi_t * context, scpi_bool_t mandatory);
 
 /*
 int _strnicmp(const char* s1, const char* s2, size_t len) {
@@ -191,6 +192,19 @@
 }
 
 /**
+ * Conditionaly write ";"
+ * @param context
+ * @return number of characters written
+ */
+static size_t writeSemicolon(scpi_t * context) {
+    if (context->output_count > 0) {
+        return writeData(context, ";", 1);
+    } else {
+        return 0;
+    }
+}
+
+/**
  * Process command
  * @param context
  */
@@ -198,6 +212,10 @@
     const scpi_command_t * cmd = context->paramlist.cmd;
 
     context->cmd_error = FALSE;
+
+    /* conditionaly write ; */
+    writeSemicolon(context);
+
     context->output_count = 0;
     context->input_count = 0;
 
@@ -208,9 +226,6 @@
             SCPI_ErrorPush(context, SCPI_ERROR_EXECUTION_ERROR);
         }
     }
-
-    /* conditionaly write new line */
-    writeNewLine(context);
 
     /* skip all whitespaces */
     paramSkipWhitespace(context);
@@ -226,7 +241,7 @@
  * @param context
  * @result TRUE if context->paramlist is filled with correct values
  */
-static bool_t findCommand(scpi_t * context, const char * cmdline_ptr, size_t cmdline_len, size_t cmd_len) {
+static scpi_bool_t findCommand(scpi_t * context, const char * cmdline_ptr, size_t cmdline_len, size_t cmd_len) {
     int32_t i;
     const scpi_command_t * cmd;
 
@@ -236,6 +251,9 @@
             context->paramlist.cmd = cmd;
             context->paramlist.parameters = cmdline_ptr + cmd_len;
             context->paramlist.length = cmdline_len - cmd_len;
+            context->paramlist.cmd_raw.data = cmdline_ptr;
+            context->paramlist.cmd_raw.length = cmd_len;
+            context->paramlist.cmd_raw.position = 0;
             return TRUE;
         }
     }
@@ -262,6 +280,8 @@
         return -1;
     }
 
+    context->output_count = 0;
+
     while (cmdline_ptr < cmdline_end) {
         result = 0;
         cmd_len = cmdTerminatorPos(cmdline_ptr, cmdline_end - cmdline_ptr);
@@ -281,6 +301,10 @@
         cmdline_ptr += skipCmdLine(cmdline_ptr, cmdline_end - cmdline_ptr);
         cmdline_ptr += skipWhitespace(cmdline_ptr, cmdline_end - cmdline_ptr);
     }
+
+    /* conditionaly write new line */
+    writeNewLine(context);
+
     return result;
 }
 
@@ -379,7 +403,7 @@
 size_t SCPI_ResultInt(scpi_t * context, int32_t val) {
     char buffer[12];
     size_t result = 0;
-    size_t len = longToStr(val, buffer, sizeof (buffer));
+    size_t len = SCPI_LongToStr(val, buffer, sizeof (buffer), 10);
     result += writeDelimiter(context);
     result += writeData(context, buffer, len);
     context->output_count++;
@@ -392,7 +416,7 @@
  * @param val
  * @return 
  */
-size_t SCPI_ResultBool(scpi_t * context, bool_t val) {
+size_t SCPI_ResultBool(scpi_t * context, scpi_bool_t val) {
 	return SCPI_ResultInt(context, val ? 1 : 0);
 }
 
@@ -405,7 +429,7 @@
 size_t SCPI_ResultDouble(scpi_t * context, double val) {
     char buffer[32];
     size_t result = 0;
-    size_t len = doubleToStr(val, buffer, sizeof (buffer));
+    size_t len = SCPI_DoubleToStr(val, buffer, sizeof (buffer));
     result += writeDelimiter(context);
     result += writeData(context, buffer, len);
     context->output_count++;
@@ -459,7 +483,7 @@
  * @param mandatory
  * @return 
  */
-bool_t paramNext(scpi_t * context, bool_t mandatory) {
+scpi_bool_t paramNext(scpi_t * context, scpi_bool_t mandatory) {
     paramSkipWhitespace(context);
     if (context->paramlist.length == 0) {
         if (mandatory) {
@@ -487,7 +511,7 @@
  * @param mandatory
  * @return 
  */
-bool_t SCPI_ParamInt(scpi_t * context, int32_t * value, bool_t mandatory) {
+scpi_bool_t SCPI_ParamInt(scpi_t * context, int32_t * value, scpi_bool_t mandatory) {
     const char * param;
     size_t param_len;
     size_t num_len;
@@ -517,7 +541,7 @@
  * @param mandatory
  * @return 
  */
-bool_t SCPI_ParamDouble(scpi_t * context, double * value, bool_t mandatory) {
+scpi_bool_t SCPI_ParamDouble(scpi_t * context, double * value, scpi_bool_t mandatory) {
     const char * param;
     size_t param_len;
     size_t num_len;
@@ -543,12 +567,12 @@
 /**
  * Parse string parameter
  * @param context
- * @param value
- * @param len
+ * @param value Pointer to string buffer where pointer to non-null terminated string will be returned
+ * @param len Length of returned non-null terminated string
  * @param mandatory
  * @return 
  */
-bool_t SCPI_ParamString(scpi_t * context, const char ** value, size_t * len, bool_t mandatory) {
+scpi_bool_t SCPI_ParamString(scpi_t * context, const char ** value, size_t * len, scpi_bool_t mandatory) {
     size_t length;
 
     if (!value || !len) {
@@ -574,12 +598,12 @@
 /**
  * Parse text parameter (can be inside "")
  * @param context
- * @param value
- * @param len
+ * @param value Pointer to string buffer where pointer to non-null terminated string will be returned
+ * @param len Length of returned non-null terminated string
  * @param mandatory
  * @return 
  */
-bool_t SCPI_ParamText(scpi_t * context, const char ** value, size_t * len, bool_t mandatory) {
+scpi_bool_t SCPI_ParamText(scpi_t * context, const char ** value, size_t * len, scpi_bool_t mandatory) {
     size_t length;
 
     if (!value || !len) {
@@ -590,8 +614,10 @@
         return FALSE;
     }
 
+    paramSkipWhitespace(context);
     if (locateText(context->paramlist.parameters, context->paramlist.length, value, &length)) {
-        paramSkipBytes(context, length);
+        paramSkipBytes(context, length + 2);
+        paramSkipWhitespace(context);
         if (len) {
             *len = length;
         }
@@ -608,7 +634,7 @@
  * @param mandatory
  * @return 
  */
-bool_t SCPI_ParamBool(scpi_t * context, bool_t * value, bool_t mandatory) {
+scpi_bool_t SCPI_ParamBool(scpi_t * context, scpi_bool_t * value, scpi_bool_t mandatory) {
     const char * param;
     size_t param_len;
     size_t num_len;
@@ -648,7 +674,7 @@
  * @param mandatory
  * @return 
  */
-bool_t SCPI_ParamChoice(scpi_t * context, const char * options[], int32_t * value, bool_t mandatory) {
+scpi_bool_t SCPI_ParamChoice(scpi_t * context, const char * options[], int32_t * value, scpi_bool_t mandatory) {
     const char * param;
     size_t param_len;
     size_t res;
@@ -672,3 +698,15 @@
     return FALSE;
 }
 
+scpi_bool_t SCPI_IsCmd(scpi_t * context, const char * cmd) {
+    if (! context->paramlist.cmd) {
+        return FALSE;
+    }
+
+    const char * pattern = context->paramlist.cmd->pattern;
+    return matchCommand (pattern, cmd, strlen (cmd));
+}
+
+scpi_bool_t SCPI_Match(const char * pattern, const char * value, size_t len) {
+    return matchCommand (pattern, value, len);
+}

--
Gitblit v1.9.1