LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/jvmfwk/plugins/sunmajor/pluginlib - sunversion.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 72 141 51.1 %
Date: 2013-07-09 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         210 : SunVersion::SunVersion(const OUString &usVer):
      42             :     m_nUpdateSpecial(0), m_preRelease(Rel_NONE),
      43         210 :     usVersion(usVer)
      44             : {
      45         210 :     memset(m_arVersionParts, 0, sizeof(m_arVersionParts));
      46         210 :     OString sVersion= OUStringToOString(usVer, osl_getThreadTextEncoding());
      47         210 :     m_bValid = init(sVersion.getStr());
      48         210 : }
      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         210 : bool SunVersion::init(const char *szVersion)
      61             : {
      62         210 :     if ( ! szVersion || strlen(szVersion) == 0)
      63           0 :         return false;
      64             : 
      65             :     //first get the major,minor,maintainance
      66         210 :     const char * pLast = szVersion;
      67         210 :     const char * pCur = szVersion;
      68             :     //pEnd point to the position after the last character
      69         210 :     const char * pEnd = szVersion + strlen(szVersion);
      70             :     // 0 = major, 1 = minor, 2 = maintainance, 3 = update
      71         210 :     int nPart = 0;
      72             :     // position within part beginning with 0
      73         210 :     int nPartPos = 0;
      74             :     char buf[128];
      75             : 
      76             :     //char must me a number 0 - 999 and no leading
      77             :     while (1)
      78             :     {
      79        1260 :         if (pCur < pEnd && isdigit(*pCur))
      80             :         {
      81         630 :             if (pCur < pEnd)
      82         630 :                 pCur ++;
      83         630 :             nPartPos ++;
      84             :         }
      85             :         //if  correct separator then form integer
      86         630 :         else if (
      87             :             ! (nPartPos == 0) // prevents: ".4.1", "..1", part must start with digit
      88         630 :             && (
      89             :                 //separators after maintainance (1.4.1_01, 1.4.1-beta, or1.4.1
      90         525 :                 ((pCur == pEnd || *pCur == '_' || *pCur == '-') && (nPart == 2 ))
      91         420 :                 ||
      92             :                 //separators between major-minor and minor-maintainance
      93         420 :                 (nPart < 2 && *pCur == '.') )
      94         630 :             && (
      95             :                 //prevent 1.4.0. 1.4.0-
      96         630 :                 pCur + 1 == pEnd ? isdigit(*(pCur)) : 1) )
      97             :         {
      98         630 :             int len = pCur - pLast;
      99         630 :             if (len >= 127)
     100           0 :                 return false;
     101             : 
     102         630 :             strncpy(buf, pLast, len);
     103         630 :             buf[len] = 0;
     104         630 :             pCur ++;
     105         630 :             pLast = pCur;
     106             : 
     107         630 :             m_arVersionParts[nPart] = atoi(buf);
     108         630 :             nPart ++;
     109         630 :             nPartPos = 0;
     110         630 :             if (nPart == 3)
     111         210 :                 break;
     112             : 
     113             :             //check next character
     114         420 :             if (! ( (pCur < pEnd)
     115         420 :                     && ( (nPart < 3) && isdigit(*pCur))))
     116           0 :                 return false;
     117             :         }
     118             :         else
     119             :         {
     120           0 :             return false;
     121             :         }
     122             :     }
     123         210 :     if (pCur >= pEnd)
     124         105 :         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         105 :     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 (1)
     132             :         {
     133         315 :             if (pCur <= pEnd)
     134             :             {
     135         315 :                 if ( ! isdigit(*pCur))
     136             :                 {
     137             :                     //1.4.1_01-, 1.4.1_01a, the numerical part may only be 2 chars.
     138         105 :                     int len = pCur - pLast;
     139         105 :                     if (len > 2)
     140           0 :                         return false;
     141             :                     //we've got the update: 01, 02 etc
     142         105 :                     strncpy(buf, pLast, len);
     143         105 :                     buf[len] = 0;
     144         105 :                     m_arVersionParts[nPart] = atoi(buf);
     145         105 :                     if (pCur == pEnd)
     146             :                     {
     147         105 :                         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         210 :                 if (pCur < pEnd)
     170         210 :                     pCur ++;
     171             :                 else
     172           0 :                     break;
     173             :             }
     174         210 :         }
     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        1155 :     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,"ea"))
     210           0 :         return  Rel_EA;
     211           0 :     else if( ! strcmp(szRelease,"ea1"))
     212           0 :         return Rel_EA1;
     213           0 :     else if( ! strcmp(szRelease,"ea2"))
     214           0 :         return Rel_EA2;
     215           0 :     else if( ! strcmp(szRelease,"ea3"))
     216           0 :         return Rel_EA3;
     217           0 :     else if ( ! strcmp(szRelease,"beta"))
     218           0 :         return Rel_BETA;
     219           0 :     else if ( ! strcmp(szRelease,"beta1"))
     220           0 :         return Rel_BETA1;
     221           0 :     else if ( ! strcmp(szRelease,"beta2"))
     222           0 :         return Rel_BETA2;
     223           0 :     else if ( ! strcmp(szRelease,"beta3"))
     224           0 :         return Rel_BETA3;
     225           0 :     else if (! strcmp(szRelease, "rc"))
     226           0 :         return Rel_RC;
     227           0 :     else if (! strcmp(szRelease, "rc1"))
     228           0 :         return Rel_RC1;
     229           0 :     else if (! strcmp(szRelease, "rc2"))
     230           0 :         return Rel_RC2;
     231           0 :     else if (! strcmp(szRelease, "rc3"))
     232           0 :         return Rel_RC3;
     233             : #if defined (FREEBSD)
     234             :     else if (! strncmp(szRelease, "p", 1))
     235             :         return Rel_FreeBSD;
     236             : #endif
     237             :     else
     238           0 :         return Rel_NONE;
     239             : }
     240             : 
     241         210 : SunVersion::~SunVersion()
     242             : {
     243             : 
     244         210 : }
     245             : 
     246             : /* Examples:
     247             :    a) 1.0 < 1.1
     248             :    b) 1.0 < 1.0.0
     249             :    c)  1.0 < 1.0_00
     250             : 
     251             :    returns false if both values are equal
     252             : */
     253         105 : bool SunVersion::operator > (const SunVersion& ver) const
     254             : {
     255         105 :     if( &ver == this)
     256           0 :         return false;
     257             : 
     258             :     //compare major.minor.maintainance
     259         210 :     for( int i= 0; i < 4; i ++)
     260             :     {
     261             :         // 1.4 > 1.3
     262         210 :         if(m_arVersionParts[i] > ver.m_arVersionParts[i])
     263             :         {
     264         105 :             return true;
     265             :         }
     266         105 :         else if (m_arVersionParts[i] < ver.m_arVersionParts[i])
     267             :         {
     268           0 :             return false;
     269             :         }
     270             :     }
     271             :     //major.minor.maintainance_update are equal. test for a trailing char
     272           0 :     if (m_nUpdateSpecial > ver.m_nUpdateSpecial)
     273             :     {
     274           0 :         return true;
     275             :     }
     276             : 
     277             :     //Until here the versions are equal
     278             :     //compare pre -release values
     279           0 :     if ((m_preRelease == Rel_NONE && ver.m_preRelease == Rel_NONE)
     280           0 :         ||
     281           0 :         (m_preRelease != Rel_NONE && ver.m_preRelease == Rel_NONE))
     282           0 :         return false;
     283           0 :     else if (m_preRelease == Rel_NONE && ver.m_preRelease != Rel_NONE)
     284           0 :         return true;
     285           0 :     else if (m_preRelease > ver.m_preRelease)
     286           0 :         return true;
     287             : 
     288           0 :     return false;
     289             : }
     290             : 
     291           0 : bool SunVersion::operator < (const SunVersion& ver) const
     292             : {
     293           0 :     return (! operator > (ver)) && (! operator == (ver));
     294             : }
     295             : 
     296         105 : bool SunVersion::operator == (const SunVersion& ver) const
     297             : {
     298         105 :     bool bRet= true;
     299         210 :     for(int i= 0; i < 4; i++)
     300             :     {
     301         210 :         if( m_arVersionParts[i] != ver.m_arVersionParts[i])
     302             :         {
     303         105 :             bRet= false;
     304         105 :             break;
     305             :         }
     306             :     }
     307         105 :     bRet = m_nUpdateSpecial == ver.m_nUpdateSpecial && bRet;
     308         105 :     bRet = m_preRelease == ver.m_preRelease && bRet;
     309         105 :     return bRet;
     310             : }
     311             : 
     312         105 : SunVersion::operator bool()
     313             : {
     314         105 :     return m_bValid;
     315             : }
     316             : 
     317             : #if OSL_DEBUG_LEVEL >= 2
     318             : SelfTest::SelfTest()
     319             : {
     320             :     bool bRet = true;
     321             : 
     322             :     char const * versions[] = {"1.4.0", "1.4.1", "1.0.0", "10.0.0", "10.10.0",
     323             :                          "10.2.2", "10.10.0", "10.10.10", "111.0.999",
     324             :                          "1.4.1_01", "9.90.99_09", "1.4.1_99",
     325             :                          "1.4.1_00a",
     326             :                          "1.4.1-ea", "1.4.1-beta", "1.4.1-rc1",
     327             :                          "1.5.0_01-ea", "1.5.0_01-rc2"};
     328             :     char const * badVersions[] = {".4.0", "..1", "", "10.0", "10.10.0.", "10.10.0-", "10.10.0.",
     329             :                             "10.2-2", "10_10.0", "10..10","10.10", "a.0.999",
     330             :                             "1.4b.1_01", "9.90.-99_09", "1.4.1_99-",
     331             :                             "1.4.1_00a2", "1.4.0_z01z", "1.4.1__99A",
     332             :                             "1.4.1-1ea", "1.5.0_010", "1.5.0._01-", "1.5.0_01-eac"};
     333             :     char const * orderedVer[] = { "1.3.1-ea", "1.3.1-beta", "1.3.1-rc1",
     334             :                             "1.3.1", "1.3.1_00a", "1.3.1_01", "1.3.1_01a",
     335             :                             "1.3.2", "1.4.0", "1.5.0_01-ea", "2.0.0"};
     336             : 
     337             :     int num = sizeof (versions) / sizeof(char*);
     338             :     int numBad = sizeof (badVersions) / sizeof(char*);
     339             :     int numOrdered = sizeof (orderedVer) / sizeof(char*);
     340             :     //parsing test (positive)
     341             :     for (int i = 0; i < num; i++)
     342             :     {
     343             :         SunVersion ver(versions[i]);
     344             :         if ( ! ver)
     345             :         {
     346             :             bRet = false;
     347             :             break;
     348             :         }
     349             :     }
     350             :     OSL_ENSURE(bRet, "SunVersion selftest failed");
     351             :     //Parsing test (negative)
     352             :     for ( int i = 0; i < numBad; i++)
     353             :     {
     354             :         SunVersion ver(badVersions[i]);
     355             :         if (ver)
     356             :         {
     357             :             bRet = false;
     358             :             break;
     359             :         }
     360             :     }
     361             :     OSL_ENSURE(bRet, "SunVersion selftest failed");
     362             : 
     363             :     // Ordering test
     364             :     bRet = true;
     365             :     int j = 0;
     366             :     for (int i = 0; i < numOrdered; i ++)
     367             :     {
     368             :         SunVersion curVer(orderedVer[i]);
     369             :         if ( ! curVer)
     370             :         {
     371             :             bRet = false;
     372             :             break;
     373             :         }
     374             :         for (j = 0; j < numOrdered; j++)
     375             :         {
     376             :             SunVersion compVer(orderedVer[j]);
     377             :             if (i < j)
     378             :             {
     379             :                 if ( !(curVer < compVer))
     380             :                 {
     381             :                     bRet = false;
     382             :                     break;
     383             :                 }
     384             :             }
     385             :             else if ( i == j)
     386             :             {
     387             :                 if (! (curVer == compVer
     388             :                        && ! (curVer > compVer)
     389             :                        && ! (curVer < compVer)))
     390             :                 {
     391             :                     bRet = false;
     392             :                     break;
     393             :                 }
     394             :             }
     395             :             else if (i > j)
     396             :             {
     397             :                 if ( !(curVer > compVer))
     398             :                 {
     399             :                     bRet = false;
     400             :                     break;
     401             :                 }
     402             :             }
     403             :         }
     404             :         if ( ! bRet)
     405             :             break;
     406             :     }
     407             :     if (bRet)
     408             :         JFW_TRACE2("[Java framework] sunjavaplugin: Testing class SunVersion succeeded.\n");
     409             :     else
     410             :         OSL_ENSURE(bRet, "[Java framework] sunjavaplugin: SunVersion self test failed.\n");
     411             : }
     412             : #endif
     413             : 
     414             : }
     415             : 
     416             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10