Commit c19b9eed authored by Colomban Wendling's avatar Colomban Wendling

New files xmlutils.[ch]

Added utilities needed by both tilesdef.c and tileset.c to read XML
in new files xmlutils.[ch].
parent 3ae2e73c
......@@ -6,7 +6,8 @@ libovcc_la_SOURCES = tile.c \
tileset.c \
tilesdef.c \
stack.c \
board.c
board.c \
xmlutils.c
test_LDADD = libovcc.la -lpthread
test_SOURCES = test.c
/*
*
* Copyright (C) 2009 Colomban Wendling <ban@herbesfolles.org>
* Jonathan Michalon <studios.chalmion@no-log.org>
*
* 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 3 of the License, 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "xmlutils.h"
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
#include <glib.h>
GQuark
ovcc_xmlutils_error_quark (void)
{
static GQuark error_quark = 0;
if (G_UNLIKELY (error_quark == 0)) {
error_quark = g_quark_from_static_string ("OVCCXmlUtilsError");
}
return error_quark;
}
gulong
ovcc_xmlutils_read_ulong (const gchar *num,
GError **error)
{
gulong value = 0;
char *endptr;
value = strtoul (num, &endptr, 0);
if (*num == 0 || num == endptr) {
g_set_error (error, OVCC_XMLUTILS_ERROR, OVCC_XMLUTILS_ERROR_MISSING_VALUE,
"'%s' contains no number", num);
} else if (*endptr != 0) {
g_set_error (error, OVCC_XMLUTILS_ERROR, OVCC_XMLUTILS_ERROR_INVALID_DATA,
"Unparsed data at end of number '%s': %s", num, endptr);
} else if (value >= ULONG_MAX && errno == ERANGE) {
g_set_error (error, OVCC_XMLUTILS_ERROR, OVCC_XMLUTILS_ERROR_OUT_RANGE,
"number '%s' is out of range (> %lu)", num, ULONG_MAX);
}
return value;
}
guint
ovcc_xmlutils_read_uint (const gchar *num,
GError **error)
{
GError *err = NULL;
gulong v_l;
v_l = ovcc_xmlutils_read_ulong (num, &err);
if (err) {
g_propagate_error (error, err);
} else if (v_l > G_MAXUINT) {
g_set_error (error, OVCC_XMLUTILS_ERROR, OVCC_XMLUTILS_ERROR_OUT_RANGE,
"number '%s' is out of range (> %u)", num, G_MAXUINT);
}
return (guint)v_l;
}
guint
ovcc_xmlutils_read_id (const gchar *id,
const gchar *prefix,
GError **error)
{
guint value = 0;
if (! g_str_has_prefix (id, prefix)) {
g_set_error (error, OVCC_XMLUTILS_ERROR, OVCC_XMLUTILS_ERROR_MISSING_PREFIX,
"ID '%s' does not have prefix '%s'", id, prefix);
} else {
id = &id[strlen (prefix)];
value = ovcc_xmlutils_read_uint (id, error);
}
return value;
}
/*
*
* Copyright (C) 2009 Colomban Wendling <ban@herbesfolles.org>
* Jonathan Michalon <studios.chalmion@no-log.org>
*
* 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 3 of the License, 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef H_OVCC_XMLUTILS
#define H_OVCC_XMLUTILS
#include <glib.h>
enum e_OVCCXmlUtilsError
{
OVCC_XMLUTILS_ERROR_MISSING_PREFIX,
OVCC_XMLUTILS_ERROR_MISSING_VALUE,
OVCC_XMLUTILS_ERROR_INVALID_DATA,
OVCC_XMLUTILS_ERROR_OUT_RANGE,
OVCC_XMLUTILS_ERROR_FAILED
};
typedef enum e_OVCCXmlUtilsError OVCCXmlUtilsError;
#define OVCC_XMLUTILS_ERROR (ovcc_xmlutils_error_quark ())
GQuark ovcc_xmlutils_error_quark (void) G_GNUC_CONST;
gulong ovcc_xmlutils_read_ulong (const gchar *num,
GError **error);
guint ovcc_xmlutils_read_uint (const gchar *num,
GError **error);
guint ovcc_xmlutils_read_id (const gchar *id,
const gchar *prefix,
GError **error);
#endif /* guard */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment