nancy.liao
2025-05-08 bf8143c649292042de87c0cef63e6cb3c523388f
libscpi/src/utils.c
@@ -1,30 +1,29 @@
/*-
 * Copyright (c) 2013 Jan Breuer
 *                    Richard.hmm
 * Copyright (c) 2012 Jan Breuer
 * BSD 2-Clause License
 *
 * All Rights Reserved
 * Copyright (c) 2012-2018, Jan Breuer, Richard.hmm
 * 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.
 * modification, are permitted provided that the following conditions are met:
 *
 * 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.
 * * Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 *
 * * 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 COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT HOLDER 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.
 */
/**
@@ -41,7 +40,6 @@
#include <string.h>
#include <ctype.h>
#include <math.h>
#include "utils_private.h"
#include "scpi/utils.h"
@@ -92,20 +90,20 @@
    } else {
        switch (base) {
            case 2:
                x = 0x80000000L;
                break;
            case 8:
                x = 0x40000000L;
                break;
            default:
            case 10:
                base = 10;
                x = 1000000000L;
                break;
            case 16:
                x = 0x10000000L;
                break;
        case 2:
            x = 0x80000000L;
            break;
        case 8:
            x = 0x40000000L;
            break;
        default:
        case 10:
            base = 10;
            x = 1000000000L;
            break;
        case 16:
            x = 0x10000000L;
            break;
        }
        /* add sign for numbers in base 10 */
@@ -178,20 +176,20 @@
    } else {
        switch (base) {
            case 2:
                x = 0x8000000000000000ULL;
                break;
            case 8:
                x = 0x8000000000000000ULL;
                break;
            default:
            case 10:
                x = 10000000000000000000ULL;
                base = 10;
                break;
            case 16:
                x = 0x1000000000000000ULL;
                break;
        case 2:
            x = 0x8000000000000000ULL;
            break;
        case 8:
            x = 0x8000000000000000ULL;
            break;
        default:
        case 10:
            x = 10000000000000000000ULL;
            base = 10;
            break;
        case 16:
            x = 0x1000000000000000ULL;
            break;
        }
        /* add sign for numbers in base 10 */
@@ -321,7 +319,7 @@
 */
size_t strToFloat(const char * str, float * val) {
    char * endptr;
    *val = SCPIDEFINE_strtof(str, &endptr);
    *val = (float)SCPIDEFINE_strtof(str, &endptr);
    return endptr - str;
}
@@ -482,16 +480,16 @@
    if ((pattern_len > 0) && pattern[pattern_len - 1] == '#') {
        size_t new_pattern_len = pattern_len - 1;
        pattern_sep_pos_short = patternSeparatorShortPos(pattern, new_pattern_len);
        pattern_sep_pos_short = (int)patternSeparatorShortPos(pattern, new_pattern_len);
        return compareStrAndNum(pattern, new_pattern_len, str, str_len, num) ||
                compareStrAndNum(pattern, pattern_sep_pos_short, str, str_len, num);
               compareStrAndNum(pattern, pattern_sep_pos_short, str, str_len, num);
    } else {
        pattern_sep_pos_short = patternSeparatorShortPos(pattern, pattern_len);
        pattern_sep_pos_short = (int)patternSeparatorShortPos(pattern, pattern_len);
        return compareStr(pattern, pattern_len, str, str_len) ||
                compareStr(pattern, pattern_sep_pos_short, str, str_len);
               compareStr(pattern, pattern_sep_pos_short, str, str_len);
    }
}
@@ -514,7 +512,7 @@
    int32_t *number_ptr = NULL;
    const char * pattern_ptr = pattern;
    int pattern_len = strlen(pattern);
    int pattern_len = (int)strlen(pattern);
    const char * cmd_ptr = cmd;
    size_t cmd_len = SCPIDEFINE_strnlen(cmd, len);
