From c49b34ae42b691518a1100346d7c749e35bc3ba3 Mon Sep 17 00:00:00 2001 From: Jan Breuer <jan.breuer@jaybee.cz> Date: 周三, 22 4月 2015 02:22:07 +0800 Subject: [PATCH] Make public SCPI_LongToStr, SCPI_DoubleToStr --- libscpi/src/parser.c | 7 +- libscpi/src/units.c | 2 libscpi/test/test_scpi_utils.c | 10 +- libscpi/inc/scpi/config.h | 16 ++-- libscpi/src/utils_private.h | 23 ++++--- libscpi/inc/scpi/scpi.h | 1 libscpi/inc/scpi/utils.h | 54 ++++++++++++++++++ libscpi/src/utils.c | 14 ++-- 8 files changed, 94 insertions(+), 33 deletions(-) diff --git a/libscpi/inc/scpi/config.h b/libscpi/inc/scpi/config.h index 271f0c5..811d5a4 100644 --- a/libscpi/inc/scpi/config.h +++ b/libscpi/inc/scpi/config.h @@ -40,7 +40,7 @@ #ifdef __cplusplus extern "C" { #endif - + /* Compiler specific */ /* 8bit PIC - PIC16, etc */ #if defined(_MPC_) @@ -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/src/parser.c b/libscpi/src/parser.c index 9017bca..ad7ebc4 100644 --- a/libscpi/src/parser.c +++ b/libscpi/src/parser.c @@ -43,6 +43,7 @@ #include "lexer_private.h" #include "scpi/error.h" #include "scpi/constants.h" +#include "scpi/utils.h" /** * Write data to SCPI output @@ -332,7 +333,7 @@ size_t result = 0; size_t len; - len = longToStr(val, buffer, sizeof (buffer), base); + len = SCPI_LongToStr(val, buffer, sizeof (buffer), base); basePrefix = getBasePrefix(base); result += writeDelimiter(context); @@ -353,7 +354,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++; @@ -390,7 +391,7 @@ char block_header[12]; size_t header_len; block_header[0] = '#'; - longToStr(len, block_header + 2, 10, 10); + SCPI_LongToStr(len, block_header + 2, 10, 10); header_len = strlen(block_header + 2); block_header[1] = header_len + '0'; diff --git a/libscpi/src/units.c b/libscpi/src/units.c index 93f1d82..3a2ac15 100644 --- a/libscpi/src/units.c +++ b/libscpi/src/units.c @@ -319,7 +319,7 @@ } } - 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 710d8e0..e5f4ee9 100644 --- a/libscpi/src/utils.c +++ b/libscpi/src/utils.c @@ -42,6 +42,7 @@ #include <ctype.h> #include "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,9 +73,10 @@ * @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, int8_t base) { +size_t SCPI_LongToStr(int32_t val, char * str, size_t len, int8_t base) { const char digits[] = "0123456789ABCDEF"; #define ADD_CHAR(c) if (pos < len) str[pos++] = (c) @@ -137,8 +139,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); } /** @@ -178,7 +180,7 @@ return FALSE; } - if (SCPI_strncasecmp(str1, str2, len2) == 0) { + if (SCPIDEFINE_strncasecmp(str1, str2, len2) == 0) { return TRUE; } @@ -201,7 +203,7 @@ return FALSE; } - if (SCPI_strncasecmp(str1, str2, len1) == 0) { + if (SCPIDEFINE_strncasecmp(str1, str2, len1) == 0) { result = TRUE; } @@ -327,7 +329,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/src/utils_private.h b/libscpi/src/utils_private.h index 255136b..b90f08f 100644 --- a/libscpi/src/utils_private.h +++ b/libscpi/src/utils_private.h @@ -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 @@ 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, int8_t base) LOCAL; - size_t doubleToStr(double val, char * str, size_t len) LOCAL; size_t strToLong(const char * str, int32_t * val, int8_t base) 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; @@ -63,18 +61,23 @@ size_t skipWhitespace(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(const scpi_token_t * prev, scpi_token_t * current); + scpi_bool_t composeCompoundCommand(const scpi_token_t * prev, scpi_token_t * current) 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) \ @@ -93,5 +96,5 @@ } #endif -#endif /* SCPI_UTILS_H */ +#endif /* SCPI_UTILS_PRIVATE_H */ diff --git a/libscpi/test/test_scpi_utils.c b/libscpi/test/test_scpi_utils.c index dad8808..ad85399 100644 --- a/libscpi/test/test_scpi_utils.c +++ b/libscpi/test/test_scpi_utils.c @@ -72,12 +72,12 @@ char str[32]; size_t len; - len = longToStr(10, str, 32, 10); + len = SCPI_LongToStr(10, str, 32, 10); CU_ASSERT(len == 2); CU_ASSERT_STRING_EQUAL(str, "10"); CU_ASSERT(str[len] == '\0'); - len = longToStr(10, str, 32, 2); + len = SCPI_LongToStr(10, str, 32, 2); CU_ASSERT(len == 4); CU_ASSERT(str[0] == '1'); CU_ASSERT(str[1] == '0'); @@ -85,12 +85,12 @@ CU_ASSERT(str[3] == '0'); CU_ASSERT(str[4] == '\0'); - len = longToStr(10, str, 32, 16); + len = SCPI_LongToStr(10, str, 32, 16); CU_ASSERT(len == 1); CU_ASSERT(str[0] == 'A'); CU_ASSERT(str[1] == '\0'); - len = longToStr(10, str, 32, 8); + len = SCPI_LongToStr(10, str, 32, 8); CU_ASSERT(len == 2); CU_ASSERT(str[0] == '1'); CU_ASSERT(str[1] == '2'); @@ -103,7 +103,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