Skip to content
This repository
tree: fb205a2986
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 159 lines (114 sloc) 4.044 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
// Copyright (c) 2010-2011 Zipline Games, Inc. All Rights Reserved.
// http://getmoai.com

#include "pch.h"
#include <moaicore/MOAIStreamWriter.h>

//================================================================//
// lua
//================================================================//

//----------------------------------------------------------------//
/** @name close
@text Flush any remaining buffered data and detach the target stream.
(This only detatches the target from the formatter; it does
not also close the target stream).
@in MOAIStreamWriter self
@out nil
*/
int MOAIStreamWriter::_close ( lua_State* L ) {
MOAI_LUA_SETUP ( MOAIStreamWriter, "U" );

self->Close ();
return 0;
}

//----------------------------------------------------------------//
/** @name openBase64
@text Open a base 64 formatted stream for writing (i.e. encode
bytes to base64).
@in MOAIStreamWriter self
@in MOAIStream target
@out boolean success
*/
int MOAIStreamWriter::_openBase64 ( lua_State* L ) {
MOAI_LUA_SETUP ( MOAIStreamWriter, "U" );

self->Close ();

MOAIStream* stream = state.GetLuaObject < MOAIStream >( 2, true );
if ( !stream ) return 0;

USBase64Writer* writer = new USBase64Writer ();

bool result = self->Open ( stream, writer );

state.Push ( result );
return 1;
}

//----------------------------------------------------------------//
/** @name openDeflate
@text Open a 'deflate' formatted stream for writing (i.e. compress
bytes using the 'deflate' algorithm).
@in MOAIStreamWriter self
@in MOAIStream target
@out boolean success
*/
int MOAIStreamWriter::_openDeflate ( lua_State* L ) {
MOAI_LUA_SETUP ( MOAIStreamWriter, "U" );

self->Close ();

MOAIStream* stream = state.GetLuaObject < MOAIStream >( 2, true );
if ( !stream ) return 0;

int level = state.GetValue < int >( 3, USDeflateWriter::DEFAULT_LEVEL );
int windowBits = state.GetValue < int >( 4, USDeflateWriter::DEFAULT_WBITS );

USDeflateWriter* writer = new USDeflateWriter ();

writer->SetCompressionLevel ( level );
writer->SetWindowBits ( windowBits );

bool result = self->Open ( stream, writer );

state.Push ( result );
return 1;
}

//================================================================//
// MOAIStreamWriter
//================================================================//

//----------------------------------------------------------------//
void MOAIStreamWriter::Close () {

if ( this->mWriter ) {
delete this->mWriter;
this->mWriter = 0;
}

this->SetUSStream ( 0 );
this->mStream.Set ( *this, 0 );
}

//----------------------------------------------------------------//
MOAIStreamWriter::MOAIStreamWriter () :
mWriter ( 0 ) {

RTTI_BEGIN
RTTI_EXTEND ( MOAIStream )
RTTI_END
}

//----------------------------------------------------------------//
MOAIStreamWriter::~MOAIStreamWriter () {

this->Close ();
}

//----------------------------------------------------------------//
bool MOAIStreamWriter::Open ( MOAIStream* stream, USStreamWriter* writer ) {

this->Close ();
this->mWriter = writer;

if ( !stream ) return false;

USStream* usStream = stream->GetUSStream ();
if ( usStream ) {

this->mStream.Set ( *this, stream );

this->mWriter->Open ( *usStream );
this->SetUSStream ( this->mWriter );

return true;
}
return false;
}

//----------------------------------------------------------------//
void MOAIStreamWriter::RegisterLuaClass ( MOAILuaState& state ) {

MOAIStream::RegisterLuaClass ( state );

state.SetField ( -1, "DEFAULT_LEVEL", USDeflateWriter::DEFAULT_LEVEL );
state.SetField ( -1, "DEFAULT_WBITS", USDeflateWriter::DEFAULT_WBITS );
}

//----------------------------------------------------------------//
void MOAIStreamWriter::RegisterLuaFuncs ( MOAILuaState& state ) {

MOAIStream::RegisterLuaFuncs ( state );

luaL_Reg regTable [] = {
{ "close", _close },
{ "openBase64", _openBase64 },
{ "openDeflate", _openDeflate },
{ NULL, NULL }
};

luaL_register ( state, 0, regTable );
}

Something went wrong with that request. Please try again.