From 5e5d61b8eb01d146d9c06f406e38f317c0b515c7 Mon Sep 17 00:00:00 2001
From: Jan Breuer <jan.breuer@jaybee.cz>
Date: 周二, 04 12月 2012 18:25:23 +0800
Subject: [PATCH] Refactor units to use scpi context

---
 scpi/scpi_units.h |    3 +++
 test-parser.c     |    4 +++-
 scpi/scpi_types.h |    2 ++
 scpi/scpi_units.c |   31 ++++++++++++++++++++++---------
 4 files changed, 30 insertions(+), 10 deletions(-)

diff --git a/scpi/scpi_types.h b/scpi/scpi_types.h
index d028656..939fa85 100644
--- a/scpi/scpi_types.h
+++ b/scpi/scpi_types.h
@@ -117,6 +117,8 @@
         bool_t cmd_error;
         scpi_error_queue_t error_queue;
         scpi_reg_val_t * registers;
+        const scpi_unit_def_t * units;
+        const scpi_special_number_def_t * special_numbers;
     };
 
     enum _scpi_unit_t {
diff --git a/scpi/scpi_units.c b/scpi/scpi_units.c
index dc058fe..5754109 100644
--- a/scpi/scpi_units.c
+++ b/scpi/scpi_units.c
@@ -114,6 +114,10 @@
 static scpi_special_number_t translateSpecialNumber(const scpi_special_number_def_t * specs, const char * str, size_t len) {
     int i;
 
+    if (specs == NULL) {
+        return SCPI_NUM_NUMBER;
+    }
+    
     for (i = 0; specs[i].name != NULL; i++) {
         if (matchPattern(specs[i].name, strlen(specs[i].name), str, len)) {
             return specs[i].type;
@@ -126,6 +130,10 @@
 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;
@@ -137,6 +145,11 @@
 
 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];
@@ -148,6 +161,11 @@
 
 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;
@@ -192,8 +210,6 @@
     size_t len;
     size_t numlen;
 
-    // TODO: get scpi_special_numbers_def and scpi_units_def from context    
-
     result = SCPI_ParamString(context, &param, &len, mandatory);
 
     if (!value) {
@@ -211,7 +227,7 @@
 
     value->unit = SCPI_UNIT_NONE;
     value->value = 0.0;
-    value->type = translateSpecialNumber(scpi_special_numbers_def, param, len);
+    value->type = translateSpecialNumber(context->special_numbers, param, len);
 
     if (value->type != SCPI_NUM_NUMBER) {
         // found special type
@@ -221,7 +237,7 @@
     numlen = strToDouble(param, &value->value);
 
     if (numlen <= len) {
-        if (transformNumber(scpi_units_def, param + numlen, len - numlen, value)) {
+        if (transformNumber(context->units, param + numlen, len - numlen, value)) {
             return TRUE;
         } else {
             SCPI_ErrorPush(context, SCPI_ERROR_INVALID_SUFFIX);
@@ -236,14 +252,11 @@
     const char * unit;
     size_t result;
 
-    (void) context; // TODO: get scpi_special_numbers_def and scpi_units_def from context
-
-
     if (!value || !str) {
         return 0;
     }
 
-    type = translateSpecialNumberInverse(scpi_special_numbers_def, value->type);
+    type = translateSpecialNumberInverse(context->special_numbers, value->type);
 
     if (type) {
         strncpy(str, type, len);
@@ -252,7 +265,7 @@
 
     result = doubleToStr(value->value, str, len);
 
-    unit = translateUnitInverse(scpi_units_def, value->unit);
+    unit = translateUnitInverse(context->units, value->unit);
 
     if (unit) {
         strncat(str, " ", len);
diff --git a/scpi/scpi_units.h b/scpi/scpi_units.h
index 9521ff0..c02f947 100644
--- a/scpi/scpi_units.h
+++ b/scpi/scpi_units.h
@@ -43,6 +43,9 @@
 extern "C" {
 #endif
 
+    extern const scpi_unit_def_t scpi_units_def[];
+    extern const scpi_special_number_def_t scpi_special_numbers_def[];
+    
     bool_t SCPI_ParamNumber(scpi_t * context, scpi_number_t * value, bool_t mandatory);
     size_t SCPI_NumberToStr(scpi_t * context, scpi_number_t * value, char * str, size_t len);
 
diff --git a/test-parser.c b/test-parser.c
index 15e1209..6cc8b6c 100644
--- a/test-parser.c
+++ b/test-parser.c
@@ -61,7 +61,7 @@
     return SCPI_RES_OK;
 }
 
-static scpi_command_t scpi_commands[] = {
+static const scpi_command_t scpi_commands[] = {
     /* IEEE Mandated Commands (SCPI std V1999.0 4.1.1) */
     { .pattern = "*CLS", .callback = SCPI_CoreCls,},
     { .pattern = "*ESE", .callback = SCPI_CoreEse,},
@@ -152,6 +152,8 @@
     },
     .interface = &scpi_interface,
     .registers = scpi_regs,
+    .units = scpi_units_def,
+    .special_numbers = scpi_special_numbers_def,
 };
 
 /*

--
Gitblit v1.9.1