xref: /aoo41x/main/store/workben/t_base.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_store.hxx"
30 
31 #include "sal/types.h"
32 #include "osl/diagnose.h"
33 #include "osl/thread.h"
34 #include "rtl/memory.h"
35 #include "rtl/ustring.hxx"
36 
37 #include "object.hxx"
38 #include "storbase.hxx"
39 #include "storbios.hxx"
40 #include "lockbyte.hxx"
41 
42 using namespace store;
43 
44 #define TEST_PAGESIZE 1024
45 
46 /*========================================================================
47  *
48  * OTestObject.
49  *
50  *======================================================================*/
51 class OTestObject : public store::OStoreObject
52 {
53 public:
54 	OTestObject (void);
55 
56 	virtual sal_Bool SAL_CALL isKindOf (sal_uInt32 nTypeId);
57 
58 protected:
59 	virtual ~OTestObject (void);
60 };
61 
62 OTestObject::OTestObject (void)
63 {
64 }
65 
66 OTestObject::~OTestObject (void)
67 {
68 }
69 
70 sal_Bool SAL_CALL OTestObject::isKindOf (sal_uInt32 nTypeId)
71 {
72 	return (nTypeId == 42);
73 }
74 
75 namespace store
76 {
77 static OTestObject* SAL_CALL query (IStoreHandle *pHandle, OTestObject*)
78 {
79 	if (pHandle && pHandle->isKindOf (42))
80 		return static_cast<OTestObject*>(pHandle);
81 	else
82 		return 0;
83 }
84 }
85 
86 /*========================================================================
87  *
88  * OTestBIOS.
89  *
90  *======================================================================*/
91 namespace store
92 {
93 
94 class OTestBIOS : public store::OStorePageBIOS
95 {
96 	typedef store::OStorePageBIOS base;
97 
98 	friend OTestBIOS* SAL_CALL query<> (IStoreHandle * pHandle, OTestBIOS *);
99 
100 public:
101 	OTestBIOS (void);
102 
103 	virtual storeError initialize (
104 		ILockBytes *    pLockBytes,
105 		storeAccessMode eAccessMode,
106 		sal_uInt16 &    rnPageSize);
107 
108 	virtual sal_Bool SAL_CALL isKindOf (sal_uInt32 nTypeId);
109 
110 protected:
111 	virtual ~OTestBIOS (void);
112 };
113 
114 } // namespace store
115 
116 OTestBIOS::OTestBIOS (void)
117 {
118 }
119 
120 OTestBIOS::~OTestBIOS (void)
121 {
122 }
123 
124 sal_Bool SAL_CALL OTestBIOS::isKindOf (sal_uInt32 nTypeId)
125 {
126 	return (nTypeId == 4242);
127 }
128 
129 storeError OTestBIOS::initialize (
130 	ILockBytes *pLockBytes, storeAccessMode eAccessMode, sal_uInt16 & rnPageSize)
131 {
132 	return base::initialize (pLockBytes, eAccessMode, rnPageSize);
133 }
134 
135 namespace store
136 {
137 template<> OTestBIOS* SAL_CALL query (IStoreHandle *pHandle, OTestBIOS*)
138 {
139 	if (pHandle && pHandle->isKindOf (4242))
140 		return static_cast<OTestBIOS*>(pHandle);
141 	else
142 		return 0;
143 }
144 }
145 
146 /*========================================================================
147  *
148  * __store_test_handle.
149  *
150  *======================================================================*/
151 static void __store_test_handle (void* Handle)
152 {
153 	IStoreHandle *pHandle = static_cast<IStoreHandle*>(Handle);
154 	if (pHandle)
155 	{
156 		pHandle->acquire();
157 		pHandle->isKindOf (42);
158 		pHandle->release();
159 	}
160 
161 	OTestObject *pObj = query (pHandle, static_cast<OTestObject*>(0));
162 	if (pObj)
163 	{
164 		pObj->acquire();
165 		pObj->isKindOf (42);
166 		pObj->release();
167 	}
168 }
169 
170 /*========================================================================
171  *
172  * unicode.
173  *
174  *======================================================================*/
175 static void __store_string_newFromUnicode_WithLength (
176 	rtl_String **newString, const sal_Unicode *value, sal_Int32 length)
177 {
178 	rtl_uString2String (
179 		newString,
180 		value, length,
181 		RTL_TEXTENCODING_UTF8,
182 		OUSTRING_TO_OSTRING_CVTFLAGS);
183 }
184 
185 #if 0  /* UNSUSED */
186 static void __store_string_newFromUnicode (
187 	rtl_String **newString, const rtl_uString *value)
188 {
189 	__store_string_newFromUnicode_WithLength (
190 		newString, value->buffer, value->length);
191 }
192 #endif /* UNUSED */
193 
194 static void __store_string_newFromUnicode (
195 	rtl_String **newString, const sal_Unicode *value)
196 {
197 	__store_string_newFromUnicode_WithLength (
198 		newString, value, rtl_ustr_getLength (value));
199 }
200 
201 static storeError __store_namei (
202 	const sal_Unicode *pszPath,
203 	const sal_Unicode *pszName,
204 	OStorePageKey     &rKey)
205 {
206 	rtl::OString aName (
207 		pszName, rtl_ustr_getLength (pszName), RTL_TEXTENCODING_UTF8);
208 
209 	rtl_String *pszNameA = 0;
210 	__store_string_newFromUnicode (&pszNameA, pszName);
211 
212 	storeError eErrCode = store_E_NameTooLong;
213 	if (pszNameA->length < sal_Int32(sizeof(sal_Char[STORE_MAXIMUM_NAMESIZE])))
214 	{
215 		rtl_String *pszPathA = 0;
216 		__store_string_newFromUnicode (&pszPathA, pszPath);
217 
218 		rKey.m_nLow  = rtl_crc32 (0, pszNameA->buffer, pszNameA->length);
219 		rKey.m_nHigh = rtl_crc32 (0, pszPathA->buffer, pszPathA->length);
220 
221 		rtl_string_release (pszPathA);
222 		eErrCode = store_E_None;
223 	}
224 
225 	rtl_string_release (pszNameA);
226 	return eErrCode;
227 }
228 
229 static sal_Size __store_convertTextToUnicode (
230 	rtl_TextToUnicodeConverter hConvert,
231 	const sal_Char *pszText, sal_Size nTextLen,
232 	sal_Unicode    *pBuffer, sal_Size nBuffer)
233 {
234 	sal_uInt32 nInfo = 0;
235 	sal_Size   nSrcLen = 0;
236 
237 	sal_Int32 nDstLen = rtl_convertTextToUnicode (
238 		hConvert, 0,
239 		pszText, nTextLen,
240 		pBuffer, nBuffer,
241 		OSTRING_TO_OUSTRING_CVTFLAGS,
242 		&nInfo, &nSrcLen);
243 
244 	pBuffer[nDstLen] = 0;
245 	return nDstLen;
246 }
247 
248 struct MyFindData
249 {
250 	sal_Unicode m_pszName[STORE_MAXIMUM_NAMESIZE];
251 	sal_Int32   m_nLength;
252 	sal_uInt32  m_nAttrib;
253 	sal_uInt32  m_nSize;
254 	sal_uInt32  m_nReserved;
255 };
256 
257 static void __store_testUnicode (const sal_Char *pszFilename)
258 {
259 	// ...
260 	rtl_TextToUnicodeConverter hConvert;
261 	hConvert = rtl_createTextToUnicodeConverter (RTL_TEXTENCODING_UTF8);
262 
263 	MyFindData it;
264 	rtl_zeroMemory (&it, sizeof(it));
265 
266 	sal_Int32 n = rtl_str_getLength (pszFilename);
267 	n = __store_convertTextToUnicode (
268 		hConvert, pszFilename, n,
269 		it.m_pszName, STORE_MAXIMUM_NAMESIZE - 1);
270 	if (it.m_nLength > n)
271 		rtl_zeroMemory (
272 			&it.m_pszName[n], ((it.m_nLength - n) * sizeof(sal_Unicode)));
273 	it.m_nLength = n;
274 
275 	rtl_destroyTextToUnicodeConverter (hConvert);
276 
277 	// ...
278 	rtl_String  *pszFileA = NULL;
279 	rtl_uString *pszFileW = NULL;
280 
281 	// rtl_uString_newFromAscii (&pszFileW, pszFilename);
282 
283 	// ...
284 	rtl_string_newFromStr (&pszFileA, pszFilename);
285 
286 	rtl_string2UString (
287 		&pszFileW,
288 		pszFileA->buffer, pszFileA->length,
289 		RTL_TEXTENCODING_MS_1252,
290 		OSTRING_TO_OUSTRING_CVTFLAGS);
291 
292 	rtl_string_release (pszFileA);
293 
294 	// ...
295 	OStorePageKey aKey;
296 	__store_namei (pszFileW->buffer, pszFileW->buffer, aKey);
297 
298 	// ...
299 	rtl_uString2String (
300 		&pszFileA,
301 		pszFileW->buffer, pszFileW->length,
302 		RTL_TEXTENCODING_UTF8,
303 		OUSTRING_TO_OSTRING_CVTFLAGS);
304 
305 	rtl_uString_release (pszFileW);
306 
307 	// ...
308 	rtl_string_release (pszFileA);
309 }
310 
311 /*========================================================================
312  *
313  * main.
314  *
315  *======================================================================*/
316 int SAL_CALL main (int argc, char **argv)
317 {
318 	OSL_PRECOND(argc > 1, "t_base: error: insufficient number of arguments.");
319 	if (argc < 2)
320 		return 0;
321 
322 	__store_testUnicode (argv[1]);
323 
324 	rtl::Reference<ILockBytes> xLockBytes;
325 
326 	rtl::OUString aFilename (
327 		argv[1], rtl_str_getLength(argv[1]),
328 		osl_getThreadTextEncoding());
329 
330 	storeError eErrCode = FileLockBytes_createInstance (
331 		xLockBytes, aFilename.pData, store_AccessReadCreate);
332 	if (eErrCode != store_E_None)
333 		return eErrCode;
334 
335 
336 	rtl::Reference<OTestObject> xObject (new OTestObject());
337 	__store_test_handle (&*xObject);
338 
339 	rtl::Reference<OTestBIOS> xBIOS (new OTestBIOS());
340 	__store_test_handle (&*xBIOS);
341 
342 
343 	if (!xBIOS.is())
344 		return 0;
345 
346 	sal_uInt16 nPageSize = TEST_PAGESIZE;
347 	eErrCode = xBIOS->initialize (&*xLockBytes, store_AccessReadWrite, nPageSize);
348 	if (eErrCode != store_E_None)
349 	{
350 		// Check reason.
351 		if (eErrCode != store_E_NotExists)
352 			return eErrCode;
353 
354 		// Create.
355 		eErrCode = xBIOS->initialize (&*xLockBytes, store_AccessReadCreate, nPageSize);
356 		if (eErrCode != store_E_None)
357 			return eErrCode;
358 	}
359 	xLockBytes.clear();
360 
361 	sal_Char pBuffer[TEST_PAGESIZE];
362 	rtl_zeroMemory (pBuffer, sizeof (pBuffer));
363 	rtl_copyMemory (pBuffer, argv[0], rtl_str_getLength(argv[0]) + 1);
364 
365 	eErrCode = xBIOS->write (TEST_PAGESIZE, pBuffer, sizeof (pBuffer));
366 	if (eErrCode != store_E_None)
367 		return eErrCode;
368 
369 	xBIOS.clear();
370 	return 0;
371 }
372