tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / store / source / store.cxx
blob24439a4e2a81ebc56240337df85c9ea1d57665cf
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 .
20 #include <store/store.h>
22 #include <sal/types.h>
23 #include <rtl/string.hxx>
24 #include <rtl/ref.hxx>
26 #include "object.hxx"
27 #include "lockbyte.hxx"
29 #include "storbase.hxx"
30 #include "storpage.hxx"
31 #include "stordir.hxx"
32 #include "storlckb.hxx"
34 using rtl::Reference;
36 namespace store
39 namespace {
41 /** Template helper class as type safe Reference to store_handle_type.
43 template<class store_handle_type>
44 class OStoreHandle : public rtl::Reference<store_handle_type>
46 public:
47 explicit OStoreHandle (store_handle_type * pHandle)
48 : rtl::Reference<store_handle_type> (pHandle)
51 static store_handle_type * SAL_CALL query (void * pHandle)
53 return store::query (
54 static_cast<OStoreObject*>(pHandle),
55 static_cast<store_handle_type*>(nullptr));
63 using namespace store;
65 storeError store_acquireHandle (
66 storeHandle Handle
67 ) noexcept
69 OStoreObject *pHandle = static_cast<OStoreObject*>(Handle);
70 if (!pHandle)
71 return store_E_InvalidHandle;
73 pHandle->acquire();
74 return store_E_None;
77 storeError store_releaseHandle (
78 storeHandle Handle
79 ) noexcept
81 OStoreObject *pHandle = static_cast<OStoreObject*>(Handle);
82 if (!pHandle)
83 return store_E_InvalidHandle;
85 pHandle->release();
86 return store_E_None;
89 storeError store_createMemoryFile (
90 sal_uInt16 nPageSize,
91 storeFileHandle *phFile
92 ) noexcept
94 if (!phFile)
95 return store_E_InvalidParameter;
96 *phFile = nullptr;
98 Reference<ILockBytes> xLockBytes;
100 storeError eErrCode = MemoryLockBytes_createInstance(xLockBytes);
101 if (eErrCode != store_E_None)
102 return eErrCode;
103 OSL_ASSERT(xLockBytes.is());
105 Reference<OStorePageManager> xManager (new OStorePageManager());
107 eErrCode = xManager->initialize (
108 &*xLockBytes, storeAccessMode::Create, nPageSize);
109 if (eErrCode != store_E_None)
110 return eErrCode;
112 xManager->acquire();
114 *phFile = xManager.get();
115 return store_E_None;
118 storeError store_openFile (
119 rtl_uString *pFilename,
120 storeAccessMode eAccessMode,
121 sal_uInt16 nPageSize,
122 storeFileHandle *phFile
123 ) noexcept
125 if (phFile)
126 *phFile = nullptr;
128 if (!(pFilename && phFile))
129 return store_E_InvalidParameter;
131 Reference<ILockBytes> xLockBytes;
133 storeError eErrCode = FileLockBytes_createInstance (xLockBytes, pFilename, eAccessMode);
134 if (eErrCode != store_E_None)
135 return eErrCode;
136 OSL_ASSERT(xLockBytes.is());
138 Reference<OStorePageManager> xManager (new OStorePageManager());
139 eErrCode = xManager->initialize (
140 &*xLockBytes, eAccessMode, nPageSize);
141 if (eErrCode != store_E_None)
142 return eErrCode;
144 xManager->acquire();
146 *phFile = xManager.get();
147 return store_E_None;
151 * store_closeFile.
153 storeError store_closeFile (
154 storeFileHandle Handle
155 ) noexcept
157 OStorePageManager *pManager =
158 OStoreHandle<OStorePageManager>::query (Handle);
159 if (!pManager)
160 return store_E_InvalidHandle;
162 storeError eErrCode = pManager->close();
163 pManager->release();
164 return eErrCode;
167 storeError store_flushFile (
168 storeFileHandle Handle
169 ) noexcept
171 OStoreHandle<OStorePageManager> xManager (
172 OStoreHandle<OStorePageManager>::query (Handle));
173 if (!xManager.is())
174 return store_E_InvalidHandle;
176 return xManager->flush();
179 storeError store_openDirectory (
180 storeFileHandle hFile,
181 rtl_uString const *pPath,
182 rtl_uString const *pName,
183 storeAccessMode eAccessMode,
184 storeDirectoryHandle *phDirectory
185 ) noexcept
187 storeError eErrCode = store_E_None;
188 if (phDirectory)
189 *phDirectory = nullptr;
191 OStoreHandle<OStorePageManager> xManager (
192 OStoreHandle<OStorePageManager>::query (hFile));
193 if (!xManager.is())
194 return store_E_InvalidHandle;
196 if (!(pPath && pName && phDirectory))
197 return store_E_InvalidParameter;
199 Reference<OStoreDirectory_Impl> xDirectory (new OStoreDirectory_Impl());
201 OString aPath (pPath->buffer, pPath->length, RTL_TEXTENCODING_UTF8);
202 OString aName (pName->buffer, pName->length, RTL_TEXTENCODING_UTF8);
204 eErrCode = xDirectory->create (&*xManager, aPath.pData, aName.pData, eAccessMode);
205 if (eErrCode != store_E_None)
206 return eErrCode;
208 xDirectory->acquire();
210 *phDirectory = xDirectory.get();
211 return store_E_None;
214 storeError store_findFirst (
215 storeDirectoryHandle Handle,
216 storeFindData *pFindData
217 ) noexcept
219 OStoreHandle<OStoreDirectory_Impl> xDirectory (
220 OStoreHandle<OStoreDirectory_Impl>::query (Handle));
221 if (!xDirectory.is())
222 return store_E_InvalidHandle;
224 if (!pFindData)
225 return store_E_InvalidParameter;
227 // Initialize FindData.
228 memset (pFindData, 0, sizeof (storeFindData));
230 // Find first.
231 pFindData->m_nReserved = sal_uInt32(~0);
232 return xDirectory->iterate (*pFindData);
235 storeError store_findNext (
236 storeDirectoryHandle Handle,
237 storeFindData *pFindData
238 ) noexcept
240 OStoreHandle<OStoreDirectory_Impl> xDirectory (
241 OStoreHandle<OStoreDirectory_Impl>::query (Handle));
242 if (!xDirectory.is())
243 return store_E_InvalidHandle;
245 if (!pFindData)
246 return store_E_InvalidParameter;
248 // Check FindData.
249 if (!pFindData->m_nReserved)
250 return store_E_NoMoreFiles;
252 // Find next.
253 pFindData->m_nReserved -= 1;
254 return xDirectory->iterate (*pFindData);
257 storeError store_openStream (
258 storeFileHandle hFile,
259 rtl_uString const *pPath,
260 rtl_uString const *pName,
261 storeAccessMode eAccessMode,
262 storeStreamHandle *phStream
263 ) noexcept
265 storeError eErrCode = store_E_None;
266 if (phStream)
267 *phStream = nullptr;
269 OStoreHandle<OStorePageManager> xManager (
270 OStoreHandle<OStorePageManager>::query (hFile));
271 if (!xManager.is())
272 return store_E_InvalidHandle;
274 if (!(pPath && pName && phStream))
275 return store_E_InvalidParameter;
277 Reference<OStoreLockBytes> xLockBytes (new OStoreLockBytes());
279 OString aPath (pPath->buffer, pPath->length, RTL_TEXTENCODING_UTF8);
280 OString aName (pName->buffer, pName->length, RTL_TEXTENCODING_UTF8);
282 eErrCode = xLockBytes->create (&*xManager, aPath.pData, aName.pData, eAccessMode);
283 if (eErrCode != store_E_None)
284 return eErrCode;
286 xLockBytes->acquire();
288 *phStream = xLockBytes.get();
289 return store_E_None;
293 * store_readStream.
295 storeError store_readStream (
296 storeStreamHandle Handle,
297 sal_uInt32 nOffset,
298 void *pBuffer,
299 sal_uInt32 nBytes,
300 sal_uInt32 *pnDone
301 ) noexcept
303 OStoreHandle<OStoreLockBytes> xLockBytes (
304 OStoreHandle<OStoreLockBytes>::query (Handle));
305 if (!xLockBytes.is())
306 return store_E_InvalidHandle;
308 if (!(pBuffer && pnDone))
309 return store_E_InvalidParameter;
311 return xLockBytes->readAt (nOffset, pBuffer, nBytes, *pnDone);
314 storeError store_writeStream (
315 storeStreamHandle Handle,
316 sal_uInt32 nOffset,
317 const void *pBuffer,
318 sal_uInt32 nBytes,
319 sal_uInt32 *pnDone
320 ) noexcept
322 OStoreHandle<OStoreLockBytes> xLockBytes (
323 OStoreHandle<OStoreLockBytes>::query (Handle));
324 if (!xLockBytes.is())
325 return store_E_InvalidHandle;
327 if (!(pBuffer && pnDone))
328 return store_E_InvalidParameter;
330 return xLockBytes->writeAt (nOffset, pBuffer, nBytes, *pnDone);
333 storeError store_remove (
334 storeFileHandle Handle,
335 rtl_uString const *pPath,
336 rtl_uString const *pName
337 ) noexcept
339 storeError eErrCode = store_E_None;
341 OStoreHandle<OStorePageManager> xManager (
342 OStoreHandle<OStorePageManager>::query (Handle));
343 if (!xManager.is())
344 return store_E_InvalidHandle;
346 if (!(pPath && pName))
347 return store_E_InvalidParameter;
349 // Setup page key.
350 OString aPath (pPath->buffer, pPath->length, RTL_TEXTENCODING_UTF8);
351 OString aName (pName->buffer, pName->length, RTL_TEXTENCODING_UTF8);
352 OStorePageKey aKey;
354 eErrCode = OStorePageManager::namei (aPath.pData, aName.pData, aKey);
355 if (eErrCode != store_E_None)
356 return eErrCode;
358 // Remove.
359 return xManager->remove (aKey);
362 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */