bloguetronica / cp2130-qt

A C++ class that uses libusb to interface with CP2130 devices. It can be used to configure the OTP ROM of such devices, as well as to control them. The class was made for Qt. If you wish to use a derived non-Qt version, please refer to https://github.com/bloguetronica/cp2130.
0 stars 0 forks source link

Functions writeManufacturerDesc(), writeProductDesc() and writeSerialDesc() should be simplified #28

Closed samuelfmlourenco closed 2 years ago

samuelfmlourenco commented 2 years ago

Inside the functions mentioned in the title, the size of the descriptor string is being stored inside a variable, which is named strsize on every case (lines 972, 1008 and 1032). Since that variable is only used once, the size() function result could be applied directly. For instance, we currently have:

// Writes the manufacturer descriptor to the CP2130 OTP ROM
void CP2130::writeManufacturerDesc(const QString &manufacturer, int &errcnt, QString &errstr)
{
    size_t strsize = static_cast<size_t>(manufacturer.size());
    if (strsize > DESCMXL_MANUFACTURER) {
        ++errcnt;
        errstr += QObject::tr("In writeManufacturerDesc(): manufacturer descriptor string cannot be longer than 62 characters.\n");  // Program logic error
    } else {
        writeDescGeneric(manufacturer, SET_MANUFACTURING_STRING_1, errcnt, errstr);  // Refactored in version 2.1.0
    }
}

There is no need to use strsize. The function should be simply rewritten as follows:

// Writes the manufacturer descriptor to the CP2130 OTP ROM
void CP2130::writeManufacturerDesc(const QString &manufacturer, int &errcnt, QString &errstr)
{
    if (static_cast<size_t>(manufacturer.size()) > DESCMXL_MANUFACTURER) {
        ++errcnt;
        errstr += QObject::tr("In writeManufacturerDesc(): manufacturer descriptor string cannot be longer than 62 characters.\n");  // Program logic error
    } else {
        writeDescGeneric(manufacturer, SET_MANUFACTURING_STRING_1, errcnt, errstr);  // Refactored in version 2.1.0
    }
}

This also applies to the other functions mentioned above.

samuelfmlourenco commented 2 years ago

Implemented in version 2.2.2.