merge the formfield patch from ooo-build
[ooovba.git] / store / workben / t_store.cxx
blobffeb9272be27bd1760cc67cbdc7f890e29336523
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_store.cxx,v $
10 * $Revision: 1.7 $
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 #define _T_STORE_CXX "$Revision: 1.7.8.2 $"
35 #include <sal/types.h>
36 #include <osl/diagnose.h>
37 #include <osl/thread.h>
38 #include <osl/time.h>
39 #include <rtl/ustring.hxx>
40 #include <store/store.hxx>
42 #include <stdio.h>
44 #if (defined(WNT) && defined(PROFILE))
45 extern "C"
47 void StartCAP (void);
48 void StopCAP (void);
49 void DumpCAP (void);
51 #endif /* PROFILE */
53 using rtl::OUString;
55 /*========================================================================
57 * Internals.
59 *======================================================================*/
60 #define _DEMOSTOR_BUFSIZ 512 /* 4096, 1024, 512 */
61 #define _DEMOSTOR_LOOPS 1000 /* 1000, 2000 */
63 #define _DEMOSTOR_REMOVE 0
64 #define _DEMOSTOR_REBUILD 0
66 enum Options
68 OPTION_HELP = 0x0001,
69 OPTION_FILE = 0x0002,
70 OPTION_PAUSE = 0x0004,
71 OPTION_REBUILD = 0x0008,
73 OPTION_DUMP = 0x0010,
74 OPTION_ITER = 0x0020,
75 OPTION_LINK = 0x0040,
77 OPTION_READ = 0x0100,
78 OPTION_WRITE = 0x0200,
79 OPTION_CREAT = 0x0400,
80 OPTION_TRUNC = 0x0800
83 inline sal_Char ascii_toLowerCase (sal_Char ch)
85 if ((ch >= 0x41) && (ch <= 0x5A))
86 return (ch + 0x20);
87 else
88 return (ch);
91 /*========================================================================
93 * Timing.
95 *======================================================================*/
96 struct OTime : public TimeValue
98 OTime (void)
100 Seconds = 0;
101 Nanosec = 0;
104 static OTime getSystemTime (void)
106 OTime tv;
107 osl_getSystemTime (&tv);
108 return tv;
111 OTime& operator-= (const OTime& rPast)
113 Seconds -= rPast.Seconds;
114 if (Nanosec < rPast.Nanosec)
116 Seconds -= 1;
117 Nanosec += 1000000000;
119 Nanosec -= rPast.Nanosec;
120 return *this;
123 friend OTime operator- (const OTime& rTimeA, const OTime& rTimeB)
125 OTime aTimeC (rTimeA);
126 aTimeC -= rTimeB;
127 return aTimeC;
131 /*========================================================================
133 * DirectoryTraveller.
135 *======================================================================*/
136 typedef store::OStoreDirectory Directory;
138 class DirectoryTraveller : public Directory::traveller
140 typedef store::OStoreFile file;
141 typedef Directory::iterator iter;
143 store::OStoreFile m_aFile;
144 OUString m_aPath;
146 sal_uInt32 m_nOptions;
147 unsigned int m_nLevel;
148 unsigned int m_nCount;
150 public:
151 DirectoryTraveller (
152 const file& rFile,
153 const OUString &rPath,
154 const OUString &rName,
155 sal_uInt32 nOptions,
156 unsigned int nLevel = 0);
158 virtual ~DirectoryTraveller (void);
160 virtual sal_Bool visit (const iter& it);
164 * DirectoryTraveller.
166 DirectoryTraveller::DirectoryTraveller (
167 const file& rFile,
168 const OUString &rPath,
169 const OUString &rName,
170 sal_uInt32 nOptions,
171 unsigned int nLevel)
172 : m_aFile (rFile),
173 m_aPath (rPath),
174 m_nOptions (nOptions),
175 m_nLevel (nLevel),
176 m_nCount (0)
178 m_aPath += rName;
179 m_aPath += OUString::createFromAscii("/");
183 * ~DirectoryTraveller.
185 DirectoryTraveller::~DirectoryTraveller (void)
190 * visit.
192 sal_Bool DirectoryTraveller::visit (const iter& it)
194 m_nCount++;
195 if (m_nOptions & OPTION_DUMP)
197 rtl::OString aName (it.m_pszName, it.m_nLength, RTL_TEXTENCODING_UTF8);
198 printf ("Visit(%d,%d): %s [0x%08x] %d [Bytes]\n",
199 m_nLevel, m_nCount,
200 aName.pData->buffer, (unsigned int)(it.m_nAttrib), (unsigned int)(it.m_nSize));
202 if (it.m_nAttrib & STORE_ATTRIB_ISDIR)
204 OUString aName (it.m_pszName, it.m_nLength);
205 if (aName.compareToAscii ("XTextViewCursorSupplier") == 0)
207 m_nCount += 1 - 1;
209 Directory aSubDir;
211 storeError eErrCode = aSubDir.create (
212 m_aFile, m_aPath, aName, store_AccessReadOnly);
213 if (eErrCode == store_E_None)
215 sal_uInt32 nRefCount = 0;
216 m_aFile.getRefererCount (nRefCount);
218 DirectoryTraveller aSubTraveller (
219 m_aFile, m_aPath, aName, m_nOptions, m_nLevel + 1);
220 aSubDir.travel (aSubTraveller);
223 return sal_True;
226 /*========================================================================
228 * main.
230 *======================================================================*/
231 int SAL_CALL main (int argc, char **argv)
233 #if (defined(WNT) && defined(PROFILE))
234 StartCAP();
235 #else
236 OTime aMainStartTime (OTime::getSystemTime());
237 #endif /* PROFILE */
239 store::OStoreFile aFile;
240 storeError eErrCode = store_E_None;
242 sal_uInt32 nOptions = 0;
243 for (int i = 1; i < argc; i++)
245 const char *opt = argv[i];
246 if (opt[0] == '-')
248 switch (ascii_toLowerCase(sal_Char(opt[1])))
250 case 'f':
251 nOptions |= OPTION_FILE;
252 break;
254 case 'd':
255 nOptions |= OPTION_DUMP;
256 break;
257 case 'i':
258 nOptions |= OPTION_ITER;
259 break;
260 case 'l':
261 nOptions |= OPTION_LINK;
262 break;
264 case 'r':
265 nOptions |= OPTION_READ;
266 break;
267 case 'w':
268 nOptions |= OPTION_WRITE;
269 break;
270 case 'c':
271 nOptions |= OPTION_CREAT;
272 break;
273 case 't':
274 nOptions |= OPTION_TRUNC;
275 break;
277 case 'p':
278 nOptions |= OPTION_PAUSE;
279 break;
281 case 'h':
282 default:
283 nOptions |= OPTION_HELP;
284 break;
287 else
289 if (nOptions & OPTION_FILE)
291 OUString aName (
292 argv[i], rtl_str_getLength(argv[i]),
293 osl_getThreadTextEncoding());
294 if ((nOptions & OPTION_CREAT) && (nOptions & OPTION_TRUNC))
295 eErrCode = aFile.create (aName, store_AccessCreate);
296 else if (nOptions & OPTION_CREAT)
297 eErrCode = aFile.create (aName, store_AccessReadCreate);
298 else if (nOptions & OPTION_WRITE)
299 eErrCode = aFile.create (aName, store_AccessReadWrite);
300 else
301 eErrCode = aFile.create (aName, store_AccessReadOnly);
302 if (eErrCode != store_E_None)
304 printf ("Error: can't open file: %s\n", argv[i]);
305 exit (0);
311 if ((nOptions == 0) || (nOptions & OPTION_HELP))
313 printf ("Usage:\tt_store "
314 "[[-c] [-t] [-r] [-w]] [[-i] [-d] [-h]] "
315 "[-f filename]\n");
317 printf ("\nOptions:\n");
318 printf ("-c\tcreate\n");
319 printf ("-t\ttruncate\n");
320 printf ("-r\tread\n");
321 printf ("-w\twrite\n");
322 printf ("-i\titerate\n");
323 printf ("-d\tdump\n");
324 printf ("-h\thelp\n");
325 printf ("-f\tfilename\n");
327 printf ("\nExamples:");
328 printf ("\nt_store -c -w -f t_store.rdb\n");
329 printf ("\tCreate file 't_store.rdb',\n"
330 "\twrite fixed number (1000) of streams.\n");
331 printf ("\nt_store -c -i -d -f t_store.rdb\n");
332 printf ("\tOpen file 't_store.rdb', "
333 "create '/' directory,\n"
334 "\titerate directory tree, "
335 "dump directory info.\n");
337 exit (0);
340 if (!aFile.isValid())
342 eErrCode = aFile.createInMemory();
343 if (eErrCode != store_E_None)
345 printf ("Error: can't create memory file\n");
346 exit (0);
350 // Stream Read/Write.
351 OUString aPath (RTL_CONSTASCII_USTRINGPARAM("/"));
352 if ((nOptions & OPTION_READ) || (nOptions & OPTION_WRITE))
354 // Mode.
355 storeAccessMode eMode = store_AccessReadOnly;
356 if (nOptions & OPTION_WRITE)
357 eMode = store_AccessReadWrite;
358 if (nOptions & OPTION_CREAT)
359 eMode = store_AccessCreate;
361 // Buffer.
362 char pBuffer[_DEMOSTOR_BUFSIZ] = "Hello World";
363 pBuffer[_DEMOSTOR_BUFSIZ - 2] = 'B';
364 pBuffer[_DEMOSTOR_BUFSIZ - 1] = '\0';
366 // Load/Save.
367 #ifndef PROFILE
368 OTime aStartTime (OTime::getSystemTime());
369 #endif /* PROFILE */
371 for (int i = 0; i < _DEMOSTOR_LOOPS; i++)
373 OUString aName (RTL_CONSTASCII_USTRINGPARAM("demostor-"));
374 aName += OUString::valueOf ((sal_Int32)(i + 1), 10);
375 aName += OUString::createFromAscii (".dat");
377 #if (_DEMOSTOR_REMOVE == 1)
378 eErrCode = aFile.remove (aPath, aName);
379 if ((eErrCode != store_E_None ) &&
380 (eErrCode != store_E_NotExists) )
381 break;
382 #endif /* _REMOVE */
384 store::OStoreStream aStream;
385 eErrCode = aStream.create (aFile, aPath, aName, eMode);
386 if (eErrCode != store_E_None)
388 OSL_TRACE("OStoreStream(%d)::create(): error: %d", i, eErrCode);
389 break;
392 if (nOptions & OPTION_TRUNC)
394 eErrCode = aStream.setSize(0);
395 if (eErrCode != store_E_None)
397 OSL_TRACE("OStoreStream(%d)::setSize(0): error: %d", i, eErrCode);
398 break;
402 sal_uInt32 nDone = 0;
403 if (nOptions & OPTION_WRITE)
405 eErrCode = aStream.writeAt (
406 0, pBuffer, sizeof(pBuffer), nDone);
407 if (eErrCode != store_E_None)
409 OSL_TRACE("OStoreStream(%d)::writeAt(): error: %d", i, eErrCode);
410 break;
414 if (nOptions & OPTION_READ)
416 sal_uInt32 nOffset = 0;
417 for (;;)
419 eErrCode = aStream.readAt (
420 nOffset, pBuffer, sizeof(pBuffer), nDone);
421 if (eErrCode != store_E_None)
423 OSL_TRACE("OStoreStream(%d)::readAt(): error: %d", i, eErrCode);
424 break;
426 if (nDone == 0)
427 break;
428 nOffset += nDone;
432 aStream.close();
434 #ifndef PROFILE
435 if (((i + 1) % (_DEMOSTOR_LOOPS/10)) == 0)
437 OTime aDelta (OTime::getSystemTime() - aStartTime);
439 sal_uInt32 nDelta = aDelta.Seconds * 1000000;
440 nDelta += (aDelta.Nanosec / 1000);
442 printf ("%d: %12.4g[usec]\n", (i+1),
443 (double)(nDelta)/(double)(i+1));
445 #endif /* PROFILE */
448 #ifndef PROFILE
449 OTime aDelta (OTime::getSystemTime() - aStartTime);
451 sal_uInt32 nDelta = aDelta.Seconds * 1000000;
452 nDelta += (aDelta.Nanosec / 1000);
454 printf ("Total(rd,wr): %d[usec]\n", (unsigned int)(nDelta));
455 #endif /* PROFILE */
458 // Link/Rename.
459 if (nOptions & OPTION_LINK)
461 // Create symlink to (root) directory.
462 eErrCode = aFile.symlink (
463 aPath, OUString::createFromAscii("000000/"),
464 OUString(), aPath);
465 OSL_POSTCOND(
466 ((eErrCode == store_E_None ) ||
467 (eErrCode == store_E_AlreadyExists) ),
468 "t_store::main(): store_symlink() failed");
470 // Create symlink to file.
471 OUString aLinkName (RTL_CONSTASCII_USTRINGPARAM("demostor-1.lnk"));
473 eErrCode = aFile.symlink (
474 aPath, aLinkName,
475 aPath, OUString::createFromAscii("demostor-1.dat"));
476 OSL_POSTCOND(
477 ((eErrCode == store_E_None ) ||
478 (eErrCode == store_E_AlreadyExists) ),
479 "t_store::main(): store_symlink() failed");
480 if ((eErrCode == store_E_None ) ||
481 (eErrCode == store_E_AlreadyExists) )
483 OUString aShortcut (
484 RTL_CONSTASCII_USTRINGPARAM("Shortcut to demostor-1.dat"));
485 eErrCode = aFile.rename (
486 aPath, aLinkName,
487 aPath, aShortcut);
488 OSL_POSTCOND(
489 ((eErrCode == store_E_None ) ||
490 (eErrCode == store_E_AlreadyExists) ),
491 "t_store::main(): store_rename() failed");
494 // Create directory.
495 OUString aDirName (RTL_CONSTASCII_USTRINGPARAM("demostor-1.dir"));
496 store::OStoreDirectory aDir;
498 eErrCode = aDir.create (
499 aFile, aPath, aDirName, store_AccessReadCreate);
500 OSL_POSTCOND(
501 (eErrCode == store_E_None),
502 "t_store::main(): store_createDirectory() failed");
503 if (eErrCode == store_E_None)
505 #if 0 /* NYI */
506 // Rename directory.
507 eErrCode = aFile.rename (
508 aPath, "demostor-1.dir/",
509 aPath, "Renamed demostor-1.dir");
510 OSL_POSTCOND(
511 ((eErrCode == store_E_None ) ||
512 (eErrCode == store_E_AlreadyExists) ),
513 "t_store::main(): store_rename() failed");
515 eErrCode = aFile.rename (
516 aPath, "Renamed demostor-1.dir/",
517 aPath, "demostor-1.dir");
518 OSL_POSTCOND(
519 (eErrCode == store_E_None),
520 "t_store::main(): store_rename() failed");
521 #endif /* NYI */
525 // Directory iteration.
526 if (nOptions & OPTION_ITER)
528 #ifndef PROFILE
529 OTime aStartTime (OTime::getSystemTime());
530 #endif /* PROFILE */
531 OUString aEmpty;
533 // Root directory.
534 store::OStoreDirectory aRootDir;
535 if (nOptions & OPTION_LINK)
537 // Open symlink entry.
538 eErrCode = aRootDir.create (
539 aFile, aPath, OUString::createFromAscii("000000"),
540 store_AccessReadOnly);
542 else
544 // Open direct entry.
545 if (nOptions & OPTION_CREAT)
546 eErrCode = aRootDir.create (
547 aFile, aEmpty, aEmpty, store_AccessReadCreate);
548 else if (nOptions & OPTION_WRITE)
549 eErrCode = aRootDir.create (
550 aFile, aEmpty, aEmpty, store_AccessReadWrite);
551 else
552 eErrCode = aRootDir.create (
553 aFile, aEmpty, aEmpty, store_AccessReadOnly);
556 if (eErrCode == store_E_None)
558 // Traverse directory tree.
559 DirectoryTraveller aTraveller (
560 aFile, aEmpty, aEmpty, nOptions, 0);
561 aRootDir.travel (aTraveller);
563 else
565 // Failure.
566 printf ("Error: can't open directory: \"/\"\n");
569 #ifndef PROFILE
570 OTime aDelta (OTime::getSystemTime() - aStartTime);
572 sal_uInt32 nDelta = aDelta.Seconds * 1000000;
573 nDelta += (aDelta.Nanosec / 1000);
575 printf ("Total(iter): %d[usec]\n", (unsigned int)(nDelta));
576 #endif /* PROFILE */
579 if (nOptions & OPTION_PAUSE)
581 TimeValue tv;
582 tv.Seconds = 300;
583 tv.Nanosec = 0;
584 osl_waitThread (&tv);
587 // Size.
588 sal_uInt32 nSize = 0;
589 aFile.getSize (nSize);
591 // Done.
592 aFile.close();
594 #if (defined(WNT) && defined(PROFILE))
595 StopCAP();
596 DumpCAP();
597 #endif /* PROFILE */
598 #ifndef PROFILE
599 OTime aDelta (OTime::getSystemTime() - aMainStartTime);
601 sal_uInt32 nDelta = aDelta.Seconds * 1000000;
602 nDelta += (aDelta.Nanosec / 1000);
604 printf ("Total: %d[usec]\n", (unsigned int)(nDelta));
605 #endif /* PROFILE */
607 return 0;