mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2024-11-10 03:16:17 +01:00
New module to parse a simple markup language
2000-10-24 Havoc Pennington <hp@pobox.com> * gmarkup.h, gmarkup.c: New module to parse a simple markup language * Makefile.am: add gmarkup.h, gmarkup.c * tests/Makefile.am: add markup-test * gstring.h (g_string_new_len): new function to create a string with a length (g_string_new): avoid a gratuitous realloc
This commit is contained in:
parent
7ea09e4589
commit
32ef70d4b2
13
ChangeLog
13
ChangeLog
@ -1,3 +1,16 @@
|
||||
2000-10-24 Havoc Pennington <hp@pobox.com>
|
||||
|
||||
* gmarkup.h, gmarkup.c: New module to parse a simple
|
||||
markup language
|
||||
|
||||
* Makefile.am: add gmarkup.h, gmarkup.c
|
||||
|
||||
* tests/Makefile.am: add markup-test
|
||||
|
||||
* gstring.h (g_string_new_len): new function to create a string
|
||||
with a length
|
||||
(g_string_new): avoid a gratuitous realloc
|
||||
|
||||
2000-10-26 Tor Lillqvist <tml@iki.fi>
|
||||
|
||||
* makefile.{mingw,msc}.in: Cosmetics.
|
||||
|
@ -1,3 +1,16 @@
|
||||
2000-10-24 Havoc Pennington <hp@pobox.com>
|
||||
|
||||
* gmarkup.h, gmarkup.c: New module to parse a simple
|
||||
markup language
|
||||
|
||||
* Makefile.am: add gmarkup.h, gmarkup.c
|
||||
|
||||
* tests/Makefile.am: add markup-test
|
||||
|
||||
* gstring.h (g_string_new_len): new function to create a string
|
||||
with a length
|
||||
(g_string_new): avoid a gratuitous realloc
|
||||
|
||||
2000-10-26 Tor Lillqvist <tml@iki.fi>
|
||||
|
||||
* makefile.{mingw,msc}.in: Cosmetics.
|
||||
|
@ -1,3 +1,16 @@
|
||||
2000-10-24 Havoc Pennington <hp@pobox.com>
|
||||
|
||||
* gmarkup.h, gmarkup.c: New module to parse a simple
|
||||
markup language
|
||||
|
||||
* Makefile.am: add gmarkup.h, gmarkup.c
|
||||
|
||||
* tests/Makefile.am: add markup-test
|
||||
|
||||
* gstring.h (g_string_new_len): new function to create a string
|
||||
with a length
|
||||
(g_string_new): avoid a gratuitous realloc
|
||||
|
||||
2000-10-26 Tor Lillqvist <tml@iki.fi>
|
||||
|
||||
* makefile.{mingw,msc}.in: Cosmetics.
|
||||
|
@ -1,3 +1,16 @@
|
||||
2000-10-24 Havoc Pennington <hp@pobox.com>
|
||||
|
||||
* gmarkup.h, gmarkup.c: New module to parse a simple
|
||||
markup language
|
||||
|
||||
* Makefile.am: add gmarkup.h, gmarkup.c
|
||||
|
||||
* tests/Makefile.am: add markup-test
|
||||
|
||||
* gstring.h (g_string_new_len): new function to create a string
|
||||
with a length
|
||||
(g_string_new): avoid a gratuitous realloc
|
||||
|
||||
2000-10-26 Tor Lillqvist <tml@iki.fi>
|
||||
|
||||
* makefile.{mingw,msc}.in: Cosmetics.
|
||||
|
@ -1,3 +1,16 @@
|
||||
2000-10-24 Havoc Pennington <hp@pobox.com>
|
||||
|
||||
* gmarkup.h, gmarkup.c: New module to parse a simple
|
||||
markup language
|
||||
|
||||
* Makefile.am: add gmarkup.h, gmarkup.c
|
||||
|
||||
* tests/Makefile.am: add markup-test
|
||||
|
||||
* gstring.h (g_string_new_len): new function to create a string
|
||||
with a length
|
||||
(g_string_new): avoid a gratuitous realloc
|
||||
|
||||
2000-10-26 Tor Lillqvist <tml@iki.fi>
|
||||
|
||||
* makefile.{mingw,msc}.in: Cosmetics.
|
||||
|
@ -1,3 +1,16 @@
|
||||
2000-10-24 Havoc Pennington <hp@pobox.com>
|
||||
|
||||
* gmarkup.h, gmarkup.c: New module to parse a simple
|
||||
markup language
|
||||
|
||||
* Makefile.am: add gmarkup.h, gmarkup.c
|
||||
|
||||
* tests/Makefile.am: add markup-test
|
||||
|
||||
* gstring.h (g_string_new_len): new function to create a string
|
||||
with a length
|
||||
(g_string_new): avoid a gratuitous realloc
|
||||
|
||||
2000-10-26 Tor Lillqvist <tml@iki.fi>
|
||||
|
||||
* makefile.{mingw,msc}.in: Cosmetics.
|
||||
|
@ -1,3 +1,16 @@
|
||||
2000-10-24 Havoc Pennington <hp@pobox.com>
|
||||
|
||||
* gmarkup.h, gmarkup.c: New module to parse a simple
|
||||
markup language
|
||||
|
||||
* Makefile.am: add gmarkup.h, gmarkup.c
|
||||
|
||||
* tests/Makefile.am: add markup-test
|
||||
|
||||
* gstring.h (g_string_new_len): new function to create a string
|
||||
with a length
|
||||
(g_string_new): avoid a gratuitous realloc
|
||||
|
||||
2000-10-26 Tor Lillqvist <tml@iki.fi>
|
||||
|
||||
* makefile.{mingw,msc}.in: Cosmetics.
|
||||
|
@ -1,3 +1,16 @@
|
||||
2000-10-24 Havoc Pennington <hp@pobox.com>
|
||||
|
||||
* gmarkup.h, gmarkup.c: New module to parse a simple
|
||||
markup language
|
||||
|
||||
* Makefile.am: add gmarkup.h, gmarkup.c
|
||||
|
||||
* tests/Makefile.am: add markup-test
|
||||
|
||||
* gstring.h (g_string_new_len): new function to create a string
|
||||
with a length
|
||||
(g_string_new): avoid a gratuitous realloc
|
||||
|
||||
2000-10-26 Tor Lillqvist <tml@iki.fi>
|
||||
|
||||
* makefile.{mingw,msc}.in: Cosmetics.
|
||||
|
@ -61,6 +61,7 @@ libglib_1_3_la_SOURCES = \
|
||||
giounix.c \
|
||||
glist.c \
|
||||
gmain.c \
|
||||
gmarkup.c \
|
||||
gmem.c \
|
||||
gmessages.c \
|
||||
gnode.c \
|
||||
@ -109,6 +110,7 @@ glibinclude_HEADERS = \
|
||||
glist.h \
|
||||
gmacros.h \
|
||||
gmain.h \
|
||||
gmarkup.h \
|
||||
gmem.h \
|
||||
gmessages.h \
|
||||
gnode.h \
|
||||
|
1
glib.h
1
glib.h
@ -44,6 +44,7 @@
|
||||
#include <glist.h>
|
||||
#include <gmacros.h>
|
||||
#include <gmain.h>
|
||||
#include <gmarkup.h>
|
||||
#include <gmem.h>
|
||||
#include <gmessages.h>
|
||||
#include <gnode.h>
|
||||
|
@ -61,6 +61,7 @@ libglib_1_3_la_SOURCES = \
|
||||
giounix.c \
|
||||
glist.c \
|
||||
gmain.c \
|
||||
gmarkup.c \
|
||||
gmem.c \
|
||||
gmessages.c \
|
||||
gnode.c \
|
||||
@ -109,6 +110,7 @@ glibinclude_HEADERS = \
|
||||
glist.h \
|
||||
gmacros.h \
|
||||
gmain.h \
|
||||
gmarkup.h \
|
||||
gmem.h \
|
||||
gmessages.h \
|
||||
gnode.h \
|
||||
|
@ -44,6 +44,7 @@
|
||||
#include <glist.h>
|
||||
#include <gmacros.h>
|
||||
#include <gmain.h>
|
||||
#include <gmarkup.h>
|
||||
#include <gmem.h>
|
||||
#include <gmessages.h>
|
||||
#include <gnode.h>
|
||||
|
1721
glib/gmarkup.c
Normal file
1721
glib/gmarkup.c
Normal file
File diff suppressed because it is too large
Load Diff
129
glib/gmarkup.h
Normal file
129
glib/gmarkup.h
Normal file
@ -0,0 +1,129 @@
|
||||
/* gmarkup.h - Simple XML-like string parser/writer
|
||||
*
|
||||
* Copyright 2000 Red Hat, Inc.
|
||||
*
|
||||
* GLib is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as
|
||||
* published by the Free Software Foundation; either version 2 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* GLib 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
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with GLib; see the file COPYING.LIB. If not,
|
||||
* write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
||||
* Boston, MA 02111-1307, USA.
|
||||
*/
|
||||
|
||||
#ifndef __G_MARKUP_H__
|
||||
#define __G_MARKUP_H__
|
||||
|
||||
#include <gerror.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
typedef enum
|
||||
{
|
||||
G_MARKUP_ERROR_BAD_UTF8,
|
||||
G_MARKUP_ERROR_EMPTY,
|
||||
G_MARKUP_ERROR_PARSE,
|
||||
/* These three are primarily intended for specific GMarkupParser
|
||||
* implementations to set.
|
||||
*/
|
||||
G_MARKUP_ERROR_UNKNOWN_ELEMENT,
|
||||
G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
|
||||
G_MARKUP_ERROR_INVALID_CONTENT
|
||||
} GMarkupErrorType;
|
||||
|
||||
#define G_MARKUP_ERROR g_markup_error_quark ()
|
||||
|
||||
GQuark g_markup_error_quark ();
|
||||
|
||||
typedef enum
|
||||
{
|
||||
/* Hmm, can't think of any at the moment */
|
||||
G_MARKUP_FOO = 1 << 0
|
||||
|
||||
} GMarkupParseFlags;
|
||||
|
||||
typedef struct _GMarkupParseContext GMarkupParseContext;
|
||||
typedef struct _GMarkupParser GMarkupParser;
|
||||
|
||||
struct _GMarkupParser
|
||||
{
|
||||
/* Called for open tags <foo bar="baz"> */
|
||||
void (*start_element) (GMarkupParseContext *context,
|
||||
const gchar *element_name,
|
||||
const gchar **attribute_names,
|
||||
const gchar **attribute_values,
|
||||
gpointer user_data,
|
||||
GError **error);
|
||||
|
||||
/* Called for close tags </foo> */
|
||||
void (*end_element) (GMarkupParseContext *context,
|
||||
const gchar *element_name,
|
||||
gpointer user_data,
|
||||
GError **error);
|
||||
|
||||
/* Called for character data */
|
||||
/* text is not nul-terminated */
|
||||
void (*text) (GMarkupParseContext *context,
|
||||
const gchar *text,
|
||||
gint text_len,
|
||||
gpointer user_data,
|
||||
GError **error);
|
||||
|
||||
/* Called for strings that should be re-saved verbatim in this same
|
||||
* position, but are not otherwise interpretable. At the moment
|
||||
* this includes comments and processing instructions.
|
||||
*/
|
||||
/* text is not nul-terminated. */
|
||||
void (*passthrough) (GMarkupParseContext *context,
|
||||
const gchar *passthrough_text,
|
||||
gint text_len,
|
||||
gpointer user_data,
|
||||
GError **error);
|
||||
|
||||
/* Called on error, including one set by other
|
||||
* methods in the vtable. The GError should not be freed.
|
||||
*/
|
||||
void (*error) (GMarkupParseContext *context,
|
||||
GError *error,
|
||||
gpointer user_data);
|
||||
};
|
||||
|
||||
GMarkupParseContext *g_markup_parse_context_new (const GMarkupParser *parser,
|
||||
GMarkupParseFlags flags,
|
||||
gpointer user_data,
|
||||
GDestroyNotify user_data_dnotify);
|
||||
void g_markup_parse_context_free (GMarkupParseContext *context);
|
||||
gboolean g_markup_parse_context_parse (GMarkupParseContext *context,
|
||||
const gchar *text,
|
||||
gint text_len,
|
||||
GError **error);
|
||||
|
||||
gboolean g_markup_parse_context_end_parse (GMarkupParseContext *context,
|
||||
GError **error);
|
||||
|
||||
/* For user-constructed error messages, has no precise semantics */
|
||||
void g_markup_parse_context_get_position (GMarkupParseContext *context,
|
||||
gint *line_number,
|
||||
gint *char_number);
|
||||
|
||||
/* useful when saving */
|
||||
gchar* g_markup_escape_text (const gchar *text,
|
||||
gint length);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __G_MARKUP_H__ */
|
||||
|
@ -243,7 +243,7 @@ g_string_new (const gchar *init)
|
||||
{
|
||||
GString *string;
|
||||
|
||||
string = g_string_sized_new (2);
|
||||
string = g_string_sized_new (init ? strlen (init) + 2 : 2);
|
||||
|
||||
if (init)
|
||||
g_string_append (string, init);
|
||||
@ -251,6 +251,25 @@ g_string_new (const gchar *init)
|
||||
return string;
|
||||
}
|
||||
|
||||
GString*
|
||||
g_string_new_len (const gchar *init,
|
||||
gint len)
|
||||
{
|
||||
GString *string;
|
||||
|
||||
if (len < 0)
|
||||
return g_string_new (init);
|
||||
else
|
||||
{
|
||||
string = g_string_sized_new (len);
|
||||
|
||||
if (init)
|
||||
g_string_append_len (string, init, len);
|
||||
|
||||
return string;
|
||||
}
|
||||
}
|
||||
|
||||
gchar*
|
||||
g_string_free (GString *string,
|
||||
gboolean free_segment)
|
||||
|
@ -53,6 +53,8 @@ gchar* g_string_chunk_insert_const (GStringChunk *chunk,
|
||||
/* Strings
|
||||
*/
|
||||
GString* g_string_new (const gchar *init);
|
||||
GString* g_string_new_len (const gchar *init,
|
||||
gint len);
|
||||
GString* g_string_sized_new (guint dfl_size);
|
||||
gchar* g_string_free (GString *string,
|
||||
gboolean free_segment);
|
||||
|
129
gmarkup.h
Normal file
129
gmarkup.h
Normal file
@ -0,0 +1,129 @@
|
||||
/* gmarkup.h - Simple XML-like string parser/writer
|
||||
*
|
||||
* Copyright 2000 Red Hat, Inc.
|
||||
*
|
||||
* GLib is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as
|
||||
* published by the Free Software Foundation; either version 2 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* GLib 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
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with GLib; see the file COPYING.LIB. If not,
|
||||
* write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
||||
* Boston, MA 02111-1307, USA.
|
||||
*/
|
||||
|
||||
#ifndef __G_MARKUP_H__
|
||||
#define __G_MARKUP_H__
|
||||
|
||||
#include <gerror.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
typedef enum
|
||||
{
|
||||
G_MARKUP_ERROR_BAD_UTF8,
|
||||
G_MARKUP_ERROR_EMPTY,
|
||||
G_MARKUP_ERROR_PARSE,
|
||||
/* These three are primarily intended for specific GMarkupParser
|
||||
* implementations to set.
|
||||
*/
|
||||
G_MARKUP_ERROR_UNKNOWN_ELEMENT,
|
||||
G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
|
||||
G_MARKUP_ERROR_INVALID_CONTENT
|
||||
} GMarkupErrorType;
|
||||
|
||||
#define G_MARKUP_ERROR g_markup_error_quark ()
|
||||
|
||||
GQuark g_markup_error_quark ();
|
||||
|
||||
typedef enum
|
||||
{
|
||||
/* Hmm, can't think of any at the moment */
|
||||
G_MARKUP_FOO = 1 << 0
|
||||
|
||||
} GMarkupParseFlags;
|
||||
|
||||
typedef struct _GMarkupParseContext GMarkupParseContext;
|
||||
typedef struct _GMarkupParser GMarkupParser;
|
||||
|
||||
struct _GMarkupParser
|
||||
{
|
||||
/* Called for open tags <foo bar="baz"> */
|
||||
void (*start_element) (GMarkupParseContext *context,
|
||||
const gchar *element_name,
|
||||
const gchar **attribute_names,
|
||||
const gchar **attribute_values,
|
||||
gpointer user_data,
|
||||
GError **error);
|
||||
|
||||
/* Called for close tags </foo> */
|
||||
void (*end_element) (GMarkupParseContext *context,
|
||||
const gchar *element_name,
|
||||
gpointer user_data,
|
||||
GError **error);
|
||||
|
||||
/* Called for character data */
|
||||
/* text is not nul-terminated */
|
||||
void (*text) (GMarkupParseContext *context,
|
||||
const gchar *text,
|
||||
gint text_len,
|
||||
gpointer user_data,
|
||||
GError **error);
|
||||
|
||||
/* Called for strings that should be re-saved verbatim in this same
|
||||
* position, but are not otherwise interpretable. At the moment
|
||||
* this includes comments and processing instructions.
|
||||
*/
|
||||
/* text is not nul-terminated. */
|
||||
void (*passthrough) (GMarkupParseContext *context,
|
||||
const gchar *passthrough_text,
|
||||
gint text_len,
|
||||
gpointer user_data,
|
||||
GError **error);
|
||||
|
||||
/* Called on error, including one set by other
|
||||
* methods in the vtable. The GError should not be freed.
|
||||
*/
|
||||
void (*error) (GMarkupParseContext *context,
|
||||
GError *error,
|
||||
gpointer user_data);
|
||||
};
|
||||
|
||||
GMarkupParseContext *g_markup_parse_context_new (const GMarkupParser *parser,
|
||||
GMarkupParseFlags flags,
|
||||
gpointer user_data,
|
||||
GDestroyNotify user_data_dnotify);
|
||||
void g_markup_parse_context_free (GMarkupParseContext *context);
|
||||
gboolean g_markup_parse_context_parse (GMarkupParseContext *context,
|
||||
const gchar *text,
|
||||
gint text_len,
|
||||
GError **error);
|
||||
|
||||
gboolean g_markup_parse_context_end_parse (GMarkupParseContext *context,
|
||||
GError **error);
|
||||
|
||||
/* For user-constructed error messages, has no precise semantics */
|
||||
void g_markup_parse_context_get_position (GMarkupParseContext *context,
|
||||
gint *line_number,
|
||||
gint *char_number);
|
||||
|
||||
/* useful when saving */
|
||||
gchar* g_markup_escape_text (const gchar *text,
|
||||
gint length);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __G_MARKUP_H__ */
|
||||
|
21
gstring.c
21
gstring.c
@ -243,7 +243,7 @@ g_string_new (const gchar *init)
|
||||
{
|
||||
GString *string;
|
||||
|
||||
string = g_string_sized_new (2);
|
||||
string = g_string_sized_new (init ? strlen (init) + 2 : 2);
|
||||
|
||||
if (init)
|
||||
g_string_append (string, init);
|
||||
@ -251,6 +251,25 @@ g_string_new (const gchar *init)
|
||||
return string;
|
||||
}
|
||||
|
||||
GString*
|
||||
g_string_new_len (const gchar *init,
|
||||
gint len)
|
||||
{
|
||||
GString *string;
|
||||
|
||||
if (len < 0)
|
||||
return g_string_new (init);
|
||||
else
|
||||
{
|
||||
string = g_string_sized_new (len);
|
||||
|
||||
if (init)
|
||||
g_string_append_len (string, init, len);
|
||||
|
||||
return string;
|
||||
}
|
||||
}
|
||||
|
||||
gchar*
|
||||
g_string_free (GString *string,
|
||||
gboolean free_segment)
|
||||
|
@ -53,6 +53,8 @@ gchar* g_string_chunk_insert_const (GStringChunk *chunk,
|
||||
/* Strings
|
||||
*/
|
||||
GString* g_string_new (const gchar *init);
|
||||
GString* g_string_new_len (const gchar *init,
|
||||
gint len);
|
||||
GString* g_string_sized_new (guint dfl_size);
|
||||
gchar* g_string_free (GString *string,
|
||||
gboolean free_segment);
|
||||
|
@ -18,6 +18,7 @@ TESTS = \
|
||||
gio-test \
|
||||
hash-test \
|
||||
list-test \
|
||||
markup-test \
|
||||
node-test \
|
||||
queue-test \
|
||||
rand-test \
|
||||
@ -43,6 +44,7 @@ dirname_test_LDADD = $(progs_LDADD)
|
||||
gio_test_LDADD = $(progs_LDADD)
|
||||
hash_test_LDADD = $(progs_LDADD)
|
||||
list_test_LDADD = $(progs_LDADD)
|
||||
markup_test_LDADD = $(progs_LDADD)
|
||||
node_test_LDADD = $(progs_LDADD)
|
||||
queue_test_LDADD = $(progs_LDADD)
|
||||
rand_test_LDADD = $(progs_LDADD)
|
||||
|
202
tests/markup-test.c
Normal file
202
tests/markup-test.c
Normal file
@ -0,0 +1,202 @@
|
||||
#include <stdio.h>
|
||||
#include <glib.h>
|
||||
|
||||
static int depth = 0;
|
||||
|
||||
static void
|
||||
indent (int extra)
|
||||
{
|
||||
int i = 0;
|
||||
while (i < depth)
|
||||
{
|
||||
fputs (" ", stdout);
|
||||
++i;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
start_element_handler (GMarkupParseContext *context,
|
||||
const gchar *element_name,
|
||||
const gchar **attribute_names,
|
||||
const gchar **attribute_values,
|
||||
gpointer user_data,
|
||||
GError **error)
|
||||
{
|
||||
int i;
|
||||
|
||||
indent (0);
|
||||
printf ("ELEMENT '%s'\n", element_name);
|
||||
|
||||
i = 0;
|
||||
while (attribute_names[i] != NULL)
|
||||
{
|
||||
indent (1);
|
||||
|
||||
printf ("%s=\"%s\"\n",
|
||||
attribute_names[i],
|
||||
attribute_values[i]);
|
||||
|
||||
++i;
|
||||
}
|
||||
|
||||
++depth;
|
||||
}
|
||||
|
||||
static void
|
||||
end_element_handler (GMarkupParseContext *context,
|
||||
const gchar *element_name,
|
||||
gpointer user_data,
|
||||
GError **error)
|
||||
{
|
||||
--depth;
|
||||
indent (0);
|
||||
printf ("END '%s'\n", element_name);
|
||||
}
|
||||
|
||||
static void
|
||||
text_handler (GMarkupParseContext *context,
|
||||
const gchar *text,
|
||||
gint text_len,
|
||||
gpointer user_data,
|
||||
GError **error)
|
||||
{
|
||||
indent (0);
|
||||
printf ("TEXT '%s'\n", text);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
passthrough_handler (GMarkupParseContext *context,
|
||||
const gchar *passthrough_text,
|
||||
gint text_len,
|
||||
gpointer user_data,
|
||||
GError **error)
|
||||
{
|
||||
indent (0);
|
||||
|
||||
printf ("PASS '%s'\n", passthrough_text);
|
||||
}
|
||||
|
||||
static void
|
||||
error_handler (GMarkupParseContext *context,
|
||||
GError *error,
|
||||
gpointer user_data)
|
||||
{
|
||||
fprintf (stderr, " %s\n", error->message);
|
||||
}
|
||||
|
||||
static GMarkupParser parser = {
|
||||
start_element_handler,
|
||||
end_element_handler,
|
||||
text_handler,
|
||||
passthrough_handler,
|
||||
error_handler
|
||||
};
|
||||
|
||||
static int
|
||||
test_in_chunks (const gchar *contents,
|
||||
gint length,
|
||||
gint chunk_size)
|
||||
{
|
||||
GMarkupParseContext *context;
|
||||
int i = 0;
|
||||
|
||||
context = g_markup_parse_context_new (&parser, 0, NULL, NULL);
|
||||
|
||||
while (i < length)
|
||||
{
|
||||
int this_chunk = MIN (length - i, chunk_size);
|
||||
|
||||
if (!g_markup_parse_context_parse (context,
|
||||
contents + i,
|
||||
this_chunk,
|
||||
NULL))
|
||||
{
|
||||
g_markup_parse_context_free (context);
|
||||
return 1;
|
||||
}
|
||||
|
||||
i += this_chunk;
|
||||
}
|
||||
|
||||
if (!g_markup_parse_context_end_parse (context, NULL))
|
||||
{
|
||||
g_markup_parse_context_free (context);
|
||||
return 1;
|
||||
}
|
||||
|
||||
g_markup_parse_context_free (context);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
test_file (const gchar *filename)
|
||||
{
|
||||
gchar *contents;
|
||||
gint length;
|
||||
GError *error;
|
||||
GMarkupParseContext *context;
|
||||
|
||||
error = NULL;
|
||||
if (!g_file_get_contents (filename,
|
||||
&contents,
|
||||
&length,
|
||||
&error))
|
||||
{
|
||||
fprintf (stderr, "%s\n", error->message);
|
||||
g_error_free (error);
|
||||
return 1;
|
||||
}
|
||||
|
||||
context = g_markup_parse_context_new (&parser, 0, NULL, NULL);
|
||||
|
||||
if (!g_markup_parse_context_parse (context, contents, length, NULL))
|
||||
{
|
||||
g_markup_parse_context_free (context);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!g_markup_parse_context_end_parse (context, NULL))
|
||||
{
|
||||
g_markup_parse_context_free (context);
|
||||
return 1;
|
||||
}
|
||||
|
||||
g_markup_parse_context_free (context);
|
||||
|
||||
/* A byte at a time */
|
||||
if (test_in_chunks (contents, length, 1) != 0)
|
||||
return 1;
|
||||
|
||||
/* 2 bytes */
|
||||
if (test_in_chunks (contents, length, 2) != 0)
|
||||
return 1;
|
||||
|
||||
/*5 bytes */
|
||||
if (test_in_chunks (contents, length, 5) != 0)
|
||||
return 1;
|
||||
|
||||
/* 12 bytes */
|
||||
if (test_in_chunks (contents, length, 12) != 0)
|
||||
return 1;
|
||||
|
||||
/* 1024 bytes */
|
||||
if (test_in_chunks (contents, length, 1024) != 0)
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc,
|
||||
char *argv[])
|
||||
{
|
||||
if (argc > 1)
|
||||
return test_file (argv[1]);
|
||||
else
|
||||
{
|
||||
fprintf (stderr, "Give a markup file on the command line\n");
|
||||
return 1;
|
||||
}
|
||||
}
|
0
tests/markups/fail-1.gmarkup
Normal file
0
tests/markups/fail-1.gmarkup
Normal file
2
tests/markups/fail-10.gmarkup
Normal file
2
tests/markups/fail-10.gmarkup
Normal file
@ -0,0 +1,2 @@
|
||||
<foo>
|
||||
</|foo>
|
4
tests/markups/fail-11.gmarkup
Normal file
4
tests/markups/fail-11.gmarkup
Normal file
@ -0,0 +1,4 @@
|
||||
<foo>
|
||||
<bar>
|
||||
</foo>
|
||||
</bar>
|
1
tests/markups/fail-12.gmarkup
Normal file
1
tests/markups/fail-12.gmarkup
Normal file
@ -0,0 +1 @@
|
||||
</foo>
|
1
tests/markups/fail-13.gmarkup
Normal file
1
tests/markups/fail-13.gmarkup
Normal file
@ -0,0 +1 @@
|
||||
</foo|>
|
2
tests/markups/fail-14.gmarkup
Normal file
2
tests/markups/fail-14.gmarkup
Normal file
@ -0,0 +1,2 @@
|
||||
<foo>
|
||||
<
|
3
tests/markups/fail-15.gmarkup
Normal file
3
tests/markups/fail-15.gmarkup
Normal file
@ -0,0 +1,3 @@
|
||||
<foo>
|
||||
<bar>
|
||||
</bar>
|
1
tests/markups/fail-16.gmarkup
Normal file
1
tests/markups/fail-16.gmarkup
Normal file
@ -0,0 +1 @@
|
||||
<foo/
|
1
tests/markups/fail-17.gmarkup
Normal file
1
tests/markups/fail-17.gmarkup
Normal file
@ -0,0 +1 @@
|
||||
<fo
|
1
tests/markups/fail-18.gmarkup
Normal file
1
tests/markups/fail-18.gmarkup
Normal file
@ -0,0 +1 @@
|
||||
<foo bar
|
1
tests/markups/fail-19.gmarkup
Normal file
1
tests/markups/fail-19.gmarkup
Normal file
@ -0,0 +1 @@
|
||||
<foo
|
1
tests/markups/fail-2.gmarkup
Normal file
1
tests/markups/fail-2.gmarkup
Normal file
@ -0,0 +1 @@
|
||||
蔽借獮
|
1
tests/markups/fail-20.gmarkup
Normal file
1
tests/markups/fail-20.gmarkup
Normal file
@ -0,0 +1 @@
|
||||
<foo bar=
|
1
tests/markups/fail-21.gmarkup
Normal file
1
tests/markups/fail-21.gmarkup
Normal file
@ -0,0 +1 @@
|
||||
<foo bar="fdsf
|
1
tests/markups/fail-22.gmarkup
Normal file
1
tests/markups/fail-22.gmarkup
Normal file
@ -0,0 +1 @@
|
||||
<foo>
|
2
tests/markups/fail-23.gmarkup
Normal file
2
tests/markups/fail-23.gmarkup
Normal file
@ -0,0 +1,2 @@
|
||||
<foo>
|
||||
<fo
|
1
tests/markups/fail-24.gmarkup
Normal file
1
tests/markups/fail-24.gmarkup
Normal file
@ -0,0 +1 @@
|
||||
<!-- dfklsjdf;kljsdf;ljk document ends here
|
1
tests/markups/fail-25.gmarkup
Normal file
1
tests/markups/fail-25.gmarkup
Normal file
@ -0,0 +1 @@
|
||||
<? document ending unexpectedly
|
1
tests/markups/fail-26.gmarkup
Normal file
1
tests/markups/fail-26.gmarkup
Normal file
@ -0,0 +1 @@
|
||||
<foo>&;</foo>
|
1
tests/markups/fail-27.gmarkup
Normal file
1
tests/markups/fail-27.gmarkup
Normal file
@ -0,0 +1 @@
|
||||
<foo>&|;</foo>
|
1
tests/markups/fail-28.gmarkup
Normal file
1
tests/markups/fail-28.gmarkup
Normal file
@ -0,0 +1 @@
|
||||
<foo>&am|;</foo>
|
1
tests/markups/fail-29.gmarkup
Normal file
1
tests/markups/fail-29.gmarkup
Normal file
@ -0,0 +1 @@
|
||||
<foo>&bar;</foo>
|
49
tests/markups/fail-3.gmarkup
Normal file
49
tests/markups/fail-3.gmarkup
Normal file
@ -0,0 +1,49 @@
|
||||
<foobar>
|
||||
Παν語
|
||||
|
||||
This is a list of ways to say hello in various languages. Its purpose is to illustrate a number of scripts.
|
||||
|
||||
(Converted into UTF-8)
|
||||
|
||||
---------------------------------------------------------
|
||||
Arabic السلام عليكم
|
||||
Czech (Ä<>esky) Dobrý den
|
||||
Danish (Dansk) Hej, Goddag
|
||||
English Hello
|
||||
Esperanto Saluton
|
||||
Estonian Tere, Tervist
|
||||
FORTRAN PROGRAM
|
||||
Finnish (Suomi) Hei
|
||||
French (Français) Bonjour, Salut
|
||||
German (Deutsch Nord) Guten Tag
|
||||
German (Deutsch Süd) Grüß Gott
|
||||
Greek (Ελληνικά) Γειά σας
|
||||
Hebrew שלו×<E280A2>
|
||||
Hindi नमसà¥<C3A0>ते, à¨à¤®à¤¸à¥<C3A0>कार।
|
||||
Italiano Ciao, Buon giorno
|
||||
Maltese Ċaw, Saħħa
|
||||
Nederlands, Vlaams Hallo, Dag
|
||||
Norwegian (Norsk) Hei, God dag
|
||||
Polish Dzień dobry, Hej
|
||||
Russian (РуÑ<C692>Ñ<EFBFBD>кий) ЗдравÑ<C2B2>твуйте!‎
|
||||
Slovak Dobrý deň
|
||||
Spanish (Español) ‎¡Hola!‎
|
||||
Swedish (Svenska) Hej, Goddag
|
||||
Thai (ภาษาไทย) สวัสดีครับ, สวัสดีค่ะ
|
||||
Turkish (Türkçe) Merhaba
|
||||
Vietnamese (Tiếng Việt) Xin Chà o
|
||||
Yiddish (ײַדישע) ד×<E2809C>ָס הײַזעלע
|
||||
|
||||
Japanese (日本語) ã<>“ã‚“ã<E2809C>«ã<C2AB>¡ã<C2A1>¯, コï¾<C3AF>ニï¾<C3AF>ハ
|
||||
Chinese (ä¸æ–‡,普通è¯<C3A8>,汉è¯) ä½ å¥½
|
||||
Cantonese (粵語,廣æ<C2A3>±è©±) 早晨, ä½ å¥½
|
||||
Korean (한글) 안녕하세요, 안녕하ì‹ë‹ˆê¹Œ
|
||||
|
||||
Difference among chinese characters in GB, JIS, KSC, BIG5:‎
|
||||
GB -- 元气 å¼€å<E282AC>‘
|
||||
JIS -- 元気 開発
|
||||
KSC -- 元氣 開發
|
||||
BIG5 -- 元氣 開發
|
||||
|
||||
|
||||
</foobar>
|
1
tests/markups/fail-30.gmarkup
Normal file
1
tests/markups/fail-30.gmarkup
Normal file
@ -0,0 +1 @@
|
||||
<foo>&sdfkljsdsdfsdfsdfsdf</foo>
|
1
tests/markups/fail-31.gmarkup
Normal file
1
tests/markups/fail-31.gmarkup
Normal file
@ -0,0 +1 @@
|
||||
<foo>�</foo>
|
1
tests/markups/fail-32.gmarkup
Normal file
1
tests/markups/fail-32.gmarkup
Normal file
@ -0,0 +1 @@
|
||||
<foo></foo>
|
1
tests/markups/fail-33.gmarkup
Normal file
1
tests/markups/fail-33.gmarkup
Normal file
@ -0,0 +1 @@
|
||||
<foo>&#;</foo>
|
1
tests/markups/fail-34.gmarkup
Normal file
1
tests/markups/fail-34.gmarkup
Normal file
@ -0,0 +1 @@
|
||||
<foo>𹋺</foo>
|
1
tests/markups/fail-4.gmarkup
Normal file
1
tests/markups/fail-4.gmarkup
Normal file
@ -0,0 +1 @@
|
||||
foo
|
2
tests/markups/fail-5.gmarkup
Normal file
2
tests/markups/fail-5.gmarkup
Normal file
@ -0,0 +1,2 @@
|
||||
<|foo>
|
||||
</|foo>
|
2
tests/markups/fail-6.gmarkup
Normal file
2
tests/markups/fail-6.gmarkup
Normal file
@ -0,0 +1,2 @@
|
||||
<foo|>
|
||||
</foo>
|
2
tests/markups/fail-7.gmarkup
Normal file
2
tests/markups/fail-7.gmarkup
Normal file
@ -0,0 +1,2 @@
|
||||
<foo bar}"baz">
|
||||
</foo>
|
2
tests/markups/fail-8.gmarkup
Normal file
2
tests/markups/fail-8.gmarkup
Normal file
@ -0,0 +1,2 @@
|
||||
<foo/}>
|
||||
</foo>
|
2
tests/markups/fail-9.gmarkup
Normal file
2
tests/markups/fail-9.gmarkup
Normal file
@ -0,0 +1,2 @@
|
||||
<foo bar={baz">
|
||||
</foo>
|
9
tests/markups/valid-1.gmarkup
Normal file
9
tests/markups/valid-1.gmarkup
Normal file
@ -0,0 +1,9 @@
|
||||
<!-- Comment -->
|
||||
<?PI ?>
|
||||
<foobar>
|
||||
<e1>Hi & this is some text inside an element Two 'E' chars as character refs: E E and some 'J': J J</e1>
|
||||
<e2:foo> Text <childfree/> with some <nested>nested elements</nested> and entities "& < >> ' and whitespace </e2:foo>
|
||||
<tag ab="fo<o" bar="foo" baz="blah">This element has attributes</tag>
|
||||
<nochildren a="b" xyz="qrs"/>
|
||||
</foobar>
|
||||
|
49
tests/markups/valid-2.gmarkup
Normal file
49
tests/markups/valid-2.gmarkup
Normal file
@ -0,0 +1,49 @@
|
||||
<foobar>
|
||||
Παν語
|
||||
|
||||
This is a list of ways to say hello in various languages. Its purpose is to illustrate a number of scripts.
|
||||
|
||||
(Converted into UTF-8)
|
||||
|
||||
---------------------------------------------------------
|
||||
Arabic السلام عليكم
|
||||
Czech (česky) Dobrý den
|
||||
Danish (Dansk) Hej, Goddag
|
||||
English Hello
|
||||
Esperanto Saluton
|
||||
Estonian Tere, Tervist
|
||||
FORTRAN PROGRAM
|
||||
Finnish (Suomi) Hei
|
||||
French (Français) Bonjour, Salut
|
||||
German (Deutsch Nord) Guten Tag
|
||||
German (Deutsch Süd) Grüß Gott
|
||||
Greek (Ελληνικά) Γειά σας
|
||||
Hebrew שלום
|
||||
Hindi नमस्ते, नमस्कार।
|
||||
Italiano Ciao, Buon giorno
|
||||
Maltese Ċaw, Saħħa
|
||||
Nederlands, Vlaams Hallo, Dag
|
||||
Norwegian (Norsk) Hei, God dag
|
||||
Polish Dzień dobry, Hej
|
||||
Russian (Русский) Здравствуйте!
|
||||
Slovak Dobrý deň
|
||||
Spanish (Español) ¡Hola!
|
||||
Swedish (Svenska) Hej, Goddag
|
||||
Thai (ภาษาไทย) สวัสดีครับ, สวัสดีค่ะ
|
||||
Turkish (Türkçe) Merhaba
|
||||
Vietnamese (Tiếng Việt) Xin Chào
|
||||
Yiddish (ײַדישע) דאָס הײַזעלע
|
||||
|
||||
Japanese (日本語) こんにちは, コンニチハ
|
||||
Chinese (中文,普通话,汉语) 你好
|
||||
Cantonese (粵語,廣東話) 早晨, 你好
|
||||
Korean (한글) 안녕하세요, 안녕하십니까
|
||||
|
||||
Difference among chinese characters in GB, JIS, KSC, BIG5:
|
||||
GB -- 元气 开发
|
||||
JIS -- 元気 開発
|
||||
KSC -- 元氣 開發
|
||||
BIG5 -- 元氣 開發
|
||||
|
||||
|
||||
</foobar>
|
19
tests/run-markup-tests.sh
Executable file
19
tests/run-markup-tests.sh
Executable file
@ -0,0 +1,19 @@
|
||||
#! /bin/sh
|
||||
|
||||
fail ()
|
||||
{
|
||||
echo "Test failed: $*"
|
||||
exit 1
|
||||
}
|
||||
|
||||
for I in markups/fail-*.gmarkup; do
|
||||
echo "Parsing $I, should fail"
|
||||
./markup-test $I > /dev/null && fail "failed to generate error on $I"
|
||||
done
|
||||
|
||||
for I in markups/valid-*.gmarkup; do
|
||||
echo "Parsing $I, should succeed"
|
||||
./markup-test $I > /dev/null || fail "failed on $I"
|
||||
done
|
||||
|
||||
echo "All tests passed."
|
Loading…
Reference in New Issue
Block a user