mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-07-23 10:27:51 +02:00
schema compiler
This commit is contained in:
1
gio/.gitignore
vendored
1
gio/.gitignore
vendored
@@ -3,3 +3,4 @@ gio-marshal.[ch]
|
||||
gioenumtypes.[ch]
|
||||
gioalias.h
|
||||
gioaliasdef.c
|
||||
gschema-compile
|
||||
|
@@ -82,7 +82,9 @@ gio-marshal.c: gio-marshal.h gio-marshal.list
|
||||
settings_sources = \
|
||||
gvdb/gvdb-format.h \
|
||||
gvdb/gvdb-reader.h \
|
||||
gvdb/gvdb-reader.c
|
||||
gvdb/gvdb-reader.c \
|
||||
gsettingsschema.h \
|
||||
gsettingsschema.c
|
||||
|
||||
local_sources = \
|
||||
glocaldirectorymonitor.c \
|
||||
@@ -469,7 +471,7 @@ gioenumtypes.c: $(gio_headers) gioenumtypes.c.template
|
||||
gio-2.0.lib: libgio-2.0.la gio.def
|
||||
lib -machine:@LIB_EXE_MACHINE_FLAG@ -name:libgio-2.0-$(LT_CURRENT_MINUS_AGE).dll -def:gio.def -out:$@
|
||||
|
||||
bin_PROGRAMS = gio-querymodules
|
||||
bin_PROGRAMS = gio-querymodules gschema-compile
|
||||
gio_querymodules_SOURCES = gio-querymodules.c
|
||||
gio_querymodules_LDADD = \
|
||||
$(top_builddir)/glib/libglib-2.0.la \
|
||||
@@ -478,6 +480,13 @@ gio_querymodules_LDADD = \
|
||||
libgio-2.0.la \
|
||||
$(NULL)
|
||||
|
||||
gschema_compile_LDADD = libgio-2.0.la
|
||||
gschema_compile_SOURCES = \
|
||||
gvdb/gvdb-format.h \
|
||||
gvdb/gvdb-builder.h \
|
||||
gvdb/gvdb-builder.c \
|
||||
gschema-compile.c
|
||||
|
||||
dist-hook: $(BUILT_EXTRA_DIST) ../build/win32/vs9/gio.vcproj
|
||||
files='$(BUILT_EXTRA_DIST)'; \
|
||||
for f in $$files; do \
|
||||
|
333
gio/gschema-compile.c
Normal file
333
gio/gschema-compile.c
Normal file
@@ -0,0 +1,333 @@
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <glob.h>
|
||||
|
||||
#include "gvdb/gvdb-builder.h"
|
||||
|
||||
typedef struct
|
||||
{
|
||||
GHashTable *schemas;
|
||||
gchar *schemalist_domain;
|
||||
|
||||
GHashTable *schema;
|
||||
gchar *schema_domain;
|
||||
|
||||
GString *string;
|
||||
|
||||
GvdbItem *key;
|
||||
GVariant *value;
|
||||
GVariant *min, *max;
|
||||
GVariant *strings;
|
||||
gchar *l10n;
|
||||
GVariantType *type;
|
||||
} ParseState;
|
||||
|
||||
static void
|
||||
start_element (GMarkupParseContext *context,
|
||||
const gchar *element_name,
|
||||
const gchar **attribute_names,
|
||||
const gchar **attribute_values,
|
||||
gpointer user_data,
|
||||
GError **error)
|
||||
{
|
||||
ParseState *state = user_data;
|
||||
const GSList *element_stack;
|
||||
const gchar *container;
|
||||
|
||||
element_stack = g_markup_parse_context_get_element_stack (context);
|
||||
container = element_stack->next ? element_stack->next->data : NULL;
|
||||
|
||||
#define COLLECT(first, ...) \
|
||||
g_markup_collect_attributes (element_name, \
|
||||
attribute_names, attribute_values, error, \
|
||||
first, __VA_ARGS__, G_MARKUP_COLLECT_INVALID)
|
||||
#define OPTIONAL G_MARKUP_COLLECT_OPTIONAL
|
||||
#define STRDUP G_MARKUP_COLLECT_STRDUP
|
||||
#define STRING G_MARKUP_COLLECT_STRING
|
||||
#define NO_ARGS() COLLECT (G_MARKUP_COLLECT_INVALID, NULL)
|
||||
|
||||
if (container == NULL)
|
||||
{
|
||||
if (strcmp (element_name, "schemalist") == 0)
|
||||
{
|
||||
COLLECT (OPTIONAL | STRDUP,
|
||||
"gettext-domain",
|
||||
&state->schemalist_domain);
|
||||
return;
|
||||
}
|
||||
}
|
||||
else if (strcmp (container, "schemalist") == 0)
|
||||
{
|
||||
if (strcmp (element_name, "schema") == 0)
|
||||
{
|
||||
const gchar *id, *path;
|
||||
|
||||
if (COLLECT (STRING, "id", &id,
|
||||
OPTIONAL | STRING, "path", &path,
|
||||
OPTIONAL | STRDUP, "gettext-domain",
|
||||
&state->schema_domain))
|
||||
{
|
||||
if (!g_hash_table_lookup (state->schemas, id))
|
||||
{
|
||||
state->schema = gvdb_hash_table_new (state->schemas, id);
|
||||
|
||||
if (path != NULL)
|
||||
gvdb_hash_table_insert_string (state->schema,
|
||||
".path", path);
|
||||
}
|
||||
else
|
||||
g_set_error (error, G_MARKUP_ERROR,
|
||||
G_MARKUP_ERROR_INVALID_CONTENT,
|
||||
"<schema id='%s'> already specified", id);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
else if (strcmp (container, "schema") == 0)
|
||||
{
|
||||
if (strcmp (element_name, "key") == 0)
|
||||
{
|
||||
const gchar *name, *type;
|
||||
|
||||
if (COLLECT (STRING, "name", &name, STRING, "type", &type))
|
||||
{
|
||||
if (!g_hash_table_lookup (state->schema, name))
|
||||
state->key = gvdb_hash_table_insert (state->schema, name);
|
||||
|
||||
else
|
||||
g_set_error (error, G_MARKUP_ERROR,
|
||||
G_MARKUP_ERROR_INVALID_CONTENT,
|
||||
"<key name='%s'> already specified", name);
|
||||
|
||||
if (g_variant_type_string_is_valid (type))
|
||||
state->type = g_variant_type_new (type);
|
||||
|
||||
else
|
||||
g_set_error (error, G_MARKUP_ERROR,
|
||||
G_MARKUP_ERROR_INVALID_CONTENT,
|
||||
"invalid GVariant type string");
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
else if (strcmp (container, "key") == 0)
|
||||
{
|
||||
if (strcmp (element_name, "default") == 0)
|
||||
{
|
||||
if (COLLECT (STRDUP | OPTIONAL, "l10n", &state->l10n))
|
||||
{
|
||||
if (state->l10n != NULL)
|
||||
{
|
||||
if (!g_hash_table_lookup (state->schema, ".gettext-domain"))
|
||||
{
|
||||
const gchar *domain = state->schema_domain ?
|
||||
state->schema_domain :
|
||||
state->schemalist_domain;
|
||||
|
||||
if (domain == NULL)
|
||||
{
|
||||
g_set_error (error, G_MARKUP_ERROR,
|
||||
G_MARKUP_ERROR_INVALID_CONTENT,
|
||||
"l10n requested, but no "
|
||||
"gettext domain given");
|
||||
return;
|
||||
}
|
||||
|
||||
gvdb_hash_table_insert_string (state->schema,
|
||||
".gettext-domain",
|
||||
domain);
|
||||
|
||||
/* XXX: todo: check l10n category validity */
|
||||
}
|
||||
}
|
||||
|
||||
state->string = g_string_new (NULL);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
else if (strcmp (element_name, "summary") == 0)
|
||||
{
|
||||
NO_ARGS ();
|
||||
return;
|
||||
}
|
||||
else if (strcmp (element_name, "description") == 0)
|
||||
{
|
||||
NO_ARGS ();
|
||||
return;
|
||||
}
|
||||
else if (strcmp (element_name, "range") == 0)
|
||||
{
|
||||
NO_ARGS ();
|
||||
return;
|
||||
}
|
||||
}
|
||||
else if (strcmp (container, "range") == 0)
|
||||
{
|
||||
if (strcmp (element_name, "choice") == 0)
|
||||
{
|
||||
gchar *value;
|
||||
|
||||
if (COLLECT (STRDUP, "value", &value))
|
||||
{
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
else if (strcmp (element_name, "min") == 0)
|
||||
{
|
||||
NO_ARGS ();
|
||||
return;
|
||||
}
|
||||
else if (strcmp (element_name, "max") == 0)
|
||||
{
|
||||
NO_ARGS ();
|
||||
return;
|
||||
}
|
||||
}
|
||||
else if (strcmp (container, "choice") == 0)
|
||||
{
|
||||
}
|
||||
|
||||
if (container)
|
||||
g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
|
||||
"Element <%s> not allowed inside <%s>\n",
|
||||
element_name, container);
|
||||
else
|
||||
g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
|
||||
"Element <%s> not allowed at toplevel\n", element_name);
|
||||
}
|
||||
|
||||
static void
|
||||
end_element (GMarkupParseContext *context,
|
||||
const gchar *element_name,
|
||||
gpointer user_data,
|
||||
GError **error)
|
||||
{
|
||||
ParseState *state = user_data;
|
||||
|
||||
if (strcmp (element_name, "default") == 0)
|
||||
{
|
||||
state->value = g_variant_parse (state->type, state->string->str,
|
||||
state->string->str + state->string->len,
|
||||
NULL, error);
|
||||
}
|
||||
|
||||
else if (strcmp (element_name, "key") == 0)
|
||||
{
|
||||
gvdb_item_set_value (state->key, state->value);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
text (GMarkupParseContext *context,
|
||||
const gchar *text,
|
||||
gsize text_len,
|
||||
gpointer user_data,
|
||||
GError **error)
|
||||
{
|
||||
ParseState *state = user_data;
|
||||
gsize i;
|
||||
|
||||
for (i = 0; i < text_len; i++)
|
||||
if (!g_ascii_isspace (text[i]))
|
||||
{
|
||||
if (state->string)
|
||||
g_string_append_len (state->string, text, text_len);
|
||||
|
||||
else
|
||||
g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
|
||||
"text may not appear inside <%s>\n",
|
||||
g_markup_parse_context_get_element (context));
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static GHashTable *
|
||||
parse_gschema_files (gchar **files,
|
||||
GError **error)
|
||||
{
|
||||
GMarkupParser parser = { start_element, end_element, text };
|
||||
GMarkupParseContext *context;
|
||||
ParseState state = { 0, };
|
||||
const gchar *filename;
|
||||
|
||||
context = g_markup_parse_context_new (&parser,
|
||||
G_MARKUP_PREFIX_ERROR_POSITION,
|
||||
&state, NULL);
|
||||
state.schemas = gvdb_hash_table_new (NULL, NULL);
|
||||
|
||||
while ((filename = *files++))
|
||||
{
|
||||
gchar *contents;
|
||||
gsize size;
|
||||
|
||||
if (!g_file_get_contents (filename, &contents, &size, error))
|
||||
return FALSE;
|
||||
|
||||
if (!g_markup_parse_context_parse (context, contents, size, error))
|
||||
{
|
||||
g_prefix_error (error, "%s: ", filename);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if (!g_markup_parse_context_end_parse (context, error))
|
||||
{
|
||||
g_prefix_error (error, "%s: ", filename);
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
return state.schemas;
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc, char **argv)
|
||||
{
|
||||
GError *error = NULL;
|
||||
GHashTable *table;
|
||||
glob_t matched;
|
||||
gint status;
|
||||
|
||||
if (argc != 2)
|
||||
{
|
||||
fprintf (stderr, "you should give exactly one directory name\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (chdir (argv[1]))
|
||||
{
|
||||
perror ("chdir");
|
||||
return 1;
|
||||
}
|
||||
|
||||
status = glob ("*.gschema", 0, NULL, &matched);
|
||||
|
||||
if (status == GLOB_ABORTED)
|
||||
{
|
||||
perror ("glob");
|
||||
return 1;
|
||||
}
|
||||
else if (status == GLOB_NOMATCH)
|
||||
{
|
||||
fprintf (stderr, "no schema files found\n");
|
||||
return 1;
|
||||
}
|
||||
else if (status != 0)
|
||||
{
|
||||
fprintf (stderr, "unknown glob error\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!(table = parse_gschema_files (matched.gl_pathv, &error)) ||
|
||||
!gvdb_table_write_contents (table, "compiled", &error))
|
||||
{
|
||||
fprintf (stderr, "%s\n", error->message);
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
@@ -1,11 +1,11 @@
|
||||
#include "gvdb-builder.h"
|
||||
#include "gvdb-format.h"
|
||||
|
||||
#include <glib.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "gvdb.h"
|
||||
|
||||
struct _GvdbItem
|
||||
{
|
||||
@@ -181,8 +181,6 @@ hash_table_insert (gpointer key,
|
||||
HashTable *table = data;
|
||||
GvdbItem *item = value;
|
||||
|
||||
g_print ("i see %s\n", (char*) key);
|
||||
|
||||
hash_value = djb_hash (key);
|
||||
bucket = hash_value % table->n_buckets;
|
||||
item->next = table->buckets[bucket];
|
||||
@@ -440,62 +438,59 @@ file_builder_new (void)
|
||||
return builder;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
file_builder_write (FileBuilder *fb,
|
||||
GOutputStream *output,
|
||||
struct gvdb_pointer root,
|
||||
GError **error)
|
||||
static GString *
|
||||
file_builder_serialise (FileBuilder *fb,
|
||||
struct gvdb_pointer root)
|
||||
{
|
||||
struct gvdb_header header = { { GVDB_SIGNATURE0, GVDB_SIGNATURE1 } };
|
||||
gchar zero[8] = { 0, };
|
||||
gsize offset;
|
||||
GString *result = g_string_new (NULL);
|
||||
|
||||
result = g_string_new (NULL);
|
||||
|
||||
header.root = root;
|
||||
if (!g_output_stream_write_all (output, &header, sizeof header,
|
||||
NULL, NULL, error))
|
||||
return FALSE;
|
||||
g_string_append_len (result, (gpointer) &header, sizeof header);
|
||||
|
||||
offset = sizeof header;
|
||||
while (!g_queue_is_empty (fb->chunks))
|
||||
{
|
||||
FileChunk *chunk = g_queue_pop_head (fb->chunks);
|
||||
|
||||
if (chunk->offset != offset)
|
||||
if (result->len != chunk->offset)
|
||||
{
|
||||
g_assert (chunk->offset > offset);
|
||||
g_assert (chunk->offset - offset < 8);
|
||||
gchar zero[8] = { 0, };
|
||||
|
||||
if (!g_output_stream_write_all (output, zero,
|
||||
chunk->offset - offset,
|
||||
NULL, NULL, error))
|
||||
return FALSE;
|
||||
g_assert (chunk->offset > result->len);
|
||||
g_assert (chunk->offset - result->len < 8);
|
||||
|
||||
offset = chunk->offset;
|
||||
g_string_append_len (result, zero, chunk->offset - result->len);
|
||||
g_assert (result->len == chunk->offset);
|
||||
}
|
||||
|
||||
if (!g_output_stream_write_all (output, chunk->data, chunk->size,
|
||||
NULL, NULL, error))
|
||||
return FALSE;
|
||||
|
||||
offset += chunk->size;
|
||||
g_string_append_len (result, chunk->data, chunk->size);
|
||||
g_free (chunk->data);
|
||||
}
|
||||
|
||||
g_queue_free (fb->chunks);
|
||||
g_slice_free (FileBuilder, fb);
|
||||
|
||||
return TRUE;
|
||||
return result;
|
||||
}
|
||||
|
||||
gboolean
|
||||
gvdb_file_write (GOutputStream *output,
|
||||
GHashTable *table,
|
||||
GError **error)
|
||||
gvdb_table_write_contents (GHashTable *table,
|
||||
const gchar *filename,
|
||||
GError **error)
|
||||
{
|
||||
struct gvdb_pointer root;
|
||||
gboolean status;
|
||||
FileBuilder *fb;
|
||||
GString *str;
|
||||
|
||||
fb = file_builder_new ();
|
||||
file_builder_add_hash (fb, table, &root);
|
||||
return file_builder_write (fb, output, root, error);
|
||||
str = file_builder_serialise (fb, root);
|
||||
|
||||
status = g_file_set_contents (filename, str->str, str->len, error);
|
||||
g_string_free (str, TRUE);
|
||||
|
||||
return status;
|
||||
}
|
||||
|
@@ -50,8 +50,8 @@ void gvdb_item_set_parent (GvdbIte
|
||||
GvdbItem *parent);
|
||||
|
||||
G_GNUC_INTERNAL
|
||||
gboolean gvdb_file_write (GOutputStream *output,
|
||||
GHashTable *table,
|
||||
gboolean gvdb_table_write_contents (GHashTable *table,
|
||||
const gchar *filename,
|
||||
GError **error);
|
||||
|
||||
#endif /* __gvdb_builder_h__ */
|
||||
|
@@ -388,7 +388,7 @@ gvdb_table_get_value (GvdbTable *file,
|
||||
}
|
||||
|
||||
/**
|
||||
* gvdb_table_get_hash:
|
||||
* gvdb_table_get_table:
|
||||
* @file: a #GvdbTable
|
||||
* @key: a string
|
||||
* @returns: a new #GvdbTable, or %NULL
|
||||
@@ -407,8 +407,8 @@ gvdb_table_get_value (GvdbTable *file,
|
||||
* longer require it.
|
||||
**/
|
||||
GvdbTable *
|
||||
gvdb_table_get_hash (GvdbTable *file,
|
||||
const gchar *key)
|
||||
gvdb_table_get_table (GvdbTable *file,
|
||||
const gchar *key)
|
||||
{
|
||||
const struct gvdb_hash_item *item;
|
||||
GvdbTable *new;
|
||||
|
Reference in New Issue
Block a user