nancy.liao
2025-04-18 a3fdd3589c713678eafe20840a7334de6bed038b
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.
 */
/**
@@ -676,6 +675,8 @@
 *
 * 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;
@@ -688,6 +689,7 @@
        return TRUE;
    /* Common command or command root - nothing to do */
    // 当前命令是公共命令(*)或根命令(:)
    if (current->ptr[0] == '*' || current->ptr[0] == ':')
        return TRUE;
@@ -708,6 +710,7 @@
    current->ptr -= i;
    current->len += i;
    //复合命令组合
    memmove(current->ptr, prev->ptr, i);
    return TRUE;
}
@@ -752,10 +755,11 @@
}
#endif
#if !HAVE_STRNDUP
#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;
    }
@@ -1120,10 +1124,10 @@
 * @return
 */
uint32_t SCPI_Swap32(uint32_t val) {
    return ((val & 0x000000FF) << 24) |
            ((val & 0x0000FF00) << 8) |
            ((val & 0x00FF0000) >> 8) |
            ((val & 0xFF000000) >> 24);
    return ((val & 0x000000FFul) << 24) |
            ((val & 0x0000FF00ul) << 8) |
            ((val & 0x00FF0000ul) >> 8) |
            ((val & 0xFF000000ul) >> 24);
}
/**
@@ -1132,12 +1136,12 @@
 * @return
 */
uint64_t SCPI_Swap64(uint64_t val) {
    return ((val & 0x00000000000000FFul) << 56) |
            ((val & 0x000000000000FF00ul) << 40) |
            ((val & 0x0000000000FF0000ul) << 24) |
            ((val & 0x00000000FF000000ul) << 8) |
            ((val & 0x000000FF00000000ul) >> 8) |
            ((val & 0x0000FF0000000000ul) >> 24) |
            ((val & 0x00FF000000000000ul) >> 40) |
            ((val & 0xFF00000000000000ul) >> 56);
    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);
}