merge the formfield patch from ooo-build
[ooovba.git] / store / workben / t_file.cxx
blobb70654040c0475ffdde33c74b37201d5a41706ff
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_file.cxx,v $
10 * $Revision: 1.8 $
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 #include "sal/types.h"
35 #include "osl/thread.h"
36 #include "rtl/ustring.hxx"
38 #include "lockbyte.hxx"
40 #ifndef INCLUDED_STDIO_H
41 #include <stdio.h>
42 #define INCLUDED_STDIO_H
43 #endif
45 #include "osl/file.h"
46 #include "osl/process.h"
48 using namespace store;
50 #define TEST_PAGESIZE 16384
52 /*========================================================================
54 * main.
56 *======================================================================*/
57 int SAL_CALL main (int argc, char **argv)
59 storeError eErrCode = store_E_None;
60 rtl::Reference<ILockBytes> xLockBytes;
62 if (argc > 1)
64 rtl::OUString aFilename (
65 argv[1], rtl_str_getLength(argv[1]),
66 osl_getThreadTextEncoding());
68 #if 0 /* EXP */
69 oslFileError result;
70 rtl::OUString aPath;
72 result = osl_getFileURLFromSystemPath(aFilename.pData, &(aPath.pData));
73 if (result != osl_File_E_None)
75 // not SystemPath, assume FileUrl.
76 aPath = aFilename;
78 if (rtl_ustr_ascii_shortenedCompare_WithLength(aPath.pData->buffer, aPath.pData->length, "file://", 7) != 0)
80 // not FileUrl, assume relative path.
81 rtl::OUString aBase;
82 (void) osl_getProcessWorkingDir (&(aBase.pData));
84 // absolute FileUrl.
85 (void) osl_getAbsoluteFileURL(aBase.pData, aPath.pData, &(aPath.pData));
87 aFilename = aPath;
88 #endif /* EXP */
90 eErrCode = FileLockBytes_createInstance (
91 xLockBytes, aFilename.pData, store_AccessReadWrite);
92 if (eErrCode != store_E_None)
94 // Check reason.
95 if (eErrCode != store_E_NotExists)
97 fprintf (stderr, "t_file: create() error: %d\n", eErrCode);
98 return eErrCode;
101 // Create.
102 eErrCode = FileLockBytes_createInstance (
103 xLockBytes, aFilename.pData, store_AccessReadCreate);
104 if (eErrCode != store_E_None)
106 fprintf (stderr, "t_file: create() error: %d\n", eErrCode);
107 return eErrCode;
110 fprintf (stdout, "t_file: using FileLockBytes(\"%s\") implementation.\n", argv[1]);
112 else
114 eErrCode = MemoryLockBytes_createInstance (xLockBytes);
115 if (eErrCode != store_E_None)
117 fprintf (stderr, "t_file: create() error: %d\n", eErrCode);
118 return eErrCode;
120 fprintf (stdout, "t_file: using MemoryLockBytes implementation.\n");
123 rtl::Reference< PageData::Allocator > xAllocator;
124 eErrCode = xLockBytes->initialize (xAllocator, TEST_PAGESIZE);
125 if (eErrCode != store_E_None)
127 fprintf (stderr, "t_file: initialize() error: %d\n", eErrCode);
128 return eErrCode;
131 sal_Char buffer[TEST_PAGESIZE];
132 rtl_fillMemory (buffer, sizeof(buffer), sal_uInt8('B'));
134 sal_uInt32 i, k;
135 for (k = 0; k < 4; k++)
137 sal_uInt32 index = k * TEST_PAGESIZE / 4;
138 buffer[index] = 'A';
141 for (i = 0; i < 256; i++)
143 sal_uInt32 offset = i * TEST_PAGESIZE;
144 eErrCode = xLockBytes->setSize (offset + TEST_PAGESIZE);
145 if (eErrCode != store_E_None)
147 fprintf (stderr, "t_file: setSize() error: %d\n", eErrCode);
148 return eErrCode;
151 for (k = 0; k < 4; k++)
153 sal_uInt32 magic = i * 4 + k;
154 if (magic)
156 sal_uInt32 verify = 0;
157 eErrCode = xLockBytes->readAt (
158 0, &verify, sizeof(verify));
159 if (eErrCode != store_E_None)
161 fprintf (stderr, "t_file: readAt() error: %d\n", eErrCode);
162 return eErrCode;
164 if (verify != magic)
166 // Failure.
167 fprintf (stderr, "Expected %ld read %ld\n", (unsigned long)(magic), (unsigned long)(verify));
171 sal_uInt32 index = k * TEST_PAGESIZE / 4;
172 eErrCode = xLockBytes->writeAt (
173 offset + index, &(buffer[index]), TEST_PAGESIZE / 4);
174 if (eErrCode != store_E_None)
176 fprintf (stderr, "t_file: writeAt() error: %d\n", eErrCode);
177 return eErrCode;
180 magic += 1;
181 eErrCode = xLockBytes->writeAt (
182 0, &magic, sizeof(magic));
183 if (eErrCode != store_E_None)
185 fprintf (stderr, "t_file: writeAt() error: %d\n", eErrCode);
186 return eErrCode;
191 eErrCode = xLockBytes->flush();
192 if (eErrCode != store_E_None)
194 fprintf (stderr, "t_file: flush() error: %d\n", eErrCode);
195 return eErrCode;
198 sal_Char verify[TEST_PAGESIZE];
199 for (i = 0; i < 256; i++)
201 sal_uInt32 offset = i * TEST_PAGESIZE;
203 eErrCode = xLockBytes->readAt (offset, verify, TEST_PAGESIZE);
204 if (eErrCode != store_E_None)
206 fprintf (stderr, "t_file: readAt() error: %d\n", eErrCode);
207 return eErrCode;
210 sal_uInt32 index = 0;
211 if (offset == 0)
213 sal_uInt32 magic = 256 * 4;
214 if (rtl_compareMemory (&verify[index], &magic, sizeof(magic)))
216 // Failure.
217 fprintf (stderr, "t_file: Unexpected value at 0x00000000\n");
219 index += 4;
221 if (rtl_compareMemory (
222 &verify[index], &buffer[index], TEST_PAGESIZE - index))
224 // Failure.
225 fprintf (stderr, "t_file: Unexpected block at 0x%08x\n", (unsigned)(offset));
229 for (i = 0; i < 256; i++)
231 PageHolder xPage;
232 sal_uInt32 offset = i * TEST_PAGESIZE;
234 eErrCode = xLockBytes->readPageAt (xPage, offset);
235 if (eErrCode != store_E_None)
237 fprintf (stderr, "t_file: readPageAt() error: %d\n", eErrCode);
238 return eErrCode;
241 PageData * page = xPage.get();
242 (void)page; // UNUSED
245 xLockBytes.clear();
246 return 0;