/*!\file KML_GroundOverlay.cpp
 * \brief: implementation of the kml_groundoverlay object
 */

/*Headers:*/
/*{{{*/
#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 "../../../shared/shared.h"
#include "../../../io/io.h"
#include "../../../Container/Container.h"
#include "../../../include/include.h"
/*}}}*/

/*Constructors/destructor/copy*/
/*FUNCTION KML_GroundOverlay::KML_GroundOverlay(){{{*/
KML_GroundOverlay::KML_GroundOverlay(){

	altitude  = 0.;
	memcpy(altmode,"clampToGround",(strlen("clampToGround")+1)*sizeof(char));

	llbox     =NULL;

}
/*}}}*/
/*FUNCTION KML_GroundOverlay::~KML_GroundOverlay(){{{*/
KML_GroundOverlay::~KML_GroundOverlay(){

	if (llbox) {
		delete llbox;
		llbox     =NULL;
	}

}
/*}}}*/

/*Other*/
/*FUNCTION KML_GroundOverlay::Echo {{{*/
void  KML_GroundOverlay::Echo(){

	_printLine_("KML_GroundOverlay:");
	KML_Overlay::Echo();

	_printLine_("         altitude: " << altitude);
	_printLine_("          altmode: " << altmode);
	_printLine_("            llbox: " << llbox);
}
/*}}}*/
/*FUNCTION KML_GroundOverlay::DeepEcho {{{*/
void  KML_GroundOverlay::DeepEcho(){

	char  indent[81]="";

	KML_GroundOverlay::DeepEcho(indent);

	return;
}
/*}}}*/
/*FUNCTION KML_GroundOverlay::DeepEcho {{{*/
void  KML_GroundOverlay::DeepEcho(const char* indent){

	char  indent2[81];

	_printLine_(indent << "KML_GroundOverlay:");
	KML_Overlay::DeepEcho(indent);

	memcpy(indent2,indent,(strlen(indent)+1)*sizeof(char));
	strcat(indent2,"  ");

	_printLine_(indent<<"      altitude: " << altitude);
	_printLine_(indent<<"       altmode: " << altmode);
	if (llbox)
	 llbox->DeepEcho(indent2);
	else
	 _printLine_(indent<<"         llbox: " << llbox);
}
/*}}}*/
/*FUNCTION KML_GroundOverlay::Write {{{*/
void  KML_GroundOverlay::Write(FILE* filout,const char* indent){

	char  indent2[81];

	fprintf(filout,"%s<GroundOverlay",indent);
	WriteAttrib(filout," ");
	fprintf(filout,">\n");
	WriteCommnt(filout,indent);

	KML_Overlay::Write(filout,indent);

	memcpy(indent2,indent,(strlen(indent)+1)*sizeof(char));

	strcat(indent2,"  ");

	fprintf(filout,"%s  <altitude>%0.16g</altitude>\n",indent,altitude);
	fprintf(filout,"%s  <altitudeMode>%s</altitudeMode>\n",indent,altmode);
	if (llbox)
		llbox->Write(filout,indent2);

	fprintf(filout,"%s</GroundOverlay>\n",indent);

	return;
}
/*}}}*/
/*FUNCTION KML_GroundOverlay::Read {{{*/
void  KML_GroundOverlay::Read(FILE* fid,char* kstr){

	char*        kstri;
	int          ncom=0;
	char**       pcom=NULL;

/*  get object attributes and check for solo tag  */

	if (KMLFileTagAttrib(this,
						 kstr))
		return;

/*  loop over and process fields within opening and closing tags  */

	while (kstri=KMLFileToken(fid,
							  &ncom,&pcom)) {
		if      (!strncmp(kstri,"</GroundOverlay",15)) {
			xfree((void**)&kstri);
			break;
		}
		else if (!strncmp(kstri,"</",2))
		  {_error2_("KML_GroundOverlay::Read -- Unexpected closing tag " << kstri << ".\n");}
		else if (strncmp(kstri,"<",1))
		  {_error2_("KML_GroundOverlay::Read -- Unexpected field \"" << kstri << "\".\n");}

		else if (!strcmp(kstri,"<altitude>"))
			KMLFileTokenParse(&altitude  ,
							  kstri,
							  fid);
		else if (!strcmp(kstri,"<altitudeMode>"))
			KMLFileTokenParse( altmode   ,NULL,KML_GROUNDOVERLAY_ALTMODE_LENGTH,
							  kstri,
							  fid);
		else if (!strncmp(kstri,"<LatLonBox",10)) {
			llbox     =new KML_LatLonBox();
			llbox     ->Read(fid,kstri);
		}

		else if (!strncmp(kstri,"<",1))
			KML_Overlay::Read(fid,kstri);

		xfree((void**)&kstri);
	}

	this->AddCommnt(ncom,pcom);

	for (ncom; ncom>0; ncom--)
		xfree((void**)&(pcom[ncom-1]));
	xfree((void**)&pcom);

	return;
}
/*}}}*/
