File: scenarioDescription.cpp

package info (click to toggle)
attal 1.0~rc2-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, squeeze, wheezy
  • size: 4,348 kB
  • ctags: 7,428
  • sloc: cpp: 55,101; sh: 267; ansic: 100; makefile: 54
file content (239 lines) | stat: -rw-r--r-- 5,742 bytes parent folder | download | duplicates (2)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/****************************************************************
**
** Attal : Lords of Doom
**
** scenarioDescription.cpp
** Desciption of a scenario
**
** Version : $Id: scenarioDescription.cpp,v 1.2 2007/04/07 13:44:32 lusum Exp $
**
** Author(s) : Pascal Audoux
**
** Date : 09/08/2004
**
** Licence :    
**	This program is free software; you can redistribute it and/or modify
**   	it under the terms of the GNU General Public License as published by
**     	the Free Software Foundation; either version 2, or (at your option)
**      any later version.
**
**	This program is distributed in the hope that it will be useful,
** 	but WITHOUT ANY WARRANTY; without even the implied warranty of
**	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**	GNU General Public License for more details.
**
****************************************************************/

#include "scenarioDescription.h"
 
// generic include files
// include files for QT
#include <QFile>
// application specific include files
#include "libCommon/log.h"

//
// ----- ScenarioDescription -----
//

ScenarioDescription::ScenarioDescription()
{
	clear();
}


void ScenarioDescription::clear()
{
	_nbPlayers = 0;
	_width = 0;
	_height = 0;
	_name = QObject::tr( "Unknown" );
	_description = "";
}
	
bool ScenarioDescription::load( const QString & fileName )
{
	_fileName = fileName;
	ScenarioDescriptionParser handler( this );
	QFile file( fileName );
	QXmlInputSource source( &file );
	QXmlSimpleReader reader;
	reader.setContentHandler( &handler );
	reader.setErrorHandler( &handler );
	bool ok = reader.parse( source );
	file.close();
	if ( !ok ) {
		logEE( "Parse Error (%s) : %s", fileName.toLatin1().constData(), handler.errorProtocol().toLatin1().constData() );
		return false;
	}

	return true;
}

//
//  ----- ScenarioDescriptionParser -----
//

ScenarioDescriptionParser::ScenarioDescriptionParser( ScenarioDescription * desc )
{
	_desc = desc;
}

bool ScenarioDescriptionParser::startDocument()
{
	// at the beginning of parsing: do some initialization
	_desc->clear();
	_errorProt = "";
	_state = StateInit;
	return true;
}

bool ScenarioDescriptionParser::startElement( const QString& /* namespaceURI */, const QString& /* localName */, const QString& qName, const QXmlAttributes& atts )
{
	int number;
	bool ret = true;

	if( qName == "scenario" && _state == StateInit ) {
		_state = StateDocument;
		_otherCpt = 0;
		_desc->clear();
		_desc->setNbPlayers( atts.value( "nbPlayer" ).toInt() );
	} else if( qName == "attalversion" && _state == StateDocument ) {
		_state = StateAttalVersion;
	} else if( qName == "name" && _state == StateDocument ) {
		_state = StateName;
	} else if( qName == "description" && _state == StateDocument ) {
		_state = StateDescription;
	} else if( qName == "calendar" && _state == StateDocument ) {
		_state = StateCalendar;
	} else if( qName == "value" && _state == StateCalendar ) {
		_state = StateCalendarDate;
		number = atts.value( "type" ).toInt();
	} else if( qName == "startcalendar" && _state == StateCalendar ) {
		_state = StateCalendarStart;
	} else if( qName == "value" && _state == StateCalendarStart ) {
		_state = StateCalendarStartDate;
		number = atts.value( "type" ).toInt();
	} else if( qName == "turn" && _state == StateCalendar ) {
		_state = StateCalendarTurn;
	} else if( qName == "map" && _state == StateDocument ) {
		_state = StateMap;
	} else if( qName == "width" && _state == StateMap ) {
		_state = StateWidth;
	} else if( qName == "height" && _state == StateMap ) {
		_state = StateHeight;
	} else if( ( _state == StateDocument ) || ( _state == StateDocumentOther ) ) {
		_state = StateDocumentOther;
		_otherCpt++;
	} else if( ( _state == StateMap ) || ( _state == StateMapOther ) ) {
		_state = StateMapOther;
		_otherCpt++;
	}
	return ret;
}

bool ScenarioDescriptionParser::endElement( const QString& /* namespaceURI */, const QString& /* localName */, const QString& /* qName */)
{
	bool ret = true;

	switch ( _state ) {
	case StateName:
		_state = StateDocument;
		break;
	case StateAttalVersion:
		_state = StateDocument;
		break;
	case StateDescription:
		_state = StateDocument;
		break;
	case StateCalendar:
		_state = StateDocument;
		break;
	case StateCalendarDate:
		_state = StateCalendar;
		break;
	case StateCalendarStart:
		_state = StateCalendar;
		break;
	case StateCalendarStartDate:
		_state = StateCalendarStart;
		break;
	case StateCalendarTurn:
		_state = StateCalendar;
		break;
	case StateMap:
		_state = StateFinish;
		break;
	case StateWidth:
		_state = StateMap;
		break;
	case StateHeight:
		_state = StateMap;
		break;
	case StateDocumentOther:
		if( _otherCpt == 1 ) {
			_state = StateDocument;
		} 
		_otherCpt--;
	case StateMapOther:
		if( _otherCpt == 1 ) {
			_state = StateMap;
		} 
		_otherCpt--;
		break;
	default:
		break;
    }
    return ret;
}

bool ScenarioDescriptionParser::characters( const QString & ch )
{
	bool ret = true;
	QString ch_simplified = ch.simplified();

	if ( ch_simplified.isEmpty() )
		return true;

	switch( _state ) {
	case StateName:
		_desc->setName( ch_simplified );
		break;
	case StateDescription:
		_desc->setDescription( ch_simplified );
		break;
	case StateWidth:
		_desc->setWidth( ch_simplified.toUInt() );
		break;
	case StateHeight:
		_desc->setHeight( ch_simplified.toUInt() );
		break;
	default:
		break;
	}
	return ret;
}



bool ScenarioDescriptionParser::fatalError( const QXmlParseException& exception )
{
     logEE( "state %d", _state );

    _errorProt += QString( "fatal parsing error: %1 in line %2, column %3\n" )
	.arg( exception.message() )
	.arg( exception.lineNumber() )
	.arg( exception.columnNumber() );

    return QXmlDefaultHandler::fatalError( exception );
}