summaryrefslogblamecommitdiff
path: root/hw6/src/sceneIO_core.cpp
blob: 123287ea39d809f5b615bd5ec56e2df58abe92b2 (plain) (tree)


































































































                                                                                                     
/******************************************************************/
/* This file is part of the homework assignments for CSCI-427/527 */
/* at The College of William & Mary and authored by Pieter Peers. */
/* No part of this file, whether altered or in original form, can */
/* be distributed or used outside the context of CSCI-427/527     */
/* without consent of either the College of William & Mary or     */
/* Pieter Peers.                                                  */
/******************************************************************/
#include <string>
#include <cstdlib>

#include "sceneIO_core.h"
#include "errorMessage.h"

color getColor(const XMLNode& node, const std::string& name, const color& default_value)
{
  std::string value = node.attribute(name);

  // if defined, process..
  if(value != "") 
  {
    color result;
    int s = sscanf(value.c_str(), "%f %f %f", &result.r, &result.g, &result.b);

    // check if only 1 value is given or 3.
    if(s == 1) return color(result.r, result.r, result.r);
    else if(s == 3) return result;
    else errorMessage("Failed to parse color value (%s)", value.c_str());
  }

  // else return default
  return default_value;
}


vec3d getVec3d(const XMLNode& node, const std::string& name, const vec3d& default_value)
{
  std::string value = node.attribute(name);

  // if defined, process..
  if(value != "") 
  {
    vec3d result;
    int s = sscanf(value.c_str(), "%f %f %f", &result.x, &result.y, &result.z);

    // check if only 1 value is given or 3.
    if(s == 1) return vec3d(result.x, result.x, result.x);
    else if(s == 3) return result;
    else errorMessage("Failed to parse vec3d value (%s)", value.c_str());
  }

  // else return default
  return default_value;
}


vec2d getVec2d(const XMLNode& node, const std::string& name, const vec2d& default_value)
{
  std::string value = node.attribute(name);

  // if defined, process..
  if(value != "") 
  {
    vec2d result;
    int s = sscanf(value.c_str(), "%f %f", &result.x, &result.y);

    // check if only 1 value is given or 2.
    if(s == 1) return vec2d(result.x, result.x);
    else if(s == 2) return result;
    else errorMessage("Failed to parse vec2d value (%s)", value.c_str());
  }

  // else returnd default
  return default_value;
}


std::string getString(const XMLNode& node, const std::string& name, const std::string& default_value)
{
  std::string value = node.attribute(name);
  if(value != "") return std::string(value);
  else return default_value;
}


float getFloat(const XMLNode& node, const std::string& name, float default_value)
{
  std::string value = node.attribute(name);
if(value != "") return atof(value.c_str());
  else return default_value;
}


unsigned int getInteger(const XMLNode& node, const std::string& name, unsigned int default_value)
{
  std::string value = node.attribute(name);
if(value != "") return atoi(value.c_str());
  else return default_value;
}