tdf#149186: Table of contents editor not showing buttons in Dutch UI
[LibreOffice.git] / store / source / store.cxx
blobdfd41571e2dd6779cf3a3c8e3db08f59a1f93ad9
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*>(0));
63 using namespace store;
65 /*========================================================================
67 * storeHandle implementation.
69 *======================================================================*/
71 * store_acquireHandle.
73 storeError store_acquireHandle (
74 storeHandle Handle
75 ) SAL_THROW_EXTERN_C()
77 OStoreObject *pHandle = static_cast<OStoreObject*>(Handle);
78 if (!pHandle)
79 return store_E_InvalidHandle;
81 pHandle->acquire();
82 return store_E_None;
86 * store_releaseHandle.
88 storeError store_releaseHandle (
89 storeHandle Handle
90 ) SAL_THROW_EXTERN_C()
92 OStoreObject *pHandle = static_cast<OStoreObject*>(Handle);
93 if (!pHandle)
94 return store_E_InvalidHandle;
96 pHandle->release();
97 return store_E_None;
100 /*========================================================================
102 * storeFileHandle implementation.
104 *======================================================================*/
106 * store_createMemoryFile.
108 storeError store_createMemoryFile (
109 sal_uInt16 nPageSize,
110 storeFileHandle *phFile
111 ) SAL_THROW_EXTERN_C()
113 if (!phFile)
114 return store_E_InvalidParameter;
115 *phFile = nullptr;
117 Reference<ILockBytes> xLockBytes;
119 storeError eErrCode = MemoryLockBytes_createInstance(xLockBytes);
120 if (eErrCode != store_E_None)
121 return eErrCode;
122 OSL_ASSERT(xLockBytes.is());
124 Reference<OStorePageManager> xManager (new OStorePageManager());
125 if (!xManager.is())
126 return store_E_OutOfMemory;
128 eErrCode = xManager->initialize (
129 &*xLockBytes, storeAccessMode::Create, nPageSize);
130 if (eErrCode != store_E_None)
131 return eErrCode;
133 xManager->acquire();
135 *phFile = xManager.get();
136 return store_E_None;
140 * store_openFile.
142 storeError store_openFile (
143 rtl_uString *pFilename,
144 storeAccessMode eAccessMode,
145 sal_uInt16 nPageSize,
146 storeFileHandle *phFile
147 ) SAL_THROW_EXTERN_C()
149 if (phFile)
150 *phFile = nullptr;
152 if (!(pFilename && phFile))
153 return store_E_InvalidParameter;
155 Reference<ILockBytes> xLockBytes;
157 storeError eErrCode = FileLockBytes_createInstance (xLockBytes, pFilename, eAccessMode);
158 if (eErrCode != store_E_None)
159 return eErrCode;
160 OSL_ASSERT(xLockBytes.is());
162 Reference<OStorePageManager> xManager (new OStorePageManager());
163 if (!xManager.is())
164 return store_E_OutOfMemory;
166 eErrCode = xManager->initialize (
167 &*xLockBytes, eAccessMode, nPageSize);
168 if (eErrCode != store_E_None)
169 return eErrCode;
171 xManager->acquire();
173 *phFile = xManager.get();
174 return store_E_None;
178 * store_closeFile.
180 storeError store_closeFile (
181 storeFileHandle Handle
182 ) SAL_THROW_EXTERN_C()
184 OStorePageManager *pManager =
185 OStoreHandle<OStorePageManager>::query (Handle);
186 if (!pManager)
187 return store_E_InvalidHandle;
189 storeError eErrCode = pManager->close();
190 pManager->release();
191 return eErrCode;
195 * store_flushFile.
197 storeError store_flushFile (
198 storeFileHandle Handle
199 ) SAL_THROW_EXTERN_C()
201 OStoreHandle<OStorePageManager> xManager (
202 OStoreHandle<OStorePageManager>::query (Handle));
203 if (!xManager.is())
204 return store_E_InvalidHandle;
206 return xManager->flush();
209 /*========================================================================
211 * storeDirectoryHandle implementation.
213 *======================================================================*/
215 * store_openDirectory.
217 storeError store_openDirectory (
218 storeFileHandle hFile,
219 rtl_uString const *pPath,
220 rtl_uString const *pName,
221 storeAccessMode eAccessMode,
222 storeDirectoryHandle *phDirectory
223 ) SAL_THROW_EXTERN_C()
225 storeError eErrCode = store_E_None;
226 if (phDirectory)
227 *phDirectory = nullptr;
229 OStoreHandle<OStorePageManager> xManager (
230 OStoreHandle<OStorePageManager>::query (hFile));
231 if (!xManager.is())
232 return store_E_InvalidHandle;
234 if (!(pPath && pName && phDirectory))
235 return store_E_InvalidParameter;
237 Reference<OStoreDirectory_Impl> xDirectory (new OStoreDirectory_Impl());
238 if (!xDirectory.is())
239 return store_E_OutOfMemory;
241 OString aPath (pPath->buffer, pPath->length, RTL_TEXTENCODING_UTF8);
242 OString aName (pName->buffer, pName->length, RTL_TEXTENCODING_UTF8);
244 eErrCode = xDirectory->create (&*xManager, aPath.pData, aName.pData, eAccessMode);
245 if (eErrCode != store_E_None)
246 return eErrCode;
248 xDirectory->acquire();
250 *phDirectory = xDirectory.get();
251 return store_E_None;
255 * store_findFirst.
257 storeError store_findFirst (
258 storeDirectoryHandle Handle,
259 storeFindData *pFindData
260 ) SAL_THROW_EXTERN_C()
262 OStoreHandle<OStoreDirectory_Impl> xDirectory (
263 OStoreHandle<OStoreDirectory_Impl>::query (Handle));
264 if (!xDirectory.is())
265 return store_E_InvalidHandle;
267 if (!pFindData)
268 return store_E_InvalidParameter;
270 // Initialize FindData.
271 memset (pFindData, 0, sizeof (storeFindData));
273 // Find first.
274 pFindData->m_nReserved = sal_uInt32(~0);
275 return xDirectory->iterate (*pFindData);
279 * store_findNext.
281 storeError store_findNext (
282 storeDirectoryHandle Handle,
283 storeFindData *pFindData
284 ) SAL_THROW_EXTERN_C()
286 OStoreHandle<OStoreDirectory_Impl> xDirectory (
287 OStoreHandle<OStoreDirectory_Impl>::query (Handle));
288 if (!xDirectory.is())
289 return store_E_InvalidHandle;
291 if (!pFindData)
292 return store_E_InvalidParameter;
294 // Check FindData.
295 if (!pFindData->m_nReserved)
296 return store_E_NoMoreFiles;
298 // Find next.
299 pFindData->m_nReserved -= 1;
300 return xDirectory->iterate (*pFindData);
303 /*========================================================================
305 * storeStreamHandle implementation.
307 *======================================================================*/
309 * store_openStream
311 storeError store_openStream (
312 storeFileHandle hFile,
313 rtl_uString const *pPath,
314 rtl_uString const *pName,
315 storeAccessMode eAccessMode,
316 storeStreamHandle *phStream
317 ) SAL_THROW_EXTERN_C()
319 storeError eErrCode = store_E_None;
320 if (phStream)
321 *phStream = nullptr;
323 OStoreHandle<OStorePageManager> xManager (
324 OStoreHandle<OStorePageManager>::query (hFile));
325 if (!xManager.is())
326 return store_E_InvalidHandle;
328 if (!(pPath && pName && phStream))
329 return store_E_InvalidParameter;
331 Reference<OStoreLockBytes> xLockBytes (new OStoreLockBytes());
332 if (!xLockBytes.is())
333 return store_E_OutOfMemory;
335 OString aPath (pPath->buffer, pPath->length, RTL_TEXTENCODING_UTF8);
336 OString aName (pName->buffer, pName->length, RTL_TEXTENCODING_UTF8);
338 eErrCode = xLockBytes->create (&*xManager, aPath.pData, aName.pData, eAccessMode);
339 if (eErrCode != store_E_None)
340 return eErrCode;
342 xLockBytes->acquire();
344 *phStream = xLockBytes.get();
345 return store_E_None;
349 * store_readStream.
351 storeError store_readStream (
352 storeStreamHandle Handle,
353 sal_uInt32 nOffset,
354 void *pBuffer,
355 sal_uInt32 nBytes,
356 sal_uInt32 *pnDone
357 ) SAL_THROW_EXTERN_C()
359 OStoreHandle<OStoreLockBytes> xLockBytes (
360 OStoreHandle<OStoreLockBytes>::query (Handle));
361 if (!xLockBytes.is())
362 return store_E_InvalidHandle;
364 if (!(pBuffer && pnDone))
365 return store_E_InvalidParameter;
367 return xLockBytes->readAt (nOffset, pBuffer, nBytes, *pnDone);
371 * store_writeStream.
373 storeError store_writeStream (
374 storeStreamHandle Handle,
375 sal_uInt32 nOffset,
376 const void *pBuffer,
377 sal_uInt32 nBytes,
378 sal_uInt32 *pnDone
379 ) SAL_THROW_EXTERN_C()
381 OStoreHandle<OStoreLockBytes> xLockBytes (
382 OStoreHandle<OStoreLockBytes>::query (Handle));
383 if (!xLockBytes.is())
384 return store_E_InvalidHandle;
386 if (!(pBuffer && pnDone))
387 return store_E_InvalidParameter;
389 return xLockBytes->writeAt (nOffset, pBuffer, nBytes, *pnDone);
393 * store_remove.
395 storeError store_remove (
396 storeFileHandle Handle,
397 rtl_uString const *pPath,
398 rtl_uString const *pName
399 ) SAL_THROW_EXTERN_C()
401 storeError eErrCode = store_E_None;
403 OStoreHandle<OStorePageManager> xManager (
404 OStoreHandle<OStorePageManager>::query (Handle));
405 if (!xManager.is())
406 return store_E_InvalidHandle;
408 if (!(pPath && pName))
409 return store_E_InvalidParameter;
411 // Setup page key.
412 OString aPath (pPath->buffer, pPath->length, RTL_TEXTENCODING_UTF8);
413 OString aName (pName->buffer, pName->length, RTL_TEXTENCODING_UTF8);
414 OStorePageKey aKey;
416 eErrCode = OStorePageManager::namei (aPath.pData, aName.pData, aKey);
417 if (eErrCode != store_E_None)
418 return eErrCode;
420 // Remove.
421 return xManager->remove (aKey);
424 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */