GithubHelp home page GithubHelp logo

Comments (6)

koeart avatar koeart commented on May 31, 2024

Have a look at https://github.com/j123b567/scpi-parser/blob/master/libscpi/src/parser.c#L653.
How SCPI_ParamInt works is, you copy your parameter to a variable (see https://github.com/j123b567/scpi-parser/blob/master/examples/common/scpi-def.c#L100 for an example), it just returns TRUE (Success) or FALSE (otherwise) if the parameter wasn't an Integer value.

The function SCPI_ParamInt will return
TRUE if successful and
FALSE in case the Datatype doesn't match or there was a parameter with a suffix (one should handle this with SCPI_ParamNumber).

You have to check the integer value afterwards yourself (for example if it is in the correct range or whatever).

Thats why there is an

if (!SCPI_ParamInt( context, &param, FALSE)) {
     param = 23; //some default value in case there wasn't a parameter given;
}
//handle param

You can figure out the error by using the Error Subsystem.
Update: If there wasn't a parameter entered, SCPI_ERROR_MISSING_PARAMETER gets pushed to the Error Subsystem (https://github.com/j123b567/scpi-parser/blob/master/libscpi/src/parser.c#L470)

from scpi-parser.

j123b567 avatar j123b567 commented on May 31, 2024

@koeart I think that @mvladic has different problem and he is right that there is no souch direct functin for it. There are some solutions more or less ugly.

  • split parameter handling into two functions SCPI_Parameter() and SCPI_ParamToInt(). Result of SCPI_Parameter() tells you, if parameter was here or not. You can later check the type of the parameter. If the function returns FALSE and parameter type is SCPI_TOKEN_PROGRAM_MNEMONIC, this means missing parameter. If type is SCPI_TOKEN_UNKNOWN, error occured. If SCPI_ParamIsNumber() then it is safe to convert it to integer.
  • Before and after calling SCPI_ParamInt(), you can readout SCPI_ErrorCount(). If the number changes, it meas, there was an error.
  • Do some magic in error() callback and set some global variable to detect this.

I don't want to break current API so there are some solution for the future:

  • introduce something like errorno. There will be last error of the SCPI_ParamXYZ() function or 0.
  • introduce API functionSCPI_ParamIsInvalid() so you can check parameter from SCPI_Parameter() without relaying on some parser internals.

What do you think?

from scpi-parser.

mvladic avatar mvladic commented on May 31, 2024

I like errorno solution more because I don't need to split parameter handling into two functions (SCPI_Parameter() and SCPI_ParamToInt()). In that case I will do the following:

if (!SCPI_ParamToInt(context, ...)) {
    if (errorno) {
        return SCPI_RES_ERR;   
    }
    // not a real error, i.e. optional parameter is missing, continue
}

You could also add SCPI_ParamIsInvalid(), it looks like an useful function.

from scpi-parser.

j123b567 avatar j123b567 commented on May 31, 2024

Sounds good. I will extend context with errno and implement SCPI_ParamIsInvalid().

This is almost mechanical but large change so it will take some time to implement this. I would like to add also unit tests for this to be sure it is correct. So please be patient.

from scpi-parser.

mvladic avatar mvladic commented on May 31, 2024

Thanks! In the meantime I will use one of the workarounds that you proposed.

from scpi-parser.

j123b567 avatar j123b567 commented on May 31, 2024

It was easier than I thought. The functionality was already there.

You can use

    if (!SCPI_ParamToInt(context, ...)) {
        if (SCPI_ParamErrorOccurred(context)) {
            return SCPI_RES_ERR;   
        }
        // not a real error, i.e. optional parameter is missing, continue
    }

from scpi-parser.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.