@@ -550,9 +548,9 @@
    }
    while (1) {
        int pattern_sep_pos = patternSeparatorPos(pattern_ptr, pattern_len);
        int pattern_sep_pos = (int)patternSeparatorPos(pattern_ptr, pattern_len);
        cmd_sep_pos = cmdSeparatorPos(cmd_ptr, cmd_len);
        cmd_sep_pos = (int)cmdSeparatorPos(cmd_ptr, cmd_len);
        if ((pattern_sep_pos > 0) && pattern_ptr[pattern_sep_pos - 1] == '#') {
            if (numbers && (numbers_idx < numbers_len)) {
@@ -586,16 +584,16 @@
            if (cmd_len == 0) {
                /* verify all subsequent pattern parts are also optional */
                while (pattern_len) {
                    pattern_sep_pos = patternSeparatorPos(pattern_ptr, pattern_len);
                    pattern_sep_pos = (int)patternSeparatorPos(pattern_ptr, pattern_len);
                    switch (pattern_ptr[pattern_sep_pos]) {
                        case '[':
                            brackets++;
                            break;
                        case ']':
                            brackets--;
                            break;
                        default:
                            break;
                    case '[':
                        brackets++;
                        break;
                    case ']':
                        brackets--;
                        break;
                    default:
                        break;
                    }
                    SKIP_PATTERN(pattern_sep_pos + 1);
                    if (brackets == 0) {
@@ -614,29 +612,29 @@
            /* both command and patter contains command separator at this position */
            if ((pattern_len > 0)
                    && ((pattern_ptr[0] == cmd_ptr[0])
                && ((pattern_ptr[0] == cmd_ptr[0])
                    && (pattern_ptr[0] == ':'))) {
                SKIP_PATTERN(1);
                SKIP_CMD(1);
            } else if ((pattern_len > 1)
                    && (pattern_ptr[1] == cmd_ptr[0])
                    && (pattern_ptr[0] == '[')
                    && (pattern_ptr[1] == ':')) {
                       && (pattern_ptr[1] == cmd_ptr[0])
                       && (pattern_ptr[0] == '[')
                       && (pattern_ptr[1] == ':')) {
                SKIP_PATTERN(2); /* for skip '[' in "[:" */
                SKIP_CMD(1);
                brackets++;
            } else if ((pattern_len > 1)
                    && (pattern_ptr[1] == cmd_ptr[0])
                    && (pattern_ptr[0] == ']')
                    && (pattern_ptr[1] == ':')) {
                       && (pattern_ptr[1] == cmd_ptr[0])
                       && (pattern_ptr[0] == ']')
                       && (pattern_ptr[1] == ':')) {
                SKIP_PATTERN(2); /* for skip ']' in "]:" */
                SKIP_CMD(1);
                brackets--;
            } else if ((pattern_len > 2)
                    && (pattern_ptr[2] == cmd_ptr[0])
                    && (pattern_ptr[0] == ']')
                    && (pattern_ptr[1] == '[')
                    && (pattern_ptr[2] == ':')) {
                       && (pattern_ptr[2] == cmd_ptr[0])
                       && (pattern_ptr[0] == ']')
                       && (pattern_ptr[1] == '[')
                       && (pattern_ptr[2] == ':')) {
                SKIP_PATTERN(3); /* for skip '][' in "][:" */
                SKIP_CMD(1);
                /* brackets++; */
@@ -651,8 +649,8 @@
                SKIP_PATTERN(2); /* for skip ']' in "]:" , pattern_ptr continue, while cmd_ptr remain unchanged */
                brackets--;
            } else if ((pattern_len > 2) && (pattern_ptr[0] == ']')
                    && (pattern_ptr[1] == '[')
                    && (pattern_ptr[2] == ':')) {
                       && (pattern_ptr[1] == '[')
                       && (pattern_ptr[2] == ':')) {
                SKIP_PATTERN(3); /* for skip ']' in "][:" , pattern_ptr continue, while cmd_ptr remain unchanged */
                /* brackets++; */
                /* brackets--; */
@@ -676,8 +674,10 @@
 *
 * prev and current should be in the same memory buffer
 */
//组合复合SCPI命令
scpi_bool_t composeCompoundCommand(const scpi_token_t * prev, scpi_token_t * current) {
    size_t i;
    int i;
    /* Invalid input */
    if (current == NULL || current->ptr == NULL || current->len == 0)
@@ -688,6 +688,7 @@
        return TRUE;
    /* Common command or command root - nothing to do */
    // 当前命令是公共命令(*)或根命令(:)
    if (current->ptr[0] == '*' || current->ptr[0] == ':')
        return TRUE;
@@ -708,6 +709,7 @@
    current->ptr -= i;
    current->len += i;
    //复合命令组合
    memmove(current->ptr, prev->ptr, i);
    return TRUE;
}
@@ -739,8 +741,8 @@
    unsigned char c1, c2;
    for (; n != 0; n--) {
        c1 = tolower((unsigned char) *s1++);
        c2 = tolower((unsigned char) *s2++);
        c1 = (unsigned char)tolower((unsigned char) *s1++);
        c2 = (unsigned char)tolower((unsigned char) *s2++);
        if (c1 != c2) {
            return c1 - c2;
        }
@@ -755,7 +757,8 @@
#if USE_MEMORY_ALLOCATION_FREE && !HAVE_STRNDUP
char *OUR_strndup(const char *s, size_t n) {
    size_t len = SCPIDEFINE_strnlen(s, n);
    char * result = malloc(len + 1);
    //msvc下需要强转
    char * result =(char*) malloc(len + 1);
    if (!result) {
        return NULL;
    }
@@ -955,7 +958,7 @@
    if (fi != 0) {
        r1 = r1 * 308 / 1024 - ndigits;
        w2 = bufsize;
        w2 = (int)bufsize;
        while (r1 > 0) {
            fj = modf(fi / 10, &fi);
            r2++;
@@ -963,7 +966,7 @@
        }
        while (fi != 0) {
            fj = modf(fi / 10, &fi);
            buf[--w2] = (int) ((fj + .03) * 10) + '0';
            buf[--w2] = (char) ((fj + .03) * 10) + '0';
            r2++;
        }
        while (w2 < (int) bufsize) buf[w1++] = buf[w2++];
@@ -982,7 +985,7 @@
    while (w1 <= w2 && w1 < (int) bufsize) {
        arg *= 10;
        arg = modf(arg, &fj);
        buf[w1++] = (int) fj + '0';
        buf[w1++] = (char) fj + '0';
    }
    if (w2 >= (int) bufsize) {
        buf[bufsize - 1] = '\0';
@@ -1111,7 +1114,7 @@
 */
uint16_t SCPI_Swap16(uint16_t val) {
    return ((val & 0x00FF) << 8) |
            ((val & 0xFF00) >> 8);
           ((val & 0xFF00) >> 8);
}
/**
@@ -1121,9 +1124,9 @@
 */
uint32_t SCPI_Swap32(uint32_t val) {
    return ((val & 0x000000FFul) << 24) |
            ((val & 0x0000FF00ul) << 8) |
            ((val & 0x00FF0000ul) >> 8) |
            ((val & 0xFF000000ul) >> 24);
           ((val & 0x0000FF00ul) << 8) |
           ((val & 0x00FF0000ul) >> 8) |
           ((val & 0xFF000000ul) >> 24);
}
/**
@@ -1133,11 +1136,11 @@
 */
uint64_t SCPI_Swap64(uint64_t val) {
    return ((val & 0x00000000000000FFull) << 56) |
            ((val & 0x000000000000FF00ull) << 40) |
            ((val & 0x0000000000FF0000ull) << 24) |
            ((val & 0x00000000FF000000ull) << 8) |
            ((val & 0x000000FF00000000ull) >> 8) |
            ((val & 0x0000FF0000000000ull) >> 24) |
            ((val & 0x00FF000000000000ull) >> 40) |
            ((val & 0xFF00000000000000ull) >> 56);
           ((val & 0x000000000000FF00ull) << 40) |
           ((val & 0x0000000000FF0000ull) << 24) |
           ((val & 0x00000000FF000000ull) << 8) |
           ((val & 0x000000FF00000000ull) >> 8) |
           ((val & 0x0000FF0000000000ull) >> 24) |
           ((val & 0x00FF000000000000ull) >> 40) |
           ((val & 0xFF00000000000000ull) >> 56);
}