From a2e83e5ff635f040cffe4c12cfdaa60fa320c3de Mon Sep 17 00:00:00 2001 From: Jan Breuer <jan.breuer@jaybee.cz> Date: 周三, 22 4月 2015 01:59:50 +0800 Subject: [PATCH] Make public SCPI_LongToStr, SCPI_DoubleToStr --- libscpi/inc/scpi/utils_private.h | 25 ++++--- libscpi/src/parser.c | 5 + libscpi/src/units.c | 3 libscpi/test/test_scpi_utils.c | 5 + libscpi/inc/scpi/config.h | 14 ++-- libscpi/inc/scpi/scpi.h | 1 libscpi/inc/scpi/utils.h | 54 ++++++++++++++++++ libscpi/src/utils.c | 16 +++-- 8 files changed, 94 insertions(+), 29 deletions(-) diff --git a/libscpi/inc/scpi/config.h b/libscpi/inc/scpi/config.h index 48ad3e9..811d5a4 100644 --- a/libscpi/inc/scpi/config.h +++ b/libscpi/inc/scpi/config.h @@ -84,24 +84,24 @@ /* define local macros depending on existance of strnlen */ #if HAVE_STRNLEN -#define SCPI_strnlen(s, l) strnlen((s), (l)) +#define SCPIDEFINE_strnlen(s, l) strnlen((s), (l)) #else -#define SCPI_strnlen(s, l) BSD_strnlen((s), (l)) +#define SCPIDEFINE_strnlen(s, l) BSD_strnlen((s), (l)) #endif /* define local macros depending on existance of strncasecmp and strnicmp */ #if HAVE_STRNCASECMP -#define SCPI_strncasecmp(s1, s2, l) strncasecmp((s1), (s2), (l)) +#define SCPIDEFINE_strncasecmp(s1, s2, l) strncasecmp((s1), (s2), (l)) #elif HAVE_STRNICMP -#define SCPI_strncasecmp(s1, s2, l) strnicmp((s1), (s2), (l)) +#define SCPIDEFINE_strncasecmp(s1, s2, l) strnicmp((s1), (s2), (l)) #else -#define SCPI_strncasecmp(s1, s2, l) OUR_strncasecmp((s1), (s2), (l)) +#define SCPIDEFINE_strncasecmp(s1, s2, l) OUR_strncasecmp((s1), (s2), (l)) #endif #if HAVE_DTOSTRE -#define SCPI_doubleToStr(v, s, l) strlen(dtostre((v), (s), 6, DTOSTR_PLUS_SIGN | DTOSTR_ALWAYS_SIGN | DTOSTR_UPPERCASE)) +#define SCPIDEFINE_doubleToStr(v, s, l) strlen(dtostre((v), (s), 6, DTOSTR_PLUS_SIGN | DTOSTR_ALWAYS_SIGN | DTOSTR_UPPERCASE)) #else -#define SCPI_doubleToStr(v, s, l) snprintf((s), (l), "%lg", (v)) +#define SCPIDEFINE_doubleToStr(v, s, l) snprintf((s), (l), "%lg", (v)) #endif diff --git a/libscpi/inc/scpi/scpi.h b/libscpi/inc/scpi/scpi.h index 7858488..f511213 100644 --- a/libscpi/inc/scpi/scpi.h +++ b/libscpi/inc/scpi/scpi.h @@ -43,6 +43,7 @@ #include "scpi/constants.h" #include "scpi/minimal.h" #include "scpi/units.h" +#include "scpi/utils.h" diff --git a/libscpi/inc/scpi/utils.h b/libscpi/inc/scpi/utils.h new file mode 100644 index 0000000..1731d0e --- /dev/null +++ b/libscpi/inc/scpi/utils.h @@ -0,0 +1,54 @@ +/*- + * Copyright (c) 2012-2015 Jan Breuer, + * + * All Rights Reserved + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * @file utils.h + * + * @brief Conversion routines and string manipulation routines + * + * + */ + +#ifndef SCPI_UTILS_H +#define SCPI_UTILS_H + +#include <stdint.h> +#include "scpi/types.h" + +#ifdef __cplusplus +extern "C" { +#endif + + size_t SCPI_LongToStr(int32_t val, char * str, size_t len, int8_t base); + size_t SCPI_DoubleToStr(double val, char * str, size_t len); + +#ifdef __cplusplus +} +#endif + +#endif /* SCPI_UTILS_H */ + diff --git a/libscpi/inc/scpi/utils_private.h b/libscpi/inc/scpi/utils_private.h index 230ce98..0f8fdec 100644 --- a/libscpi/inc/scpi/utils_private.h +++ b/libscpi/inc/scpi/utils_private.h @@ -26,7 +26,7 @@ */ /** - * @file scpi_utils.h + * @file utils_private.h * @date Thu Nov 15 10:58:45 UTC 2012 * * @brief Conversion routines and string manipulation routines @@ -34,8 +34,8 @@ * */ -#ifndef SCPI_UTILS_H -#define SCPI_UTILS_H +#ifndef SCPI_UTILS_PRIVATE_H +#define SCPI_UTILS_PRIVATE_H #include <stdint.h> #include "scpi/config.h" @@ -54,8 +54,6 @@ const char * strnpbrk(const char *str, size_t size, const char *set) LOCAL; scpi_bool_t compareStr(const char * str1, size_t len1, const char * str2, size_t len2) LOCAL; scpi_bool_t compareStrAndNum(const char * str1, size_t len1, const char * str2, size_t len2) LOCAL; - size_t longToStr(int32_t val, char * str, size_t len) LOCAL; - size_t doubleToStr(double val, char * str, size_t len) LOCAL; size_t strToLong(const char * str, int32_t * val) LOCAL; size_t strToDouble(const char * str, double * val) LOCAL; scpi_bool_t locateText(const char * str1, size_t len1, const char ** str2, size_t * len2) LOCAL; @@ -64,18 +62,23 @@ size_t skipColon(const char * cmd, size_t len) LOCAL; scpi_bool_t matchPattern(const char * pattern, size_t pattern_len, const char * str, size_t str_len) LOCAL; scpi_bool_t matchCommand(const char * pattern, const char * cmd, size_t len) LOCAL; - scpi_bool_t composeCompoundCommand(char * ptr_prev, size_t len_prev, char ** pptr, size_t * plen); + scpi_bool_t composeCompoundCommand(char * ptr_prev, size_t len_prev, char ** pptr, size_t * plen) LOCAL; #if !HAVE_STRNLEN - size_t BSD_strnlen(const char *s, size_t maxlen); + size_t BSD_strnlen(const char *s, size_t maxlen) LOCAL; #endif #if !HAVE_STRNCASECMP && !HAVE_STRNICMP - int OUR_strncasecmp(const char *s1, const char *s2, size_t n); + int OUR_strncasecmp(const char *s1, const char *s2, size_t n) LOCAL; #endif -#define min(a, b) (((a) < (b)) ? (a) : (b)) -#define max(a, b) (((a) > (b)) ? (a) : (b)) +#ifndef min + #define min(a, b) (((a) < (b)) ? (a) : (b)) +#endif + +#ifndef max + #define max(a, b) (((a) > (b)) ? (a) : (b)) +#endif #if 0 #define max(a,b) \ @@ -94,5 +97,5 @@ } #endif -#endif /* SCPI_UTILS_H */ +#endif /* SCPI_UTILS_PRIVATE_H */ diff --git a/libscpi/src/parser.c b/libscpi/src/parser.c index 5940432..49080ff 100644 --- a/libscpi/src/parser.c +++ b/libscpi/src/parser.c @@ -40,6 +40,7 @@ #include "scpi/config.h" #include "scpi/parser.h" #include "scpi/utils_private.h" +#include "scpi/utils.h" #include "scpi/error.h" #include "scpi/constants.h" @@ -382,7 +383,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++; @@ -408,7 +409,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++; diff --git a/libscpi/src/units.c b/libscpi/src/units.c index a4c7592..a4224cd 100644 --- a/libscpi/src/units.c +++ b/libscpi/src/units.c @@ -38,6 +38,7 @@ #include "scpi/parser.h" #include "scpi/units.h" #include "scpi/utils_private.h" +#include "scpi/utils.h" #include "scpi/error.h" @@ -312,7 +313,7 @@ return min(strlen(type), len); } - result = doubleToStr(value->value, str, len); + result = SCPI_DoubleToStr(value->value, str, len); unit = translateUnitInverse(context->units, value->unit); diff --git a/libscpi/src/utils.c b/libscpi/src/utils.c index fd9f9f3..eb74eea 100644 --- a/libscpi/src/utils.c +++ b/libscpi/src/utils.c @@ -42,6 +42,7 @@ #include <ctype.h> #include "scpi/utils_private.h" +#include "scpi/utils.h" static size_t patternSeparatorShortPos(const char * pattern, size_t len); static size_t patternSeparatorPos(const char * pattern, size_t len); @@ -72,12 +73,15 @@ * @param val integer value * @param str converted textual representation * @param len string buffer length + * @param base output base * @return number of bytes written to str (without '\0') */ -size_t longToStr(int32_t val, char * str, size_t len) { +size_t SCPI_LongToStr(int32_t val, char * str, size_t len, int8_t base) { uint32_t x = 1000000000L; int_fast8_t digit; size_t pos = 0; + + (void) base; // currently not supported if (val == 0) { if (pos < len) str[pos++] = '0'; @@ -110,8 +114,8 @@ * @param len string buffer length * @return number of bytes written to str (without '\0') */ -size_t doubleToStr(double val, char * str, size_t len) { - return SCPI_doubleToStr(val, str, len); +size_t SCPI_DoubleToStr(double val, char * str, size_t len) { + return SCPIDEFINE_doubleToStr(val, str, len); } /** @@ -151,7 +155,7 @@ return FALSE; } - if (SCPI_strncasecmp(str1, str2, len2) == 0) { + if (SCPIDEFINE_strncasecmp(str1, str2, len2) == 0) { return TRUE; } @@ -174,7 +178,7 @@ return FALSE; } - if (SCPI_strncasecmp(str1, str2, len1) == 0) { + if (SCPIDEFINE_strncasecmp(str1, str2, len1) == 0) { result = TRUE; } @@ -502,7 +506,7 @@ const char * pattern_end = pattern + pattern_len; const char * cmd_ptr = cmd; - size_t cmd_len = SCPI_strnlen(cmd, len); + size_t cmd_len = SCPIDEFINE_strnlen(cmd, len); const char * cmd_end = cmd + cmd_len; /* now support optional keywords in pattern style, e.g. [:MEASure]:VOLTage:DC? */ diff --git a/libscpi/test/test_scpi_utils.c b/libscpi/test/test_scpi_utils.c index 5817f61..e357b65 100644 --- a/libscpi/test/test_scpi_utils.c +++ b/libscpi/test/test_scpi_utils.c @@ -40,6 +40,7 @@ #include "scpi/scpi.h" #include "scpi/utils_private.h" +#include "scpi/utils.h" /* * CUnit Test Suite @@ -67,7 +68,7 @@ char str[32]; size_t len; - len = longToStr(10, str, 32); + len = SCPI_LongToStr(10, str, 32, 10); CU_ASSERT(len == 2); CU_ASSERT(str[0] == '1'); CU_ASSERT(str[1] == '0'); @@ -80,7 +81,7 @@ #define TEST_DOUBLE_TO_STR(v, r, s) \ do { \ - result = doubleToStr(v, str, sizeof(str)); \ + result = SCPI_DoubleToStr(v, str, sizeof(str)); \ CU_ASSERT_EQUAL(result, r); \ CU_ASSERT_STRING_EQUAL(str, s); \ } while(0) \ -- Gitblit v1.9.1