merge the formfield patch from ooo-build
[ooovba.git] / store / workben / t_base.cxx
blobc77c5bb7535885eaed3a678ba48ad3fe161e04d2
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: t_base.cxx,v $
10 * $Revision: 1.8 $
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"
40 #include "object.hxx"
41 #include "storbase.hxx"
42 #include "storbios.hxx"
43 #include "lockbyte.hxx"
45 using namespace store;
47 #define TEST_PAGESIZE 1024
49 /*========================================================================
51 * OTestObject.
53 *======================================================================*/
54 class OTestObject : public store::OStoreObject
56 public:
57 OTestObject (void);
59 virtual sal_Bool SAL_CALL isKindOf (sal_uInt32 nTypeId);
61 protected:
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);
78 namespace store
80 static OTestObject* SAL_CALL query (IStoreHandle *pHandle, OTestObject*)
82 if (pHandle && pHandle->isKindOf (42))
83 return static_cast<OTestObject*>(pHandle);
84 else
85 return 0;
89 /*========================================================================
91 * OTestBIOS.
93 *======================================================================*/
94 namespace store
97 class OTestBIOS : public store::OStorePageBIOS
99 typedef store::OStorePageBIOS base;
101 friend OTestBIOS* SAL_CALL query<> (IStoreHandle * pHandle, OTestBIOS *);
103 public:
104 OTestBIOS (void);
106 virtual storeError initialize (
107 ILockBytes * pLockBytes,
108 storeAccessMode eAccessMode,
109 sal_uInt16 & rnPageSize);
111 virtual sal_Bool SAL_CALL isKindOf (sal_uInt32 nTypeId);
113 protected:
114 virtual ~OTestBIOS (void);
117 } // namespace store
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);
138 namespace store
140 template<> OTestBIOS* SAL_CALL query (IStoreHandle *pHandle, OTestBIOS*)
142 if (pHandle && pHandle->isKindOf (4242))
143 return static_cast<OTestBIOS*>(pHandle);
144 else
145 return 0;
149 /*========================================================================
151 * __store_test_handle.
153 *======================================================================*/
154 static void __store_test_handle (void* Handle)
156 IStoreHandle *pHandle = static_cast<IStoreHandle*>(Handle);
157 if (pHandle)
159 pHandle->acquire();
160 pHandle->isKindOf (42);
161 pHandle->release();
164 OTestObject *pObj = query (pHandle, static_cast<OTestObject*>(0));
165 if (pObj)
167 pObj->acquire();
168 pObj->isKindOf (42);
169 pObj->release();
173 /*========================================================================
175 * unicode.
177 *======================================================================*/
178 static void __store_string_newFromUnicode_WithLength (
179 rtl_String **newString, const sal_Unicode *value, sal_Int32 length)
181 rtl_uString2String (
182 newString,
183 value, length,
184 RTL_TEXTENCODING_UTF8,
185 OUSTRING_TO_OSTRING_CVTFLAGS);
188 #if 0 /* UNSUSED */
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);
195 #endif /* UNUSED */
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,
207 OStorePageKey &rKey)
209 rtl::OString aName (
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);
229 return eErrCode;
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 (
241 hConvert, 0,
242 pszText, nTextLen,
243 pBuffer, nBuffer,
244 OSTRING_TO_OUSTRING_CVTFLAGS,
245 &nInfo, &nSrcLen);
247 pBuffer[nDstLen] = 0;
248 return nDstLen;
251 struct MyFindData
253 sal_Unicode m_pszName[STORE_MAXIMUM_NAMESIZE];
254 sal_Int32 m_nLength;
255 sal_uInt32 m_nAttrib;
256 sal_uInt32 m_nSize;
257 sal_uInt32 m_nReserved;
260 static void __store_testUnicode (const sal_Char *pszFilename)
262 // ...
263 rtl_TextToUnicodeConverter hConvert;
264 hConvert = rtl_createTextToUnicodeConverter (RTL_TEXTENCODING_UTF8);
266 MyFindData it;
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)
274 rtl_zeroMemory (
275 &it.m_pszName[n], ((it.m_nLength - n) * sizeof(sal_Unicode)));
276 it.m_nLength = n;
278 rtl_destroyTextToUnicodeConverter (hConvert);
280 // ...
281 rtl_String *pszFileA = NULL;
282 rtl_uString *pszFileW = NULL;
284 // rtl_uString_newFromAscii (&pszFileW, pszFilename);
286 // ...
287 rtl_string_newFromStr (&pszFileA, pszFilename);
289 rtl_string2UString (
290 &pszFileW,
291 pszFileA->buffer, pszFileA->length,
292 RTL_TEXTENCODING_MS_1252,
293 OSTRING_TO_OUSTRING_CVTFLAGS);
295 rtl_string_release (pszFileA);
297 // ...
298 OStorePageKey aKey;
299 __store_namei (pszFileW->buffer, pszFileW->buffer, aKey);
301 // ...
302 rtl_uString2String (
303 &pszFileA,
304 pszFileW->buffer, pszFileW->length,
305 RTL_TEXTENCODING_UTF8,
306 OUSTRING_TO_OSTRING_CVTFLAGS);
308 rtl_uString_release (pszFileW);
310 // ...
311 rtl_string_release (pszFileA);
314 /*========================================================================
316 * main.
318 *======================================================================*/
319 int SAL_CALL main (int argc, char **argv)
321 OSL_PRECOND(argc > 1, "t_base: error: insufficient number of arguments.");
322 if (argc < 2)
323 return 0;
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)
336 return eErrCode;
339 rtl::Reference<OTestObject> xObject (new OTestObject());
340 __store_test_handle (&*xObject);
342 rtl::Reference<OTestBIOS> xBIOS (new OTestBIOS());
343 __store_test_handle (&*xBIOS);
346 if (!xBIOS.is())
347 return 0;
349 sal_uInt16 nPageSize = TEST_PAGESIZE;
350 eErrCode = xBIOS->initialize (&*xLockBytes, store_AccessReadWrite, nPageSize);
351 if (eErrCode != store_E_None)
353 // Check reason.
354 if (eErrCode != store_E_NotExists)
355 return eErrCode;
357 // Create.
358 eErrCode = xBIOS->initialize (&*xLockBytes, store_AccessReadCreate, nPageSize);
359 if (eErrCode != store_E_None)
360 return eErrCode;
362 xLockBytes.clear();
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)
370 return eErrCode;
372 eErrCode = xBIOS->write (TEST_PAGESIZE, pBuffer, sizeof (pBuffer));
373 if (eErrCode != store_E_None)
375 xBIOS->releaseLock (TEST_PAGESIZE, sizeof(pBuffer));
376 return eErrCode;
379 eErrCode = xBIOS->releaseLock (TEST_PAGESIZE, sizeof(pBuffer));
380 if (eErrCode != store_E_None)
381 return eErrCode;
383 xBIOS.clear();
384 return 0;