1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: t_base.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_store.hxx"
34 #include "sal/types.h"
35 #include "osl/diagnose.h"
36 #include "osl/thread.h"
37 #include "rtl/memory.h"
38 #include "rtl/ustring.hxx"
41 #include "storbase.hxx"
42 #include "storbios.hxx"
43 #include "lockbyte.hxx"
45 using namespace store
;
47 #define TEST_PAGESIZE 1024
49 /*========================================================================
53 *======================================================================*/
54 class OTestObject
: public store::OStoreObject
59 virtual sal_Bool SAL_CALL
isKindOf (sal_uInt32 nTypeId
);
62 virtual ~OTestObject (void);
65 OTestObject::OTestObject (void)
69 OTestObject::~OTestObject (void)
73 sal_Bool SAL_CALL
OTestObject::isKindOf (sal_uInt32 nTypeId
)
75 return (nTypeId
== 42);
80 static OTestObject
* SAL_CALL
query (IStoreHandle
*pHandle
, OTestObject
*)
82 if (pHandle
&& pHandle
->isKindOf (42))
83 return static_cast<OTestObject
*>(pHandle
);
89 /*========================================================================
93 *======================================================================*/
97 class OTestBIOS
: public store::OStorePageBIOS
99 typedef store::OStorePageBIOS base
;
101 friend OTestBIOS
* SAL_CALL query
<> (IStoreHandle
* pHandle
, OTestBIOS
*);
106 virtual storeError
initialize (
107 ILockBytes
* pLockBytes
,
108 storeAccessMode eAccessMode
,
109 sal_uInt16
& rnPageSize
);
111 virtual sal_Bool SAL_CALL
isKindOf (sal_uInt32 nTypeId
);
114 virtual ~OTestBIOS (void);
119 OTestBIOS::OTestBIOS (void)
123 OTestBIOS::~OTestBIOS (void)
127 sal_Bool SAL_CALL
OTestBIOS::isKindOf (sal_uInt32 nTypeId
)
129 return (nTypeId
== 4242);
132 storeError
OTestBIOS::initialize (
133 ILockBytes
*pLockBytes
, storeAccessMode eAccessMode
, sal_uInt16
& rnPageSize
)
135 return base::initialize (pLockBytes
, eAccessMode
, rnPageSize
);
140 template<> OTestBIOS
* SAL_CALL
query (IStoreHandle
*pHandle
, OTestBIOS
*)
142 if (pHandle
&& pHandle
->isKindOf (4242))
143 return static_cast<OTestBIOS
*>(pHandle
);
149 /*========================================================================
151 * __store_test_handle.
153 *======================================================================*/
154 static void __store_test_handle (void* Handle
)
156 IStoreHandle
*pHandle
= static_cast<IStoreHandle
*>(Handle
);
160 pHandle
->isKindOf (42);
164 OTestObject
*pObj
= query (pHandle
, static_cast<OTestObject
*>(0));
173 /*========================================================================
177 *======================================================================*/
178 static void __store_string_newFromUnicode_WithLength (
179 rtl_String
**newString
, const sal_Unicode
*value
, sal_Int32 length
)
184 RTL_TEXTENCODING_UTF8
,
185 OUSTRING_TO_OSTRING_CVTFLAGS
);
189 static void __store_string_newFromUnicode (
190 rtl_String
**newString
, const rtl_uString
*value
)
192 __store_string_newFromUnicode_WithLength (
193 newString
, value
->buffer
, value
->length
);
197 static void __store_string_newFromUnicode (
198 rtl_String
**newString
, const sal_Unicode
*value
)
200 __store_string_newFromUnicode_WithLength (
201 newString
, value
, rtl_ustr_getLength (value
));
204 static storeError
__store_namei (
205 const sal_Unicode
*pszPath
,
206 const sal_Unicode
*pszName
,
210 pszName
, rtl_ustr_getLength (pszName
), RTL_TEXTENCODING_UTF8
);
212 rtl_String
*pszNameA
= 0;
213 __store_string_newFromUnicode (&pszNameA
, pszName
);
215 storeError eErrCode
= store_E_NameTooLong
;
216 if (pszNameA
->length
< sal_Int32(sizeof(sal_Char
[STORE_MAXIMUM_NAMESIZE
])))
218 rtl_String
*pszPathA
= 0;
219 __store_string_newFromUnicode (&pszPathA
, pszPath
);
221 rKey
.m_nLow
= rtl_crc32 (0, pszNameA
->buffer
, pszNameA
->length
);
222 rKey
.m_nHigh
= rtl_crc32 (0, pszPathA
->buffer
, pszPathA
->length
);
224 rtl_string_release (pszPathA
);
225 eErrCode
= store_E_None
;
228 rtl_string_release (pszNameA
);
232 static sal_Size
__store_convertTextToUnicode (
233 rtl_TextToUnicodeConverter hConvert
,
234 const sal_Char
*pszText
, sal_Size nTextLen
,
235 sal_Unicode
*pBuffer
, sal_Size nBuffer
)
237 sal_uInt32 nInfo
= 0;
238 sal_Size nSrcLen
= 0;
240 sal_Int32 nDstLen
= rtl_convertTextToUnicode (
244 OSTRING_TO_OUSTRING_CVTFLAGS
,
247 pBuffer
[nDstLen
] = 0;
253 sal_Unicode m_pszName
[STORE_MAXIMUM_NAMESIZE
];
255 sal_uInt32 m_nAttrib
;
257 sal_uInt32 m_nReserved
;
260 static void __store_testUnicode (const sal_Char
*pszFilename
)
263 rtl_TextToUnicodeConverter hConvert
;
264 hConvert
= rtl_createTextToUnicodeConverter (RTL_TEXTENCODING_UTF8
);
267 rtl_zeroMemory (&it
, sizeof(it
));
269 sal_Int32 n
= rtl_str_getLength (pszFilename
);
270 n
= __store_convertTextToUnicode (
271 hConvert
, pszFilename
, n
,
272 it
.m_pszName
, STORE_MAXIMUM_NAMESIZE
- 1);
273 if (it
.m_nLength
> n
)
275 &it
.m_pszName
[n
], ((it
.m_nLength
- n
) * sizeof(sal_Unicode
)));
278 rtl_destroyTextToUnicodeConverter (hConvert
);
281 rtl_String
*pszFileA
= NULL
;
282 rtl_uString
*pszFileW
= NULL
;
284 // rtl_uString_newFromAscii (&pszFileW, pszFilename);
287 rtl_string_newFromStr (&pszFileA
, pszFilename
);
291 pszFileA
->buffer
, pszFileA
->length
,
292 RTL_TEXTENCODING_MS_1252
,
293 OSTRING_TO_OUSTRING_CVTFLAGS
);
295 rtl_string_release (pszFileA
);
299 __store_namei (pszFileW
->buffer
, pszFileW
->buffer
, aKey
);
304 pszFileW
->buffer
, pszFileW
->length
,
305 RTL_TEXTENCODING_UTF8
,
306 OUSTRING_TO_OSTRING_CVTFLAGS
);
308 rtl_uString_release (pszFileW
);
311 rtl_string_release (pszFileA
);
314 /*========================================================================
318 *======================================================================*/
319 int SAL_CALL
main (int argc
, char **argv
)
321 OSL_PRECOND(argc
> 1, "t_base: error: insufficient number of arguments.");
325 __store_testUnicode (argv
[1]);
327 rtl::Reference
<ILockBytes
> xLockBytes
;
329 rtl::OUString
aFilename (
330 argv
[1], rtl_str_getLength(argv
[1]),
331 osl_getThreadTextEncoding());
333 storeError eErrCode
= FileLockBytes_createInstance (
334 xLockBytes
, aFilename
.pData
, store_AccessReadCreate
);
335 if (eErrCode
!= store_E_None
)
339 rtl::Reference
<OTestObject
> xObject (new OTestObject());
340 __store_test_handle (&*xObject
);
342 rtl::Reference
<OTestBIOS
> xBIOS (new OTestBIOS());
343 __store_test_handle (&*xBIOS
);
349 sal_uInt16 nPageSize
= TEST_PAGESIZE
;
350 eErrCode
= xBIOS
->initialize (&*xLockBytes
, store_AccessReadWrite
, nPageSize
);
351 if (eErrCode
!= store_E_None
)
354 if (eErrCode
!= store_E_NotExists
)
358 eErrCode
= xBIOS
->initialize (&*xLockBytes
, store_AccessReadCreate
, nPageSize
);
359 if (eErrCode
!= store_E_None
)
364 sal_Char pBuffer
[TEST_PAGESIZE
];
365 rtl_zeroMemory (pBuffer
, sizeof (pBuffer
));
366 rtl_copyMemory (pBuffer
, argv
[0], rtl_str_getLength(argv
[0]) + 1);
368 eErrCode
= xBIOS
->acquireLock (TEST_PAGESIZE
, sizeof(pBuffer
));
369 if (eErrCode
!= store_E_None
)
372 eErrCode
= xBIOS
->write (TEST_PAGESIZE
, pBuffer
, sizeof (pBuffer
));
373 if (eErrCode
!= store_E_None
)
375 xBIOS
->releaseLock (TEST_PAGESIZE
, sizeof(pBuffer
));
379 eErrCode
= xBIOS
->releaseLock (TEST_PAGESIZE
, sizeof(pBuffer
));
380 if (eErrCode
!= store_E_None
)