// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// This program 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 General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
// Copyright 2003 Liam Girdwood
#include "astro_object.hh"
namespace Castor
{
/*! \fn AstroObject::AstroObject()
*
* Create an AstroObject.
*/
AstroObject::AstroObject()
{
}
/*! \fn AstroObject::~AstroObject()
*
* Destroy an AstroObject.
* Free's all attached data elements.
*/
AstroObject::~AstroObject()
{
m_unknown_elements.clear();
}
/*! \fn bool AstroObject::check_element (std::string& name);
* \param name Name of the element type
* \return True, if the element type is known
*
* Is the element type known to the object.
*
* Elements known to this class are
*
* - ID Object Identification.
*
*/
bool AstroObject::check_element (std::string& name)
{
if (name == "ID")
return true;
return false;
}
/*! \fn bool AstroObject::add_element (std::string& name, element_type type, std::string& value)
* \param name Name of the element type
* \param type Type of element, INT, DOUBLE or STRING
* \param value Value of the element
* \return True, if the element type is known
*
* Add the data element to this object. The element type is specified as either an
* int, double or a string. If the element is unknown at this level, it
* is added as an unkown element.
*
* Elements known to this class are
*
* - ID Object Identification.
*
*/
bool AstroObject::add_element (std::string& name, element_type type, std::string& value)
{
switch (type) {
case STRING:
if (name == "ID") {
m_cat_no = value;
return true;
}
break;
}
// unknown element
return add_unknown_element (name, type, value);
}
/*! \fn bool AstroObject::add_unknown_element (std::string& name, element_type type, std::string& value)
* \param name Element name
* \param type Type of element
* \param value Value of element
* \return False, if the element type is unknown
*
* Add the unknown element data to the object.
*/
bool AstroObject::add_unknown_element (std::string& name, element_type type,
std::string& value)
{
std::string* val = new std::string(value);
m_unknown_elements.push_back(val);
return false;
}
/*! \fn int AstroObject::load (std::ifstream* file)
* \param file Pointer to file stream
*
* Load object from file
*/
int AstroObject::load (std::ifstream* file)
{
int size;
// safety first !
if (file->eof())
return 0;
load_item(file, m_cat_no);
//std::cout << "load cat no " << m_cat_no << std::endl;
// load the extra elements
load_item (file, size);
for (int i =0; i < size; i++) {
if (file->eof())
return 0;
std::string* value = new std::string();
load_item (file, *value);
m_unknown_elements.push_back(value);
}
}
/*! \fn int AstroObject::save (std::ofstream* file)
* \param file Pointer to file stream
*
* Save object to file
*/
int AstroObject::save (std::ofstream* file)
{
//std::cout << "save cat no " << m_cat_no << std::endl;
save_item(file, m_cat_no);
// save the extra elements
save_item (file, (int)m_unknown_elements.size());
std::vector::iterator i;
for (i = m_unknown_elements.begin(); i != m_unknown_elements.end(); i++)
save_item (file, **i);
}
/*! \fn void AstroObject::save_item (std::ofstream* file, int value)
* \param file Pointer to file stream
* \param value Data value
*
* Save Integer value to file.
*/
void AstroObject::save_item (std::ofstream* file, int value)
{
file->write ((char*)&value, sizeof (value));
}
/*! \fn void AstroObject::save_item (std::ofstream* file, double value)
* \param file Pointer to file stream
* \param value Data value
*
* Save Double value to file.
*/
void AstroObject::save_item (std::ofstream* file, double value)
{
file->write ((char*)&value, sizeof (value));
}
/*! \fn void AstroObject::save_item (std::ofstream* file, std::string& value)
* \param file Pointer to file stream
* \param value Data value
*
* Save std::string value to file.
*/
void AstroObject::save_item (std::ofstream* file, std::string& value)
{
char size = value.size();
file->write (&size, sizeof(size));
if (size)
file->write (value.c_str(), size);
}
/*! \fn void AstroObject::load_item (std::ifstream* file, int& value)
* \param file Pointer to file stream
* \param value Data value
*
* Load Integer value from file.
*/
void AstroObject::load_item (std::ifstream* file, int& value)
{
file->read ((char*)&value, sizeof (int));
}
/*! \fn void AstroObject::load_item (std::ifstream* file, double& value)
* \param file Pointer to file stream
* \param value Data value
*
* Load Double value from file.
*/
void AstroObject::load_item (std::ifstream* file, double& value)
{
file->read ((char*)&value, sizeof (double));
}
/*! \fn void AstroObject::load_item (std::ifstream* file, std::string& value)
* \param file Pointer to file stream
* \param value Data value
*
* Load string value from file.
*/
void AstroObject::load_item (std::ifstream* file, std::string& value)
{
char size;
char buffer[256];
file->read (&size, sizeof(size));
if (size)
file->read (buffer, size);
buffer[size] = 0;
value = buffer;
}
/*! \fn int AstroObject::init ()
* \return 0 on success.
*
* Perform any initialisation for this object
*/
int AstroObject::init ()
{
return 0;
}
/*! \fn void AstroObject::get_id(std::string& id)
* \param id Object ID
*
* Get the object ID
*/
void AstroObject::get_id(std::string& id)
{
id = m_cat_no;
}
/*! \fn virtual void get_name(std::string& name);
* \param name Object name
*
* Get object name.
*/
void AstroObject::get_name(std::string& name)
{
get_id (name);
}
/*! \fn virtual void get_info (std::list value);
* \param value List of parameter values.
*/
void AstroObject::get_info (std::list& value)
{
value.push_front(Glib::ustring(m_cat_no));
std::vector::iterator i;
//Fill the TreeView's model
for (i = m_unknown_elements.begin(); i != m_unknown_elements.end(); ++i) {
value.push_back(**i);
}
}
}