Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / registry / source / registry.cxx
blob61b2c7ba14451252ae80750adbe097ce84189eae
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 <regapi.hxx>
22 #include <registry/registry.hxx>
24 #include <osl/process.h>
26 #include "keyimpl.hxx"
27 #include "regimpl.hxx"
28 #include "regkey.hxx"
30 #if defined(_WIN32)
31 #include <io.h>
32 #endif
34 #include <string.h>
35 #if defined(UNX)
36 #include <stdlib.h>
37 #include <unistd.h>
38 #endif
40 extern "C" {
43 // acquire
45 static void REGISTRY_CALLTYPE acquire(RegHandle hReg)
47 ORegistry* pReg = static_cast<ORegistry*>(hReg);
49 if (pReg != nullptr)
50 pReg->acquire();
54 // release
56 static void REGISTRY_CALLTYPE release(RegHandle hReg)
58 ORegistry* pReg = static_cast<ORegistry*>(hReg);
60 if (pReg)
62 if (pReg->release() == 0)
64 delete pReg;
65 hReg = nullptr;
71 // getName
73 static RegError REGISTRY_CALLTYPE getName(RegHandle hReg, rtl_uString** pName)
75 if (hReg)
77 ORegistry* pReg = static_cast<ORegistry*>(hReg);
78 if ( pReg->isOpen() )
80 rtl_uString_assign(pName, pReg->getName().pData);
81 return RegError::NO_ERROR;
82 } else
84 rtl_uString_new(pName);
85 return RegError::REGISTRY_NOT_OPEN;
89 rtl_uString_new(pName);
90 return RegError::INVALID_REGISTRY;
94 // isReadOnly
96 static sal_Bool REGISTRY_CALLTYPE isReadOnly(RegHandle hReg)
98 if (hReg)
99 return static_cast<ORegistry*>(hReg)->isReadOnly();
100 else
101 return false;
105 // createRegistry
107 static RegError REGISTRY_CALLTYPE createRegistry(rtl_uString* registryName,
108 RegHandle* phRegistry)
110 RegError ret;
112 ORegistry* pReg = new ORegistry();
113 if ((ret = pReg->initRegistry(registryName, RegAccessMode::READWRITE, true/*bCreate*/)) != RegError::NO_ERROR)
115 delete pReg;
116 *phRegistry = nullptr;
117 return ret;
120 *phRegistry = pReg;
122 return RegError::NO_ERROR;
126 // openRootKey
128 static RegError REGISTRY_CALLTYPE openRootKey(RegHandle hReg,
129 RegKeyHandle* phRootKey)
131 ORegistry* pReg;
133 if (hReg)
135 pReg = static_cast<ORegistry*>(hReg);
136 if (!pReg->isOpen())
137 return RegError::REGISTRY_NOT_OPEN;
138 } else
140 phRootKey = nullptr;
141 return RegError::INVALID_REGISTRY;
144 *phRootKey = pReg->getRootKey();
146 return RegError::NO_ERROR;
150 // openRegistry
152 static RegError REGISTRY_CALLTYPE openRegistry(rtl_uString* registryName,
153 RegHandle* phRegistry,
154 RegAccessMode accessMode)
156 RegError _ret;
158 ORegistry* pReg = new ORegistry();
159 if ((_ret = pReg->initRegistry(registryName, accessMode)) != RegError::NO_ERROR)
161 *phRegistry = nullptr;
162 delete pReg;
163 return _ret;
167 *phRegistry = pReg;
169 return RegError::NO_ERROR;
173 // closeRegistry
175 static RegError REGISTRY_CALLTYPE closeRegistry(RegHandle hReg)
177 ORegistry *pReg;
179 if (hReg)
181 pReg = static_cast<ORegistry*>(hReg);
182 if (!pReg->isOpen())
183 return RegError::REGISTRY_NOT_OPEN;
185 RegError ret = RegError::NO_ERROR;
186 if (pReg->release() == 0)
188 delete pReg;
189 hReg = nullptr;
191 else
192 ret = pReg->closeRegistry();
194 return ret;
195 } else
197 return RegError::INVALID_REGISTRY;
202 // destroyRegistry
204 static RegError REGISTRY_CALLTYPE destroyRegistry(RegHandle hReg,
205 rtl_uString* registryName)
207 ORegistry *pReg;
209 if (hReg)
211 pReg = static_cast<ORegistry*>(hReg);
212 if (!pReg->isOpen())
213 return RegError::INVALID_REGISTRY;
215 RegError ret = pReg->destroyRegistry(registryName);
216 if (ret == RegError::NO_ERROR)
218 if (!registryName->length)
220 delete pReg;
221 hReg = nullptr;
224 return ret;
225 } else
227 return RegError::INVALID_REGISTRY;
232 // mergeKey
234 static RegError REGISTRY_CALLTYPE mergeKey(RegHandle hReg,
235 RegKeyHandle hKey,
236 rtl_uString* keyName,
237 rtl_uString* regFileName,
238 sal_Bool bWarnings,
239 sal_Bool bReport)
241 ORegistry* pReg = static_cast< ORegistry* >(hReg);
242 if (!pReg)
243 return RegError::INVALID_REGISTRY;
244 if (!pReg->isOpen())
245 return RegError::REGISTRY_NOT_OPEN;
247 ORegKey* pKey = static_cast< ORegKey* >(hKey);
248 if (!pKey)
249 return RegError::INVALID_KEY;
250 if (pKey->getRegistry() != pReg)
251 return RegError::INVALID_KEY;
252 if (pKey->isDeleted())
253 return RegError::INVALID_KEY;
254 if (pKey->isReadOnly())
255 return RegError::REGISTRY_READONLY;
257 if (keyName->length)
259 ORegKey* pNewKey = nullptr;
260 RegError _ret = pKey->createKey(keyName, reinterpret_cast<RegKeyHandle*>(&pNewKey));
261 if (_ret != RegError::NO_ERROR)
262 return _ret;
264 _ret = pReg->loadKey(pNewKey, regFileName, bWarnings, bReport);
265 if (_ret != RegError::NO_ERROR && (_ret != RegError::MERGE_CONFLICT || bWarnings))
267 if (pNewKey != pKey)
268 (void) pKey->closeKey(pNewKey);
269 else
270 (void) pKey->releaseKey(pNewKey);
271 return _ret;
274 return (pNewKey != pKey) ? pKey->closeKey(pNewKey) : pKey->releaseKey(pNewKey);
277 return pReg->loadKey(pKey, regFileName, bWarnings, bReport);
281 // dumpRegistry
283 static RegError REGISTRY_CALLTYPE dumpRegistry(RegHandle hReg,
284 RegKeyHandle hKey)
286 ORegistry* pReg = static_cast< ORegistry* >(hReg);
287 if (!pReg)
288 return RegError::INVALID_REGISTRY;
289 if (!pReg->isOpen())
290 return RegError::REGISTRY_NOT_OPEN;
292 ORegKey* pKey = static_cast< ORegKey* >(hKey);
293 if (!pKey)
294 return RegError::INVALID_KEY;
295 if (pKey->getRegistry() != pReg)
296 return RegError::INVALID_KEY;
297 if (pKey->isDeleted())
298 return RegError::INVALID_KEY;
300 return pReg->dumpRegistry(hKey);
304 // initRegistry_Api
306 Registry_Api* REGISTRY_CALLTYPE initRegistry_Api()
308 static Registry_Api aApi= {&acquire,
309 &release,
310 &isReadOnly,
311 &openRootKey,
312 &getName,
313 &createRegistry,
314 &openRegistry,
315 &closeRegistry,
316 &destroyRegistry,
317 &mergeKey,
318 &acquireKey,
319 &releaseKey,
320 &isKeyReadOnly,
321 &getKeyName,
322 &createKey,
323 &openKey,
324 &openSubKeys,
325 &closeSubKeys,
326 &deleteKey,
327 &closeKey,
328 &setValue,
329 &setLongListValue,
330 &setStringListValue,
331 &setUnicodeListValue,
332 &getValueInfo,
333 &getValue,
334 &getLongListValue,
335 &getStringListValue,
336 &getUnicodeListValue,
337 &freeValueList,
338 &getResolvedKeyName,
339 &getKeyNames,
340 &freeKeyNames};
342 return (&aApi);
348 // reg_openRootKey
350 RegError REGISTRY_CALLTYPE reg_openRootKey(RegHandle hRegistry,
351 RegKeyHandle* phRootKey)
353 return openRootKey(hRegistry, phRootKey);
357 // reg_openRegistry
359 RegError REGISTRY_CALLTYPE reg_openRegistry(rtl_uString* registryName,
360 RegHandle* phRegistry)
362 RegError _ret;
364 ORegistry* pReg = new ORegistry();
365 if ((_ret = pReg->initRegistry(registryName, RegAccessMode::READONLY)) != RegError::NO_ERROR)
367 delete pReg;
368 *phRegistry = nullptr;
369 return _ret;
372 *phRegistry = pReg;
374 return RegError::NO_ERROR;
378 // reg_closeRegistry
380 RegError REGISTRY_CALLTYPE reg_closeRegistry(RegHandle hRegistry)
382 if (hRegistry)
384 ORegistry* pReg = static_cast<ORegistry*>(hRegistry);
385 delete pReg;
386 return RegError::NO_ERROR;
387 } else
389 return RegError::REGISTRY_NOT_OPEN;
394 // reg_dumpRegistry
396 RegError REGISTRY_CALLTYPE reg_dumpRegistry(RegKeyHandle hKey)
398 ORegKey *pKey;
400 if (hKey)
401 pKey = static_cast<ORegKey*>(hKey);
402 else
403 return RegError::INVALID_KEY;
405 return dumpRegistry(pKey->getRegistry(), hKey);
409 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */