#include "precompiled.h" /* * wxJavaScript - ffile.cpp * * Copyright (c) 2002-2007 Franky Braem and the wxJavaScript project * * Project Info: http://www.wxjavascript.net or http://wxjs.sourceforge.net * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, * USA. * * $Id: ffile.cpp 810 2007-07-13 20:07:05Z fbraem $ */ // file.cpp #include #include "../common/main.h" #include "../common/apiwrap.h" #include "../common/type.h" #include "../ext/wxjs_ext.h" #include "../ext/jsmembuf.h" #include "ffile.h" using namespace wxjs; using namespace wxjs::io; /*** * ffile * io * * A wxFFile performs raw file I/O. * This is a very small class designed to minimize the overhead of using it - * in fact, there is hardly any overhead at all, but using it brings you * automatic error checking and hides differences between platforms. * It wraps inside it a FILE * handle used by standard C IO library (also known as stdio). *

Remark : * A FILE* structure can't be used directly in JavaScript. That's why some methods are not * ported. *
*/ WXJS_INIT_CLASS(FFile, "wxFFile", 0) /*** * * * * * * * * * * * * * */ WXJS_BEGIN_CONSTANT_MAP(FFile) WXJS_CONSTANT(wxFILE_, KIND_UNKNOWN) WXJS_CONSTANT(wxFILE_, KIND_DISK) WXJS_CONSTANT(wxFILE_, KIND_TERMINAL) WXJS_CONSTANT(wxFILE_, KIND_PIPE) WXJS_CONSTANT(wx, FromCurrent) WXJS_CONSTANT(wx, FromStart) WXJS_CONSTANT(wx, FromEnd) WXJS_END_CONSTANT_MAP() /*** * * * True when end of file is reached. When the file is not open, undefined is returned. * * * Returns the type of the file * * * True when the file is open. * * * Returns the length of the file. When the file is not open, undefined is returned. * * * Returns the current position or -1 (invalid offset). * When the file is not open, undefined is returned. * * */ WXJS_BEGIN_PROPERTY_MAP(FFile) WXJS_READONLY_PROPERTY(P_EOF, "eof") WXJS_READONLY_PROPERTY(P_KIND, "kind") WXJS_READONLY_PROPERTY(P_OPENED, "opened") WXJS_READONLY_PROPERTY(P_LENGTH, "length") WXJS_READONLY_PROPERTY(P_TELL, "tell") WXJS_END_PROPERTY_MAP() bool FFile::GetProperty(wxFFile *p, JSContext *cx, JSObject *obj, int id, jsval *vp) { switch(id) { case P_EOF: if ( p->IsOpened() ) *vp = ToJS(cx, p->Eof()); else *vp = JSVAL_VOID; break; case P_KIND: *vp = ToJS(cx, (int) p->GetKind()); break; case P_OPENED: *vp = ToJS(cx, p->IsOpened()); break; case P_LENGTH: if ( p->IsOpened() ) *vp = ToJS(cx, (int) p->Length()); else *vp = JSVAL_VOID; break; case P_TELL: if ( p->IsOpened() ) *vp = ToJS(cx, (int) p->Tell()); else *vp = JSVAL_VOID; break; } return true; } /*** * * * Filename * * The mode in which to open the file * * * * Creates a new wxFFile object * * */ wxFFile *FFile::Construct(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, bool constructing) { if ( argc > 2 ) argc = 2; if ( argc == 0 ) return new wxFFile(); wxString fileName; wxString mode = wxT("r"); if ( argc == 2 ) FromJS(cx, argv[1], mode); FromJS(cx, argv[0], fileName); return new wxFFile(fileName, mode); } WXJS_BEGIN_METHOD_MAP(FFile) WXJS_METHOD("close", close, 0) WXJS_METHOD("flush", flush, 0) WXJS_METHOD("open", open, 2) WXJS_METHOD("read", read, 1) WXJS_METHOD("readAll", readAll, 0) WXJS_METHOD("seek", seek, 2) WXJS_METHOD("seekEnd", seekEnd, 1) WXJS_METHOD("write", write, 1) WXJS_END_METHOD_MAP() /*** * * * * Closes the file. * * */ JSBool FFile::close(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { wxFFile *p = GetPrivate(cx, obj); wxASSERT_MSG(p != NULL, wxT("No private data associated with wxFFile")); if ( p->IsOpened() ) *rval = ToJS(cx, p->Close()); else *rval = JSVAL_FALSE; return JS_TRUE; } /*** * * * * Flushes the file. * * */ JSBool FFile::flush(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { wxFFile *p = GetPrivate(cx, obj); wxASSERT_MSG(p != NULL, wxT("No private data associated with wxFFile")); if ( p->IsOpened() ) *rval = ToJS(cx, p->Flush()); else *rval = JSVAL_FALSE; return JS_TRUE; } /*** * * * Name of the file * The mode in which to open the file * * * Opens a file. * * */ JSBool FFile::open(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { wxFFile *p = GetPrivate(cx, obj); if ( p == NULL ) return JS_FALSE; wxString fileName; wxString mode = wxT("r"); if ( argc == 2 ) FromJS(cx, argv[1], mode); FromJS(cx, argv[0], fileName); *rval = ToJS(cx, p->Open(fileName, mode)); return JS_TRUE; } /*** * * * The number of bytes to read. * * * Reads the specified number of bytes. * * */ JSBool FFile::read(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { wxFFile *p = GetPrivate(cx, obj); wxASSERT_MSG(p != NULL, wxT("No private data associated with wxFFile")); if ( ! p->IsOpened() ) { return JS_FALSE; } int count; if ( FromJS(cx, argv[0], count) && count > 0 ) { unsigned char *buffer = new unsigned char[count]; int readCount = p->Read(buffer, count); if ( readCount == wxInvalidOffset ) { *rval = JSVAL_VOID; } else { *rval = wxjs::ext::CreateMemoryBuffer(cx, buffer, count); } delete[] buffer; return JS_TRUE; } return JS_FALSE; } /*** * * * * The encoding of the file. * * * * Reads all the data and return it in a String. Internally JavaScript * stores the Strings as UTF-16. But because most files are stored * in UTF-8 or ASCII, the default of the encoding is UTF-8. * * */ JSBool FFile::readAll(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { wxFFile *p = GetPrivate(cx, obj); if ( p == NULL ) return JS_FALSE; if ( ! p->IsOpened() ) { return JS_FALSE; } wxString encoding(wxJS_EXTERNAL_ENCODING); if ( argc > 0 ) { FromJS(cx, argv[0], encoding); } wxCSConv conv(encoding); wxString data; if ( p->ReadAll(&data, conv) ) { *rval = ToJS(cx, data); return JS_TRUE; } return JS_FALSE; } /*** * * * Offset to seek to * * * * Seeks the offset. Returns the actual position or -1 on error. * * */ JSBool FFile::seek(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { wxFFile *p = GetPrivate(cx, obj); wxASSERT_MSG(p != NULL, wxT("No private data associated with wxFFile")); if ( ! p->IsOpened() ) { return JS_FALSE; } int offset; if ( ! FromJS(cx, argv[0], offset) ) { return JS_FALSE; } int pos; if ( argc > 1 ) { int mode; if ( FromJS(cx, argv[1], mode) ) { pos = p->Seek((wxFileOffset) offset, (wxSeekMode) mode); } else { return JS_FALSE; } } else { pos = p->Seek((wxFileOffset) offset); } *rval = ToJS(cx, pos); return JS_TRUE; } /*** * * * Offset to seek to. * * * Moves the file pointer to the specified number of bytes before the end of the file. * Returns true on success. * * */ JSBool FFile::seekEnd(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { wxFFile *p = GetPrivate(cx, obj); wxASSERT_MSG(p != NULL, wxT("No private data associated with wxFFile")); if ( ! p->IsOpened() ) { return JS_FALSE; } int offset; if ( FromJS(cx, argv[0], offset) ) { *rval = ToJS(cx, p->SeekEnd((wxFileOffset) offset)); return JS_TRUE; } return JS_FALSE; } /*** * * * * * * * * * * Writes the string or buffer to the file. When you write * a String you can specify an encoding. Because most files * are still written as UTF-8 or ASCII, UTF-8 is the default * (wxJS uses UTF-16 internally). * Returns the actual number of bytes written to the file when * a buffer is used. Otherwise a boolean indicating * success or failure is returned. * * */ JSBool FFile::write(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { wxFFile *p = GetPrivate(cx, obj); wxASSERT_MSG(p != NULL, wxT("No private data associated with wxFFile")); if ( ! p->IsOpened() ) { return JS_FALSE; } if ( JSVAL_IS_OBJECT(argv[0]) ) { wxMemoryBuffer* buffer = wxjs::ext::GetMemoryBuffer(cx, JSVAL_TO_OBJECT(argv[0])); if ( buffer != NULL ) { *rval = ToJS(cx, p->Write(buffer->GetData(), buffer->GetDataLen())); } } else { wxString encoding(wxJS_EXTERNAL_ENCODING); if ( argc > 1 ) { FromJS(cx, argv[1], encoding); } wxCSConv conv(encoding); wxString content; FromJS(cx, argv[0], content); *rval = ToJS(cx, (int) p->Write(content, conv)); } return JS_TRUE; }