Question

SCENARIO: I have a simple application that checks its RSS feed and looks if there's a newer version available. Thus, I want to check if the current version is less than the one present on the RSS feed. Ideally as simple as:

CURRENTVERSION < updateVersion

PROBLEM: The versioning consists of major.minor.revision.build and I don't know how to parse this into a number to perform the version check.

These are the parameters to compare:

#define CURRENTVERSION = 0,2,5,1

The version downloaded from the web is "0.2.6.1" ( as a string).

What would be the best way to check if one is less than the other?

I've tried converting it to a double, but the value becomes 0.2 (only the first . is being parsed, rest is ignored).

CONSTRAINT: It must not be a solution using .NET libraries as the application must work when no .NET framework is present.

(EDIT) I settled for the following solution, thanks to Karthik T's answer.

struct Version
{
    Version(string versionStr)
    {
        sscanf(versionStr.c_str(), "%d.%d.%d.%d", &major, &minor, &revision, &build);
    }

    bool operator<(const Version &otherVersion)
    {
        if(major < otherVersion.major)
            return true;
        if(minor < otherVersion.minor)
            return true;
        if(revision < otherVersion.revision)
            return true;
        if(build < otherVersion.build)
            return true;
        return false;
    }

    int major, minor, revision, build;
};
Was it helpful?

Solution

struct Version{
    Version(std::string versionStr);     //remember to use  versionStr.c_str() if using C functions like sscanf or strtok
    bool operator<(const Version &other); // could and perhaps should be a free function

    int major,minor,revision,build;
};


bool needtoUpdate = Version(CURRENTVERSION)<Version(latestVersion);

Please fill in the definitions.

Also, your #define is a mistake. You want it like below. Or use a const char * if you can.

#define CURRENTVERSION "0.2.5.1"

You can use sscanf or something like strtok to parse in the constructor.

OTHER TIPS

I recommend to just use a function whose argument is two strings stand for the version number. There are no need to use a struct or class to do such a simple thing. I think it is better to keep things simple. For example:

#include <stdio.h>
#include <string.h>

/*
 * return 1 if v1 > v2
 * return 0 if v1 = v2
 * return -1 if v1 < v2
 */

int cmpVersion(const char *v1, const char *v2)
{
    int i;
    int oct_v1[4], oct_v2[4];
    sscanf(v1, "%d.%d.%d.%d", &oct_v1[0], &oct_v1[1], &oct_v1[2], &oct_v1[3]);
    sscanf(v2, "%d.%d.%d.%d", &oct_v2[0], &oct_v2[1], &oct_v2[2], &oct_v2[3]);

    for (i = 0; i < 4; i++) {
        if (oct_v1[i] > oct_v2[i])
            return 1;
        else if (oct_v1[i] < oct_v2[i])
            return -1;
    }

    return 0;
}

int main()
{
    printf("%d\n", cmpVersion("0.1.2.3", "0.2.3.4"));
}

If you have a compiler that support C++11, you can std::tuple to make the code cleaner:

 struct Version
  {
      Version(std::string versionStr)
      {
          sscanf(versionStr.c_str(), "%d.%d.%d.%d", &major, &minor, &revision, &build);
      }
      VersionTuple ToTuple() const
      {
          return VersionTuple{ major,minor,revision,build };
      }
      int major, minor, revision, build;
  };

  bool operator<(const Version& v1, const Version& v2)
  {
      return v1.ToTuple() < v2.ToTuple();
  }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top