1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include "sunversion.hxx"
22 #include <osl/thread.h>
23 #include <rtl/character.hxx>
24 #include <rtl/ustring.hxx>
26 namespace jfw_plugin
{ //stoc_javadetect
29 SunVersion::SunVersion(std::u16string_view usVer
):
30 m_nUpdateSpecial(0), m_preRelease(Rel_NONE
)
32 OString sVersion
= OUStringToOString(usVer
, osl_getThreadTextEncoding());
33 m_bValid
= init(sVersion
.getStr());
35 SunVersion::SunVersion(const char * szVer
):
36 m_nUpdateSpecial(0), m_preRelease(Rel_NONE
)
38 m_bValid
= init(szVer
);
42 /**Format major.minor.maintenance_update
44 bool SunVersion::init(const char *szVersion
)
46 if (!szVersion
|| szVersion
[0] == '\0')
49 //first get the major,minor,maintenance
50 const char * pLast
= szVersion
;
51 const char * pCur
= szVersion
;
52 //pEnd point to the position after the last character
53 const char * pEnd
= szVersion
+ strlen(szVersion
);
54 // 0 = major, 1 = minor, 2 = maintenance, 3 = update
56 // position within part beginning with 0
60 //char must me a number 0 - 999 and no leading
63 if (pCur
< pEnd
&& rtl::isAsciiDigit(static_cast<unsigned char>(*pCur
)))
68 //if correct separator then form integer
70 (nPartPos
!= 0) // prevents: ".4.1", "..1", part must start with digit
72 //separators after maintenance (1.4.1_01, 1.4.1-beta, or 1.4.1)
73 (pCur
== pEnd
|| *pCur
== '_' || *pCur
== '-')
75 //separators between major-minor and minor-maintenance (or fourth segment)
76 (nPart
< 3 && *pCur
== '.') )
78 //prevent 1.4.0. 1.4.0-
80 || rtl::isAsciiDigit(static_cast<unsigned char>(*pCur
))) )
82 bool afterMaint
= pCur
== pEnd
|| *pCur
== '_' || *pCur
== '-';
84 int len
= pCur
- pLast
;
88 strncpy(buf
, pLast
, len
);
93 m_arVersionParts
[nPart
] = atoi(buf
);
102 //check next character
103 if (! ( (pCur
< pEnd
)
105 && rtl::isAsciiDigit(
106 static_cast<unsigned char>(*pCur
)))))
116 //We have now 1.4.1. This can be followed by _01 (or a fourth segment .1), -beta, etc.
117 // _01 (update) According to docu must not be followed by any other
118 //characters, but on Solaris 9 we have a 1.4.1_01a!!
119 if (* (pCur
- 1) == '_' || *(pCur
- 1) == '.')
121 // update is the last part _01, _01a, part 0 is the digits parts and 1 the trailing alpha
126 if ( ! rtl::isAsciiDigit(static_cast<unsigned char>(*pCur
)))
128 //1.8.0_102-, 1.8.0_01a,
129 size_t len
= pCur
- pLast
;
130 if (len
> sizeof(buf
) - 1)
132 //we've got the update: 01, 02 etc
133 strncpy(buf
, pLast
, len
);
135 m_arVersionParts
[nPart
] = atoi(buf
);
140 if (*pCur
== 'a' && (pCur
+ 1) == pEnd
)
142 //check if it s followed by a simple "a" (not specified)
143 m_nUpdateSpecial
= *pCur
;
146 else if (*pCur
== '-' && pCur
< pEnd
)
149 PreRelease pr
= getPreRelease(++pCur
);
152 //just ignore -ea because its no official release
168 else if (*(pCur
- 1) == '-')
170 m_preRelease
= getPreRelease(pCur
);
171 if (m_preRelease
== Rel_NONE
)
174 if (m_preRelease
== Rel_FreeBSD
)
176 pCur
++; //eliminate 'p'
178 && rtl::isAsciiDigit(static_cast<unsigned char>(*pCur
)))
180 int len
= pCur
- pLast
-1; //eliminate 'p'
183 strncpy(buf
, (pLast
+1), len
); //eliminate 'p'
185 m_nUpdateSpecial
= atoi(buf
)+100; //hack for FBSD #i56953#
197 SunVersion::PreRelease
SunVersion::getPreRelease(const char *szRelease
)
199 if (szRelease
== nullptr)
201 if( ! strcmp(szRelease
,"internal"))
203 else if( ! strcmp(szRelease
,"ea"))
205 else if( ! strcmp(szRelease
,"ea1"))
207 else if( ! strcmp(szRelease
,"ea2"))
209 else if( ! strcmp(szRelease
,"ea3"))
211 else if ( ! strcmp(szRelease
,"beta"))
213 else if ( ! strcmp(szRelease
,"beta1"))
215 else if ( ! strcmp(szRelease
,"beta2"))
217 else if ( ! strcmp(szRelease
,"beta3"))
219 else if (! strcmp(szRelease
, "rc"))
221 else if (! strcmp(szRelease
, "rc1"))
223 else if (! strcmp(szRelease
, "rc2"))
225 else if (! strcmp(szRelease
, "rc3"))
227 #if defined (FREEBSD)
228 else if (! strncmp(szRelease
, "p", 1))
240 returns false if both values are equal
242 bool SunVersion::operator > (const SunVersion
& ver
) const
247 //compare major.minor.maintenance
248 for( int i
= 0; i
< 4; i
++)
251 if(m_arVersionParts
[i
] > ver
.m_arVersionParts
[i
])
255 else if (m_arVersionParts
[i
] < ver
.m_arVersionParts
[i
])
260 //major.minor.maintenance_update are equal. Test for a trailing char
261 if (m_nUpdateSpecial
> ver
.m_nUpdateSpecial
)
266 //Until here the versions are equal
267 //compare pre -release values
268 if ((m_preRelease
== Rel_NONE
&& ver
.m_preRelease
== Rel_NONE
)
270 (m_preRelease
!= Rel_NONE
&& ver
.m_preRelease
== Rel_NONE
))
272 else if (m_preRelease
== Rel_NONE
&& ver
.m_preRelease
!= Rel_NONE
)
274 else if (m_preRelease
> ver
.m_preRelease
)
280 bool SunVersion::operator < (const SunVersion
& ver
) const
282 return (! operator > (ver
)) && (! operator == (ver
));
285 bool SunVersion::operator == (const SunVersion
& ver
) const
288 for(int i
= 0; i
< 4; i
++)
290 if( m_arVersionParts
[i
] != ver
.m_arVersionParts
[i
])
296 bRet
= m_nUpdateSpecial
== ver
.m_nUpdateSpecial
&& bRet
;
297 bRet
= m_preRelease
== ver
.m_preRelease
&& bRet
;
303 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */