Don't import ogdf namespace
[TortoiseGit.git] / ext / OGDF / ogdf / fileformats / GmlParser.h
blob337c5bb48f81e98c2ccd51bb6d4b093729b6b58b
1 /*
2 * $Revision: 2564 $
4 * last checkin:
5 * $Author: gutwenger $
6 * $Date: 2012-07-07 00:03:48 +0200 (Sa, 07. Jul 2012) $
7 ***************************************************************/
9 /** \file
10 * \brief Declaration of classes GmlObject and GmlParser.
12 * \author Carsten Gutwenger
14 * \par License:
15 * This file is part of the Open Graph Drawing Framework (OGDF).
17 * \par
18 * Copyright (C)<br>
19 * See README.txt in the root directory of the OGDF installation for details.
21 * \par
22 * This program is free software; you can redistribute it and/or
23 * modify it under the terms of the GNU General Public License
24 * Version 2 or 3 as published by the Free Software Foundation;
25 * see the file LICENSE.txt included in the packaging of this file
26 * for details.
28 * \par
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * \par
35 * You should have received a copy of the GNU General Public
36 * License along with this program; if not, write to the Free
37 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
38 * Boston, MA 02110-1301, USA.
40 * \see http://www.gnu.org/copyleft/gpl.html
41 ***************************************************************/
44 #ifdef _MSC_VER
45 #pragma once
46 #endif
48 #ifndef OGDF_GML_PARSER_H
49 #define OGDF_GML_PARSER_H
52 #include <ogdf/basic/Hashing.h>
53 #include <ogdf/basic/String.h>
54 #include <ogdf/basic/GraphAttributes.h>
55 #include <ogdf/cluster/ClusterGraph.h>
56 #include <ogdf/cluster/ClusterGraphAttributes.h>
59 namespace ogdf {
62 typedef HashElement<String,int> *GmlKey;
63 enum GmlObjectType { gmlIntValue, gmlDoubleValue, gmlStringValue, gmlListBegin,
64 gmlListEnd, gmlKey, gmlEOF, gmlError };
67 //---------------------------------------------------------
68 // GmlObject
69 // represents node in GML parse tree
70 //---------------------------------------------------------
71 struct OGDF_EXPORT GmlObject {
72 GmlObject *m_pBrother; // brother of node in tree
73 GmlKey m_key; // tag of node
74 GmlObjectType m_valueType; // type of node
76 // the entry in the union is selected according to m_valueType:
77 // gmlIntValue -> m_intValue
78 // gmlDoubleValue -> m_doubleValue
79 // gmlStringValue -> m_stringValue
80 // gmlListBegin -> m_pFirstSon (in case of a list, m_pFirstSon is pointer
81 // to first son and the sons are chained by m_pBrother)
82 union {
83 int m_intValue;
84 double m_doubleValue;
85 const char *m_stringValue;
86 GmlObject *m_pFirstSon;
89 // construction
90 GmlObject(GmlKey key, int intValue) : m_pBrother(0), m_key(key),
91 m_valueType(gmlIntValue), m_intValue(intValue) { }
93 GmlObject(GmlKey key, double doubleValue) : m_pBrother(0), m_key(key),
94 m_valueType(gmlDoubleValue), m_doubleValue(doubleValue) { }
96 GmlObject(GmlKey key, const char *stringValue) : m_pBrother(0), m_key(key),
97 m_valueType(gmlStringValue), m_stringValue(stringValue) { }
99 GmlObject(GmlKey key) : m_pBrother(0), m_key(key),
100 m_valueType(gmlListBegin), m_pFirstSon(0) { }
102 OGDF_NEW_DELETE
106 //---------------------------------------------------------
107 // GmlParser
108 // reads GML file and constructs GML parse tree
109 //---------------------------------------------------------
110 class OGDF_EXPORT GmlParser {
111 Hashing<String,int> m_hashTable; // hash table for tags
112 int m_num;
114 istream *m_is;
115 bool m_error;
116 String m_errorString;
118 char *m_rLineBuffer, *m_lineBuffer, *m_pCurrent, *m_pStore, m_cStore;
120 int m_intSymbol;
121 double m_doubleSymbol;
122 const char *m_stringSymbol;
123 GmlKey m_keySymbol;
124 String m_longString;
126 GmlObject *m_objectTree; // root node of GML parse tree
128 bool m_doCheck;
129 Array<node> m_mapToNode;
130 GmlObject *m_graphObject;
132 public:
133 // predefined id constants for all used keys
134 enum PredefinedKey { idPredefKey = 0, labelPredefKey, CreatorPredefKey,
135 namePredefKey, graphPredefKey, versionPredefKey, directedPredefKey,
136 nodePredefKey, edgePredefKey, graphicsPredefKey, xPredefKey,
137 yPredefKey, wPredefKey, hPredefKey, typePredefKey, widthPredefKey,
138 sourcePredefKey, targetPredefKey, arrowPredefKey, LinePredefKey,
139 pointPredefKey, generalizationPredefKey, subGraphPredefKey, fillPredefKey, clusterPredefKey,
140 rootClusterPredefKey, vertexPredefKey, colorPredefKey,
141 heightPredefKey, stipplePredefKey, patternPredefKey,
142 linePredefKey, lineWidthPredefKey, templatePredefKey,
143 edgeWeightPredefKey, NEXTPREDEFKEY };
145 // construction: creates object tree
146 // sets m_error flag if an error occured
147 GmlParser(const char *fileName, bool doCheck = false);
148 GmlParser(istream &is, bool doCheck = false);
150 // destruction: destroys object tree
151 ~GmlParser();
153 // returns id of object
154 int id(GmlObject *object) const { return object->m_key->info(); }
156 // true <=> an error in GML files has been detected
157 bool error() const { return m_error; }
158 // returns error message
159 const String &errorString() const { return m_errorString; }
161 // creates graph from GML parse tree
162 bool read(Graph &G);
163 // creates attributed graph from GML parse tree
164 bool read(Graph &G, GraphAttributes &AG);
165 //creates clustergraph from GML parse tree
166 //bool read(Graph &G, ClusterGraph & CG);
167 //read only cluster part of object tree and create cluster graph structure
168 bool readCluster(Graph &G, ClusterGraph& CG);
169 //the same with attributes
170 bool readAttributedCluster(
171 Graph &G,
172 ClusterGraph& CG,
173 ClusterGraphAttributes& ACG);
175 protected:
177 //read all cluster tree information
178 bool clusterRead(
179 GmlObject* rootCluster,
180 ClusterGraph& CG);
182 //with attributes
183 bool attributedClusterRead(
184 GmlObject* rootCluster,
185 ClusterGraph& CG,
186 ClusterGraphAttributes& ACG);
188 //recursively read cluster subtree information
189 bool recursiveClusterRead(
190 GmlObject* clusterObject,
191 ClusterGraph& CG,
192 cluster c);
194 bool recursiveAttributedClusterRead(
195 GmlObject* clusterObject,
196 ClusterGraph& CG,
197 ClusterGraphAttributes& ACG,
198 cluster c);
200 bool readClusterAttributes(
201 GmlObject* cGraphics,
202 cluster c,
203 ClusterGraphAttributes& ACG);
205 private:
206 void doInit(istream &is, bool doCheck);
207 void createObjectTree(istream &is, bool doCheck);
208 void initPredefinedKeys();
209 void setError(const char *errorString);
211 GmlObject *parseList(GmlObjectType closingKey, GmlObjectType errorKey);
212 GmlObjectType getNextSymbol();
213 bool getLine();
215 GmlKey hashString(const String &str);
217 GmlObject *getNodeIdRange(int &minId,int &maxId);
218 void readLineAttribute(GmlObject *object, DPolyline &dpl);
220 void destroyObjectList(GmlObject *object);
222 void indent(ostream &os, int d);
223 void output(ostream &os, GmlObject *object, int d);
228 } // end namespace ogdf
230 #endif