summaryrefslogtreecommitdiff
path: root/modules/atoms/macros/atoms_internals/atomsVersionCompare.sci
blob: 5301fe8b69d122252e9c3d1f77b07a8e7a95d6b8 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
// Copyright (C) 2009 - DIGITEO - Pierre MARECHAL <pierre.marechal@scilab.org>
//
// This file must be used under the terms of the CeCILL.
// This source file is licensed as described in the file COPYING, which
// you should have received as part of this distribution.  The terms
// are also available at
// http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt

// Internal function

// Return  1 if version_1 is superior than version_2
// Return -1 if version_2 is superior than version_1
// Return  0 if version_1 and version_2 are equal

// version can be an array

function result = atomsVersionCompare( version_1 , version_2 )
    rhs    = argn(2);

    // Check number of input arguments
    // =========================================================================

    if rhs <> 2 then
        error(msprintf(gettext("%s: Wrong number of input argument: %d expected.\n"),"atomsVersionCompare",2));
    end

    // Check input parameters type
    // =========================================================================

    if type(version_1) <> 10 then
        error(msprintf(gettext("%s: Wrong type for input argument #%d: Single string expected.\n"),"atomsVersionCompare",1));
    end

    if type(version_2) <> 10 then
        error(msprintf(gettext("%s: Wrong type for input argument #%d: Single string expected.\n"),"atomsVersionCompare",2));
    end

    // Check input parameters size
    // =========================================================================

    if size(version_2,"*") <> 1 then
        error(msprintf(gettext("%s: Wrong size for input argument #%d: Single string expected.\n"),"atomsVersionCompare",2));
    end

    // Check input parameters values
    // =========================================================================

    for i=1:size(version_1,"*")

        if (length(version_1(i)) > 1) & (regexp(version_1(i),"/^[0-9]([0-9\.])*[0-9](\-[0-9]([0-9])*)?$/") == []) then
            error(msprintf(gettext("%s: Wrong value for input argument #%d: This is not a valid version.\n"),"atomsVersionCompare",1));
        end

        if (length(version_1(i)) == 1) & (regexp(version_1(i),"/^[0-9]$/") == []) then
            error(msprintf(gettext("%s: Wrong value for input argument #%d: This is not a valid version.\n"),"atomsVersionCompare",1));
        end

    end

    // version_2 == -1 means no more recent version found
    if version_2 == "-1" then
        result = ones(version_1);
        return
    end


    if (length(version_2) > 1) & (regexp(version_2,"/^[0-9]([0-9\.])*[0-9](\-[0-9]([0-9])*)?$/") == []) then
        error(msprintf(gettext("%s: Wrong value for input argument #%d: This is not a valid version.\n"),"atomsVersionCompare",2));
    end

    if (length(version_2) == 1) & (regexp(version_2,"/^[0-9]$/") == []) then
        error(msprintf(gettext("%s: Wrong value for input argument #%d: This is not a valid version.\n"),"atomsVersionCompare",2));
    end

    // Init the result matrix
    // =========================================================================
    result = zeros(version_1);

    // Now : action
    // =========================================================================

    // Split version and packaging version

    if strindex(version_2,"-")==[] then
        version_2_pack = 0;
    else
        version_2_tmp  = strsubst( strsplit(version_2,strindex(version_2,"-")) , "-" , "" );
        version_2      = version_2_tmp(1);
        version_2_pack = strtod(version_2_tmp(2));
    end

    // Split the version

    if regexp(version_2,"/\./","o") == [] then
        version_2_mat = strtod(version_2);
    else
        version_2_mat = strtod(strsubst( strsplit(version_2,strindex(version_2,".")) , "." , "" ));
    end

    version_2_mat_size = size(version_2_mat,"*");

    for i=1:size(version_1,"*")

        // Split version and packaging version

        if strindex(version_1(i),"-")==[] then
            version_1_pack = 0;
        else
            version_1_tmp  = strsubst( strsplit(version_1(i),strindex(version_1(i),"-")) , "-" , "" );
            version_1(i)   = version_1_tmp(1);
            version_1_pack = strtod(version_1_tmp(2));
        end

        // split the version

        if regexp(version_1(i),"/\./","o") == [] then
            version_1_mat = strtod(version_1(i));
        else
            version_1_mat = strtod(strsubst( strsplit(version_1(i),strindex(version_1(i),".")) , "." , "" ));
        end

        version_1_mat_size = size(version_1_mat,"*");

        if version_1_mat_size>=version_2_mat_size then
            for j=1:version_1_mat_size

                if result(i) <> 0 then
                    break;
                end

                if j > version_2_mat_size then
                    if version_1_mat(j) > 0 then
                        // Version_1 is superior than version_2
                        result(i) = 1;
                        break;
                    end
                    continue;
                end

                if version_1_mat(j) > version_2_mat(j) then
                    // Version_1 is superior than version_2
                    result(i) = 1;
                    break;
                end

                if version_2_mat(j) > version_1_mat(j) then
                    // Version_2 is superior than version_1
                    result(i) = -1;
                    break;
                end

            end
        else
            for j=1:version_2_mat_size

                if result(i) <> 0 then
                    break;
                end

                if j > version_1_mat_size then
                    if version_2_mat(j) > 0 then
                        // Version_2 is superior than version_1
                        result(i) = -1;
                        break;
                    end
                    continue;
                end

                if version_2_mat(j) > version_1_mat(j) then
                    // Version_2 is superior than version_1
                    result(i) = -1;
                    break;
                end

                if version_1_mat(j) > version_2_mat(j) then
                    // Version_1 is superior than version_2
                    result(i) = 1;
                    break;
                end

            end

        end

        // Last test : check if the two version are equivalent
        // If yes : compare the packaging versions

        if result(i) == 0 then

            if version_1_pack > version_2_pack then
                result(i) = 1;

            elseif version_1_pack < version_2_pack then
                result(i) = -1;

            else
                result(i) = 0;

            end

        end

    end

endfunction