Bug 415080: Add Testcase (r=brbaker)
[tamarin-stm.git] / shell / FileClass.cpp
blobadaf31e037229550770bb6ab4f6a4b05bb9b5a1b
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 MMgc::GC::AllocaAutoPtr _c;
101 union {
102 uint8_t* c;
103 wchar* c_w;
105 c = (uint8_t*)VMPI_alloca(core, _c, len+1);
107 len = (int)fp->read(c, len); //need to force since the string creation functions expect an int
108 c[len] = 0;
110 fp->close();
111 Platform::GetInstance()->destroyFile(fp);
113 if (len >= 3)
115 // UTF8 BOM
116 if ((c[0] == 0xef) && (c[1] == 0xbb) && (c[2] == 0xbf))
118 return core->newStringUTF8((const char*)c + 3, len - 3);
120 else if ((c[0] == 0xfe) && (c[1] == 0xff))
122 //UTF-16 big endian
123 c += 2;
124 len = (len - 2) >> 1;
125 return core->newStringEndianUTF16(/*littleEndian*/false, c_w, len);
127 else if ((c[0] == 0xff) && (c[1] == 0xfe))
129 //UTF-16 little endian
130 c += 2;
131 len = (len - 2) >> 1;
132 return core->newStringEndianUTF16(/*littleEndian*/true, c_w, len);
136 // Use newStringUTF8() with "strict" explicitly set to false to mimick old,
137 // buggy behavior, where malformed UTF-8 sequences are stored as single characters.
138 return core->newStringUTF8((const char*)c, len, false);
141 void FileClass::write(Stringp filename,
142 Stringp data)
144 Toplevel* toplevel = this->toplevel();
146 if (!filename) {
147 toplevel->throwArgumentError(kNullArgumentError, "filename");
149 if (!data) {
150 toplevel->throwArgumentError(kNullArgumentError, "data");
152 StUTF8String filenameUTF8(filename);
153 File* fp = Platform::GetInstance()->createFile();
154 if (!fp || !fp->open(filenameUTF8.c_str(), File::OPEN_WRITE))
156 if(fp)
158 Platform::GetInstance()->destroyFile(fp);
160 toplevel->throwError(kFileWriteError, filename);
162 StUTF8String dataUTF8(data);
163 if ((int32_t)fp->write(dataUTF8.c_str(), dataUTF8.length()) != dataUTF8.length()) {
164 toplevel->throwError(kFileWriteError, filename);
166 fp->close();
167 Platform::GetInstance()->destroyFile(fp);