/*!\file StringExternalResult.c
 * \brief: implementation of the StringExternalResult object
 */

/*header files: */
/*{{{1*/
#ifdef HAVE_CONFIG_H
	#include <config.h>
#else
#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
#endif

#include <stdio.h>
#include <string.h>
#include "../objects.h"
#include "../../EnumDefinitions/EnumDefinitions.h"
#include "../../shared/shared.h"
#include "../../Container/Container.h"
#include "../../include/include.h"
/*}}}*/

/*StringExternalResult constructors and destructor*/
/*FUNCTION StringExternalResult::StringExternalResult(){{{1*/
StringExternalResult::StringExternalResult(){
	return;
}
/*}}}*/
/*FUNCTION StringExternalResult::StringExternalResult(int enum_type,IssmString value){{{1*/
StringExternalResult::StringExternalResult(int in_id, int in_enum_type,char* in_value,int in_step, double in_time){

	id=in_id;
	enum_type=in_enum_type;
	value=(char*)xmalloc((strlen(in_value)+1)*sizeof(char));
	memcpy(value,in_value,(strlen(in_value)+1)*sizeof(char));

	step=in_step;
	time=in_time;
	
}
/*}}}*/
/*FUNCTION StringExternalResult::~StringExternalResult(){{{1*/
StringExternalResult::~StringExternalResult(){
	xfree((void**)&value);
}
/*}}}*/

/*Object virtual functions definitions:*/
/*FUNCTION StringExternalResult::Echo {{{1*/
void StringExternalResult::Echo(void){
	this->DeepEcho();
}
/*}}}*/
/*FUNCTION StringExternalResult::DeepEcho{{{1*/
void StringExternalResult::DeepEcho(void){

	printf("StringExternalResult:\n");
	printf("   id: %i\n",this->id);
	printf("   enum:  %i (%s)\n",this->enum_type,EnumToStringx(this->enum_type));
	printf("   value: %s\n",this->value);
	printf("   step: %i\n",this->step);
	printf("   time: %g\n",this->time);
}
/*}}}*/
/*FUNCTION StringExternalResult::Id{{{1*/
int    StringExternalResult::Id(void){ return -1; }
/*}}}*/
/*FUNCTION StringExternalResult::MyRank{{{1*/
int    StringExternalResult::MyRank(void){ 
	extern int my_rank;
	return my_rank; 
}
/*}}}*/
/*FUNCTION StringExternalResult::ObjectEnum{{{1*/
int StringExternalResult::ObjectEnum(void){

	return StringExternalResultEnum;

}
/*}}}*/
/*FUNCTION StringExternalResult::copy{{{1*/
Object* StringExternalResult::copy() {
	
	return new StringExternalResult(this->id,this->enum_type,this->value,this->step,this->time);

}
/*}}}*/

/*StringExternalResult management: */
/*FUNCTION StringExternalResult::WriteData{{{1*/
void   StringExternalResult::WriteData(FILE* fid,bool io_gather){

	int     length;
	int     type;
	char   *name      = NULL;
	extern  int my_rank;

	/*return if now on cpu 0: */
	if(my_rank)return;

	/*First write enum: */
	EnumToStringx(&name,this->enum_type);
	length=(strlen(name)+1)*sizeof(char);
	fwrite(&length,sizeof(int),1,fid);
	fwrite(name,length,1,fid);
	xfree((void**)&name);

	/*Now write time and step: */
	fwrite(&time,sizeof(double),1,fid);
	fwrite(&step,sizeof(int),1,fid);

	/*writing a string, type is 2: */
	type=2;
	fwrite(&type,sizeof(int),1,fid);
	
	length=(strlen(this->value)+1)*sizeof(char);
	fwrite(&length,sizeof(int),1,fid);
	fwrite(this->value,length,1,fid);

}
/*}}}1*/
/*FUNCTION StringExternalResult::GetResultName{{{1*/
void StringExternalResult::GetResultName(char**pname){
	EnumToStringx(pname,this->enum_type);
}
/*}}}*/
/*FUNCTION StringExternalResult::GetStep{{{1*/
int StringExternalResult::GetStep(void){

	return this->step;
}
/*}}}*/
