cid#1636693 COPY_INSTEAD_OF_MOVE
[LibreOffice.git] / registry / source / regkey.cxx
blob450df235ce2130dcccf1f51f2041425d82ec456d
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 "regkey.hxx"
23 #include <osl/diagnose.h>
24 #include "regimpl.hxx"
25 #include "keyimpl.hxx"
27 void REGISTRY_CALLTYPE acquireKey(RegKeyHandle hKey)
29 ORegKey* pKey = static_cast< ORegKey* >(hKey);
30 if (pKey != nullptr)
32 ORegistry* pReg = pKey->getRegistry();
33 (void) pReg->acquireKey(pKey);
37 void REGISTRY_CALLTYPE releaseKey(RegKeyHandle hKey)
39 ORegKey* pKey = static_cast< ORegKey* >(hKey);
40 if (pKey != nullptr)
42 ORegistry* pReg = pKey->getRegistry();
43 (void) pReg->releaseKey(pKey);
47 sal_Bool REGISTRY_CALLTYPE isKeyReadOnly(RegKeyHandle hKey)
49 ORegKey* pKey = static_cast< ORegKey* >(hKey);
50 return pKey != nullptr && pKey->isReadOnly();
53 RegError REGISTRY_CALLTYPE getKeyName(RegKeyHandle hKey, rtl_uString** pKeyName)
55 ORegKey* pKey = static_cast< ORegKey* >(hKey);
56 if (pKey)
58 rtl_uString_assign( pKeyName, pKey->getName().pData );
59 return RegError::NO_ERROR;
60 } else
62 rtl_uString_new(pKeyName);
63 return RegError::INVALID_KEY;
67 RegError REGISTRY_CALLTYPE createKey(RegKeyHandle hKey,
68 rtl_uString* keyName,
69 RegKeyHandle* phNewKey)
71 *phNewKey = nullptr;
73 ORegKey* pKey = static_cast< ORegKey* >(hKey);
74 if (!pKey)
75 return RegError::INVALID_KEY;
77 if (pKey->isDeleted())
78 return RegError::INVALID_KEY;
80 if (pKey->isReadOnly())
81 return RegError::REGISTRY_READONLY;
83 return pKey->createKey(OUString::unacquired(&keyName), phNewKey);
86 RegError REGISTRY_CALLTYPE openKey(RegKeyHandle hKey,
87 rtl_uString* keyName,
88 RegKeyHandle* phOpenKey)
90 *phOpenKey = nullptr;
92 ORegKey* pKey = static_cast< ORegKey* >(hKey);
93 if (!pKey)
94 return RegError::INVALID_KEY;
96 if (pKey->isDeleted())
97 return RegError::INVALID_KEY;
99 return pKey->openKey(OUString::unacquired(&keyName), phOpenKey);
102 RegError REGISTRY_CALLTYPE openSubKeys(RegKeyHandle hKey,
103 rtl_uString* keyName,
104 RegKeyHandle** pphSubKeys,
105 sal_uInt32* pnSubKeys)
107 *pphSubKeys = nullptr;
108 *pnSubKeys = 0;
110 ORegKey* pKey = static_cast< ORegKey* >(hKey);
111 if (!pKey)
112 return RegError::INVALID_KEY;
114 if (pKey->isDeleted())
115 return RegError::INVALID_KEY;
117 return pKey->openSubKeys(OUString::unacquired(&keyName), pphSubKeys, pnSubKeys);
120 RegError REGISTRY_CALLTYPE closeSubKeys(RegKeyHandle* phSubKeys,
121 sal_uInt32 nSubKeys)
123 if (phSubKeys == nullptr || nSubKeys == 0)
124 return RegError::INVALID_KEY;
126 ORegistry* pReg = static_cast<ORegKey*>(phSubKeys[0])->getRegistry();
127 for (sal_uInt32 i = 0; i < nSubKeys; i++)
129 (void) pReg->closeKey(phSubKeys[i]);
131 std::free(phSubKeys);
133 return RegError::NO_ERROR;
136 RegError REGISTRY_CALLTYPE deleteKey(RegKeyHandle hKey,
137 rtl_uString* keyName)
139 ORegKey* pKey = static_cast< ORegKey* >(hKey);
140 if (!pKey)
141 return RegError::INVALID_KEY;
143 if (pKey->isDeleted())
144 return RegError::INVALID_KEY;
146 if (pKey->isReadOnly())
147 return RegError::REGISTRY_READONLY;
149 return pKey->deleteKey(OUString::unacquired(&keyName));
152 RegError REGISTRY_CALLTYPE closeKey(RegKeyHandle hKey)
154 ORegKey* pKey = static_cast< ORegKey* >(hKey);
155 if (!pKey)
156 return RegError::INVALID_KEY;
158 return pKey->closeKey(hKey);
161 RegError REGISTRY_CALLTYPE setValue(RegKeyHandle hKey,
162 rtl_uString* keyName,
163 RegValueType valueType,
164 RegValue pData,
165 sal_uInt32 valueSize)
167 ORegKey* pKey = static_cast< ORegKey* >(hKey);
168 if (!pKey)
169 return RegError::INVALID_KEY;
171 if (pKey->isDeleted())
172 return RegError::INVALID_KEY;
174 if (pKey->isReadOnly())
175 return RegError::REGISTRY_READONLY;
177 OUString valueName(u"value"_ustr);
178 if (keyName->length)
180 ORegKey* pSubKey = nullptr;
181 RegError _ret1 = pKey->openKey(OUString::unacquired(&keyName), reinterpret_cast<RegKeyHandle*>(&pSubKey));
182 if (_ret1 != RegError::NO_ERROR)
183 return _ret1;
185 _ret1 = pSubKey->setValue(valueName, valueType, pData, valueSize);
186 if (_ret1 != RegError::NO_ERROR)
188 RegError _ret2 = pKey->closeKey(pSubKey);
189 if (_ret2 != RegError::NO_ERROR)
190 return _ret2;
191 else
192 return _ret1;
195 return pKey->closeKey(pSubKey);
198 return pKey->setValue(valueName, valueType, pData, valueSize);
201 RegError REGISTRY_CALLTYPE setLongListValue(RegKeyHandle hKey,
202 rtl_uString* keyName,
203 sal_Int32 const * pValueList,
204 sal_uInt32 len)
206 ORegKey* pKey = static_cast< ORegKey* >(hKey);
207 if (!pKey)
208 return RegError::INVALID_KEY;
210 if (pKey->isDeleted())
211 return RegError::INVALID_KEY;
213 if (pKey->isReadOnly())
214 return RegError::REGISTRY_READONLY;
216 OUString valueName(u"value"_ustr);
217 if (keyName->length)
219 ORegKey* pSubKey = nullptr;
220 RegError _ret1 = pKey->openKey(OUString::unacquired(&keyName), reinterpret_cast<RegKeyHandle*>(&pSubKey));
221 if (_ret1 != RegError::NO_ERROR)
222 return _ret1;
224 _ret1 = pSubKey->setLongListValue(valueName, pValueList, len);
225 if (_ret1 != RegError::NO_ERROR)
227 RegError _ret2 = pKey->closeKey(pSubKey);
228 if (_ret2 != RegError::NO_ERROR)
229 return _ret2;
230 else
231 return _ret1;
234 return pKey->closeKey(pSubKey);
237 return pKey->setLongListValue(valueName, pValueList, len);
240 RegError REGISTRY_CALLTYPE setStringListValue(RegKeyHandle hKey,
241 rtl_uString* keyName,
242 char** pValueList,
243 sal_uInt32 len)
245 ORegKey* pKey = static_cast< ORegKey* >(hKey);
246 if (!pKey)
247 return RegError::INVALID_KEY;
249 if (pKey->isDeleted())
250 return RegError::INVALID_KEY;
252 if (pKey->isReadOnly())
253 return RegError::REGISTRY_READONLY;
255 OUString valueName(u"value"_ustr);
256 if (keyName->length)
258 ORegKey* pSubKey = nullptr;
259 RegError _ret1 = pKey->openKey(OUString::unacquired(&keyName), reinterpret_cast<RegKeyHandle*>(&pSubKey));
260 if (_ret1 != RegError::NO_ERROR)
261 return _ret1;
263 _ret1 = pSubKey->setStringListValue(valueName, pValueList, len);
264 if (_ret1 != RegError::NO_ERROR)
266 RegError _ret2 = pKey->closeKey(pSubKey);
267 if (_ret2 != RegError::NO_ERROR)
268 return _ret2;
269 else
270 return _ret1;
273 return pKey->closeKey(pSubKey);
276 return pKey->setStringListValue(valueName, pValueList, len);
279 RegError REGISTRY_CALLTYPE setUnicodeListValue(RegKeyHandle hKey,
280 rtl_uString* keyName,
281 sal_Unicode** pValueList,
282 sal_uInt32 len)
284 ORegKey* pKey = static_cast< ORegKey* >(hKey);
285 if (!pKey)
286 return RegError::INVALID_KEY;
288 if (pKey->isDeleted())
289 return RegError::INVALID_KEY;
291 if (pKey->isReadOnly())
292 return RegError::REGISTRY_READONLY;
294 OUString valueName(u"value"_ustr);
295 if (keyName->length)
297 ORegKey* pSubKey = nullptr;
298 RegError _ret1 = pKey->openKey(OUString::unacquired(&keyName), reinterpret_cast<RegKeyHandle*>(&pSubKey));
299 if (_ret1 != RegError::NO_ERROR)
300 return _ret1;
302 _ret1 = pSubKey->setUnicodeListValue(valueName, pValueList, len);
303 if (_ret1 != RegError::NO_ERROR)
305 RegError _ret2 = pKey->closeKey(pSubKey);
306 if (_ret2 != RegError::NO_ERROR)
307 return _ret2;
308 else
309 return _ret1;
312 return pKey->closeKey(pSubKey);
315 return pKey->setUnicodeListValue(valueName, pValueList, len);
318 RegError REGISTRY_CALLTYPE getValueInfo(RegKeyHandle hKey,
319 rtl_uString* keyName,
320 RegValueType* pValueType,
321 sal_uInt32* pValueSize)
323 *pValueType = RegValueType::NOT_DEFINED;
324 *pValueSize = 0;
326 ORegKey* pKey = static_cast< ORegKey* >(hKey);
327 if (!pKey)
328 return RegError::INVALID_KEY;
330 if (pKey->isDeleted())
331 return RegError::INVALID_KEY;
333 RegValueType valueType;
334 sal_uInt32 valueSize;
336 OUString valueName(u"value"_ustr);
337 if (keyName->length)
339 ORegKey* pSubKey = nullptr;
340 RegError _ret = pKey->openKey(OUString::unacquired(&keyName), reinterpret_cast<RegKeyHandle*>(&pSubKey));
341 if (_ret != RegError::NO_ERROR)
342 return _ret;
344 if (pSubKey->getValueInfo(valueName, &valueType, &valueSize) != RegError::NO_ERROR)
346 (void) pKey->releaseKey(pSubKey);
347 return RegError::INVALID_VALUE;
350 *pValueType = valueType;
351 *pValueSize = valueSize;
353 return pKey->releaseKey(pSubKey);
357 if (pKey->getValueInfo(valueName, &valueType, &valueSize) != RegError::NO_ERROR)
359 return RegError::INVALID_VALUE;
362 *pValueType = valueType;
363 *pValueSize = valueSize;
365 return RegError::NO_ERROR;
368 RegError REGISTRY_CALLTYPE getValue(RegKeyHandle hKey,
369 rtl_uString* keyName,
370 RegValue pValue)
372 ORegKey* pKey = static_cast< ORegKey* >(hKey);
373 if (!pKey)
374 return RegError::INVALID_KEY;
376 if (pKey->isDeleted())
377 return RegError::INVALID_KEY;
379 OUString valueName(u"value"_ustr);
380 if (keyName->length)
382 ORegKey* pSubKey = nullptr;
383 RegError _ret1 = pKey->openKey(OUString::unacquired(&keyName), reinterpret_cast<RegKeyHandle*>(&pSubKey));
384 if (_ret1 != RegError::NO_ERROR)
385 return _ret1;
387 _ret1 = pSubKey->getValue(valueName, pValue);
388 if (_ret1 != RegError::NO_ERROR)
390 (void) pKey->releaseKey(pSubKey);
391 return _ret1;
394 return pKey->releaseKey(pSubKey);
397 return pKey->getValue(valueName, pValue);
400 RegError REGISTRY_CALLTYPE getLongListValue(RegKeyHandle hKey,
401 rtl_uString* keyName,
402 sal_Int32** pValueList,
403 sal_uInt32* pLen)
405 assert((pValueList != nullptr) && (pLen != nullptr) && "registry::getLongListValue(): invalid parameter");
406 *pValueList = nullptr;
407 *pLen = 0;
409 ORegKey* pKey = static_cast< ORegKey* >(hKey);
410 if (!pKey)
411 return RegError::INVALID_KEY;
413 if (pKey->isDeleted())
414 return RegError::INVALID_KEY;
416 OUString valueName(u"value"_ustr);
417 if (keyName->length)
419 ORegKey* pSubKey = nullptr;
420 RegError _ret1 = pKey->openKey(OUString::unacquired(&keyName), reinterpret_cast<RegKeyHandle*>(&pSubKey));
421 if (_ret1 != RegError::NO_ERROR)
422 return _ret1;
424 _ret1 = pSubKey->getLongListValue(valueName, pValueList, pLen);
425 if (_ret1 != RegError::NO_ERROR)
427 (void) pKey->releaseKey(pSubKey);
428 return _ret1;
431 return pKey->releaseKey(pSubKey);
434 return pKey->getLongListValue(valueName, pValueList, pLen);
437 RegError REGISTRY_CALLTYPE getStringListValue(RegKeyHandle hKey,
438 rtl_uString* keyName,
439 char*** pValueList,
440 sal_uInt32* pLen)
442 assert(pValueList != nullptr && pLen != nullptr && "registry::getStringListValue(): invalid parameter");
443 *pValueList = nullptr;
444 *pLen = 0;
446 ORegKey* pKey = static_cast< ORegKey* >(hKey);
447 if (!pKey)
448 return RegError::INVALID_KEY;
450 if (pKey->isDeleted())
451 return RegError::INVALID_KEY;
453 OUString valueName(u"value"_ustr);
454 if (keyName->length)
456 ORegKey* pSubKey = nullptr;
457 RegError _ret1 = pKey->openKey(OUString::unacquired(&keyName), reinterpret_cast<RegKeyHandle*>(&pSubKey));
458 if (_ret1 != RegError::NO_ERROR)
459 return _ret1;
461 _ret1 = pSubKey->getStringListValue(valueName, pValueList, pLen);
462 if (_ret1 != RegError::NO_ERROR)
464 (void) pKey->releaseKey(pSubKey);
465 return _ret1;
468 return pKey->releaseKey(pSubKey);
471 return pKey->getStringListValue(valueName, pValueList, pLen);
474 RegError REGISTRY_CALLTYPE getUnicodeListValue(RegKeyHandle hKey,
475 rtl_uString* keyName,
476 sal_Unicode*** pValueList,
477 sal_uInt32* pLen)
479 assert((pValueList != nullptr) && (pLen != nullptr) && "registry::getUnicodeListValue(): invalid parameter");
480 *pValueList = nullptr;
481 *pLen = 0;
483 ORegKey* pKey = static_cast< ORegKey* >(hKey);
484 if (!pKey)
485 return RegError::INVALID_KEY;
487 if (pKey->isDeleted())
488 return RegError::INVALID_KEY;
490 OUString valueName(u"value"_ustr);
491 if (keyName->length)
493 ORegKey* pSubKey = nullptr;
494 RegError _ret1 = pKey->openKey(OUString::unacquired(&keyName), reinterpret_cast<RegKeyHandle*>(&pSubKey));
495 if (_ret1 != RegError::NO_ERROR)
496 return _ret1;
498 _ret1 = pSubKey->getUnicodeListValue(valueName, pValueList, pLen);
499 if (_ret1 != RegError::NO_ERROR)
501 (void) pKey->releaseKey(pSubKey);
502 return _ret1;
505 return pKey->releaseKey(pSubKey);
508 return pKey->getUnicodeListValue(valueName, pValueList, pLen);
511 RegError REGISTRY_CALLTYPE freeValueList(RegValueType valueType,
512 RegValue pValueList,
513 sal_uInt32 len)
515 switch (valueType)
517 case RegValueType::LONGLIST:
519 std::free(pValueList);
521 break;
522 case RegValueType::STRINGLIST:
524 char** pVList = static_cast<char**>(pValueList);
525 for (sal_uInt32 i=0; i < len; i++)
527 std::free(pVList[i]);
530 std::free(pVList);
532 break;
533 case RegValueType::UNICODELIST:
535 sal_Unicode** pVList = static_cast<sal_Unicode**>(pValueList);
536 for (sal_uInt32 i=0; i < len; i++)
538 std::free(pVList[i]);
541 std::free(pVList);
543 break;
544 default:
545 return RegError::INVALID_VALUE;
548 pValueList = nullptr;
549 return RegError::NO_ERROR;
552 RegError REGISTRY_CALLTYPE getResolvedKeyName(RegKeyHandle hKey,
553 rtl_uString* keyName,
554 SAL_UNUSED_PARAMETER sal_Bool,
555 rtl_uString** pResolvedName)
557 ORegKey* pKey = static_cast< ORegKey* >(hKey);
558 if (!pKey)
559 return RegError::INVALID_KEY;
561 if (pKey->isDeleted())
562 return RegError::INVALID_KEY;
564 OUString resolvedName;
565 RegError _ret = pKey->getResolvedKeyName(OUString::unacquired(&keyName), resolvedName);
566 if (_ret == RegError::NO_ERROR)
567 rtl_uString_assign(pResolvedName, resolvedName.pData);
568 return _ret;
571 RegError REGISTRY_CALLTYPE getKeyNames(RegKeyHandle hKey,
572 rtl_uString* keyName,
573 rtl_uString*** pSubKeyNames,
574 sal_uInt32* pnSubKeys)
576 ORegKey* pKey = static_cast< ORegKey* >(hKey);
577 if (!pKey)
578 return RegError::INVALID_KEY;
580 if (pKey->isDeleted())
581 return RegError::INVALID_KEY;
583 return pKey->getKeyNames(OUString::unacquired(&keyName), pSubKeyNames, pnSubKeys);
586 RegError REGISTRY_CALLTYPE freeKeyNames(rtl_uString** pKeyNames,
587 sal_uInt32 nKeys)
589 for (sal_uInt32 i=0; i < nKeys; i++)
591 rtl_uString_release(pKeyNames[i]);
594 std::free(pKeyNames);
596 return RegError::NO_ERROR;
599 RegError REGISTRY_CALLTYPE reg_openKey(RegKeyHandle hKey,
600 rtl_uString* keyName,
601 RegKeyHandle* phOpenKey)
603 if (!hKey)
604 return RegError::INVALID_KEY;
606 return openKey(hKey, keyName, phOpenKey);
609 RegError REGISTRY_CALLTYPE reg_closeKey(RegKeyHandle hKey)
611 if (!hKey)
612 return RegError::INVALID_KEY;
614 return closeKey(hKey);
618 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */