1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include "sal/types.h"
31 #include "osl/diagnose.h"
32 #include "osl/thread.h"
33 #include "rtl/memory.h"
34 #include "rtl/ustring.hxx"
37 #include "storbase.hxx"
38 #include "storbios.hxx"
39 #include "lockbyte.hxx"
41 using namespace store
;
43 #define TEST_PAGESIZE 1024
45 /*========================================================================
49 *======================================================================*/
50 class OTestObject
: public store::OStoreObject
55 virtual sal_Bool SAL_CALL
isKindOf (sal_uInt32 nTypeId
);
58 virtual ~OTestObject (void);
61 OTestObject::OTestObject (void)
65 OTestObject::~OTestObject (void)
69 sal_Bool SAL_CALL
OTestObject::isKindOf (sal_uInt32 nTypeId
)
71 return (nTypeId
== 42);
76 static OTestObject
* SAL_CALL
query (IStoreHandle
*pHandle
, OTestObject
*)
78 if (pHandle
&& pHandle
->isKindOf (42))
79 return static_cast<OTestObject
*>(pHandle
);
85 /*========================================================================
89 *======================================================================*/
93 class OTestBIOS
: public store::OStorePageBIOS
95 typedef store::OStorePageBIOS base
;
97 friend OTestBIOS
* SAL_CALL query
<> (IStoreHandle
* pHandle
, OTestBIOS
*);
102 virtual storeError
initialize (
103 ILockBytes
* pLockBytes
,
104 storeAccessMode eAccessMode
,
105 sal_uInt16
& rnPageSize
);
107 virtual sal_Bool SAL_CALL
isKindOf (sal_uInt32 nTypeId
);
110 virtual ~OTestBIOS (void);
115 OTestBIOS::OTestBIOS (void)
119 OTestBIOS::~OTestBIOS (void)
123 sal_Bool SAL_CALL
OTestBIOS::isKindOf (sal_uInt32 nTypeId
)
125 return (nTypeId
== 4242);
128 storeError
OTestBIOS::initialize (
129 ILockBytes
*pLockBytes
, storeAccessMode eAccessMode
, sal_uInt16
& rnPageSize
)
131 return base::initialize (pLockBytes
, eAccessMode
, rnPageSize
);
136 template<> OTestBIOS
* SAL_CALL
query (IStoreHandle
*pHandle
, OTestBIOS
*)
138 if (pHandle
&& pHandle
->isKindOf (4242))
139 return static_cast<OTestBIOS
*>(pHandle
);
145 /*========================================================================
147 * __store_test_handle.
149 *======================================================================*/
150 static void __store_test_handle (void* Handle
)
152 IStoreHandle
*pHandle
= static_cast<IStoreHandle
*>(Handle
);
156 pHandle
->isKindOf (42);
160 OTestObject
*pObj
= query (pHandle
, static_cast<OTestObject
*>(0));
169 /*========================================================================
173 *======================================================================*/
174 static void __store_string_newFromUnicode_WithLength (
175 rtl_String
**newString
, const sal_Unicode
*value
, sal_Int32 length
)
180 RTL_TEXTENCODING_UTF8
,
181 OUSTRING_TO_OSTRING_CVTFLAGS
);
184 static void __store_string_newFromUnicode (
185 rtl_String
**newString
, const sal_Unicode
*value
)
187 __store_string_newFromUnicode_WithLength (
188 newString
, value
, rtl_ustr_getLength (value
));
191 static storeError
__store_namei (
192 const sal_Unicode
*pszPath
,
193 const sal_Unicode
*pszName
,
197 pszName
, rtl_ustr_getLength (pszName
), RTL_TEXTENCODING_UTF8
);
199 rtl_String
*pszNameA
= 0;
200 __store_string_newFromUnicode (&pszNameA
, pszName
);
202 storeError eErrCode
= store_E_NameTooLong
;
203 if (pszNameA
->length
< sal_Int32(sizeof(sal_Char
[STORE_MAXIMUM_NAMESIZE
])))
205 rtl_String
*pszPathA
= 0;
206 __store_string_newFromUnicode (&pszPathA
, pszPath
);
208 rKey
.m_nLow
= rtl_crc32 (0, pszNameA
->buffer
, pszNameA
->length
);
209 rKey
.m_nHigh
= rtl_crc32 (0, pszPathA
->buffer
, pszPathA
->length
);
211 rtl_string_release (pszPathA
);
212 eErrCode
= store_E_None
;
215 rtl_string_release (pszNameA
);
219 static sal_Size
__store_convertTextToUnicode (
220 rtl_TextToUnicodeConverter hConvert
,
221 const sal_Char
*pszText
, sal_Size nTextLen
,
222 sal_Unicode
*pBuffer
, sal_Size nBuffer
)
224 sal_uInt32 nInfo
= 0;
225 sal_Size nSrcLen
= 0;
227 sal_Int32 nDstLen
= rtl_convertTextToUnicode (
231 OSTRING_TO_OUSTRING_CVTFLAGS
,
234 pBuffer
[nDstLen
] = 0;
240 sal_Unicode m_pszName
[STORE_MAXIMUM_NAMESIZE
];
242 sal_uInt32 m_nAttrib
;
244 sal_uInt32 m_nReserved
;
247 static void __store_testUnicode (const sal_Char
*pszFilename
)
250 rtl_TextToUnicodeConverter hConvert
;
251 hConvert
= rtl_createTextToUnicodeConverter (RTL_TEXTENCODING_UTF8
);
254 rtl_zeroMemory (&it
, sizeof(it
));
256 sal_Int32 n
= rtl_str_getLength (pszFilename
);
257 n
= __store_convertTextToUnicode (
258 hConvert
, pszFilename
, n
,
259 it
.m_pszName
, STORE_MAXIMUM_NAMESIZE
- 1);
260 if (it
.m_nLength
> n
)
262 &it
.m_pszName
[n
], ((it
.m_nLength
- n
) * sizeof(sal_Unicode
)));
265 rtl_destroyTextToUnicodeConverter (hConvert
);
268 rtl_String
*pszFileA
= NULL
;
269 rtl_uString
*pszFileW
= NULL
;
271 // rtl_uString_newFromAscii (&pszFileW, pszFilename);
274 rtl_string_newFromStr (&pszFileA
, pszFilename
);
278 pszFileA
->buffer
, pszFileA
->length
,
279 RTL_TEXTENCODING_MS_1252
,
280 OSTRING_TO_OUSTRING_CVTFLAGS
);
282 rtl_string_release (pszFileA
);
286 __store_namei (pszFileW
->buffer
, pszFileW
->buffer
, aKey
);
291 pszFileW
->buffer
, pszFileW
->length
,
292 RTL_TEXTENCODING_UTF8
,
293 OUSTRING_TO_OSTRING_CVTFLAGS
);
295 rtl_uString_release (pszFileW
);
298 rtl_string_release (pszFileA
);
301 /*========================================================================
305 *======================================================================*/
306 int SAL_CALL
main (int argc
, char **argv
)
308 OSL_PRECOND(argc
> 1, "t_base: error: insufficient number of arguments.");
312 __store_testUnicode (argv
[1]);
314 rtl::Reference
<ILockBytes
> xLockBytes
;
316 rtl::OUString
aFilename (
317 argv
[1], rtl_str_getLength(argv
[1]),
318 osl_getThreadTextEncoding());
320 storeError eErrCode
= FileLockBytes_createInstance (
321 xLockBytes
, aFilename
.pData
, store_AccessReadCreate
);
322 if (eErrCode
!= store_E_None
)
326 rtl::Reference
<OTestObject
> xObject (new OTestObject());
327 __store_test_handle (&*xObject
);
329 rtl::Reference
<OTestBIOS
> xBIOS (new OTestBIOS());
330 __store_test_handle (&*xBIOS
);
336 sal_uInt16 nPageSize
= TEST_PAGESIZE
;
337 eErrCode
= xBIOS
->initialize (&*xLockBytes
, store_AccessReadWrite
, nPageSize
);
338 if (eErrCode
!= store_E_None
)
341 if (eErrCode
!= store_E_NotExists
)
345 eErrCode
= xBIOS
->initialize (&*xLockBytes
, store_AccessReadCreate
, nPageSize
);
346 if (eErrCode
!= store_E_None
)
351 sal_Char pBuffer
[TEST_PAGESIZE
];
352 rtl_zeroMemory (pBuffer
, sizeof (pBuffer
));
353 rtl_copyMemory (pBuffer
, argv
[0], rtl_str_getLength(argv
[0]) + 1);
355 eErrCode
= xBIOS
->write (TEST_PAGESIZE
, pBuffer
, sizeof (pBuffer
));
356 if (eErrCode
!= store_E_None
)
363 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */