Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / store / workben / t_base.cxx
blobbb6e33a1c14599360485faeb1333aa0c65646b4c
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"
36 #include "object.hxx"
37 #include "storbase.hxx"
38 #include "storbios.hxx"
39 #include "lockbyte.hxx"
41 using namespace store;
43 #define TEST_PAGESIZE 1024
45 /*========================================================================
47 * OTestObject.
49 *======================================================================*/
50 class OTestObject : public store::OStoreObject
52 public:
53 OTestObject (void);
55 virtual sal_Bool SAL_CALL isKindOf (sal_uInt32 nTypeId);
57 protected:
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);
74 namespace store
76 static OTestObject* SAL_CALL query (IStoreHandle *pHandle, OTestObject*)
78 if (pHandle && pHandle->isKindOf (42))
79 return static_cast<OTestObject*>(pHandle);
80 else
81 return 0;
85 /*========================================================================
87 * OTestBIOS.
89 *======================================================================*/
90 namespace store
93 class OTestBIOS : public store::OStorePageBIOS
95 typedef store::OStorePageBIOS base;
97 friend OTestBIOS* SAL_CALL query<> (IStoreHandle * pHandle, OTestBIOS *);
99 public:
100 OTestBIOS (void);
102 virtual storeError initialize (
103 ILockBytes * pLockBytes,
104 storeAccessMode eAccessMode,
105 sal_uInt16 & rnPageSize);
107 virtual sal_Bool SAL_CALL isKindOf (sal_uInt32 nTypeId);
109 protected:
110 virtual ~OTestBIOS (void);
113 } // namespace store
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);
134 namespace store
136 template<> OTestBIOS* SAL_CALL query (IStoreHandle *pHandle, OTestBIOS*)
138 if (pHandle && pHandle->isKindOf (4242))
139 return static_cast<OTestBIOS*>(pHandle);
140 else
141 return 0;
145 /*========================================================================
147 * __store_test_handle.
149 *======================================================================*/
150 static void __store_test_handle (void* Handle)
152 IStoreHandle *pHandle = static_cast<IStoreHandle*>(Handle);
153 if (pHandle)
155 pHandle->acquire();
156 pHandle->isKindOf (42);
157 pHandle->release();
160 OTestObject *pObj = query (pHandle, static_cast<OTestObject*>(0));
161 if (pObj)
163 pObj->acquire();
164 pObj->isKindOf (42);
165 pObj->release();
169 /*========================================================================
171 * unicode.
173 *======================================================================*/
174 static void __store_string_newFromUnicode_WithLength (
175 rtl_String **newString, const sal_Unicode *value, sal_Int32 length)
177 rtl_uString2String (
178 newString,
179 value, 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,
194 OStorePageKey &rKey)
196 rtl::OString aName (
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);
216 return eErrCode;
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 (
228 hConvert, 0,
229 pszText, nTextLen,
230 pBuffer, nBuffer,
231 OSTRING_TO_OUSTRING_CVTFLAGS,
232 &nInfo, &nSrcLen);
234 pBuffer[nDstLen] = 0;
235 return nDstLen;
238 struct MyFindData
240 sal_Unicode m_pszName[STORE_MAXIMUM_NAMESIZE];
241 sal_Int32 m_nLength;
242 sal_uInt32 m_nAttrib;
243 sal_uInt32 m_nSize;
244 sal_uInt32 m_nReserved;
247 static void __store_testUnicode (const sal_Char *pszFilename)
249 // ...
250 rtl_TextToUnicodeConverter hConvert;
251 hConvert = rtl_createTextToUnicodeConverter (RTL_TEXTENCODING_UTF8);
253 MyFindData it;
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)
261 rtl_zeroMemory (
262 &it.m_pszName[n], ((it.m_nLength - n) * sizeof(sal_Unicode)));
263 it.m_nLength = n;
265 rtl_destroyTextToUnicodeConverter (hConvert);
267 // ...
268 rtl_String *pszFileA = NULL;
269 rtl_uString *pszFileW = NULL;
271 // rtl_uString_newFromAscii (&pszFileW, pszFilename);
273 // ...
274 rtl_string_newFromStr (&pszFileA, pszFilename);
276 rtl_string2UString (
277 &pszFileW,
278 pszFileA->buffer, pszFileA->length,
279 RTL_TEXTENCODING_MS_1252,
280 OSTRING_TO_OUSTRING_CVTFLAGS);
282 rtl_string_release (pszFileA);
284 // ...
285 OStorePageKey aKey;
286 __store_namei (pszFileW->buffer, pszFileW->buffer, aKey);
288 // ...
289 rtl_uString2String (
290 &pszFileA,
291 pszFileW->buffer, pszFileW->length,
292 RTL_TEXTENCODING_UTF8,
293 OUSTRING_TO_OSTRING_CVTFLAGS);
295 rtl_uString_release (pszFileW);
297 // ...
298 rtl_string_release (pszFileA);
301 /*========================================================================
303 * main.
305 *======================================================================*/
306 int SAL_CALL main (int argc, char **argv)
308 OSL_PRECOND(argc > 1, "t_base: error: insufficient number of arguments.");
309 if (argc < 2)
310 return 0;
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)
323 return eErrCode;
326 rtl::Reference<OTestObject> xObject (new OTestObject());
327 __store_test_handle (&*xObject);
329 rtl::Reference<OTestBIOS> xBIOS (new OTestBIOS());
330 __store_test_handle (&*xBIOS);
333 if (!xBIOS.is())
334 return 0;
336 sal_uInt16 nPageSize = TEST_PAGESIZE;
337 eErrCode = xBIOS->initialize (&*xLockBytes, store_AccessReadWrite, nPageSize);
338 if (eErrCode != store_E_None)
340 // Check reason.
341 if (eErrCode != store_E_NotExists)
342 return eErrCode;
344 // Create.
345 eErrCode = xBIOS->initialize (&*xLockBytes, store_AccessReadCreate, nPageSize);
346 if (eErrCode != store_E_None)
347 return eErrCode;
349 xLockBytes.clear();
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)
357 return eErrCode;
359 xBIOS.clear();
360 return 0;
363 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */