C + + Implementation software version number comparison

Source: Internet
Author: User

Software version number of the check and comparison is a feature that we often use, I wrote a function, it is very convenient to use.

function function: Perform version comparison in string form//return value: Compare successful return true, compare failure return false//Compare result save to Nresult//LpszVer1 above LpszVer2 nresult value is 1//LP SzVer1 lower than lpszVer2 when the value of Nresult is -1//lpszVer1 equals lpszVer2 when Nresult value is 0//usage Description: The safe usage should be to judge the function return value first, Return to True when checking for comparison results in nresult bool Compareversion (__in lpctstr lpszVer1, __in lpctstr lpszVer2, __out Short & Nresult) {//Parameter security Sex Check if (lpszVer1 = = NULL | | lpszver1[0] = _t (' lpszVer2 ')) return false;if (= = NULL | | lpszver2[0] = = _t (') ') return FAL se;//parameter validity check for (size_t i = 0; i < _tcslen (lpszVer1); i++) {//If the current character in the specified version number is outside the range of 0 to 9 and is not a split point, it is considered invalid if ((Lpszver1[i] &l T _t (' 0 ') | | Lpszver1[i] > _t (' 9 ') && lpszver1[i]! = _t ('. ')) return FALSE;}  for (size_t i = 0; i < _tcslen (lpszVer2); i++) {//If the current character in the specified version number is outside the range 0 to 9 and is not a split point, it is considered invalid if ((Lpszver2[i] < _t (' 0 ') | | Lpszver2[i] > _t (' 9 ') && lpszver2[i]! = _t ('. ')) return FALSE;} The _tcstok_s function modifies the input, so the version string is copied a copy//according to the usual notation of the version number, it is not necessary to request space from the heap based on the size of the source version string, directly defined to meet the overwhelming majority of requirements, but also to raise the high efficiency of the const short Nmax_ver_len = 64; TCHAR SzveR1[nmax_ver_len] = {0}, Szver2[nmax_ver_len] = {0}; StringCchCopy (SzVer1, nmax_ver_len-1, lpszVer1); StringCchCopy (SzVer2, nmax_ver_len-1, lpszVer2); const TCHAR szseps[] = _t ("."); LPTSTR lpszToken1 = null, lpszToken2 = NULL; LPTSTR lpszNextToken1 = NULL, lpszNextToken2 = Null;lpsztoken1 = _tcstok_s (SzVer1, Szseps, &AMP;LPSZNEXTTOKEN1); LpszToken2 = _tcstok_s (SzVer2, Szseps, &lpsznexttoken2);//Progressive split while ((LpszToken1! = NULL) | | (LpszToken2! = NULL))  {int nNum1 = 0, nNum2 = 0;//checks the split result if (lpszToken1! = NULL) {nNum1 = _tstoi (lpszToken1); lpszToken1 = _tcstok_s (NULL, Szseps, &AMP;LPSZNEXTTOKEN1);} if (lpszToken2! = NULL) {nNum2 = _tstoi (lpszToken2); lpszToken2 = _tcstok_s (NULL, Szseps, &lpsznexttoken2);} Perform comparison if (NNum1 > nNum2) {nresult = 1;break;} else if (NNum1 < nNum2) {nresult = -1;break;} Else{nresult = 0;}} return TRUE;}


C + + Implementation software version number comparison

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.