LCOV - code coverage report
Current view: top level - jvmfwk/plugins/sunmajor/pluginlib - sunversion.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 72 143 50.3 %
Date: 2014-04-14 Functions: 6 9 66.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : 
      21             : #include "sunversion.hxx"
      22             : #include "osl/thread.h"
      23             : #include "osl/process.h"
      24             : #include "osl/security.hxx"
      25             : #include <string.h>
      26             : #include <ctype.h>
      27             : #include "diagnostics.h"
      28             : using namespace osl;
      29             : 
      30             : namespace jfw_plugin  { //stoc_javadetect
      31             : 
      32             : 
      33             : #if OSL_DEBUG_LEVEL >= 2
      34             : class SelfTest
      35             : {
      36             : public:
      37             :     SelfTest();
      38             : } test;
      39             : #endif
      40             : 
      41           2 : SunVersion::SunVersion(const OUString &usVer):
      42             :     m_nUpdateSpecial(0), m_preRelease(Rel_NONE),
      43           2 :     usVersion(usVer)
      44             : {
      45           2 :     memset(m_arVersionParts, 0, sizeof(m_arVersionParts));
      46           2 :     OString sVersion= OUStringToOString(usVer, osl_getThreadTextEncoding());
      47           2 :     m_bValid = init(sVersion.getStr());
      48           2 : }
      49           0 : SunVersion::SunVersion(const char * szVer):
      50           0 :     m_nUpdateSpecial(0), m_preRelease(Rel_NONE)
      51             : {
      52           0 :     memset(m_arVersionParts, 0, sizeof(m_arVersionParts));
      53           0 :     m_bValid = init(szVer);
      54           0 :     usVersion= OUString(szVer,strlen(szVer),osl_getThreadTextEncoding());
      55           0 : }
      56             : 
      57             : 
      58             : /**Format major.minor.maintainance_update
      59             :  */
      60           2 : bool SunVersion::init(const char *szVersion)
      61             : {
      62           2 :     if ( ! szVersion || strlen(szVersion) == 0)
      63           0 :         return false;
      64             : 
      65             :     //first get the major,minor,maintainance
      66           2 :     const char * pLast = szVersion;
      67           2 :     const char * pCur = szVersion;
      68             :     //pEnd point to the position after the last character
      69           2 :     const char * pEnd = szVersion + strlen(szVersion);
      70             :     // 0 = major, 1 = minor, 2 = maintainance, 3 = update
      71           2 :     int nPart = 0;
      72             :     // position within part beginning with 0
      73           2 :     int nPartPos = 0;
      74             :     char buf[128];
      75             : 
      76             :     //char must me a number 0 - 999 and no leading
      77             :     while (true)
      78             :     {
      79          12 :         if (pCur < pEnd && isdigit(*pCur))
      80             :         {
      81           6 :             if (pCur < pEnd)
      82           6 :                 pCur ++;
      83           6 :             nPartPos ++;
      84             :         }
      85             :         //if  correct separator then form integer
      86           6 :         else if (
      87             :             ! (nPartPos == 0) // prevents: ".4.1", "..1", part must start with digit
      88           6 :             && (
      89             :                 //separators after maintainance (1.4.1_01, 1.4.1-beta, or1.4.1
      90           5 :                 ((pCur == pEnd || *pCur == '_' || *pCur == '-') && (nPart == 2 ))
      91           4 :                 ||
      92             :                 //separators between major-minor and minor-maintainance
      93           4 :                 (nPart < 2 && *pCur == '.') )
      94           6 :             && (
      95             :                 //prevent 1.4.0. 1.4.0-
      96           6 :                 pCur + 1 == pEnd ? isdigit(*(pCur)) : 1) )
      97             :         {
      98           6 :             int len = pCur - pLast;
      99           6 :             if (len >= 127)
     100           0 :                 return false;
     101             : 
     102           6 :             strncpy(buf, pLast, len);
     103           6 :             buf[len] = 0;
     104           6 :             pCur ++;
     105           6 :             pLast = pCur;
     106             : 
     107           6 :             m_arVersionParts[nPart] = atoi(buf);
     108           6 :             nPart ++;
     109           6 :             nPartPos = 0;
     110           6 :             if (nPart == 3)
     111           2 :                 break;
     112             : 
     113             :             //check next character
     114           4 :             if (! ( (pCur < pEnd)
     115           4 :                     && ( (nPart < 3) && isdigit(*pCur))))
     116           0 :                 return false;
     117             :         }
     118             :         else
     119             :         {
     120           0 :             return false;
     121             :         }
     122             :     }
     123           2 :     if (pCur >= pEnd)
     124           1 :         return true;
     125             :     //We have now 1.4.1. This can be followed by _01, -beta, etc.
     126             :     // _01 (update) According to docu must not be followed by any other
     127             :     //characters, but on Solaris 9 we have a 1.4.1_01a!!
     128           1 :     if (* (pCur - 1) == '_')
     129             :     {// _01, _02
     130             :         // update is the last part _01, _01a, part 0 is the digits parts and 1 the trailing alpha
     131             :         while (true)
     132             :         {
     133           3 :             if (pCur <= pEnd)
     134             :             {
     135           3 :                 if ( ! isdigit(*pCur))
     136             :                 {
     137             :                     //1.4.1_01-, 1.4.1_01a, the numerical part may only be 2 chars.
     138           1 :                     int len = pCur - pLast;
     139           1 :                     if (len > 2)
     140           0 :                         return false;
     141             :                     //we've got the update: 01, 02 etc
     142           1 :                     strncpy(buf, pLast, len);
     143           1 :                     buf[len] = 0;
     144           1 :                     m_arVersionParts[nPart] = atoi(buf);
     145           1 :                     if (pCur == pEnd)
     146             :                     {
     147           1 :                         break;
     148             :                     }
     149           0 :                     if (*pCur == 'a' && (pCur + 1) == pEnd)
     150             :                     {
     151             :                         //check if it s followed by a simple "a" (not specified)
     152           0 :                         m_nUpdateSpecial = *pCur;
     153           0 :                         break;
     154             :                     }
     155           0 :                     else if (*pCur == '-' && pCur < pEnd)
     156             :                     {
     157             :                         //check 1.5.0_01-ea
     158           0 :                         PreRelease pr = getPreRelease(++pCur);
     159           0 :                         if (pr == Rel_NONE)
     160           0 :                             return false;
     161             :                         //just ignore -ea because its no official release
     162           0 :                         break;
     163             :                     }
     164             :                     else
     165             :                     {
     166           0 :                         return false;
     167             :                     }
     168             :                 }
     169           2 :                 if (pCur < pEnd)
     170           2 :                     pCur ++;
     171             :                 else
     172           0 :                     break;
     173             :             }
     174           2 :         }
     175             :     }
     176             :     // 1.4.1-ea
     177           0 :     else if (*(pCur - 1) == '-')
     178             :     {
     179           0 :         m_preRelease = getPreRelease(pCur);
     180           0 :         if (m_preRelease == Rel_NONE)
     181           0 :             return false;
     182             : #if defined(FREEBSD)
     183             :       if (m_preRelease == Rel_FreeBSD)
     184             :       {
     185             :           pCur++; //eliminate 'p'
     186             :           if (pCur < pEnd && isdigit(*pCur))
     187             :               pCur ++;
     188             :           int len = pCur - pLast -1; //eliminate 'p'
     189             :           if (len >= 127)
     190             :               return false;
     191             :           strncpy(buf, (pLast+1), len); //eliminate 'p'
     192             :           buf[len] = 0;
     193             :           m_nUpdateSpecial = atoi(buf)+100; //hack for FBSD #i56953#
     194             :           return true;
     195             :       }
     196             : #endif
     197             :     }
     198             :     else
     199             :     {
     200           0 :         return false;
     201             :     }
     202          11 :     return true;
     203             : }
     204             : 
     205           0 : SunVersion::PreRelease SunVersion::getPreRelease(const char *szRelease)
     206             : {
     207           0 :     if (szRelease == NULL)
     208           0 :         return Rel_NONE;
     209           0 :     if( ! strcmp(szRelease,"internal"))
     210           0 :         return  Rel_INTERNAL;
     211           0 :     else if( ! strcmp(szRelease,"ea"))
     212           0 :         return  Rel_EA;
     213           0 :     else if( ! strcmp(szRelease,"ea1"))
     214           0 :         return Rel_EA1;
     215           0 :     else if( ! strcmp(szRelease,"ea2"))
     216           0 :         return Rel_EA2;
     217           0 :     else if( ! strcmp(szRelease,"ea3"))
     218           0 :         return Rel_EA3;
     219           0 :     else if ( ! strcmp(szRelease,"beta"))
     220           0 :         return Rel_BETA;
     221           0 :     else if ( ! strcmp(szRelease,"beta1"))
     222           0 :         return Rel_BETA1;
     223           0 :     else if ( ! strcmp(szRelease,"beta2"))
     224           0 :         return Rel_BETA2;
     225           0 :     else if ( ! strcmp(szRelease,"beta3"))
     226           0 :         return Rel_BETA3;
     227           0 :     else if (! strcmp(szRelease, "rc"))
     228           0 :         return Rel_RC;
     229           0 :     else if (! strcmp(szRelease, "rc1"))
     230           0 :         return Rel_RC1;
     231           0 :     else if (! strcmp(szRelease, "rc2"))
     232           0 :         return Rel_RC2;
     233           0 :     else if (! strcmp(szRelease, "rc3"))
     234           0 :         return Rel_RC3;
     235             : #if defined (FREEBSD)
     236             :     else if (! strncmp(szRelease, "p", 1))
     237             :         return Rel_FreeBSD;
     238             : #endif
     239             :     else
     240           0 :         return Rel_NONE;
     241             : }
     242             : 
     243           2 : SunVersion::~SunVersion()
     244             : {
     245             : 
     246           2 : }
     247             : 
     248             : /* Examples:
     249             :    a) 1.0 < 1.1
     250             :    b) 1.0 < 1.0.0
     251             :    c)  1.0 < 1.0_00
     252             : 
     253             :    returns false if both values are equal
     254             : */
     255           1 : bool SunVersion::operator > (const SunVersion& ver) const
     256             : {
     257           1 :     if( &ver == this)
     258           0 :         return false;
     259             : 
     260             :     //compare major.minor.maintainance
     261           2 :     for( int i= 0; i < 4; i ++)
     262             :     {
     263             :         // 1.4 > 1.3
     264           2 :         if(m_arVersionParts[i] > ver.m_arVersionParts[i])
     265             :         {
     266           1 :             return true;
     267             :         }
     268           1 :         else if (m_arVersionParts[i] < ver.m_arVersionParts[i])
     269             :         {
     270           0 :             return false;
     271             :         }
     272             :     }
     273             :     //major.minor.maintainance_update are equal. test for a trailing char
     274           0 :     if (m_nUpdateSpecial > ver.m_nUpdateSpecial)
     275             :     {
     276           0 :         return true;
     277             :     }
     278             : 
     279             :     //Until here the versions are equal
     280             :     //compare pre -release values
     281           0 :     if ((m_preRelease == Rel_NONE && ver.m_preRelease == Rel_NONE)
     282           0 :         ||
     283           0 :         (m_preRelease != Rel_NONE && ver.m_preRelease == Rel_NONE))
     284           0 :         return false;
     285           0 :     else if (m_preRelease == Rel_NONE && ver.m_preRelease != Rel_NONE)
     286           0 :         return true;
     287           0 :     else if (m_preRelease > ver.m_preRelease)
     288           0 :         return true;
     289             : 
     290           0 :     return false;
     291             : }
     292             : 
     293           0 : bool SunVersion::operator < (const SunVersion& ver) const
     294             : {
     295           0 :     return (! operator > (ver)) && (! operator == (ver));
     296             : }
     297             : 
     298           1 : bool SunVersion::operator == (const SunVersion& ver) const
     299             : {
     300           1 :     bool bRet= true;
     301           2 :     for(int i= 0; i < 4; i++)
     302             :     {
     303           2 :         if( m_arVersionParts[i] != ver.m_arVersionParts[i])
     304             :         {
     305           1 :             bRet= false;
     306           1 :             break;
     307             :         }
     308             :     }
     309           1 :     bRet = m_nUpdateSpecial == ver.m_nUpdateSpecial && bRet;
     310           1 :     bRet = m_preRelease == ver.m_preRelease && bRet;
     311           1 :     return bRet;
     312             : }
     313             : 
     314           1 : SunVersion::operator bool()
     315             : {
     316           1 :     return m_bValid;
     317             : }
     318             : 
     319             : #if OSL_DEBUG_LEVEL >= 2
     320             : SelfTest::SelfTest()
     321             : {
     322             :     bool bRet = true;
     323             : 
     324             :     char const * versions[] = {"1.4.0", "1.4.1", "1.0.0", "10.0.0", "10.10.0",
     325             :                          "10.2.2", "10.10.0", "10.10.10", "111.0.999",
     326             :                          "1.4.1_01", "9.90.99_09", "1.4.1_99",
     327             :                          "1.4.1_00a",
     328             :                          "1.4.1-ea", "1.4.1-beta", "1.4.1-rc1",
     329             :                          "1.5.0_01-ea", "1.5.0_01-rc2"};
     330             :     char const * badVersions[] = {".4.0", "..1", "", "10.0", "10.10.0.", "10.10.0-", "10.10.0.",
     331             :                             "10.2-2", "10_10.0", "10..10","10.10", "a.0.999",
     332             :                             "1.4b.1_01", "9.90.-99_09", "1.4.1_99-",
     333             :                             "1.4.1_00a2", "1.4.0_z01z", "1.4.1__99A",
     334             :                             "1.4.1-1ea", "1.5.0_010", "1.5.0._01-", "1.5.0_01-eac"};
     335             :     char const * orderedVer[] = { "1.3.1-ea", "1.3.1-beta", "1.3.1-rc1",
     336             :                             "1.3.1", "1.3.1_00a", "1.3.1_01", "1.3.1_01a",
     337             :                             "1.3.2", "1.4.0", "1.5.0_01-ea", "2.0.0"};
     338             : 
     339             :     int num = sizeof (versions) / sizeof(char*);
     340             :     int numBad = sizeof (badVersions) / sizeof(char*);
     341             :     int numOrdered = sizeof (orderedVer) / sizeof(char*);
     342             :     //parsing test (positive)
     343             :     for (int i = 0; i < num; i++)
     344             :     {
     345             :         SunVersion ver(versions[i]);
     346             :         if ( ! ver)
     347             :         {
     348             :             bRet = false;
     349             :             break;
     350             :         }
     351             :     }
     352             :     OSL_ENSURE(bRet, "SunVersion selftest failed");
     353             :     //Parsing test (negative)
     354             :     for ( int i = 0; i < numBad; i++)
     355             :     {
     356             :         SunVersion ver(badVersions[i]);
     357             :         if (ver)
     358             :         {
     359             :             bRet = false;
     360             :             break;
     361             :         }
     362             :     }
     363             :     OSL_ENSURE(bRet, "SunVersion selftest failed");
     364             : 
     365             :     // Ordering test
     366             :     bRet = true;
     367             :     int j = 0;
     368             :     for (int i = 0; i < numOrdered; i ++)
     369             :     {
     370             :         SunVersion curVer(orderedVer[i]);
     371             :         if ( ! curVer)
     372             :         {
     373             :             bRet = false;
     374             :             break;
     375             :         }
     376             :         for (j = 0; j < numOrdered; j++)
     377             :         {
     378             :             SunVersion compVer(orderedVer[j]);
     379             :             if (i < j)
     380             :             {
     381             :                 if ( !(curVer < compVer))
     382             :                 {
     383             :                     bRet = false;
     384             :                     break;
     385             :                 }
     386             :             }
     387             :             else if ( i == j)
     388             :             {
     389             :                 if (! (curVer == compVer
     390             :                        && ! (curVer > compVer)
     391             :                        && ! (curVer < compVer)))
     392             :                 {
     393             :                     bRet = false;
     394             :                     break;
     395             :                 }
     396             :             }
     397             :             else if (i > j)
     398             :             {
     399             :                 if ( !(curVer > compVer))
     400             :                 {
     401             :                     bRet = false;
     402             :                     break;
     403             :                 }
     404             :             }
     405             :         }
     406             :         if ( ! bRet)
     407             :             break;
     408             :     }
     409             :     if (bRet)
     410             :         JFW_TRACE2("[Java framework] sunjavaplugin: Testing class SunVersion succeeded.\n");
     411             :     else
     412             :         OSL_ENSURE(bRet, "[Java framework] sunjavaplugin: SunVersion self test failed.\n");
     413             : }
     414             : #endif
     415             : 
     416             : }
     417             : 
     418             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10