1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
27 #include "lockbyte.hxx"
29 #include "storbase.hxx"
30 #include "storpage.hxx"
31 #include "stordir.hxx"
32 #include "storlckb.hxx"
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
>
47 explicit OStoreHandle (store_handle_type
* pHandle
)
48 : rtl::Reference
<store_handle_type
> (pHandle
)
51 static store_handle_type
* SAL_CALL
query (void * pHandle
)
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 (
75 ) SAL_THROW_EXTERN_C()
77 OStoreObject
*pHandle
= static_cast<OStoreObject
*>(Handle
);
79 return store_E_InvalidHandle
;
86 * store_releaseHandle.
88 storeError
store_releaseHandle (
90 ) SAL_THROW_EXTERN_C()
92 OStoreObject
*pHandle
= static_cast<OStoreObject
*>(Handle
);
94 return store_E_InvalidHandle
;
100 /*========================================================================
102 * storeFileHandle implementation.
104 *======================================================================*/
106 * store_createMemoryFile.
108 storeError
store_createMemoryFile (
109 sal_uInt16 nPageSize
,
110 storeFileHandle
*phFile
111 ) SAL_THROW_EXTERN_C()
114 return store_E_InvalidParameter
;
117 Reference
<ILockBytes
> xLockBytes
;
119 storeError eErrCode
= MemoryLockBytes_createInstance(xLockBytes
);
120 if (eErrCode
!= store_E_None
)
122 OSL_ASSERT(xLockBytes
.is());
124 Reference
<OStorePageManager
> xManager (new OStorePageManager());
126 return store_E_OutOfMemory
;
128 eErrCode
= xManager
->initialize (
129 &*xLockBytes
, storeAccessMode::Create
, nPageSize
);
130 if (eErrCode
!= store_E_None
)
135 *phFile
= xManager
.get();
142 storeError
store_openFile (
143 rtl_uString
*pFilename
,
144 storeAccessMode eAccessMode
,
145 sal_uInt16 nPageSize
,
146 storeFileHandle
*phFile
147 ) SAL_THROW_EXTERN_C()
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
)
160 OSL_ASSERT(xLockBytes
.is());
162 Reference
<OStorePageManager
> xManager (new OStorePageManager());
164 return store_E_OutOfMemory
;
166 eErrCode
= xManager
->initialize (
167 &*xLockBytes
, eAccessMode
, nPageSize
);
168 if (eErrCode
!= store_E_None
)
173 *phFile
= xManager
.get();
180 storeError
store_closeFile (
181 storeFileHandle Handle
182 ) SAL_THROW_EXTERN_C()
184 OStorePageManager
*pManager
=
185 OStoreHandle
<OStorePageManager
>::query (Handle
);
187 return store_E_InvalidHandle
;
189 storeError eErrCode
= pManager
->close();
197 storeError
store_flushFile (
198 storeFileHandle Handle
199 ) SAL_THROW_EXTERN_C()
201 OStoreHandle
<OStorePageManager
> xManager (
202 OStoreHandle
<OStorePageManager
>::query (Handle
));
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
;
227 *phDirectory
= nullptr;
229 OStoreHandle
<OStorePageManager
> xManager (
230 OStoreHandle
<OStorePageManager
>::query (hFile
));
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
)
248 xDirectory
->acquire();
250 *phDirectory
= xDirectory
.get();
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
;
268 return store_E_InvalidParameter
;
270 // Initialize FindData.
271 memset (pFindData
, 0, sizeof (storeFindData
));
274 pFindData
->m_nReserved
= sal_uInt32(~0);
275 return xDirectory
->iterate (*pFindData
);
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
;
292 return store_E_InvalidParameter
;
295 if (!pFindData
->m_nReserved
)
296 return store_E_NoMoreFiles
;
299 pFindData
->m_nReserved
-= 1;
300 return xDirectory
->iterate (*pFindData
);
303 /*========================================================================
305 * storeStreamHandle implementation.
307 *======================================================================*/
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
;
323 OStoreHandle
<OStorePageManager
> xManager (
324 OStoreHandle
<OStorePageManager
>::query (hFile
));
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
)
342 xLockBytes
->acquire();
344 *phStream
= xLockBytes
.get();
351 storeError
store_readStream (
352 storeStreamHandle Handle
,
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
);
373 storeError
store_writeStream (
374 storeStreamHandle Handle
,
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
);
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
));
406 return store_E_InvalidHandle
;
408 if (!(pPath
&& pName
))
409 return store_E_InvalidParameter
;
412 OString
aPath (pPath
->buffer
, pPath
->length
, RTL_TEXTENCODING_UTF8
);
413 OString
aName (pName
->buffer
, pName
->length
, RTL_TEXTENCODING_UTF8
);
416 eErrCode
= OStorePageManager::namei (aPath
.pData
, aName
.pData
, aKey
);
417 if (eErrCode
!= store_E_None
)
421 return xManager
->remove (aKey
);
424 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */