bump product version to 4.1.6.2
[LibreOffice.git] / store / workben / t_base.cxx
blob99320b7052a63bf031600b6c21b5d7eee40daca3
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
21 #include <string.h>
22 #include "sal/types.h"
23 #include "osl/diagnose.h"
24 #include "osl/thread.h"
25 #include "rtl/ustring.hxx"
27 #include "object.hxx"
28 #include "storbase.hxx"
29 #include "storbios.hxx"
30 #include "lockbyte.hxx"
32 using namespace store;
34 #define TEST_PAGESIZE 1024
36 /*========================================================================
38 * OTestObject.
40 *======================================================================*/
41 class OTestObject : public store::OStoreObject
43 public:
44 OTestObject (void);
46 virtual sal_Bool SAL_CALL isKindOf (sal_uInt32 nTypeId);
48 protected:
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);
65 namespace store
67 static OTestObject* SAL_CALL query (IStoreHandle *pHandle, OTestObject*)
69 if (pHandle && pHandle->isKindOf (42))
70 return static_cast<OTestObject*>(pHandle);
71 else
72 return 0;
76 /*========================================================================
78 * OTestBIOS.
80 *======================================================================*/
81 namespace store
84 class OTestBIOS : public store::OStorePageBIOS
86 typedef store::OStorePageBIOS base;
88 friend OTestBIOS* SAL_CALL query<> (IStoreHandle * pHandle, OTestBIOS *);
90 public:
91 OTestBIOS (void);
93 virtual storeError initialize (
94 ILockBytes * pLockBytes,
95 storeAccessMode eAccessMode,
96 sal_uInt16 & rnPageSize);
98 virtual sal_Bool SAL_CALL isKindOf (sal_uInt32 nTypeId);
100 protected:
101 virtual ~OTestBIOS (void);
104 } // namespace store
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);
125 namespace store
127 template<> OTestBIOS* SAL_CALL query (IStoreHandle *pHandle, OTestBIOS*)
129 if (pHandle && pHandle->isKindOf (4242))
130 return static_cast<OTestBIOS*>(pHandle);
131 else
132 return 0;
136 /*========================================================================
138 * __store_test_handle.
140 *======================================================================*/
141 static void __store_test_handle (void* Handle)
143 IStoreHandle *pHandle = static_cast<IStoreHandle*>(Handle);
144 if (pHandle)
146 pHandle->acquire();
147 pHandle->isKindOf (42);
148 pHandle->release();
151 OTestObject *pObj = query (pHandle, static_cast<OTestObject*>(0));
152 if (pObj)
154 pObj->acquire();
155 pObj->isKindOf (42);
156 pObj->release();
160 /*========================================================================
162 * unicode.
164 *======================================================================*/
165 static void __store_string_newFromUnicode_WithLength (
166 rtl_String **newString, const sal_Unicode *value, sal_Int32 length)
168 rtl_uString2String (
169 newString,
170 value, 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,
185 OStorePageKey &rKey)
187 OString aName (
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);
207 return eErrCode;
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 (
219 hConvert, 0,
220 pszText, nTextLen,
221 pBuffer, nBuffer,
222 OSTRING_TO_OUSTRING_CVTFLAGS,
223 &nInfo, &nSrcLen);
225 pBuffer[nDstLen] = 0;
226 return nDstLen;
229 struct MyFindData
231 sal_Unicode m_pszName[STORE_MAXIMUM_NAMESIZE];
232 sal_Int32 m_nLength;
233 sal_uInt32 m_nAttrib;
234 sal_uInt32 m_nSize;
235 sal_uInt32 m_nReserved;
238 static void __store_testUnicode (const sal_Char *pszFilename)
240 // ...
241 rtl_TextToUnicodeConverter hConvert;
242 hConvert = rtl_createTextToUnicodeConverter (RTL_TEXTENCODING_UTF8);
244 MyFindData it;
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)
252 memset (
253 &it.m_pszName[n], 0, ((it.m_nLength - n) * sizeof(sal_Unicode)));
254 it.m_nLength = n;
256 rtl_destroyTextToUnicodeConverter (hConvert);
258 // ...
259 rtl_String *pszFileA = NULL;
260 rtl_uString *pszFileW = NULL;
262 // rtl_uString_newFromAscii (&pszFileW, pszFilename);
264 // ...
265 rtl_string_newFromStr (&pszFileA, pszFilename);
267 rtl_string2UString (
268 &pszFileW,
269 pszFileA->buffer, pszFileA->length,
270 RTL_TEXTENCODING_MS_1252,
271 OSTRING_TO_OUSTRING_CVTFLAGS);
273 rtl_string_release (pszFileA);
275 // ...
276 OStorePageKey aKey;
277 __store_namei (pszFileW->buffer, pszFileW->buffer, aKey);
279 // ...
280 rtl_uString2String (
281 &pszFileA,
282 pszFileW->buffer, pszFileW->length,
283 RTL_TEXTENCODING_UTF8,
284 OUSTRING_TO_OSTRING_CVTFLAGS);
286 rtl_uString_release (pszFileW);
288 // ...
289 rtl_string_release (pszFileA);
292 /*========================================================================
294 * main.
296 *======================================================================*/
297 int SAL_CALL main (int argc, char **argv)
299 OSL_PRECOND(argc > 1, "t_base: error: insufficient number of arguments.");
300 if (argc < 2)
301 return 0;
303 __store_testUnicode (argv[1]);
305 rtl::Reference<ILockBytes> xLockBytes;
307 OUString aFilename (
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)
314 return eErrCode;
317 rtl::Reference<OTestObject> xObject (new OTestObject());
318 __store_test_handle (&*xObject);
320 rtl::Reference<OTestBIOS> xBIOS (new OTestBIOS());
321 __store_test_handle (&*xBIOS);
324 if (!xBIOS.is())
325 return 0;
327 sal_uInt16 nPageSize = TEST_PAGESIZE;
328 eErrCode = xBIOS->initialize (&*xLockBytes, store_AccessReadWrite, nPageSize);
329 if (eErrCode != store_E_None)
331 // Check reason.
332 if (eErrCode != store_E_NotExists)
333 return eErrCode;
335 // Create.
336 eErrCode = xBIOS->initialize (&*xLockBytes, store_AccessReadCreate, nPageSize);
337 if (eErrCode != store_E_None)
338 return eErrCode;
340 xLockBytes.clear();
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)
348 return eErrCode;
350 xBIOS.clear();
351 return 0;
354 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */