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 .
22 #include "sal/types.h"
23 #include "osl/diagnose.h"
24 #include "osl/thread.h"
25 #include "rtl/ustring.hxx"
28 #include "storbase.hxx"
29 #include "storbios.hxx"
30 #include "lockbyte.hxx"
32 using namespace store
;
34 #define TEST_PAGESIZE 1024
36 /*========================================================================
40 *======================================================================*/
41 class OTestObject
: public store::OStoreObject
46 virtual sal_Bool SAL_CALL
isKindOf (sal_uInt32 nTypeId
);
49 virtual ~OTestObject (void);
52 OTestObject::OTestObject (void)
56 OTestObject::~OTestObject (void)
60 sal_Bool SAL_CALL
OTestObject::isKindOf (sal_uInt32 nTypeId
)
62 return (nTypeId
== 42);
67 static OTestObject
* SAL_CALL
query (IStoreHandle
*pHandle
, OTestObject
*)
69 if (pHandle
&& pHandle
->isKindOf (42))
70 return static_cast<OTestObject
*>(pHandle
);
76 /*========================================================================
80 *======================================================================*/
84 class OTestBIOS
: public store::OStorePageBIOS
86 typedef store::OStorePageBIOS base
;
88 friend OTestBIOS
* SAL_CALL query
<> (IStoreHandle
* pHandle
, OTestBIOS
*);
93 virtual storeError
initialize (
94 ILockBytes
* pLockBytes
,
95 storeAccessMode eAccessMode
,
96 sal_uInt16
& rnPageSize
);
98 virtual sal_Bool SAL_CALL
isKindOf (sal_uInt32 nTypeId
);
101 virtual ~OTestBIOS (void);
106 OTestBIOS::OTestBIOS (void)
110 OTestBIOS::~OTestBIOS (void)
114 sal_Bool SAL_CALL
OTestBIOS::isKindOf (sal_uInt32 nTypeId
)
116 return (nTypeId
== 4242);
119 storeError
OTestBIOS::initialize (
120 ILockBytes
*pLockBytes
, storeAccessMode eAccessMode
, sal_uInt16
& rnPageSize
)
122 return base::initialize (pLockBytes
, eAccessMode
, rnPageSize
);
127 template<> OTestBIOS
* SAL_CALL
query (IStoreHandle
*pHandle
, OTestBIOS
*)
129 if (pHandle
&& pHandle
->isKindOf (4242))
130 return static_cast<OTestBIOS
*>(pHandle
);
136 /*========================================================================
138 * __store_test_handle.
140 *======================================================================*/
141 static void __store_test_handle (void* Handle
)
143 IStoreHandle
*pHandle
= static_cast<IStoreHandle
*>(Handle
);
147 pHandle
->isKindOf (42);
151 OTestObject
*pObj
= query (pHandle
, static_cast<OTestObject
*>(0));
160 /*========================================================================
164 *======================================================================*/
165 static void __store_string_newFromUnicode_WithLength (
166 rtl_String
**newString
, const sal_Unicode
*value
, sal_Int32 length
)
171 RTL_TEXTENCODING_UTF8
,
172 OUSTRING_TO_OSTRING_CVTFLAGS
);
175 static void __store_string_newFromUnicode (
176 rtl_String
**newString
, const sal_Unicode
*value
)
178 __store_string_newFromUnicode_WithLength (
179 newString
, value
, rtl_ustr_getLength (value
));
182 static storeError
__store_namei (
183 const sal_Unicode
*pszPath
,
184 const sal_Unicode
*pszName
,
188 pszName
, rtl_ustr_getLength (pszName
), RTL_TEXTENCODING_UTF8
);
190 rtl_String
*pszNameA
= 0;
191 __store_string_newFromUnicode (&pszNameA
, pszName
);
193 storeError eErrCode
= store_E_NameTooLong
;
194 if (pszNameA
->length
< sal_Int32(sizeof(sal_Char
[STORE_MAXIMUM_NAMESIZE
])))
196 rtl_String
*pszPathA
= 0;
197 __store_string_newFromUnicode (&pszPathA
, pszPath
);
199 rKey
.m_nLow
= rtl_crc32 (0, pszNameA
->buffer
, pszNameA
->length
);
200 rKey
.m_nHigh
= rtl_crc32 (0, pszPathA
->buffer
, pszPathA
->length
);
202 rtl_string_release (pszPathA
);
203 eErrCode
= store_E_None
;
206 rtl_string_release (pszNameA
);
210 static sal_Size
__store_convertTextToUnicode (
211 rtl_TextToUnicodeConverter hConvert
,
212 const sal_Char
*pszText
, sal_Size nTextLen
,
213 sal_Unicode
*pBuffer
, sal_Size nBuffer
)
215 sal_uInt32 nInfo
= 0;
216 sal_Size nSrcLen
= 0;
218 sal_Int32 nDstLen
= rtl_convertTextToUnicode (
222 OSTRING_TO_OUSTRING_CVTFLAGS
,
225 pBuffer
[nDstLen
] = 0;
231 sal_Unicode m_pszName
[STORE_MAXIMUM_NAMESIZE
];
233 sal_uInt32 m_nAttrib
;
235 sal_uInt32 m_nReserved
;
238 static void __store_testUnicode (const sal_Char
*pszFilename
)
241 rtl_TextToUnicodeConverter hConvert
;
242 hConvert
= rtl_createTextToUnicodeConverter (RTL_TEXTENCODING_UTF8
);
245 memset (&it
, 0, sizeof(it
));
247 sal_Int32 n
= rtl_str_getLength (pszFilename
);
248 n
= __store_convertTextToUnicode (
249 hConvert
, pszFilename
, n
,
250 it
.m_pszName
, STORE_MAXIMUM_NAMESIZE
- 1);
251 if (it
.m_nLength
> n
)
253 &it
.m_pszName
[n
], 0, ((it
.m_nLength
- n
) * sizeof(sal_Unicode
)));
256 rtl_destroyTextToUnicodeConverter (hConvert
);
259 rtl_String
*pszFileA
= NULL
;
260 rtl_uString
*pszFileW
= NULL
;
262 // rtl_uString_newFromAscii (&pszFileW, pszFilename);
265 rtl_string_newFromStr (&pszFileA
, pszFilename
);
269 pszFileA
->buffer
, pszFileA
->length
,
270 RTL_TEXTENCODING_MS_1252
,
271 OSTRING_TO_OUSTRING_CVTFLAGS
);
273 rtl_string_release (pszFileA
);
277 __store_namei (pszFileW
->buffer
, pszFileW
->buffer
, aKey
);
282 pszFileW
->buffer
, pszFileW
->length
,
283 RTL_TEXTENCODING_UTF8
,
284 OUSTRING_TO_OSTRING_CVTFLAGS
);
286 rtl_uString_release (pszFileW
);
289 rtl_string_release (pszFileA
);
292 /*========================================================================
296 *======================================================================*/
297 int SAL_CALL
main (int argc
, char **argv
)
299 OSL_PRECOND(argc
> 1, "t_base: error: insufficient number of arguments.");
303 __store_testUnicode (argv
[1]);
305 rtl::Reference
<ILockBytes
> xLockBytes
;
308 argv
[1], rtl_str_getLength(argv
[1]),
309 osl_getThreadTextEncoding());
311 storeError eErrCode
= FileLockBytes_createInstance (
312 xLockBytes
, aFilename
.pData
, store_AccessReadCreate
);
313 if (eErrCode
!= store_E_None
)
317 rtl::Reference
<OTestObject
> xObject (new OTestObject());
318 __store_test_handle (&*xObject
);
320 rtl::Reference
<OTestBIOS
> xBIOS (new OTestBIOS());
321 __store_test_handle (&*xBIOS
);
327 sal_uInt16 nPageSize
= TEST_PAGESIZE
;
328 eErrCode
= xBIOS
->initialize (&*xLockBytes
, store_AccessReadWrite
, nPageSize
);
329 if (eErrCode
!= store_E_None
)
332 if (eErrCode
!= store_E_NotExists
)
336 eErrCode
= xBIOS
->initialize (&*xLockBytes
, store_AccessReadCreate
, nPageSize
);
337 if (eErrCode
!= store_E_None
)
342 sal_Char pBuffer
[TEST_PAGESIZE
];
343 memset (pBuffer
, 0, sizeof (pBuffer
));
344 memcpy (pBuffer
, argv
[0], rtl_str_getLength(argv
[0]) + 1);
346 eErrCode
= xBIOS
->write (TEST_PAGESIZE
, pBuffer
, sizeof (pBuffer
));
347 if (eErrCode
!= store_E_None
)
354 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */