Menu

#80 Spaces in string parameters of XSPICE code-models

open
nobody
None
5
2020-11-16
2020-11-16
Taras Rosa
No

Hello,

I've tried to create XSPICE code-mode with parameter of data type "string" and found that is prohibited to use spaces in there.
My test model:

NAME_TABLE:
Spice_Model_Name:  test_string
C_Function_Name:   cm_test_string
Description:       "Test string parameter"


PORT_TABLE:
Port_Name:        out
Description:      " "
Direction:        out
Default_Type:     d
Allowed_Types:    [d]
Vector:           no
Vector_Bounds:    -
Null_Allowed:     no


PARAMETER_TABLE:
Parameter_Name:    string_param
Description:       " "   
Data_Type:         string
Default_Value:     -
Limits:            -
Vector:            no
Vector_Bounds:     -
Null_Allowed:      no
#include <ngspice/cm.h>

void cm_test_string(ARGS)
{
    if (INIT)
    {
        cm_message_send(PARAM(string_param));
    }
    else
    {
        OUTPUT_DELAY(out) = 1e-12;
    }
    OUTPUT_STATE(out) = 0;
}

Testing netlist:

// Testing string parameter with spaces

A_TEST_STRING  %d(out)  CM_TEST_STRING
    .model  CM_TEST_STRING  test_string (string_param = "one TWO three FOUR five")

V_DUMMY nc gnd DC(1)

.control
    tran 1u 1u
.endc

.end

Results:

After deep dive into Ngspice sources I've found that there is the function: void inp_casefix(char string) at src/frontend/inpcom.c:2423 which replaces double quotes with spaces in input deck.
I've commented this part of the function:

void inp_casefix(char *string)
{
#ifdef HAVE_CTYPE_H
    /* single non-printable character */
    if (string && !isspace_c(*string) && !isprint_c(*string) &&
            (string[1] == '\0' || isspace_c(string[1]))) {
        *string = '*';
        return;
    }
    if (string)
        while (*string) {
            if (*string == '"') {
                // *string++ = ' ';
                while (*string && *string != '"')
                    string++;
                if (*string == '\0')
                    continue; /* needed if string is "something ! */
                // if (*string == '"')
                //     *string = ' ';
            }
            if (!isspace_c(*string) && !isprint_c(*string))
                *string = '_';
            if (isupper_c(*string))
                *string = tolower_c(*string);
            string++;
        }
#endif
}

And my model began to work fine:

Even though whole string parameter converted to lower case it much better than it was before. After debugging I found that the lowercase conversion happens in some earlier stages of netlist parsing.
I don't see any obvious reasons to remove double quotes because it make possible to create some advanced code-models with complex string-defined state transition tables etc.
If you consider this changes safe, please apply them to current Ngspice source (patch in the attachment)

Regards

3 Attachments

Discussion


Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.