Get the style color and number just once
[LibreOffice.git] / jvmfwk / source / fwkutil.cxx
blob0c3c8f4beb3d42f07f18b2a49af31b2be9ba8c44
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 #if defined(_WIN32)
22 #if !defined WIN32_LEAN_AND_MEAN
23 # define WIN32_LEAN_AND_MEAN
24 #endif
25 #include <windows.h>
26 #include <algorithm>
27 #endif
29 #include <osl/module.hxx>
30 #include <rtl/ustring.hxx>
31 #include <osl/file.hxx>
32 #include <sal/log.hxx>
34 #include "framework.hxx"
35 #include <fwkutil.hxx>
36 #include <memory>
38 using namespace osl;
41 namespace jfw
44 /** provides a bootstrap class which already knows the values from the
45 jvmfkwrc file.
47 const rtl::Bootstrap* Bootstrap()
49 static const rtl::Bootstrap* SINGLETON = []()
51 OUString sIni = getLibraryLocation() +
52 #ifdef MACOSX
53 // For some reason the jvmfwk3rc file is traditionally in
54 // LIBO_URE_ETC_FOLDER
55 "/../" LIBO_URE_ETC_FOLDER
56 #endif
57 SAL_CONFIGFILE("/jvmfwk3");
58 ::rtl::Bootstrap * bootstrap = new ::rtl::Bootstrap(sIni);
59 SAL_INFO("jfw.level2", "Using configuration file " << sIni);
60 return bootstrap;
61 }();
62 return SINGLETON;
65 osl::Mutex& FwkMutex()
67 static osl::Mutex SINGLETON;
68 return SINGLETON;
72 rtl::ByteSequence encodeBase16(const rtl::ByteSequence& rawData)
74 static const char EncodingTable[] =
75 {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
76 sal_Int32 lenRaw = rawData.getLength();
77 std::unique_ptr<char[]> pBuf(new char[lenRaw * 2]);
78 const sal_Int8* arRaw = rawData.getConstArray();
80 char* pCurBuf = pBuf.get();
81 for (int i = 0; i < lenRaw; i++)
83 unsigned char curChar = arRaw[i];
84 curChar >>= 4;
86 *pCurBuf = EncodingTable[curChar];
87 pCurBuf++;
89 curChar = arRaw[i];
90 curChar &= 0x0F;
92 *pCurBuf = EncodingTable[curChar];
93 pCurBuf++;
96 rtl::ByteSequence ret(reinterpret_cast<sal_Int8*>(pBuf.get()), lenRaw * 2);
97 return ret;
100 rtl::ByteSequence decodeBase16(const rtl::ByteSequence& data)
102 static const char decodingTable[] =
103 {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
104 sal_Int32 lenData = data.getLength();
105 sal_Int32 lenBuf = lenData / 2; //always divisible by two
106 std::unique_ptr<unsigned char[]> pBuf(new unsigned char[lenBuf]);
107 const sal_Int8* pData = data.getConstArray();
108 for (sal_Int32 i = 0; i < lenBuf; i++)
110 sal_Int8 curChar = *pData++;
111 //find the index of the first 4bits
112 // TODO What happens if text is not valid Hex characters?
113 unsigned char nibble = 0;
114 for (unsigned char j = 0; j < 16; j++)
116 if (curChar == decodingTable[j])
118 nibble = j;
119 break;
122 nibble <<= 4;
123 curChar = *pData++;
124 //find the index for the next 4bits
125 for (unsigned char j = 0; j < 16; j++)
127 if (curChar == decodingTable[j])
129 nibble |= j;
130 break;
133 pBuf[i] = nibble;
135 rtl::ByteSequence ret(reinterpret_cast<sal_Int8*>(pBuf.get()), lenBuf );
136 return ret;
139 OUString getDirFromFile(std::u16string_view usFilePath)
141 size_t index = usFilePath.rfind('/');
142 return OUString(usFilePath.substr(0, index));
145 OUString getLibraryLocation()
147 OUString libraryFileUrl;
149 if (!osl::Module::getUrlFromAddress(
150 reinterpret_cast< oslGenericFunction >(getLibraryLocation),
151 libraryFileUrl))
152 throw FrameworkException(JFW_E_ERROR,
153 "[Java framework] Error in function getLibraryLocation (fwkutil.cxx)."_ostr);
155 return getDirFromFile(libraryFileUrl);
158 jfw::FileStatus checkFileURL(const OUString & sURL)
160 jfw::FileStatus ret = jfw::FILE_OK;
161 DirectoryItem item;
162 File::RC rc_item = DirectoryItem::get(sURL, item);
163 if (File::E_None == rc_item)
165 osl::FileStatus status(osl_FileStatus_Mask_Validate);
167 File::RC rc_stat = item.getFileStatus(status);
168 if (File::E_None == rc_stat)
170 ret = FILE_OK;
172 else if (File::E_NOENT == rc_stat)
174 ret = FILE_DOES_NOT_EXIST;
176 else
178 ret = FILE_INVALID;
181 else if (File::E_NOENT == rc_item)
183 ret = FILE_DOES_NOT_EXIST;
185 else
187 ret = FILE_INVALID;
189 return ret;
194 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */