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"
38 /** Template helper class as type safe Reference to store_handle_type.
40 template<class store_handle_type
>
41 class OStoreHandle
: public rtl::Reference
<store_handle_type
>
44 explicit OStoreHandle (store_handle_type
* pHandle
)
45 : rtl::Reference
<store_handle_type
> (pHandle
)
48 static store_handle_type
* SAL_CALL
query (void * pHandle
)
51 static_cast<OStoreObject
*>(pHandle
),
52 static_cast<store_handle_type
*>(0));
57 using namespace store
;
59 /*========================================================================
61 * storeHandle implementation.
63 *======================================================================*/
65 * store_acquireHandle.
67 storeError SAL_CALL
store_acquireHandle (
69 ) SAL_THROW_EXTERN_C()
71 OStoreObject
*pHandle
= static_cast<OStoreObject
*>(Handle
);
73 return store_E_InvalidHandle
;
80 * store_releaseHandle.
82 storeError SAL_CALL
store_releaseHandle (
84 ) SAL_THROW_EXTERN_C()
86 OStoreObject
*pHandle
= static_cast<OStoreObject
*>(Handle
);
88 return store_E_InvalidHandle
;
94 /*========================================================================
96 * storeFileHandle implementation.
98 *======================================================================*/
100 * store_createMemoryFile.
102 storeError SAL_CALL
store_createMemoryFile (
103 sal_uInt16 nPageSize
,
104 storeFileHandle
*phFile
105 ) SAL_THROW_EXTERN_C()
108 return store_E_InvalidParameter
;
111 Reference
<ILockBytes
> xLockBytes
;
113 storeError eErrCode
= MemoryLockBytes_createInstance(xLockBytes
);
114 if (eErrCode
!= store_E_None
)
116 OSL_ASSERT(xLockBytes
.is());
118 Reference
<OStorePageManager
> xManager (new OStorePageManager());
120 return store_E_OutOfMemory
;
122 eErrCode
= xManager
->initialize (
123 &*xLockBytes
, storeAccessMode::Create
, nPageSize
);
124 if (eErrCode
!= store_E_None
)
129 *phFile
= xManager
.get();
136 storeError SAL_CALL
store_openFile (
137 rtl_uString
*pFilename
,
138 storeAccessMode eAccessMode
,
139 sal_uInt16 nPageSize
,
140 storeFileHandle
*phFile
141 ) SAL_THROW_EXTERN_C()
146 if (!(pFilename
&& phFile
))
147 return store_E_InvalidParameter
;
149 Reference
<ILockBytes
> xLockBytes
;
151 storeError eErrCode
= FileLockBytes_createInstance (xLockBytes
, pFilename
, eAccessMode
);
152 if (eErrCode
!= store_E_None
)
154 OSL_ASSERT(xLockBytes
.is());
156 Reference
<OStorePageManager
> xManager (new OStorePageManager());
158 return store_E_OutOfMemory
;
160 eErrCode
= xManager
->initialize (
161 &*xLockBytes
, eAccessMode
, nPageSize
);
162 if (eErrCode
!= store_E_None
)
167 *phFile
= xManager
.get();
174 storeError SAL_CALL
store_closeFile (
175 storeFileHandle Handle
176 ) SAL_THROW_EXTERN_C()
178 OStorePageManager
*pManager
=
179 OStoreHandle
<OStorePageManager
>::query (Handle
);
181 return store_E_InvalidHandle
;
183 storeError eErrCode
= pManager
->close();
191 storeError SAL_CALL
store_flushFile (
192 storeFileHandle Handle
193 ) SAL_THROW_EXTERN_C()
195 OStoreHandle
<OStorePageManager
> xManager (
196 OStoreHandle
<OStorePageManager
>::query (Handle
));
198 return store_E_InvalidHandle
;
200 return xManager
->flush();
203 /*========================================================================
205 * storeDirectoryHandle implementation.
207 *======================================================================*/
209 * store_openDirectory.
211 storeError SAL_CALL
store_openDirectory (
212 storeFileHandle hFile
,
213 rtl_uString
const *pPath
,
214 rtl_uString
const *pName
,
215 storeAccessMode eAccessMode
,
216 storeDirectoryHandle
*phDirectory
217 ) SAL_THROW_EXTERN_C()
219 storeError eErrCode
= store_E_None
;
221 *phDirectory
= nullptr;
223 OStoreHandle
<OStorePageManager
> xManager (
224 OStoreHandle
<OStorePageManager
>::query (hFile
));
226 return store_E_InvalidHandle
;
228 if (!(pPath
&& pName
&& phDirectory
))
229 return store_E_InvalidParameter
;
231 Reference
<OStoreDirectory_Impl
> xDirectory (new OStoreDirectory_Impl());
232 if (!xDirectory
.is())
233 return store_E_OutOfMemory
;
235 OString
aPath (pPath
->buffer
, pPath
->length
, RTL_TEXTENCODING_UTF8
);
236 OString
aName (pName
->buffer
, pName
->length
, RTL_TEXTENCODING_UTF8
);
238 eErrCode
= xDirectory
->create (&*xManager
, aPath
.pData
, aName
.pData
, eAccessMode
);
239 if (eErrCode
!= store_E_None
)
242 xDirectory
->acquire();
244 *phDirectory
= xDirectory
.get();
251 storeError SAL_CALL
store_findFirst (
252 storeDirectoryHandle Handle
,
253 storeFindData
*pFindData
254 ) SAL_THROW_EXTERN_C()
256 OStoreHandle
<OStoreDirectory_Impl
> xDirectory (
257 OStoreHandle
<OStoreDirectory_Impl
>::query (Handle
));
258 if (!xDirectory
.is())
259 return store_E_InvalidHandle
;
262 return store_E_InvalidParameter
;
264 // Initialize FindData.
265 memset (pFindData
, 0, sizeof (storeFindData
));
268 pFindData
->m_nReserved
= (sal_uInt32
)(~0);
269 return xDirectory
->iterate (*pFindData
);
275 storeError SAL_CALL
store_findNext (
276 storeDirectoryHandle Handle
,
277 storeFindData
*pFindData
278 ) SAL_THROW_EXTERN_C()
280 OStoreHandle
<OStoreDirectory_Impl
> xDirectory (
281 OStoreHandle
<OStoreDirectory_Impl
>::query (Handle
));
282 if (!xDirectory
.is())
283 return store_E_InvalidHandle
;
286 return store_E_InvalidParameter
;
289 if (!pFindData
->m_nReserved
)
290 return store_E_NoMoreFiles
;
293 pFindData
->m_nReserved
-= 1;
294 return xDirectory
->iterate (*pFindData
);
297 /*========================================================================
299 * storeStreamHandle implementation.
301 *======================================================================*/
305 storeError SAL_CALL
store_openStream (
306 storeFileHandle hFile
,
307 rtl_uString
const *pPath
,
308 rtl_uString
const *pName
,
309 storeAccessMode eAccessMode
,
310 storeStreamHandle
*phStream
311 ) SAL_THROW_EXTERN_C()
313 storeError eErrCode
= store_E_None
;
317 OStoreHandle
<OStorePageManager
> xManager (
318 OStoreHandle
<OStorePageManager
>::query (hFile
));
320 return store_E_InvalidHandle
;
322 if (!(pPath
&& pName
&& phStream
))
323 return store_E_InvalidParameter
;
325 Reference
<OStoreLockBytes
> xLockBytes (new OStoreLockBytes());
326 if (!xLockBytes
.is())
327 return store_E_OutOfMemory
;
329 OString
aPath (pPath
->buffer
, pPath
->length
, RTL_TEXTENCODING_UTF8
);
330 OString
aName (pName
->buffer
, pName
->length
, RTL_TEXTENCODING_UTF8
);
332 eErrCode
= xLockBytes
->create (&*xManager
, aPath
.pData
, aName
.pData
, eAccessMode
);
333 if (eErrCode
!= store_E_None
)
336 xLockBytes
->acquire();
338 *phStream
= xLockBytes
.get();
345 storeError SAL_CALL
store_readStream (
346 storeStreamHandle Handle
,
351 ) SAL_THROW_EXTERN_C()
353 OStoreHandle
<OStoreLockBytes
> xLockBytes (
354 OStoreHandle
<OStoreLockBytes
>::query (Handle
));
355 if (!xLockBytes
.is())
356 return store_E_InvalidHandle
;
358 if (!(pBuffer
&& pnDone
))
359 return store_E_InvalidParameter
;
361 return xLockBytes
->readAt (nOffset
, pBuffer
, nBytes
, *pnDone
);
367 storeError SAL_CALL
store_writeStream (
368 storeStreamHandle Handle
,
373 ) SAL_THROW_EXTERN_C()
375 OStoreHandle
<OStoreLockBytes
> xLockBytes (
376 OStoreHandle
<OStoreLockBytes
>::query (Handle
));
377 if (!xLockBytes
.is())
378 return store_E_InvalidHandle
;
380 if (!(pBuffer
&& pnDone
))
381 return store_E_InvalidParameter
;
383 return xLockBytes
->writeAt (nOffset
, pBuffer
, nBytes
, *pnDone
);
389 storeError SAL_CALL
store_remove (
390 storeFileHandle Handle
,
391 rtl_uString
const *pPath
,
392 rtl_uString
const *pName
393 ) SAL_THROW_EXTERN_C()
395 storeError eErrCode
= store_E_None
;
397 OStoreHandle
<OStorePageManager
> xManager (
398 OStoreHandle
<OStorePageManager
>::query (Handle
));
400 return store_E_InvalidHandle
;
402 if (!(pPath
&& pName
))
403 return store_E_InvalidParameter
;
406 OString
aPath (pPath
->buffer
, pPath
->length
, RTL_TEXTENCODING_UTF8
);
407 OString
aName (pName
->buffer
, pName
->length
, RTL_TEXTENCODING_UTF8
);
410 eErrCode
= OStorePageManager::namei (aPath
.pData
, aName
.pData
, aKey
);
411 if (eErrCode
!= store_E_None
)
415 return xManager
->remove (aKey
);
418 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */