cool#10610 Ensure the parent-child relations of comments.
[LibreOffice.git] / jvmfwk / plugins / sunmajor / pluginlib / sunversion.cxx
blob16a1e14f36622bfcdfa679b494a4e5532e53dc4a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
25 #include <string.h>
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')
47 return false;
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
55 int nPart = 0;
56 // position within part beginning with 0
57 int nPartPos = 0;
58 char buf[128];
60 //char must me a number 0 - 999 and no leading
61 while (true)
63 if (pCur < pEnd && rtl::isAsciiDigit(static_cast<unsigned char>(*pCur)))
65 pCur ++;
66 nPartPos ++;
68 //if correct separator then form integer
69 else if (
70 (nPartPos != 0) // prevents: ".4.1", "..1", part must start with digit
71 && (
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 == '.') )
77 && (
78 //prevent 1.4.0. 1.4.0-
79 pCur + 1 != pEnd
80 || rtl::isAsciiDigit(static_cast<unsigned char>(*pCur))) )
82 bool afterMaint = pCur == pEnd || *pCur == '_' || *pCur == '-';
84 int len = pCur - pLast;
85 if (len >= 127)
86 return false;
88 strncpy(buf, pLast, len);
89 buf[len] = 0;
90 pCur ++;
91 pLast = pCur;
93 m_arVersionParts[nPart] = atoi(buf);
95 if (afterMaint)
96 nPart = 2;
97 nPart ++;
98 nPartPos = 0;
99 if (nPart == 3)
100 break;
102 //check next character
103 if (! ( (pCur < pEnd)
104 && ( (nPart < 3)
105 && rtl::isAsciiDigit(
106 static_cast<unsigned char>(*pCur)))))
107 return false;
109 else
111 return false;
114 if (pCur >= pEnd)
115 return true;
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) == '.')
120 {// _01, _02
121 // update is the last part _01, _01a, part 0 is the digits parts and 1 the trailing alpha
122 while (true)
124 if (pCur <= pEnd)
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)
131 return false;
132 //we've got the update: 01, 02 etc
133 strncpy(buf, pLast, len);
134 buf[len] = 0;
135 m_arVersionParts[nPart] = atoi(buf);
136 if (pCur == pEnd)
138 break;
140 if (*pCur == 'a' && (pCur + 1) == pEnd)
142 //check if it s followed by a simple "a" (not specified)
143 m_nUpdateSpecial = *pCur;
144 break;
146 else if (*pCur == '-' && pCur < pEnd)
148 //check 1.5.0_01-ea
149 PreRelease pr = getPreRelease(++pCur);
150 if (pr == Rel_NONE)
151 return false;
152 //just ignore -ea because its no official release
153 break;
155 else
157 return false;
160 if (pCur < pEnd)
161 pCur ++;
162 else
163 break;
167 // 1.4.1-ea
168 else if (*(pCur - 1) == '-')
170 m_preRelease = getPreRelease(pCur);
171 if (m_preRelease == Rel_NONE)
172 return false;
173 #if defined(FREEBSD)
174 if (m_preRelease == Rel_FreeBSD)
176 pCur++; //eliminate 'p'
177 if (pCur < pEnd
178 && rtl::isAsciiDigit(static_cast<unsigned char>(*pCur)))
179 pCur ++;
180 int len = pCur - pLast -1; //eliminate 'p'
181 if (len >= 127)
182 return false;
183 strncpy(buf, (pLast+1), len); //eliminate 'p'
184 buf[len] = 0;
185 m_nUpdateSpecial = atoi(buf)+100; //hack for FBSD #i56953#
186 return true;
188 #endif
190 else
192 return false;
194 return true;
197 SunVersion::PreRelease SunVersion::getPreRelease(const char *szRelease)
199 if (szRelease == nullptr)
200 return Rel_NONE;
201 if( ! strcmp(szRelease,"internal"))
202 return Rel_INTERNAL;
203 else if( ! strcmp(szRelease,"ea"))
204 return Rel_EA;
205 else if( ! strcmp(szRelease,"ea1"))
206 return Rel_EA1;
207 else if( ! strcmp(szRelease,"ea2"))
208 return Rel_EA2;
209 else if( ! strcmp(szRelease,"ea3"))
210 return Rel_EA3;
211 else if ( ! strcmp(szRelease,"beta"))
212 return Rel_BETA;
213 else if ( ! strcmp(szRelease,"beta1"))
214 return Rel_BETA1;
215 else if ( ! strcmp(szRelease,"beta2"))
216 return Rel_BETA2;
217 else if ( ! strcmp(szRelease,"beta3"))
218 return Rel_BETA3;
219 else if (! strcmp(szRelease, "rc"))
220 return Rel_RC;
221 else if (! strcmp(szRelease, "rc1"))
222 return Rel_RC1;
223 else if (! strcmp(szRelease, "rc2"))
224 return Rel_RC2;
225 else if (! strcmp(szRelease, "rc3"))
226 return Rel_RC3;
227 #if defined (FREEBSD)
228 else if (! strncmp(szRelease, "p", 1))
229 return Rel_FreeBSD;
230 #endif
231 else
232 return Rel_NONE;
235 /* Examples:
236 a) 1.0 < 1.1
237 b) 1.0 < 1.0.0
238 c) 1.0 < 1.0_00
240 returns false if both values are equal
242 bool SunVersion::operator > (const SunVersion& ver) const
244 if( &ver == this)
245 return false;
247 //compare major.minor.maintenance
248 for( int i= 0; i < 4; i ++)
250 // 1.4 > 1.3
251 if(m_arVersionParts[i] > ver.m_arVersionParts[i])
253 return true;
255 else if (m_arVersionParts[i] < ver.m_arVersionParts[i])
257 return false;
260 //major.minor.maintenance_update are equal. Test for a trailing char
261 if (m_nUpdateSpecial > ver.m_nUpdateSpecial)
263 return true;
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))
271 return false;
272 else if (m_preRelease == Rel_NONE && ver.m_preRelease != Rel_NONE)
273 return true;
274 else if (m_preRelease > ver.m_preRelease)
275 return true;
277 return false;
280 bool SunVersion::operator < (const SunVersion& ver) const
282 return (! operator > (ver)) && (! operator == (ver));
285 bool SunVersion::operator == (const SunVersion& ver) const
287 bool bRet= true;
288 for(int i= 0; i < 4; i++)
290 if( m_arVersionParts[i] != ver.m_arVersionParts[i])
292 bRet= false;
293 break;
296 bRet = m_nUpdateSpecial == ver.m_nUpdateSpecial && bRet;
297 bRet = m_preRelease == ver.m_preRelease && bRet;
298 return bRet;
303 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */