summaryrefslogtreecommitdiff
path: root/hw6/src/sceneIO_core.cpp
blob: 123287ea39d809f5b615bd5ec56e2df58abe92b2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/******************************************************************/
/* 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;
}