From c8c00d2032c12f79df352d4eb573c228099b30cf Mon Sep 17 00:00:00 2001
From: Jan Breuer <jan.breuer@jaybee.cz>
Date: 周二, 31 5月 2016 18:15:28 +0800
Subject: [PATCH] Add channel list example call

---
 libscpi/src/utils.c |   42 ++++++++++++++++++++++++++++++++----------
 1 files changed, 32 insertions(+), 10 deletions(-)

diff --git a/libscpi/src/utils.c b/libscpi/src/utils.c
index 99dfd06..6fa5ad7 100644
--- a/libscpi/src/utils.c
+++ b/libscpi/src/utils.c
@@ -249,7 +249,8 @@
  * @return number of bytes written to str (without '\0')
  */
 size_t SCPI_FloatToStr(float val, char * str, size_t len) {
-    return SCPIDEFINE_floatToStr(val, str, len);
+    SCPIDEFINE_floatToStr(val, str, len);
+    return strlen(str);
 }
 
 /**
@@ -260,7 +261,8 @@
  * @return number of bytes written to str (without '\0')
  */
 size_t SCPI_DoubleToStr(double val, char * str, size_t len) {
-    return SCPIDEFINE_doubleToStr(val, str, len);
+    SCPIDEFINE_doubleToStr(val, str, len);
+    return strlen(str);
 }
 
 /**
@@ -753,6 +755,21 @@
 #if USE_DEVICE_DEPENDENT_ERROR_INFORMATION && !USE_MEMORY_ALLOCATION_FREE
 
 /**
+ * Initialize heap structure
+ * @param heap - pointer to manual allocated heap buffer
+ * @param error_info_heap - buffer for the heap
+ * @param error_info_heap_length - length of the heap
+ */
+void scpiheap_init(scpi_error_info_heap_t * heap, char * error_info_heap, size_t error_info_heap_length)
+{
+    heap->data = error_info_heap;
+    heap->wr = 0;
+    heap->size = error_info_heap_length;
+    heap->count = heap->size;
+    memset(heap->data, 0, heap->size);
+}
+
+/**
  * Duplicate string if "strdup" ("malloc/free") not supported on system.
  * Allocate space in heap if it possible
  *
@@ -760,8 +777,8 @@
  * @param s - current pointer of duplication string
  * @return - pointer of duplicated string or NULL, if duplicate is not possible.
  */
-char * OUR_strdup(scpi_error_info_heap_t * heap, const char *s) {
-    if (!s || !heap) {
+char * scpiheap_strndup(scpi_error_info_heap_t * heap, const char *s, size_t n) {
+    if (!s || !heap || !heap->size) {
         return NULL;
     }
 
@@ -773,14 +790,13 @@
         return NULL;
     }
 
-    size_t len = strlen(s) + 1; // additional '\0' at end
+    size_t len = SCPIDEFINE_strnlen(s, n) + 1; // additional '\0' at end
     if (len > heap->count) {
         return NULL;
     }
-    char * ptrs = s;
+    const char * ptrs = s;
     char * head = &heap->data[heap->wr];
     size_t rem = heap->size - (&heap->data[heap->wr] - heap->data);
-    size_t sstp = 0;
 
     if (len >= rem) {
         memcpy(&heap->data[heap->wr], s, rem);
@@ -794,6 +810,12 @@
     heap->wr += len;
     heap->count -= len;
 
+    // ensure '\0' a the end
+    if (heap->wr > 0) {
+        heap->data[heap->wr - 1] = '\0';
+    } else {
+        heap->data[heap->size - 1] = '\0';
+    }
     return head;
 }
 
@@ -806,7 +828,7 @@
  * @return s2 - pointer of second part of string, if string splited .
  * @return len2 - lenght of second part of string.
  */
-scpi_bool_t OUR_get_parts(scpi_error_info_heap_t * heap, const char * s, size_t * len1, const char ** s2, size_t * len2) {
+scpi_bool_t scpiheap_get_parts(scpi_error_info_heap_t * heap, const char * s, size_t * len1, const char ** s2, size_t * len2) {
     if (!heap || !s || !len1 || !s2 || !len2) {
         return FALSE;
     }
@@ -836,14 +858,14 @@
  * @param s - pointer of duplicate string
  * @param rollback - backward write pointer in heap
  */
-void OUR_free(scpi_error_info_heap_t * heap, const char * s, scpi_bool_t rollback) {
+void scpiheap_free(scpi_error_info_heap_t * heap, char * s, scpi_bool_t rollback) {
 
     if (!s) return;
 
     char * data_add;
     size_t len[2];
 
-    if (!OUR_get_parts(heap, s, &len[0], &data_add, &len[1])) return;
+    if (!scpiheap_get_parts(heap, s, &len[0], (const char **)&data_add, &len[1])) return;
 
     if (data_add) {
         len[1]++;

--
Gitblit v1.9.1