summaryrefslogtreecommitdiffstats
path: root/src/spek-utils.cc
blob: 1eb0d38692120fad7c0f607c3dae19bc9bbb8f43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <assert.h>
#include <limits.h>
#include <stdlib.h>

#include "spek-utils.h"

int spek_vercmp(const char *a, const char *b)
{
    assert(a && b);

    if (!*a && !*b) {
        return 0;
    }
    if (!*a) {
        return -1;
    }
    if (!*b) {
        return 1;
    }

    char *i, *j;
    while(1) {
        i = j = NULL;
        long x = strtol(a, &i, 10);
        long y = strtol(b, &j, 10);

        if (x < y) {
            return -1;
        }
        if (x > y) {
            return 1;
        }

        if (!*i && !*j) {
            return 0;
        }
        if (!*i) {
            return -1;
        }
        if (!*j) {
            return 1;
        }

        a = i + 1;
        b = j + 1;
    }
}