#include <stream.h>
#include <fcntl.h>
#include "ObjProComGui/cgidbg.h"
#include "ObjProDSP/sysconst.h"
#include "ObjProDSP/sysintfc.h"
#include "ObjProGui/yacintfc.h"
#include "ObjProDSPcom/binfile.h"
#include "ObjProNet/dfnode.h"

#include "ObjProUsr/outword.h"
#include "ObjProGen/outtok.h"
#include "ObjProGui/intfc.h"
#include "ObjProGui/remmen.h"
#include "ObjProGui/user.h"
#include "ObjProGui/dynmnu.h"
#include "ObjProArith/typout.h"
#include "ObjProGui/array.h"
#include "ObjProGui/interinit.h"
#include "ObjProGen/stattyp.h"
static EntityList * OutputWordNodeList = 0 ;
static InteractiveEntity * IntEntOutputWord ;
void OutputWordNodesInit();
OutputWord::OutputWord (const char * Name, const char * FileName, int16 FormatOut):
	DisplayNodeStr(Name, 1, 1, 
	 new StreamStr(SizeVariable , SizeVariable , ArithType::ArithCapabilityAny), TimingTypeLinear)
,
	FileName_1(FileName),
	FormatOut_2(FormatOut)
{
	if (!OutputWordNodeList) OutputWordNodesInit() ;
	OutputWordNodeList->Append(MakeDeclaredEntity(this, IntEntOutputWord)) ;
	InitArithType(TheArithType);
	NewMenuItem("OutputWord",GetName());
#line 59 "../outword.usr"
 
	ctor();
#line 38 "../outword.cxx"
} // end constructor

ErrCode OutputWord::EmitStaticCtorParameters(OutTokens& Out)
{
	ostream& Outs = *(Out.GetStream());
	Outs << ",\n\tOutputWordStaticInitinteger_read_flag_" << GetName() << "";
	return OK;
}

ErrCode OutputWord::EmitStaticInit(OutTokens& Out)
{
	ostream& Outs = *(Out.GetStream());
	Outs << "static int OutputWordStaticInitinteger_read_flag_" << GetName() << " = ";	
	OutStringStaticValue(Outs,"int", &(integer_read_flag)) ;

	Outs << ";\n";
	return OK;
}

OutputWord::~OutputWord()
{
	TheMenuServer->DeleteMenuItem("OutputWord",GetName());
	OutputWordNodeList->Delete(GetName()) ;
#line 63 "../outword.usr"
 
	dtor();
#line 65 "../outword.cxx"
} // end destructor

int OutputWord::CheckSafeDelete()
{
	int Safe_Check_Return = DfNode::CheckSafeDelete();
	if (!Safe_Check_Return) return 0;
	return 1;
} // end check safe delete

double OutputWord::TimeFirst( DfNodeInLink * , DfNodeOutLink * )
{
#line 90 "../outword.usr"
    return 0.0;
    // timing is irrelevant for a node with no output channels
#line 80 "../outword.cxx"
} // end timing code

OutputWord * OutputWordDef;

ErrCode OutputWord::DoNode(int32 k)
{
#line 94 "../outword.usr"
 
	return kernel(k);
#line 90 "../outword.cxx"
} // end kernel code

static UserEntity * MakeOutputWord(OutTokens& Out, EntityReq Request,
	InteractiveEntity& IntNode,
	ArithType::ArithCapabilities arith = (ArithType::ArithCapabilities) TheArithType) ;
int OutputWord::CppList(OutTokens& Out, CppListCmds Cmd)
{
	return IntEntOutputWord->CppList(Out,Cmd,this);
}

void OutputWord::Describe(OutTokens& Out, ListEntity Option)
{
	switch(Option) {
case ListSingleEntity:
		Out.NewLine();
		MakeOutputWord(Out,EntityReqDescribeFull,*IntEntOutputWord, (ArithType::ArithCapabilities) TheArithType);
		Out.NewLine();
		Out.NextQuoteOut("FileName");
		Out.NextQuoteOut(GetFileName());
		Out.NextFillOut("is the file");
		Out.NextFillOut("to be created. If no default(0) is given");
		Out.NextFillOut("the node name will be used.");
		Out.NextQuoteOut("FormatOut");
		Out.NextFillOut("(");
		Out.NextFillOut(TypeToString(GetFormatOut()));
		Out.NextFillOut(")");
		Out.NextFillOut("is the binary output format.");
		Out.NextFillOut("The options are:");
		Out.NextFillOut("`MachWord'");
		Out.NextFillOutConcat("(0),");
		Out.NextFillOut("`int8'");
		Out.NextFillOutConcat("(1),");
		Out.NextFillOut("`int16'");
		Out.NextFillOutConcat("(2),");
		Out.NextFillOut("`int32'");
		Out.NextFillOutConcat("(3),");
		Out.NextFillOut("`float'");
		Out.NextFillOutConcat("(4),");
		Out.NextFillOut("`double'");
		Out.NextFillOutConcat("(5). Integer words are written in two's compliment");
		Out.NextFillOut("format. Integer input values (");
		Out.NextFillOutConcat("`IntegerMachWord'");
		Out.NextFillOutConcat(") are treated as signed.");
		Out.NextFillOut("If overflow occurs the data is hard limited and a warning");
		Out.NextFillOut("is given. The input stream can have any value for sample size,");
		Out.NextFillOut("(");
		Out.NextFillOutConcat("`ElementSize'");
		Out.NextFillOutConcat(")");
		Out.NewLine();
		break;
case ListEntityMembers:
		Out.NextOut(GetName());
		break;
case ListGlobalClasses:
case ListEntityClasses:
		break ;
case ListSetParameterValues:
		IntEntOutputWord->GetOneParameter("FileName")->
			StringP->CurrentValue = FileName_1;
		IntEntOutputWord->GetOneParameter("FormatOut")->
			IntP->CurrentValue = FormatOut_2;
		break;
	}
} // end  list entity switch

