31 #include <lemon/error.h> |
31 #include <lemon/error.h> |
32 |
32 |
33 |
33 |
34 namespace lemon { |
34 namespace lemon { |
35 |
35 |
|
36 /// \brief Standard WriterTraits for the GraphWriter class. |
|
37 /// |
|
38 /// Standard WriterTraits for the GraphWriter class. |
|
39 /// It defines standard writing method for all type of value. |
36 struct DefaultWriterTraits { |
40 struct DefaultWriterTraits { |
37 |
41 |
|
42 /// \brief Template class for writing an value. |
|
43 /// |
|
44 /// Template class for writing an value. |
38 template <typename _Value> |
45 template <typename _Value> |
39 struct Writer { |
46 struct Writer { |
|
47 /// The value type. |
40 typedef _Value Value; |
48 typedef _Value Value; |
41 |
49 |
|
50 /// \brief Writes a value from the given stream. |
|
51 /// |
|
52 /// Writes a value from the given stream. |
42 void write(std::ostream& os, const Value& value) { |
53 void write(std::ostream& os, const Value& value) { |
43 os << value << '\t'; |
54 os << value << '\t'; |
44 } |
55 } |
45 }; |
56 }; |
46 |
57 |
47 }; |
58 }; |
48 |
59 |
49 |
60 |
|
61 /// \brief Writer class for quoted strings. |
|
62 /// |
|
63 /// Writer class for quoted strings. It can process the escape |
|
64 /// sequences in the string. |
50 class QuotedStringWriter { |
65 class QuotedStringWriter { |
51 public: |
66 public: |
52 typedef std::string Value; |
67 typedef std::string Value; |
53 |
68 |
|
69 /// \brief Constructor for the writer. |
|
70 /// |
|
71 /// Constructor for the writer. If the given parameter is true |
|
72 /// the writer creates escape sequences from special characters. |
54 QuotedStringWriter(bool _escaped = true) : escaped(_escaped) {} |
73 QuotedStringWriter(bool _escaped = true) : escaped(_escaped) {} |
55 |
74 |
|
75 /// \brief Writes a quoted string from the given stream. |
|
76 /// |
|
77 /// Writes a quoted string from the given stream. |
56 void write(std::ostream& os, const std::string& value) { |
78 void write(std::ostream& os, const std::string& value) { |
57 os << "\""; |
79 os << "\""; |
58 if (escaped) { |
80 if (escaped) { |
59 ostringstream ls; |
81 ostringstream ls; |
60 for (int i = 0; i < (int)value.size(); ++i) { |
82 for (int i = 0; i < (int)value.size(); ++i) { |
128 typedef typename Graph::NodeIt NodeIt; |
153 typedef typename Graph::NodeIt NodeIt; |
129 typedef typename Graph::Edge Edge; |
154 typedef typename Graph::Edge Edge; |
130 typedef typename Graph::EdgeIt EdgeIt; |
155 typedef typename Graph::EdgeIt EdgeIt; |
131 |
156 |
132 typedef _WriterTraits WriterTraits; |
157 typedef _WriterTraits WriterTraits; |
133 |
158 |
|
159 /// \brief Construct a new GraphWriter. |
|
160 /// |
|
161 /// Construct a new GraphWriter. It writes from the given map, |
|
162 /// it constructs the given map and it use the given writer as the |
|
163 /// default skipper. |
134 GraphWriter(std::ostream& _os, Graph& _graph) : os(_os), graph(_graph) {} |
164 GraphWriter(std::ostream& _os, Graph& _graph) : os(_os), graph(_graph) {} |
135 |
165 |
136 |
166 |
|
167 /// \brief Destruct the graph writer. |
|
168 /// |
|
169 /// Destruct the graph writer. |
137 ~GraphWriter() { |
170 ~GraphWriter() { |
138 for (typename NodeMapWriters::iterator it = node_map_writers.begin(); |
171 for (typename NodeMapWriters::iterator it = node_map_writers.begin(); |
139 it != node_map_writers.end(); ++it) { |
172 it != node_map_writers.end(); ++it) { |
140 delete it->second; |
173 delete it->second; |
141 } |
174 } |
147 |
180 |
148 } |
181 } |
149 |
182 |
150 // Node map rules |
183 // Node map rules |
151 |
184 |
|
185 /// \brief Add a new node map writer command for the writer. |
|
186 /// |
|
187 /// Add a new node map writer command for the writer. |
152 template <typename Map> |
188 template <typename Map> |
153 GraphWriter& addNodeMap(std::string name, const Map& map) { |
189 GraphWriter& addNodeMap(std::string name, const Map& map) { |
154 return addNodeMap<typename WriterTraits::template Writer< |
190 return addNodeMap<typename WriterTraits::template Writer< |
155 typename Map::Value>, Map>(name, map); |
191 typename Map::Value>, Map>(name, map); |
156 } |
192 } |
157 |
193 |
|
194 /// \brief Add a new node map writer command for the writer. |
|
195 /// |
|
196 /// Add a new node map writer command for the writer. |
158 template <typename Writer, typename Map> |
197 template <typename Writer, typename Map> |
159 GraphWriter& addNodeMap(std::string name, const Map& map, |
198 GraphWriter& addNodeMap(std::string name, const Map& map, |
160 const Writer& writer = Writer()) { |
199 const Writer& writer = Writer()) { |
161 // if (node_map_writers.find(name) != node_map_writers.end()) { |
|
162 // throw Exception() << "Multiple write rule for node map: " |
|
163 // << name; |
|
164 // } |
|
165 node_map_writers.push_back( |
200 node_map_writers.push_back( |
166 make_pair(name, new MapWriter<Node, Map, Writer>(map, writer))); |
201 make_pair(name, new MapWriter<Node, Map, Writer>(map, writer))); |
167 return *this; |
202 return *this; |
168 } |
203 } |
169 |
204 |
170 // Edge map rules |
205 // Edge map rules |
171 |
206 |
|
207 /// \brief Add a new edge map writer command for the writer. |
|
208 /// |
|
209 /// Add a new edge map writer command for the writer. |
172 template <typename Map> |
210 template <typename Map> |
173 GraphWriter& addEdgeMap(std::string name, const Map& map) { |
211 GraphWriter& addEdgeMap(std::string name, const Map& map) { |
174 return addEdgeMap<typename WriterTraits::template Writer<typename Map::Value>, Map>(name, map); |
212 return addEdgeMap<typename WriterTraits::template Writer< |
175 } |
213 typename Map::Value>, Map>(name, map); |
176 |
214 } |
177 |
215 |
|
216 |
|
217 /// \brief Add a new edge map writer command for the writer. |
|
218 /// |
|
219 /// Add a new edge map writer command for the writer. |
178 template <typename Writer, typename Map> |
220 template <typename Writer, typename Map> |
179 GraphWriter& addEdgeMap(std::string name, const Map& map, const Writer& writer = Writer()) { |
221 GraphWriter& addEdgeMap(std::string name, |
180 // if (edge_map_writers.find(name) != edge_map_writers.end()) { |
222 const Map& map, const Writer& writer = Writer()) { |
181 // throw Exception() << "Multiple write rule for edge map: " << name; |
223 edge_map_writers.push_back(make_pair(name, |
182 // } |
224 new MapWriter<Edge, Map, Writer>(map, writer))); |
183 edge_map_writers.push_back(make_pair(name, new MapWriter<Edge, Map, Writer>(map, writer))); |
|
184 return *this; |
225 return *this; |
185 } |
226 } |
186 |
227 |
187 // Node rules |
228 /// \brief Add a new labeled node writer for the writer. |
|
229 /// |
|
230 /// Add a new labeled node writer for the writer. |
188 GraphWriter& addNode(std::string name, const Node& node) { |
231 GraphWriter& addNode(std::string name, const Node& node) { |
189 // if (node_writers.find(name) != node_writers.end()) { |
|
190 // throw Exception() << "Multiple write rule for node"; |
|
191 // } |
|
192 node_writers.push_back(make_pair(name, node)); |
232 node_writers.push_back(make_pair(name, node)); |
193 return *this; |
233 return *this; |
194 } |
234 } |
195 |
235 |
196 // Edge rules |
236 /// \brief Add a new labeled edge writer for the writer. |
197 |
237 /// |
|
238 /// Add a new labeled edge writer for the writer. |
198 GraphWriter& addEdge(std::string name, const Edge& edge) { |
239 GraphWriter& addEdge(std::string name, const Edge& edge) { |
199 // if (edge_writers.find(name) != edge_writers.end()) { |
|
200 // throw Exception() << "Multiple write rule for edge"; |
|
201 // } |
|
202 edge_writers.push_back(make_pair(name, edge)); |
240 edge_writers.push_back(make_pair(name, edge)); |
203 return *this; |
241 return *this; |
204 } |
242 } |
205 |
243 |
206 void write() { |
244 /// \brief Executes the writer commands. |
|
245 /// |
|
246 /// Executes the writer commands. |
|
247 void run() { |
207 writeNodeSet(); |
248 writeNodeSet(); |
208 writeEdgeSet(); |
249 writeEdgeSet(); |
209 writeNodes(); |
250 writeNodes(); |
210 writeEdges(); |
251 writeEdges(); |
211 os << "@end" << std::endl; |
252 os << "@end" << std::endl; |