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 <dp_misc.h>
22 #include <dp_platform.hxx>
23 #include <rtl/ustring.hxx>
24 #include <rtl/ustrbuf.hxx>
25 #include <rtl/instance.hxx>
26 #include <rtl/bootstrap.hxx>
27 #include <osl/diagnose.h>
28 
29 #define PLATFORM_ALL                "all"
30 
31 
32 namespace dp_misc
33 {
34 namespace
35 {
36     struct StrOperatingSystem :
37         public rtl::StaticWithInit<OUString, StrOperatingSystem> {
operator ()dp_misc::__anon64dfc8120111::StrOperatingSystem38              OUString operator () () {
39                 OUString os( "$_OS" );
40                 ::rtl::Bootstrap::expandMacros( os );
41                 return os;
42             }
43     };
44 
45     struct StrCPU :
46         public rtl::StaticWithInit<OUString, StrCPU> {
operator ()dp_misc::__anon64dfc8120111::StrCPU47             OUString operator () () {
48                 OUString arch( "$_ARCH" );
49                 ::rtl::Bootstrap::expandMacros( arch );
50                 return arch;
51             }
52     };
53 
54 
55     struct StrPlatform : public rtl::StaticWithInit<
56         OUString, StrPlatform> {
operator ()dp_misc::__anon64dfc8120111::StrPlatform57             OUString operator () () {
58                 OUStringBuffer buf;
59                 buf.append( StrOperatingSystem::get() );
60                 buf.append( '_' );
61                 buf.append( StrCPU::get() );
62                 return buf.makeStringAndClear();
63             }
64     };
65 
checkOSandCPU(OUString const & os,OUString const & cpu)66     bool checkOSandCPU(OUString const & os, OUString const & cpu)
67     {
68         return (os == StrOperatingSystem::get())
69             && (cpu == StrCPU::get());
70     }
71 
isPlatformSupported(OUString const & token)72     bool isPlatformSupported( OUString const &  token )
73     {
74         bool ret = false;
75         if (token == PLATFORM_ALL)
76             ret = true;
77         else if (token == "windows_x86")
78             ret = checkOSandCPU("Windows", "x86");
79         else if (token == "windows_x86_64")
80             ret = checkOSandCPU("Windows", "X86_64");
81         else if (token == "solaris_sparc")
82             ret = checkOSandCPU("Solaris", "SPARC");
83         else if (token == "solaris_sparc64")
84             ret = checkOSandCPU("Solaris", "SPARC64");
85         else if (token == "solaris_x86")
86             ret = checkOSandCPU("Solaris", "x86");
87         else if (token == "aix_powerpc")
88             ret = checkOSandCPU("AIX", "PowerPC");
89         else if (token == "macosx_x86_64")
90             ret = checkOSandCPU("MacOSX", "X86_64");
91         else if (token == "linux_x86")
92             ret = checkOSandCPU("Linux", "x86");
93         else if (token == "linux_x86_64")
94             ret = checkOSandCPU("Linux", "X86_64");
95         else if (token == "linux_sparc")
96             ret = checkOSandCPU("Linux", "SPARC");
97         else if (token == "linux_sparc64")
98             ret = checkOSandCPU("Linux", "SPARC64");
99         else if (token == "linux_powerpc")
100             ret = checkOSandCPU("Linux", "PowerPC");
101         else if (token == "linux_powerpc64")
102             ret = checkOSandCPU("Linux", "PowerPC_64");
103         else if (token == "linux_powerpc64_le")
104             ret = checkOSandCPU("Linux", "PowerPC_64_LE");
105         else if (token == "linux_arm_eabi")
106             ret = checkOSandCPU("Linux", "ARM_EABI");
107         else if (token == "linux_arm_oabi")
108             ret = checkOSandCPU("Linux", "ARM_OABI");
109         else if (token == "linux_mips_el")
110             ret = checkOSandCPU("Linux", "MIPS_EL");
111         else if (token == "linux_mips64_el")
112             ret = checkOSandCPU("Linux", "MIPS64_EL");
113         else if (token == "linux_mips_eb")
114             ret = checkOSandCPU("Linux", "MIPS_EB");
115         else if (token == "linux_mips64_eb")
116             ret = checkOSandCPU("Linux", "MIPS64_EB");
117         else if (token == "linux_ia64")
118             ret = checkOSandCPU("Linux", "IA64");
119         else if (token == "linux_m68k")
120             ret = checkOSandCPU("Linux", "M68K");
121         else if (token == "linux_s390")
122             ret = checkOSandCPU("Linux", "S390");
123         else if (token == "linux_s390x")
124             ret = checkOSandCPU("Linux", "S390x");
125         else if (token == "linux_hppa")
126             ret = checkOSandCPU("Linux", "HPPA");
127         else if (token == "linux_alpha")
128             ret = checkOSandCPU("Linux", "ALPHA");
129         else if (token == "linux_aarch64")
130             ret = checkOSandCPU("Linux", "AARCH64");
131         else if (token == "freebsd_x86")
132             ret = checkOSandCPU("FreeBSD", "x86");
133         else if (token == "freebsd_x86_64")
134             ret = checkOSandCPU("FreeBSD", "X86_64");
135         else if (token == "freebsd_powerpc")
136             ret = checkOSandCPU("FreeBSD", "PowerPC");
137         else if (token == "freebsd_powerpc64")
138             ret = checkOSandCPU("FreeBSD", "PowerPC64");
139         else if (token == "kfreebsd_x86")
140             ret = checkOSandCPU("kFreeBSD", "x86");
141         else if (token == "kfreebsd_x86_64")
142             ret = checkOSandCPU("kFreeBSD", "X86_64");
143         else if (token == "netbsd_x86")
144             ret = checkOSandCPU("NetBSD", "x86");
145         else if (token == "netbsd_x86_64")
146             ret = checkOSandCPU("NetBSD", "X86_64");
147         else if (token == "openbsd_x86")
148             ret = checkOSandCPU("OpenBSD", "x86");
149         else if (token == "openbsd_x86_64")
150             ret = checkOSandCPU("OpenBSD", "X86_64");
151         else if (token == "dragonfly_x86")
152             ret = checkOSandCPU("DragonFly", "x86");
153         else if (token == "dragonfly_x86_64")
154             ret = checkOSandCPU("DragonFly", "X86_64");
155         else
156         {
157             OSL_FAIL("Extension Manager: The extension supports an unknown platform. "
158             "Check the platform in the description.xml");
159             ret = false;
160         }
161         return ret;
162     }
163 
164 } // anon namespace
165 
166 
getPlatformString()167 OUString const & getPlatformString()
168 {
169     return StrPlatform::get();
170 }
171 
platform_fits(OUString const & platform_string)172 bool platform_fits( OUString const & platform_string )
173 {
174     sal_Int32 index = 0;
175     for (;;)
176     {
177         const OUString token(
178             platform_string.getToken( 0, ',', index ).trim() );
179         // check if this platform:
180         if (token.equalsIgnoreAsciiCase( StrPlatform::get() ) ||
181             (token.indexOf( '_' ) < 0 && /* check OS part only */
182              token.equalsIgnoreAsciiCase( StrOperatingSystem::get() )))
183         {
184             return true;
185         }
186         if (index < 0)
187             break;
188     }
189     return false;
190 }
191 
hasValidPlatform(css::uno::Sequence<OUString> const & platformStrings)192 bool hasValidPlatform( css::uno::Sequence<OUString> const & platformStrings)
193 {
194     bool ret = false;
195     for (sal_Int32 i  = 0; i < platformStrings.getLength(); i++)
196     {
197         if ( isPlatformSupported( platformStrings[i] ))
198         {
199             ret = true;
200             break;
201         }
202     }
203     return ret;
204 }
205 
206 }
207 
208 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
209