From 06ca0ab8991fe36731ae715a072fb12cc22abcca Mon Sep 17 00:00:00 2001
From: Jan Breuer <jan.breuer@jaybee.cz>
Date: 周一, 24 6月 2013 22:19:34 +0800
Subject: [PATCH] Update documentation

---
 libscpi/src/units.c |  193 +++++++++++++++++++++++++++++-------------------
 1 files changed, 116 insertions(+), 77 deletions(-)

diff --git a/libscpi/src/units.c b/libscpi/src/units.c
index a22f770..3583ef5 100644
--- a/libscpi/src/units.c
+++ b/libscpi/src/units.c
@@ -39,6 +39,7 @@
 #include "scpi/units.h"
 #include "utils.h"
 #include "scpi/error.h"
+#include "scpi/lexer.h"
 
 
 /*
@@ -62,78 +63,100 @@
  */
 const scpi_unit_def_t scpi_units_def[] = {
     /* voltage */
-    { .name = "UV", .unit = SCPI_UNIT_VOLT, .mult = 1e-6},
-    { .name = "MV", .unit = SCPI_UNIT_VOLT, .mult = 1e-3},
-    { .name = "V", .unit = SCPI_UNIT_VOLT, .mult = 1},
-    { .name = "KV", .unit = SCPI_UNIT_VOLT, .mult = 1e3},
+    {/* name */ "UV", /* unit */ SCPI_UNIT_VOLT, /* mult */ 1e-6},
+    {/* name */ "MV", /* unit */ SCPI_UNIT_VOLT, /* mult */ 1e-3},
+    {/* name */ "V", /* unit */ SCPI_UNIT_VOLT, /* mult */ 1},
+    {/* name */ "KV", /* unit */ SCPI_UNIT_VOLT, /* mult */ 1e3},
 
     /* current */
-    { .name = "UA", .unit = SCPI_UNIT_AMPER, .mult = 1e-6},
-    { .name = "MA", .unit = SCPI_UNIT_AMPER, .mult = 1e-3},
-    { .name = "A", .unit = SCPI_UNIT_AMPER, .mult = 1},
-    { .name = "KA", .unit = SCPI_UNIT_AMPER, .mult = 1e3},
+    {/* name */ "UA", /* unit */ SCPI_UNIT_AMPER, /* mult */ 1e-6},
+    {/* name */ "MA", /* unit */ SCPI_UNIT_AMPER, /* mult */ 1e-3},
+    {/* name */ "A", /* unit */ SCPI_UNIT_AMPER, /* mult */ 1},
+    {/* name */ "KA", /* unit */ SCPI_UNIT_AMPER, /* mult */ 1e3},
 
     /* resistance */
-    { .name = "OHM", .unit = SCPI_UNIT_OHM, .mult = 1},
-    { .name = "KOHM", .unit = SCPI_UNIT_OHM, .mult = 1e3},
-    { .name = "MOHM", .unit = SCPI_UNIT_OHM, .mult = 1e6},
+    {/* name */ "OHM", /* unit */ SCPI_UNIT_OHM, /* mult */ 1},
+    {/* name */ "KOHM", /* unit */ SCPI_UNIT_OHM, /* mult */ 1e3},
+    {/* name */ "MOHM", /* unit */ SCPI_UNIT_OHM, /* mult */ 1e6},
 
     /* frequency */
-    { .name = "HZ", .unit = SCPI_UNIT_HERTZ, .mult = 1},
-    { .name = "KHZ", .unit = SCPI_UNIT_HERTZ, .mult = 1e3},
-    { .name = "MHZ", .unit = SCPI_UNIT_HERTZ, .mult = 1e6},
-    { .name = "GHZ", .unit = SCPI_UNIT_HERTZ, .mult = 1e9},
+    {/* name */ "HZ", /* unit */ SCPI_UNIT_HERTZ, /* mult */ 1},
+    {/* name */ "KHZ", /* unit */ SCPI_UNIT_HERTZ, /* mult */ 1e3},
+    {/* name */ "MHZ", /* unit */ SCPI_UNIT_HERTZ, /* mult */ 1e6},
+    {/* name */ "GHZ", /* unit */ SCPI_UNIT_HERTZ, /* mult */ 1e9},
 
     /* temperature */
-    { .name = "CEL", .unit = SCPI_UNIT_CELSIUS, .mult = 1},
+    {/* name */ "CEL", /* unit */ SCPI_UNIT_CELSIUS, /* mult */ 1},
 
     /* time */
-    { .name = "PS", .unit = SCPI_UNIT_SECONDS, .mult = 1e-12},
-    { .name = "NS", .unit = SCPI_UNIT_SECONDS, .mult = 1e-9},
-    { .name = "US", .unit = SCPI_UNIT_SECONDS, .mult = 1e-6},
-    { .name = "MS", .unit = SCPI_UNIT_SECONDS, .mult = 1e-3},
-    { .name = "S", .unit = SCPI_UNIT_SECONDS, .mult = 1},
-    { .name = "MIN", .unit = SCPI_UNIT_SECONDS, .mult = 60},
-    { .name = "HR", .unit = SCPI_UNIT_SECONDS, .mult = 3600},
+    {/* name */ "PS", /* unit */ SCPI_UNIT_SECONDS, /* mult */ 1e-12},
+    {/* name */ "NS", /* unit */ SCPI_UNIT_SECONDS, /* mult */ 1e-9},
+    {/* name */ "US", /* unit */ SCPI_UNIT_SECONDS, /* mult */ 1e-6},
+    {/* name */ "MS", /* unit */ SCPI_UNIT_SECONDS, /* mult */ 1e-3},
+    {/* name */ "S", /* unit */ SCPI_UNIT_SECONDS, /* mult */ 1},
+    {/* name */ "MIN", /* unit */ SCPI_UNIT_SECONDS, /* mult */ 60},
+    {/* name */ "HR", /* unit */ SCPI_UNIT_SECONDS, /* mult */ 3600},
 
     SCPI_UNITS_LIST_END,
 };
 
+/*
+ * Special number values definition
+ */
 const scpi_special_number_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 = "NINF", .type = SCPI_NUM_NINF},
+    {/* 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 */ "NINF", /* type */ SCPI_NUM_NINF},
     SCPI_SPECIAL_NUMBERS_LIST_END,
 };
 
-static scpi_special_number_t translateSpecialNumber(const scpi_special_number_def_t * specs, const char * str, size_t len) {
+/**
+ * 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 SCPI_NUM_NUMBER;
+        return FALSE;
     }
-    
+
     for (i = 0; specs[i].name != NULL; i++) {
         if (matchPattern(specs[i].name, strlen(specs[i].name), str, len)) {
-            return specs[i].type;
+            value->type = specs[i].type;
+            return TRUE;
         }
     }
 
-    return SCPI_NUM_NUMBER;
+    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;
@@ -143,13 +166,20 @@
     return NULL;
 }
 
+/**
+ * Convert string describing unit to its representation
+ * @param units units patterns
+ * @param unit text representation of unknown unit
+ * @param len length of text representation
+ * @return pointer of related unit definition or NULL
+ */
 static const scpi_unit_def_t * translateUnit(const scpi_unit_def_t * units, const char * unit, size_t len) {
     int i;
-    
+
     if (units == NULL) {
         return NULL;
     }
-    
+
     for (i = 0; units[i].name != NULL; i++) {
         if (compareStr(unit, len, units[i].name, strlen(units[i].name))) {
             return &units[i];
@@ -159,13 +189,19 @@
     return NULL;
 }
 
+/**
+ * Convert unit definition to string
+ * @param units units definitions (patterns)
+ * @param unit type of unit
+ * @return string representation of unit
+ */
 static const char * translateUnitInverse(const scpi_unit_def_t * units, const scpi_unit_t unit) {
     int i;
-    
+
     if (units == NULL) {
         return NULL;
     }
-    
+
     for (i = 0; units[i].name != NULL; i++) {
         if ((units[i].unit == unit) && (units[i].mult == 1)) {
             return units[i].name;
@@ -175,7 +211,15 @@
     return NULL;
 }
 
-static bool_t transformNumber(const scpi_unit_def_t * units, const char * unit, size_t len, scpi_number_t * value) {
+/**
+ * Transform number to base units
+ * @param context
+ * @param unit text representation of unit
+ * @param len length of text representation
+ * @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) {
     size_t s;
     const scpi_unit_def_t * unitDef;
     s = skipWhitespace(unit, len);
@@ -185,9 +229,10 @@
         return TRUE;
     }
 
-    unitDef = translateUnit(units, unit + s, len - s);
+    unitDef = translateUnit(context->units, unit + s, len - s);
 
     if (unitDef == NULL) {
+        SCPI_ErrorPush(context, SCPI_ERROR_INVALID_SUFFIX);
         return FALSE;
     }
 
@@ -204,50 +249,44 @@
  * @param mandatory if the parameter is mandatory
  * @return 
  */
-bool_t SCPI_ParamNumber(scpi_t * context, scpi_number_t * value, bool_t mandatory) {
-    bool_t result;
-    char * param;
-    size_t len;
-    size_t numlen;
+bool_t SCPI_ParamTranslateNumberVal(scpi_t * context, scpi_parameter_t * parameter) {
+    token_t token;
+    lex_state_t state;
 
-    result = SCPI_ParamString(context, &param, &len, mandatory);
+    state.buffer = parameter->data.ptr;
+    state.pos = state.buffer;
+    state.len = parameter->data.len;
 
-    if (!value) {
-        return FALSE;
-    }
+    if (parameter->type == TokDecimalNumericProgramDataWithSuffix) {
+        SCPI_LexDecimalNumericProgramData(&state, &token);
+        SCPI_LexWhiteSpace(&state, &token);
+        SCPI_LexSuffixProgramData(&state, &token);
 
-    if (!result) {
-        if (mandatory) {
-            return FALSE;
-        } else {
-            value->type = SCPI_NUM_DEF;
+        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;
         }
     }
 
-    value->unit = SCPI_UNIT_NONE;
-    value->value = 0.0;
-    value->type = translateSpecialNumber(context->special_numbers, param, len);
-
-    if (value->type != SCPI_NUM_NUMBER) {
-        // found special type
-        return TRUE;
-    }
-
-    numlen = strToDouble(param, &value->value);
-
-    if (numlen <= len) {
-        if (transformNumber(context->units, param + numlen, len - numlen, value)) {
-            return TRUE;
-        } else {
-            SCPI_ErrorPush(context, SCPI_ERROR_INVALID_SUFFIX);
-        }
-    }
     return FALSE;
-
 }
 
-size_t SCPI_NumberToStr(scpi_t * context, scpi_number_t * value, char * str, size_t len) {
+/**
+ * Convert scpi_number_t to string
+ * @param context
+ * @param value number value
+ * @param str target string
+ * @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) {
     const char * type;
     const char * unit;
     size_t result;

--
Gitblit v1.9.1