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