Home > functions > common > utility > vb_version_cmp.m

vb_version_cmp

PURPOSE ^

Compare VBMEG version strings.

SYNOPSIS ^

function cmp_result = vb_version_cmp(ver_str1,cmp_opr,ver_str2)

DESCRIPTION ^

 Compare VBMEG version strings. 

 [syntax]
 [comparative_result] = vb_version(ver_str1,cmp_opr,ver_str2);

 [input]
 ver_str1: <<string>> VBMEG version string (e.g., '0.9-0.a.0'). 
 cmp_opr : <<string>> Comparative operator ('==', '~=', '<', '<=', '>'
           or '>='). 
 ver_str2: <<string>> VBMEG version string (e.g., '0.9-0.a.0'). 

 [output]
 comparative_result: <<bool>> Return result of logical expression 
                     'ver_str1 cmp_opr ver_str2'. 

 [example]
 1st example return true because 0.9>0.7. 
 >> vb_version_cmp('0.9-0.a.0','>','0.7')
 
 ans =

      1

 >>

 2nd example return true because the 3rd component of version string
 (commonly 'a', alpha or 'b', beta). See note. 
 >> vb_version_cmp('0.9-0','==','0.9-0.a')

 ans =

      1

 >>

 [note]
 If version string is imcomplete (e.g., '0.9-0'), remained version
 numbers will be set to those of the compared version string. In the
 second example, the first version string does not have alpha/beta
 character (e.g., 'a' or 'b'), then the first one is interpreted as
 '0.9-0.a'; 'a' comes from the second version string. 
 
 @see vb_version.m

 [history]
 2010-12-07 taku-y

 Copyright (C) 2011, ATR All Rights Reserved.
 License : New BSD License(see VBMEG_LICENSE.txt)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function cmp_result = vb_version_cmp(ver_str1,cmp_opr,ver_str2)
0002 % Compare VBMEG version strings.
0003 %
0004 % [syntax]
0005 % [comparative_result] = vb_version(ver_str1,cmp_opr,ver_str2);
0006 %
0007 % [input]
0008 % ver_str1: <<string>> VBMEG version string (e.g., '0.9-0.a.0').
0009 % cmp_opr : <<string>> Comparative operator ('==', '~=', '<', '<=', '>'
0010 %           or '>=').
0011 % ver_str2: <<string>> VBMEG version string (e.g., '0.9-0.a.0').
0012 %
0013 % [output]
0014 % comparative_result: <<bool>> Return result of logical expression
0015 %                     'ver_str1 cmp_opr ver_str2'.
0016 %
0017 % [example]
0018 % 1st example return true because 0.9>0.7.
0019 % >> vb_version_cmp('0.9-0.a.0','>','0.7')
0020 %
0021 % ans =
0022 %
0023 %      1
0024 %
0025 % >>
0026 %
0027 % 2nd example return true because the 3rd component of version string
0028 % (commonly 'a', alpha or 'b', beta). See note.
0029 % >> vb_version_cmp('0.9-0','==','0.9-0.a')
0030 %
0031 % ans =
0032 %
0033 %      1
0034 %
0035 % >>
0036 %
0037 % [note]
0038 % If version string is imcomplete (e.g., '0.9-0'), remained version
0039 % numbers will be set to those of the compared version string. In the
0040 % second example, the first version string does not have alpha/beta
0041 % character (e.g., 'a' or 'b'), then the first one is interpreted as
0042 % '0.9-0.a'; 'a' comes from the second version string.
0043 %
0044 % @see vb_version.m
0045 %
0046 % [history]
0047 % 2010-12-07 taku-y
0048 %
0049 % Copyright (C) 2011, ATR All Rights Reserved.
0050 % License : New BSD License(see VBMEG_LICENSE.txt)
0051 
0052 if ~ischar(ver_str1) || ~ischar(ver_str2)
0053   error('version string should be string.');
0054 end;
0055 
0056 % Scan version string and check error
0057 [ver1,count1,errmsg1,nextindex1] = sscanf(ver_str1, '%lf-%d.%c.%d');
0058 [ver2,count2,errmsg2,nextindex2] = sscanf(ver_str2, '%lf-%d.%c.%d');
0059 
0060 if ~isempty(errmsg1), 
0061   error(['Input version string is invalid at ' num2str(nextindex1) ...
0062          ' : ' ver_str1]);
0063 end;
0064 
0065 if ~isempty(errmsg2), 
0066   error(['Input version string is invalid at ' num2str(nextindex2) ...
0067          ' : ' ver_str2]);
0068 end;
0069 
0070 % Make version string valid
0071 input_ver1 = zeros(4,1);
0072 input_ver2 = zeros(4,1);
0073 
0074 if count1>count2, 
0075   for i=1:count2
0076     input_ver1(i) = double(ver1(i));
0077     input_ver2(i) = double(ver2(i));
0078   end;
0079   
0080   for i=count2+1:count1
0081     input_ver1(i) = double(ver1(i));
0082     input_ver2(i) = double(ver1(i));
0083   end;
0084 else
0085   for i=1:count1
0086     input_ver1(i) = double(ver1(i));
0087     input_ver2(i) = double(ver2(i));
0088   end;
0089   
0090   for i=count1+1:count2
0091     input_ver1(i) = double(ver2(i));
0092     input_ver2(i) = double(ver2(i));
0093   end;
0094 end;
0095 
0096 % Compare version strings
0097 [cmp_result] = compare_version(cmp_opr,input_ver1,input_ver2);
0098 
0099 return;
0100 
0101 function [result] = compare_version(cmp_opr,lhs,rhs)
0102 
0103 result = true;
0104 count  = length(lhs);
0105 
0106 % Compare
0107 % lhs(1) lhs(2) lhs(3) lhs(4)
0108 %   |      |      |      |
0109 % rhs(1) rhs(2) rhs(3) rhs(4)
0110 % ------------> compare direction
0111 %  k==1   k==2   k==3   k==4
0112 for k=1:count
0113     if (lhs(k) == rhs(k)) && (k ~= count)
0114         % 最上位桁から下位方向へ向かって1桁ずつチェック
0115         % 同値の場合は、ひとつ下の桁に判断を先送りする。
0116         % 先送りして最下位桁まで行った場合は、
0117         % 同値でも下の比較演算子により比較結果を出力.
0118         continue;
0119     else
0120         % 比較演算子による比較.
0121         result = eval(['lhs(k)' cmp_opr 'rhs(k)']);
0122         return;
0123     end;
0124 end;
0125 
0126 return;

Generated on Tue 27-Aug-2013 11:46:04 by m2html © 2005