update utils append_parameters
authorJim Morris <morris@wolfman.com>
Mon, 14 Sep 2015 03:50:05 +0000 (20:50 -0700)
committerJim Morris <morris@wolfman.com>
Mon, 14 Sep 2015 03:50:05 +0000 (20:50 -0700)
src/libs/utils.cpp
src/libs/utils.h
src/testframework/unittests/libs/TEST_utils.cpp

index dc4ac19..9949800 100644 (file)
@@ -15,7 +15,6 @@
 #include <cstring>
 #include <stdio.h>
 #include <cstdlib>
-#include <sstream>
 
 using std::string;
 
@@ -232,11 +231,13 @@ vector<float> parse_number_list(const char *str)
     return r;
 }
 
-std::string append_parameters(std::map<char,float> params)
+int append_parameters(char *buf, std::vector<std::pair<char,float>> params, size_t bufsize)
 {
-    std::ostringstream oss;
+    size_t n= 0;
     for(auto &i : params) {
-        oss << i.first << i.second << " ";
+        if(n >= bufsize) break;
+        buf[n++]= i.first;
+        n += snprintf(&buf[n], bufsize-n, "%1.4f ", i.second);
     }
-    return oss.str();
+    return n;
 }
index 71c10b3..480ceed 100644 (file)
@@ -4,7 +4,7 @@
 #include <stdint.h>
 #include <string>
 #include <vector>
-#include <map>
+
 using std::string;
 using std::vector;
 
@@ -36,6 +36,6 @@ void system_reset( bool dfu= false );
 
 string absolute_from_relative( string path );
 
-std::string append_parameters(std::map<char,float> params);
+int append_parameters(char *buf, std::vector<std::pair<char,float>> params, size_t bufsize);
 
 #endif
index 167d089..d339654 100644 (file)
@@ -2,6 +2,7 @@
 
 #include <vector>
 #include <stdio.h>
+#include <string.h>
 
 #include "easyunit/test.h"
 
@@ -37,8 +38,10 @@ TEST(UtilsTest,parse_number_list)
 
 TEST(UtilsTest,append_parameters)
 {
-    std::string str;
+    char buf[132];
 
-    str= append_parameters({{'X', 1}, {'Y', 2}, {'Z', 3}});
-    ASSERT_EQUALS(str, "X1 Y2 Z3 ");
+    int n= append_parameters(buf, {{'X', 1}, {'Y', 2}, {'Z', 3}}, sizeof(buf));
+    //printf("%d - %s\n", n, buf);
+    ASSERT_TRUE(n == 24);
+    ASSERT_TRUE(strcmp(buf, "X1.0000 Y2.0000 Z3.0000 ") == 0);
 }