void OutputWordNodesInit()
{
	if (OutputWordNodeList)  return ;

	static StringParam OutputWordNameParam =
		{"OutputWord", MakeNewEntityName, 0, LegalEntityName};
	static StringParam OutputWordFileNameParam = {
		 0, 0, 0};
	static IntParam OutputWordFormatOutParam = {
		 0, 0,  0,  0,  0,  5};

	static OneParameter OutputWordParArray[] = {
		{"Name", 0, "node name", 0, 0, &OutputWordNameParam},
		{"FileName", 0, 
			"file to create",
			0, 0, &OutputWordFileNameParam},
		{"FormatOut", 0, 
			"format: `MachWord'(0), `int8'(1), `int16'(2), `int32'(3), `float'(4), `double'(5)",
			&OutputWordFormatOutParam},
		{0}
	};

	OutputWordNodeList = new EntityList;
	IntEntOutputWord = new InteractiveEntity("OutputWord", OutputWordNodeList,
		MakeOutputWord, InteractiveNode, "outword.h",
		0, "DisplayNodeStr");
	IntEntOutputWord->SetParameters(new UserParameters(OutputWordParArray));
	TheNodes->Append(IntEntOutputWord);
} // end initalization

static UserEntity * MakeOutputWord(OutTokens& Out, EntityReq Request,
	InteractiveEntity& IntNode,
	ArithType::ArithCapabilities arith)
{
	switch(Request) {
case EntityReqDescribe:

case EntityReqDescribeFull:
		Out.NextQuoteOut("OutputWord");
		Out.NextFillOut("writes words to a binary file. It is intended");
		Out.NextFillOut("for exporting data to other processes.");
		Out.NextFillOut("`OutputNode'");
		Out.NextFillOut("is better");
		Out.NextFillOut("for preserving data for later use in ObjectProDSP because");
		Out.NextFillOut("`Outputnode'");
		Out.NextFillOut("retains sample rate, timing, block size and sample size information.");
		Out.NextFillOut("Each input word is written in a format determined by");
		Out.NextQuoteOut("FormatOut");
		Out.NextFillOutConcat(".");
		Out.NextFillOut("The options are:");
		Out.NextFillOut("`MachWord'");
		Out.NextFillOutConcat("(0),");
		Out.NextFillOut("`int8'");
		Out.NextFillOutConcat("(1),");
		Out.NextFillOut("`int16'");
		Out.NextFillOutConcat("(2),");
		Out.NextFillOut("`int32'");
		Out.NextFillOutConcat("(3),");
		Out.NextFillOut("`float'");
		Out.NextFillOutConcat("(4),");
		Out.NextFillOut("`double'");
		Out.NextFillOutConcat("(5). Integer words are written in two's compliment");
		Out.NextFillOut("format. Integer input values (");
		Out.NextFillOutConcat("`IntegerMachWord'");
		Out.NextFillOutConcat(") are treated as signed.");
		Out.NextFillOut("If overflow occurs the data is hard limited and a warning");
		Out.NextFillOut("is given. The input stream can have any value for sample size,");
		Out.NextFillOut("(");
		Out.NextFillOutConcat("`ElementSize'");
		Out.NextFillOutConcat(") or");
		Out.NextFillOut("`BlockSize'");
		Out.NextFillOut("but these are not preserved in");
		Out.NextFillOut("the output file as they are with");
		Out.NextFillOut("`OutputNode'");
		Out.NextFillOutConcat(".");
		Out.NewLine();
		break;

case EntityReqCreate:
	{
		const char * Name = IntNode.GetStringParameterValue("Name");
		const char * FileName =
			IntNode.GetStringParameterValue("FileName");
		int16 FormatOut =
			IntNode.GetIntParameterValue("FormatOut");
		return new OutputWord(Name, FileName, FormatOut);

	}
	}
	return 0;
}

static InitObj LocalInit(OutputWordNodesInit, "OutputWord", "DisplayNodeStr");

