File: xmlscores.cpp

package info (click to toggle)
teg 0.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 19,036 kB
  • sloc: cpp: 16,819; xml: 1,313; makefile: 268; sh: 195; ansic: 112
file content (297 lines) | stat: -rw-r--r-- 6,737 bytes parent folder | download
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/* Tenes Empanadas Graciela
 *
 * Copyright (C) 2001 Ricardo Quesada
 *
 * Author: Ricardo Calixto Quesada <riq@corest.com>
 *
 * 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; only version 2 of the License
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 */
/**
 * @file xmlscores.c
 * server scores from/to xml
 */

/*
 * File based in the job example from libxml
 */
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include <assert.h>
#include <unistd.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <glib.h>
#include <time.h>

#include "../common/xml_support.h"
#include "server.h"
#include "xmlscores.h"
#include "scores.h"

#ifndef xmlChildrenNode
#define xmlChildrenNode childs
#define xmlRootNode root
#endif

namespace teg::server
{

static bool parseScore(xmlDocPtr doc, xmlNodePtr cur, PSCORES ret)
{
	xmlChar *val;

	memset(ret, 0, sizeof(*ret));

	val = xmlGetProp(cur, (const xmlChar *) "name");
	if(val == NULL) {
		fprintf(stderr, ("Score has no name\n"));
		return false;
	} else {
		strncpy((char*)ret->name, (char*)val, sizeof(ret->name) -1);
	}

	val =  xmlGetProp(cur, (const xmlChar *) "color");
	if(val == NULL) {
		fprintf(stderr, ("Score has no color\n"));
		return false;
	} else {
		ret->color = atoi((char*)val);
	}

	val = xmlGetProp(cur, (const xmlChar *) "points");
	if(val == NULL) {
		fprintf(stderr, ("Score has no score\n"));
		return false;
	} else {
		ret->score = atoi((char*)val);
	}

	val = xmlGetProp(cur, (const xmlChar *) "date");
	if(val == NULL) {
		fprintf(stderr, ("Score has no date\n"));
		return false;
	} else {
		strncpy(ret->date, (char*)val, sizeof(ret->date) -1);
	}

	val = xmlGetProp(cur, (const xmlChar *) "human");
	if(val == NULL) {
		fprintf(stderr, ("Score has no human\n"));
		return false;
	} else {
		ret->human = atoi((char*)val);
	}

	return true;
}

TEG_STATUS xmlscores_load(void)
{
	char filename[512];
	xmlDocPtr doc;
	xmlNodePtr cur;

	/*
	 * build an XML tree from a the file;
	 */

	snprintf(filename,
	         sizeof(filename)-1,
	         "%s/%s/server_scores.xml",
	         g_get_home_dir(),
	         rc_directory_name);
	filename[ sizeof(filename)-1 ] = 0;

	doc = xmlParseFile(filename);

	if(doc == NULL) {
		return TEG_STATUS_ERROR;
	}

	/*
	 * Check the document is of the right kind
	 */
	cur = xmlDocGetRootElement(doc);
	if(cur == NULL) {
		fprintf(stderr, ("Empty document\n"));
		goto error;
	}

	if(xmlStrcmp(cur->name, (const xmlChar *) "teg_scores")) {
		fprintf(stderr, ("Wrong type. root node != teg_scores\n"));
		goto error;
	}

	/*
	 * Allocate the structure to be returned.
	 */

	cur = xml_get_element_children(cur);

	while(cur != NULL) {
		if(!xmlStrcmp(cur->name, (const xmlChar *) "score")) {
			SCORES score;
			if(parseScore(doc, cur, &score)) {
				insert_score(&score);
			}
		} else {
			fprintf(stderr, ("Wrong type (%s). score was expected\n"), cur->name);
			goto error;
		}

		cur = xml_get_element_next(cur);
	}

	xmlFreeDoc(doc);
	return TEG_STATUS_SUCCESS;
error:
	xmlFreeDoc(doc);
	return TEG_STATUS_ERROR;
}

static void xmlscores_add(xmlNodePtr parent, PSCORES pS)
{
	xmlNodePtr child = xmlNewTextChild(parent, NULL, (xmlChar*)"score", NULL);

	xmlSetProp(child, (xmlChar*)"name", (xmlChar*)pS->name);
	add_numeric_attribute(child, "points", pS->score);
	add_numeric_attribute(child, "color", pS->color);
	xmlSetProp(child, (xmlChar*)"date", (xmlChar*)pS->date);
	add_numeric_attribute(child, "human", pS->human);
}

static void save_single_score(PSCORES pS, void* user)
{
	xmlNodePtr node = (xmlNodePtr) user;
	xmlscores_add(node, pS);
}

void xmlscores_save(void)
{
	xmlDocPtr doc;
	xmlNodePtr child;
	char filename[512];

	doc = xmlNewDoc((xmlChar*)"1.0");

	child = xmlNewDocRawNode(doc, NULL, (xmlChar*)"teg_scores", NULL);

	xmlDocSetRootElement(doc, child);
	scores_map(save_single_score, (void*) child);

	snprintf(filename,
	         sizeof(filename)-1,
	         "%s/%s/server_scores.xml",
	         g_get_home_dir(),
	         rc_directory_name);
	filename[ sizeof(filename)-1 ] = 0;

	xmlSaveFile(filename, doc);
	xmlFreeDoc(doc);
}

/* creates a new node with the correct values */
static void new_score_node(PSPLAYER pJ, PSCORES pS)
{
	time_t tt;
	struct tm *t;

	memset(pS, 0, sizeof(*pS));

	pS->score = pJ->player_stats.score;
	pS->color = pJ->color;
	strncpy(pS->name, pJ->name, sizeof(pS->name) -1);
	pS->human = pJ->human;

	time(&tt);
	t = localtime(&tt);

	snprintf(pS->date, sizeof(pS->date) -1, "%.2d/%.2d/%.2d %.2d:%.2d"
	         , t->tm_mon +1
	         , t->tm_mday
	         , t->tm_year + 1900
	         , t->tm_hour
	         , t->tm_min
	        );

	pS->date[ sizeof(pS->date) -1 ] = '\0';
}

TEG_STATUS scores_insert_player(PSPLAYER pJ)
{
	if(! pJ->is_player) {
		return TEG_STATUS_ERROR;
	}

	stats_score(&pJ->player_stats, g_conts);

	if(pJ->player_stats.score == 0 && pJ->player_stats.armies_killed == 0) {
		return TEG_STATUS_ERROR;
	}

	SCORES score;
	new_score_node(pJ, &score);
	insert_score(&score);

	xmlscores_save();

	return TEG_STATUS_SUCCESS;
}

struct AppendString {
	char* dest;
	size_t storage_remaining;
	char const* delim;
	bool valid;
};

void appendScoreString(PSCORES pS, void* user)
{
	struct AppendString* as = (struct AppendString*) user;
	if(!as->valid) {
		return;
	}
	int printed = snprintf(as->dest, as->storage_remaining,
	                       "%s%s,%d,%s,%d,%d",
	                       as->delim,
	                       pS->name, pS->color, pS->date, pS->score, pS->human);
	if((printed < 0) // some printf error
	        || (((unsigned)printed) >= as->storage_remaining)) { // string truncation
		as->valid = false;
	}
	as->dest += printed;
	as->storage_remaining -= printed;
	as->delim = "\\";
}

TEG_STATUS scores_dump(char *strout, size_t len)
{
	strout[0]=0; // zero out the string if there are no scores.
	struct AppendString as = {
		.dest = strout, .storage_remaining=len-1, .delim="", .valid=true
	};
	scores_map(appendScoreString, &as);
	if(!as.valid) {
		strout[0]=0; // something bad happened, so zero out the result
		return TEG_STATUS_ERROR;
	} else {
		strout[len-1] = 0; // add terminator just in case.
	}

	return TEG_STATUS_SUCCESS;
}

}