LibreOffice Module jvmfwk (master) 1
fwkutil.cxx
Go to the documentation of this file.
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#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
28
29#include <osl/module.hxx>
30#include <rtl/ustring.hxx>
31#include <osl/file.hxx>
32#include <sal/log.hxx>
33
34#include "framework.hxx"
35#include <fwkutil.hxx>
36#include <memory>
37
38using namespace osl;
39
40
41namespace jfw
42{
43
48{
49 static const rtl::Bootstrap* SINGLETON = []()
50 {
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");
59 SAL_INFO("jfw.level2", "Using configuration file " << sIni);
60 return bootstrap;
61 }();
62 return SINGLETON;
63};
64
65osl::Mutex& FwkMutex()
66{
67 static osl::Mutex SINGLETON;
68 return SINGLETON;
69}
70
71
72rtl::ByteSequence encodeBase16(const rtl::ByteSequence& rawData)
73{
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();
79
80 char* pCurBuf = pBuf.get();
81 for (int i = 0; i < lenRaw; i++)
82 {
83 unsigned char curChar = arRaw[i];
84 curChar >>= 4;
85
86 *pCurBuf = EncodingTable[curChar];
87 pCurBuf++;
88
89 curChar = arRaw[i];
90 curChar &= 0x0F;
91
92 *pCurBuf = EncodingTable[curChar];
93 pCurBuf++;
94 }
95
96 rtl::ByteSequence ret(reinterpret_cast<sal_Int8*>(pBuf.get()), lenRaw * 2);
97 return ret;
98}
99
100rtl::ByteSequence decodeBase16(const rtl::ByteSequence& data)
101{
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++)
109 {
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++)
115 {
116 if (curChar == decodingTable[j])
117 {
118 nibble = j;
119 break;
120 }
121 }
122 nibble <<= 4;
123 curChar = *pData++;
124 //find the index for the next 4bits
125 for (unsigned char j = 0; j < 16; j++)
126 {
127 if (curChar == decodingTable[j])
128 {
129 nibble |= j;
130 break;
131 }
132 }
133 pBuf[i] = nibble;
134 }
135 rtl::ByteSequence ret(reinterpret_cast<sal_Int8*>(pBuf.get()), lenBuf );
136 return ret;
137}
138
139OUString getDirFromFile(std::u16string_view usFilePath)
140{
141 size_t index = usFilePath.rfind('/');
142 return OUString(usFilePath.substr(0, index));
143}
144
146{
147 OUString libraryFileUrl;
148
149 if (!osl::Module::getUrlFromAddress(
150 reinterpret_cast< oslGenericFunction >(getLibraryLocation),
151 libraryFileUrl))
153 "[Java framework] Error in function getLibraryLocation (fwkutil.cxx).");
154
155 return getDirFromFile(libraryFileUrl);
156}
157
158jfw::FileStatus checkFileURL(const OUString & sURL)
159{
161 DirectoryItem item;
162 File::RC rc_item = DirectoryItem::get(sURL, item);
163 if (File::E_None == rc_item)
164 {
165 osl::FileStatus status(osl_FileStatus_Mask_Validate);
166
167 File::RC rc_stat = item.getFileStatus(status);
168 if (File::E_None == rc_stat)
169 {
170 ret = FILE_OK;
171 }
172 else if (File::E_NOENT == rc_stat)
173 {
175 }
176 else
177 {
178 ret = FILE_INVALID;
179 }
180 }
181 else if (File::E_NOENT == rc_item)
182 {
184 }
185 else
186 {
187 ret = FILE_INVALID;
188 }
189 return ret;
190}
191
192}
193
194/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define SAL_CONFIGFILE(name)
#define SAL_INFO(area, stream)
std::unique_ptr< sal_Int32[]> pData
Reference< XComponentContext > SAL_CALL bootstrap()
int i
Definition: elements.cxx:47
const rtl::Bootstrap * Bootstrap()
provides a bootstrap class which already knows the values from the jvmfkwrc file.
Definition: fwkutil.cxx:47
osl::Mutex & FwkMutex()
Definition: fwkutil.cxx:65
rtl::ByteSequence encodeBase16(const rtl::ByteSequence &rawData)
Definition: fwkutil.cxx:72
OUString getDirFromFile(std::u16string_view usFilePath)
Definition: fwkutil.cxx:139
rtl::ByteSequence decodeBase16(const rtl::ByteSequence &data)
Definition: fwkutil.cxx:100
jfw::FileStatus checkFileURL(const OUString &sURL)
checks if the URL is a file.
Definition: fwkutil.cxx:158
OUString getLibraryLocation()
Returns the file URL of the directory where the framework library (this library) resides.
Definition: fwkutil.cxx:145
FileStatus
Definition: fwkutil.hxx:52
@ FILE_OK
Definition: fwkutil.hxx:53
@ FILE_DOES_NOT_EXIST
Definition: fwkutil.hxx:54
@ FILE_INVALID
Definition: fwkutil.hxx:55
index
signed char sal_Int8