#include "precompiled.h" /* * wxJavaScript - dostream.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: dostream.cpp 598 2007-03-07 20:13:28Z fbraem $ */ #include #ifndef WX_PRECOMP #include #endif #include "../common/main.h" #include "constant.h" #include "stream.h" #include "ostream.h" #include "dostream.h" using namespace wxjs; using namespace wxjs::io; /*** * dostream * io * * This class provides functions that write binary data types in a portable way. * Data can be written in either big-endian or little-endian format, little-endian * being the default on all architectures. * If you want to write data to text files (or streams) use @wxTextOutputStream instead. *

* Remark :This class is not thoroughly tested. If you find problems let * it know on the project forum. *
*/ WXJS_INIT_CLASS(DataOutputStream, "wxDataOutputStream", 1) /*** * * * An output stream * * * Constructs a new wxDataOutputStream object. * * */ wxDataOutputStream* DataOutputStream::Construct(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, bool constructing) { Stream *out = OutputStream::GetPrivate(cx, argv[0]); if ( out == NULL ) return NULL; // This is needed, because otherwise the stream can be garbage collected. // Another method could be to root the stream, but how are we going to unroot it? JS_DefineProperty(cx, obj, "__stream__", argv[0], NULL, NULL, JSPROP_READONLY); return new wxDataOutputStream(*dynamic_cast(out->GetStream())); } WXJS_BEGIN_METHOD_MAP(DataOutputStream) WXJS_METHOD("bigEndianOrdered", bigEndianOrdered, 1) WXJS_METHOD("write64", write64, 1) WXJS_METHOD("write32", write32, 1) WXJS_METHOD("write16", write16, 1) WXJS_METHOD("write8", write8, 1) WXJS_METHOD("writeString", writeString, 1) WXJS_METHOD("writeDouble", writeDouble, 1) WXJS_END_METHOD_MAP() /*** * * * * * * If Ordered is true, all data will be read in big-endian order, * such as written by programs on a big endian architecture (e.g. Sparc) * or written by Java-Streams (which always use big-endian order). * * */ JSBool DataOutputStream::bigEndianOrdered(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { wxDataOutputStream *p = GetPrivate(cx, obj); if ( p == NULL ) return JS_FALSE; bool ordered; if ( FromJS(cx, argv[0], ordered) ) { p->BigEndianOrdered(ordered); return JS_TRUE; } return JS_FALSE; } /*** * * * * * * Writes a 64 bit integer to the stream. * * */ JSBool DataOutputStream::write64(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { wxDataOutputStream *p = GetPrivate(cx, obj); if ( p == NULL ) return JS_FALSE; // Check for other platforms !!! #ifdef __WXMSW__ long value; if ( FromJS(cx, argv[0], value) ) { p->Write64((wxUint64) value); return JS_TRUE; } #endif return JS_FALSE; } /*** * * * * * * Writes a 32 bit integer to the stream. * * */ JSBool DataOutputStream::write32(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { wxDataOutputStream *p = GetPrivate(cx, obj); if ( p == NULL ) return JS_FALSE; // Check for other platforms !!! #ifdef __WXMSW__ int value; if ( FromJS(cx, argv[0], value) ) { p->Write32(value); return JS_TRUE; } #endif return JS_FALSE; } /*** * * * * * * Writes a 16 bit integer to the stream. * * */ JSBool DataOutputStream::write16(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { wxDataOutputStream *p = GetPrivate(cx, obj); if ( p == NULL ) return JS_FALSE; // TODO: Check for other platforms !!! #ifdef __WXMSW__ int value; if ( FromJS(cx, argv[0], value) ) { p->Write16(value); return JS_TRUE; } #endif return JS_FALSE; } /*** * * * * * * Writes a 8 bit integer to the stream. * * */ JSBool DataOutputStream::write8(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { wxDataOutputStream *p = GetPrivate(cx, obj); if ( p == NULL ) return JS_FALSE; // TODO: Check for other platforms !!! #ifdef __WXMSW__ int value; if ( FromJS(cx, argv[0], value) ) { p->Write8(value); return JS_TRUE; } #endif return JS_FALSE; } /*** * * * * * * Writes a double (IEEE encoded) to a stream. * * */ JSBool DataOutputStream::writeDouble(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { wxDataOutputStream *p = GetPrivate(cx, obj); if ( p == NULL ) return JS_FALSE; double value; if ( FromJS(cx, argv[0], value) ) { p->WriteDouble(value); return JS_TRUE; } return JS_FALSE; } /*** * * * * * * Writes string as a line. Depending on the end-of-line mode the end of line * ('\n') characters in the string are converted to the correct line * ending terminator. * * */ JSBool DataOutputStream::writeString(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { wxDataOutputStream *p = GetPrivate(cx, obj); if ( p == NULL ) return JS_FALSE; wxString value; FromJS(cx, argv[0], value); p->WriteString(value); return JS_TRUE; }