Bug 591908: Remove Math.floor from microbenchmark iterations/sec calculation (r=fklockii)
[tamarin-stm.git] / shell / FileClass.cpp
blobd23a21503bb589e480bf594c04a6049ef3090cfb
1 /* -*- Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */
2 /* vi: set ts=4 sw=4 expandtab: (add to ~/.vimrc: set modeline modelines=5) */
3 /* ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is [Open Source Virtual Machine.].
18 * The Initial Developer of the Original Code is
19 * Adobe System Incorporated.
20 * Portions created by the Initial Developer are Copyright (C) 2004-2006
21 * the Initial Developer. All Rights Reserved.
23 * Contributor(s):
24 * Adobe AS3 Team
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
41 #include "avmshell.h"
43 namespace avmshell
45 FileClass::FileClass(VTable *cvtable)
46 : ClassClosure(cvtable)
48 createVanillaPrototype();
51 bool FileClass::exists(Stringp filename)
53 if (!filename) {
54 toplevel()->throwArgumentError(kNullArgumentError, "filename");
57 bool result = false;
59 StUTF8String filenameUTF8(filename);
60 File* fp = Platform::GetInstance()->createFile();
61 if(fp)
63 if (fp->open(filenameUTF8.c_str(), File::OPEN_READ)) {
64 result = true;
65 fp->close();
67 Platform::GetInstance()->destroyFile(fp);
69 return result;
72 Stringp FileClass::read(Stringp filename)
74 Toplevel* toplevel = this->toplevel();
75 AvmCore* core = this->core();
77 if (!filename) {
78 toplevel->throwArgumentError(kNullArgumentError, "filename");
80 StUTF8String filenameUTF8(filename);
81 File* fp = Platform::GetInstance()->createFile();
82 if(!fp || !fp->open(filenameUTF8.c_str(), File::OPEN_READ))
84 if(fp)
86 Platform::GetInstance()->destroyFile(fp);
89 toplevel->throwError(kFileOpenError, filename);
92 int64_t fileSize = fp->size();
93 if(fileSize >= (int64_t)INT32_T_MAX) //File APIs cannot handle files > 2GB
95 toplevel->throwRangeError(kOutOfRangeError, filename);
98 int len = (int)fileSize;
100 // Avoid VMPI_alloca - the buffer can be large and the memory is non-pointer-containing,
101 // but the GC will scan it conservatively.
102 uint8_t* c = (uint8_t*)core->gc->Alloc(len+1);
104 len = (int)fp->read(c, len); //need to force since the string creation functions expect an int
105 c[len] = 0;
107 fp->close();
108 Platform::GetInstance()->destroyFile(fp);
110 Stringp ret = NULL;
112 if (len >= 3)
114 // UTF8 BOM
115 if ((c[0] == 0xef) && (c[1] == 0xbb) && (c[2] == 0xbf))
117 ret = core->newStringUTF8((const char*)c + 3, len - 3);
119 else if ((c[0] == 0xfe) && (c[1] == 0xff))
121 //UTF-16 big endian
122 c += 2;
123 len = (len - 2) >> 1;
124 ret = core->newStringEndianUTF16(/*littleEndian*/false, (wchar*)(void*)c, len);
126 else if ((c[0] == 0xff) && (c[1] == 0xfe))
128 //UTF-16 little endian
129 c += 2;
130 len = (len - 2) >> 1;
131 ret = core->newStringEndianUTF16(/*littleEndian*/true, (wchar*)(void*)c, len);
135 if (ret == NULL)
137 // Use newStringUTF8() with "strict" explicitly set to false to mimick old,
138 // buggy behavior, where malformed UTF-8 sequences are stored as single characters.
139 ret = core->newStringUTF8((const char*)c, len, false);
142 core->gc->Free(c);
143 return ret;
146 void FileClass::write(Stringp filename,
147 Stringp data)
149 Toplevel* toplevel = this->toplevel();
151 if (!filename) {
152 toplevel->throwArgumentError(kNullArgumentError, "filename");
154 if (!data) {
155 toplevel->throwArgumentError(kNullArgumentError, "data");
157 StUTF8String filenameUTF8(filename);
158 File* fp = Platform::GetInstance()->createFile();
159 if (!fp || !fp->open(filenameUTF8.c_str(), File::OPEN_WRITE))
161 if(fp)
163 Platform::GetInstance()->destroyFile(fp);
165 toplevel->throwError(kFileWriteError, filename);
167 StUTF8String dataUTF8(data);
168 if ((int32_t)fp->write(dataUTF8.c_str(), dataUTF8.length()) != dataUTF8.length()) {
169 toplevel->throwError(kFileWriteError, filename);
171 fp->close();
172 Platform::GetInstance()->destroyFile(fp);
175 ByteArrayObject* FileClass::readByteArray(Stringp filename)
177 Toplevel* toplevel = this->toplevel();
178 if (!filename) {
179 toplevel->throwArgumentError(kNullArgumentError, "filename");
181 StUTF8String filenameUTF8(filename);
183 File* fp = Platform::GetInstance()->createFile();
184 if (fp == NULL || !fp->open(filenameUTF8.c_str(), File::OPEN_READ_BINARY))
186 if(fp)
188 Platform::GetInstance()->destroyFile(fp);
190 toplevel->throwError(kFileOpenError, filename);
193 int64_t len = fp->size();
194 if((uint64_t)len >= UINT32_T_MAX) //ByteArray APIs cannot handle files > 4GB
196 toplevel->throwRangeError(kOutOfRangeError, filename);
199 uint32_t readCount = (uint32_t)len;
201 unsigned char *c = mmfx_new_array( unsigned char, readCount+1);
203 ByteArrayObject* b = toplevel->byteArrayClass()->constructByteArray();
204 b->set_length(0);
206 while (readCount > 0)
208 uint32_t actual = (uint32_t) fp->read(c, readCount);
209 if (actual > 0)
211 b->GetByteArray().Write(c, actual);
212 readCount -= readCount;
214 else
216 break;
219 b->set_position(0);
221 mmfx_delete_array( c );
223 fp->close();
224 Platform::GetInstance()->destroyFile(fp);
226 return b;
229 bool FileClass::writeByteArray(Stringp filename, ByteArrayObject* bytes)
231 Toplevel* toplevel = this->toplevel();
232 if (!filename) {
233 toplevel->throwArgumentError(kNullArgumentError, "filename");
236 StUTF8String filenameUTF8(filename);
238 File* fp = Platform::GetInstance()->createFile();
239 if (fp == NULL || !fp->open(filenameUTF8.c_str(), File::OPEN_WRITE_BINARY))
241 if(fp)
243 Platform::GetInstance()->destroyFile(fp);
245 toplevel->throwError(kFileWriteError, filename);
248 int32_t len = bytes->get_length();
249 bool success = (int32_t)fp->write(&(bytes->GetByteArray())[0], len) == len;
251 fp->close();
252 Platform::GetInstance()->destroyFile(fp);
254 if (!success) {
255 toplevel->throwError(kFileWriteError, filename);
258 return true;