Jan Breuer
2015-04-22 c49b34ae42b691518a1100346d7c749e35bc3ba3
Make public SCPI_LongToStr, SCPI_DoubleToStr

Make functions SCPI_LongToStr and SCPI_DoubleToStr public. It is
possible to call them from user code. Resolve #32
7个文件已修改
1个文件已添加
121 ■■■■ 已修改文件
libscpi/inc/scpi/config.h 14 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
libscpi/inc/scpi/scpi.h 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
libscpi/inc/scpi/utils.h 54 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
libscpi/src/parser.c 7 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
libscpi/src/units.c 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
libscpi/src/utils.c 14 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
libscpi/src/utils_private.h 19 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
libscpi/test/test_scpi_utils.c 10 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
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
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"
libscpi/inc/scpi/utils.h
New file
@@ -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 */
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';
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);
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? */
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
#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 */
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)                                          \