~njansson/dolfin/hpc

« back to all changes in this revision

Viewing changes to src/kernel/io/XMLGraph.cpp

  • Committer: Johannes Ring
  • Date: 2008-03-05 22:43:06 UTC
  • Revision ID: johannr@simula.no-20080305224306-2npsdyhfdpl2esji
The BIG commit!

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Copyright (C) 2007 Magnus Vikstrom
2
 
// Licensed under the GNU LGPL Version 2.1.
3
 
//
4
 
// First added:  2007-02-12
5
 
// Last changed: 2007-03-21
6
 
 
7
 
#include <dolfin/dolfin_log.h>
8
 
#include <dolfin/XMLGraph.h>
9
 
 
10
 
using namespace dolfin;
11
 
 
12
 
//-----------------------------------------------------------------------------
13
 
XMLGraph::XMLGraph(Graph& graph) : XMLObject(), _graph(graph), state(OUTSIDE)
14
 
{
15
 
  // Do nothing
16
 
}
17
 
//-----------------------------------------------------------------------------
18
 
XMLGraph::~XMLGraph()
19
 
{
20
 
  // Do nothing
21
 
}
22
 
//-----------------------------------------------------------------------------
23
 
void XMLGraph::startElement(const xmlChar *name, const xmlChar **attrs)
24
 
{
25
 
  switch ( state )
26
 
  {
27
 
  case OUTSIDE:
28
 
    
29
 
    if ( xmlStrcasecmp(name, (xmlChar *) "graph") == 0 )
30
 
    {
31
 
      readGraph(name, attrs);
32
 
      state = INSIDE_GRAPH;
33
 
    }
34
 
    
35
 
    break;
36
 
 
37
 
  case INSIDE_GRAPH:
38
 
    
39
 
    if ( xmlStrcasecmp(name, (xmlChar *) "vertices") == 0 )
40
 
    {
41
 
      readVertices(name, attrs);
42
 
      state = INSIDE_VERTICES;
43
 
    }
44
 
    else if ( xmlStrcasecmp(name, (xmlChar *) "edges") == 0 )
45
 
    {
46
 
      readEdges(name, attrs);
47
 
      state = INSIDE_EDGES;
48
 
    }
49
 
    
50
 
    break;
51
 
    
52
 
  case INSIDE_VERTICES:
53
 
    
54
 
    if ( xmlStrcasecmp(name, (xmlChar *) "vertex") == 0 )
55
 
      readVertex(name, attrs);
56
 
 
57
 
    break;
58
 
    
59
 
  case INSIDE_EDGES:
60
 
 
61
 
    if ( xmlStrcasecmp(name, (xmlChar *) "edge") == 0 )
62
 
      readEdge(name, attrs);
63
 
 
64
 
    break;
65
 
 
66
 
  default:
67
 
    ;
68
 
  }
69
 
}
70
 
//-----------------------------------------------------------------------------
71
 
void XMLGraph::endElement(const xmlChar *name)
72
 
{
73
 
  switch ( state )
74
 
  {
75
 
  case INSIDE_GRAPH:
76
 
    if ( xmlStrcasecmp(name, (xmlChar *) "graph") == 0 )
77
 
    {
78
 
      closeGraph();
79
 
      state = DONE;
80
 
    }
81
 
 
82
 
    break;
83
 
 
84
 
  case INSIDE_VERTICES:
85
 
    
86
 
    if ( xmlStrcasecmp(name, (xmlChar *) "vertices") == 0)
87
 
    {
88
 
      state = INSIDE_GRAPH;
89
 
    }
90
 
    
91
 
    break;
92
 
 
93
 
  case INSIDE_EDGES:
94
 
    
95
 
    if ( xmlStrcasecmp(name, (xmlChar *) "edges") == 0)
96
 
    {
97
 
      state = INSIDE_GRAPH;
98
 
    }
99
 
    
100
 
    break;
101
 
    
102
 
  default:
103
 
    ;
104
 
  }
105
 
}
106
 
//-----------------------------------------------------------------------------
107
 
void XMLGraph::open(std::string filename)
108
 
{
109
 
  // Do nothing
110
 
}
111
 
//-----------------------------------------------------------------------------
112
 
bool XMLGraph::close()
113
 
{
114
 
  return state == DONE;
115
 
}
116
 
//-----------------------------------------------------------------------------
117
 
void XMLGraph::readGraph(const xmlChar *name, const xmlChar **attrs)
118
 
{
119
 
  // Parse values
120
 
  std::string type = parseString(name, attrs, "type");
121
 
  
122
 
  // Open graph for editing
123
 
  editor.open(_graph, type);
124
 
}
125
 
//-----------------------------------------------------------------------------
126
 
void XMLGraph::readVertices(const xmlChar *name, const xmlChar **attrs)
127
 
{
128
 
  dolfin_debug("readVertices()");
129
 
  uint num_vertices = parseUnsignedInt(name, attrs, "size");
130
 
  editor.initVertices(num_vertices);
131
 
}
132
 
//-----------------------------------------------------------------------------
133
 
void XMLGraph::readEdges(const xmlChar *name, const xmlChar **attrs)
134
 
{
135
 
  dolfin_debug("readEdges()");
136
 
  uint num_edges = parseUnsignedInt(name, attrs, "size");
137
 
  editor.initEdges(num_edges);
138
 
}
139
 
//-----------------------------------------------------------------------------
140
 
void XMLGraph::readVertex(const xmlChar *name, const xmlChar **attrs)
141
 
{
142
 
  // Read index
143
 
  currentVertex = parseUnsignedInt(name, attrs, "index");
144
 
 
145
 
  // Read number of incident edges
146
 
  uint num_edges = parseUnsignedInt(name, attrs, "num_edges");
147
 
  
148
 
  // Vertex weights not yet implemented
149
 
  //uint w = parseUnsignedInt(name, attrs, "weight");
150
 
  
151
 
  editor.addVertex(currentVertex, num_edges);
152
 
}
153
 
//-----------------------------------------------------------------------------
154
 
void XMLGraph::readEdge(const xmlChar *name, const xmlChar **attrs)
155
 
{
156
 
  // Read index
157
 
  uint v1 = parseUnsignedInt(name, attrs, "v1");
158
 
  uint v2 = parseUnsignedInt(name, attrs, "v2");
159
 
 
160
 
  //dolfin_debug2("readEdge, v1 = %d, v2 = %d", v1, v2);
161
 
  
162
 
  // Edge weights not yet implemented
163
 
  //uint w = parseUnsignedInt(name, attrs, "weight");
164
 
  editor.addEdge(v1, v2);
165
 
}
166
 
//-----------------------------------------------------------------------------
167
 
void XMLGraph::closeGraph()
168
 
{
169
 
  editor.close();
170
 
}
171
 
//-----------------------------------------------------------------------------