From: Thadeu Lima de Souza Cascardo Date: Sun, 12 Oct 2008 18:10:36 +0000 (-0300) Subject: Moved core implementation to the library X-Git-Url: http://git.cascardo.eti.br/?p=cascardo%2Fatompub.git;a=commitdiff_plain;h=9c238e08012656b810a31535858b292c7f0197f0 Moved core implementation to the library --- diff --git a/atom/Makefile.am b/atom/Makefile.am index 2261251..9a1def6 100644 --- a/atom/Makefile.am +++ b/atom/Makefile.am @@ -1,5 +1,6 @@ lib_LTLIBRARIES = libatom.la -libatom_la_SOURCES = id.c entry.c person.c feed.c resource.c +libatom_la_SOURCES = id.c entry.c person.c feed.c resource.c \ + config.c ctx.c backend.c error.c libatom_la_CFLAGS = -I$(top_srcdir)/include $(GLIB_CFLAGS) libatom_la_CFLAGS += $(XML_CFLAGS) $(GIO_CFLAGS) libatom_la_LIBADD = $(GIO_LIBS) $(GLIB_LIBS) $(XML_LIBS) diff --git a/atom/backend.c b/atom/backend.c new file mode 100644 index 0000000..7fe3beb --- /dev/null +++ b/atom/backend.c @@ -0,0 +1,182 @@ +/* + * Copyright (C) 2007 Thadeu Lima de Souza Cascardo + * + * 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 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, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + + +#include + +#include + +#include + +struct _atom_backend +{ + AtomEntry * (*retrieve_entry) (AtomCtx *, AtomID *); + void (*enumerate_entries) (AtomCtx *, char ***, AtomEntry ***, size_t *); + int (*is_feed) (AtomCtx *, AtomID *); +}; + +AtomBackend * +atom_backend_new () +{ + AtomBackend *backend; + backend = g_slice_new (AtomBackend); + backend->retrieve_entry = NULL; + backend->enumerate_entries = NULL; + return backend; +} + +void +atom_backend_delete (AtomBackend *backend) +{ + g_slice_free (AtomBackend, backend); +} + +void +atom_backend_retrieve_entry_set (AtomBackend *backend, + AtomEntry *retrieve_entry (AtomCtx *, + AtomID *)) +{ + backend->retrieve_entry = retrieve_entry; +} + +void +atom_backend_enumerate_entries_set (AtomBackend *backend, + void enumerate_entries (AtomCtx *, + char ***, + AtomEntry ***, + size_t *)) +{ + backend->enumerate_entries = enumerate_entries; +} + +void +atom_backend_is_feed_set (AtomBackend *backend, + int is_feed (AtomCtx *, AtomID *)) +{ + backend->is_feed = is_feed; +} + +AtomEntry * +atom_retrieve_entry (AtomCtx *ctx, AtomID *id) +{ + AtomBackend *backend; + backend = atom_backend (ctx); + if (backend && backend->retrieve_entry) + return backend->retrieve_entry (ctx, id); + return NULL; +} + +void +atom_backend_enumerate_entries (AtomCtx *ctx, char *** reqs, + AtomEntry *** entries, size_t *len) +{ + AtomBackend *backend; + char **rreqs = NULL; + AtomEntry **rentries = NULL; + size_t rlen = 0; + int i; + backend = atom_backend (ctx); + if (backend && backend->enumerate_entries) + { + backend->enumerate_entries (ctx, &rreqs, &rentries, &rlen); + } + if (reqs) + { + *reqs = rreqs; + } + else + { + for (i = 0; i < rlen; i++) + g_free (rreqs[i]); + g_free (rreqs); + } + if (entries) + { + *entries = rentries; + } + else + { + for (i = 0; i < rlen; i++) + atom_entry_delete (rentries[i]); + g_free (rentries); + } + if (len) + *len = rlen; +} + +int +atom_is_feed (AtomCtx *ctx, AtomID *id) +{ + AtomBackend *backend; + AtomError *aerr; + backend = atom_backend (ctx); + if (backend && backend->is_feed) + { + return backend->is_feed (ctx, id); + } + /* Frontend may make the decision of whether the requested resource is + * a feed or not. If it is not able to do so and backend isn't either, + * it is an error. + */ + aerr = atom_error_new (); + atom_error_code_set (aerr, 404); + atom_error_message_set (aerr, "Not Found"); + atom_error_set (ctx, aerr); + return 0; +} + +AtomFeed * +atom_retrieve_feed (AtomCtx *ctx) +{ + AtomFeed *feed; + AtomEntry **entries; + size_t len; + atom_backend_enumerate_entries (ctx, NULL, &entries, &len); + if (atom_error_get (ctx) != NULL) + return NULL; + feed = atom_feed_new (); + atom_feed_entry_append_array (feed, entries, len); + g_free (entries); + return feed; +} + +AtomResource * +atom_retrieve_resource (AtomCtx *ctx, AtomID *id) +{ + AtomResource *res; + res = NULL; + if (atom_is_feed (ctx, id)) + { + AtomFeed *feed; + feed = atom_retrieve_feed (ctx); + if (feed == NULL) + return NULL; + res = atom_resource_new_from_feed (feed); + atom_feed_delete (feed); + } + else if (atom_error_get (ctx) == NULL) + { + AtomEntry *entry; + entry = atom_retrieve_entry (ctx, id); + if (entry == NULL) + return NULL; + res = atom_resource_new_from_entry (entry); + atom_entry_delete (entry); + } + return res; +} diff --git a/atom/config.c b/atom/config.c new file mode 100644 index 0000000..833ae18 --- /dev/null +++ b/atom/config.c @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2007 Thadeu Lima de Souza Cascardo + * + * 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 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, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + + +#include + +extern void gkeyfile_atom_config_init (AtomCtx *); +extern char *gkeyfile_atom_config_get_str (AtomCtx *, char *, char *); + +void +atom_config_init (AtomCtx *ctx) +{ + gkeyfile_atom_config_init (ctx); +} + +char * +atom_config_get_str (AtomCtx *ctx, char *namespace, char *key) +{ + return gkeyfile_atom_config_get_str (ctx, namespace, key); +} diff --git a/atom/ctx.c b/atom/ctx.c new file mode 100644 index 0000000..a472dae --- /dev/null +++ b/atom/ctx.c @@ -0,0 +1,106 @@ +/* + * Copyright (C) 2007 Thadeu Lima de Souza Cascardo + * + * 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 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, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + + +#include + +#include + +struct _atom_ctx +{ + AtomError *error; + gpointer config_data; + AtomBackend *backend; + GHashTable *bemap; +}; + +AtomCtx * +atom_ctx_new () +{ + AtomCtx *ctx; + ctx = g_slice_new (AtomCtx); + ctx->error = NULL; + ctx->config_data = NULL; + ctx->backend = NULL; + ctx->bemap = g_hash_table_new_full (g_str_hash, g_str_equal, + g_free, g_free); + return ctx; +} + +void +atom_ctx_delete (AtomCtx *ctx) +{ + if (ctx->error) + atom_error_delete (ctx->error); + if (ctx->backend) + atom_backend_delete (ctx->backend); + if (ctx->bemap) + g_hash_table_destroy (ctx->bemap); + g_slice_free (AtomCtx, ctx); +} + +void +atom_error_set (AtomCtx *ctx, AtomError *error) +{ + if (ctx->error) + atom_error_delete (ctx->error); + ctx->error = error; +} + +AtomError * +atom_error_get (AtomCtx *ctx) +{ + return ctx->error; +} + +void * +atom_config_data (AtomCtx *ctx) +{ + return ctx->config_data; +} + +void +atom_config_data_set (AtomCtx *ctx, void *data) +{ + ctx->config_data = data; +} + +AtomBackend * +atom_backend (AtomCtx *ctx) +{ + return ctx->backend; +} + +void +atom_backend_set (AtomCtx *ctx, AtomBackend *backend) +{ + ctx->backend = backend; +} + +void +atom_map_backend_requests (AtomCtx *ctx, char **reqs, + AtomEntry **entries, size_t len) +{ + int i; + for (i = 0; i < len; i++) + { + char *key = g_strdup (atom_entry_id (entries[i])); + char *val = g_strdup (reqs[i]); + g_hash_table_replace (ctx->bemap, key, val); + } +} diff --git a/atom/error.c b/atom/error.c new file mode 100644 index 0000000..a8e08e4 --- /dev/null +++ b/atom/error.c @@ -0,0 +1,113 @@ +/* + * Copyright (C) 2008 Thadeu Lima de Souza Cascardo + * + * 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 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, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + + +#include + +#include +#include + +struct _atom_error +{ + int code; + char * message; +}; + +AtomError * +atom_error_new () +{ + AtomError *error; + error = g_slice_new (AtomError); + error->code = 0; + error->message = NULL; + return error; +} + +void +atom_error_delete (AtomError *error) +{ + if (error->message) + g_free (error->message); + g_slice_free (AtomError, error); +} + +int +atom_error_code (AtomError *error) +{ + return error->code; +} + +void +atom_error_code_set (AtomError *error, int code) +{ + error->code = code; +} + +char * +atom_error_message (AtomError *error) +{ + return error->message; +} + +void +atom_error_message_set (AtomError *error, char *message) +{ + error->message = g_strdup (message); +} + +AtomError * +atom_error_new_from_gerror (GError *error) +{ + AtomError *aerr; + aerr = atom_error_new (); + if (error->domain == G_FILE_ERROR) + { + switch (error->code) + { + case G_FILE_ERROR_EXIST: + case G_FILE_ERROR_ACCES: + case G_FILE_ERROR_PERM: + aerr->code = 403; + break; + case G_FILE_ERROR_NOENT: + aerr->code = 404; + break; + default: + aerr->code = 500; + break; + } + } + else if (error->domain = G_IO_ERROR) + { + switch (error->code) + { + case G_IO_ERROR_NOT_FOUND: + aerr->code = 404; + break; + default: + aerr->code = 500; + break; + } + } + else + { + aerr->code = 500; + } + aerr->message = g_strdup (error->message); + return aerr; +} diff --git a/src/Makefile.am b/src/Makefile.am index cd7c85a..cf39012 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,5 +1,5 @@ bin_PROGRAMS = atompub -atompub_SOURCES = main.c config.c ctx.c backend.c error.c +atompub_SOURCES = main.c atompub_CFLAGS = -I$(top_srcdir)/include atompub_CFLAGS += $(GLIB_CFLAGS) $(GIO_CFLAGS) atompub_LDADD = -L$(top_builddir)/atom -latom diff --git a/src/backend.c b/src/backend.c deleted file mode 100644 index 7fe3beb..0000000 --- a/src/backend.c +++ /dev/null @@ -1,182 +0,0 @@ -/* - * Copyright (C) 2007 Thadeu Lima de Souza Cascardo - * - * 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 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, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - - -#include - -#include - -#include - -struct _atom_backend -{ - AtomEntry * (*retrieve_entry) (AtomCtx *, AtomID *); - void (*enumerate_entries) (AtomCtx *, char ***, AtomEntry ***, size_t *); - int (*is_feed) (AtomCtx *, AtomID *); -}; - -AtomBackend * -atom_backend_new () -{ - AtomBackend *backend; - backend = g_slice_new (AtomBackend); - backend->retrieve_entry = NULL; - backend->enumerate_entries = NULL; - return backend; -} - -void -atom_backend_delete (AtomBackend *backend) -{ - g_slice_free (AtomBackend, backend); -} - -void -atom_backend_retrieve_entry_set (AtomBackend *backend, - AtomEntry *retrieve_entry (AtomCtx *, - AtomID *)) -{ - backend->retrieve_entry = retrieve_entry; -} - -void -atom_backend_enumerate_entries_set (AtomBackend *backend, - void enumerate_entries (AtomCtx *, - char ***, - AtomEntry ***, - size_t *)) -{ - backend->enumerate_entries = enumerate_entries; -} - -void -atom_backend_is_feed_set (AtomBackend *backend, - int is_feed (AtomCtx *, AtomID *)) -{ - backend->is_feed = is_feed; -} - -AtomEntry * -atom_retrieve_entry (AtomCtx *ctx, AtomID *id) -{ - AtomBackend *backend; - backend = atom_backend (ctx); - if (backend && backend->retrieve_entry) - return backend->retrieve_entry (ctx, id); - return NULL; -} - -void -atom_backend_enumerate_entries (AtomCtx *ctx, char *** reqs, - AtomEntry *** entries, size_t *len) -{ - AtomBackend *backend; - char **rreqs = NULL; - AtomEntry **rentries = NULL; - size_t rlen = 0; - int i; - backend = atom_backend (ctx); - if (backend && backend->enumerate_entries) - { - backend->enumerate_entries (ctx, &rreqs, &rentries, &rlen); - } - if (reqs) - { - *reqs = rreqs; - } - else - { - for (i = 0; i < rlen; i++) - g_free (rreqs[i]); - g_free (rreqs); - } - if (entries) - { - *entries = rentries; - } - else - { - for (i = 0; i < rlen; i++) - atom_entry_delete (rentries[i]); - g_free (rentries); - } - if (len) - *len = rlen; -} - -int -atom_is_feed (AtomCtx *ctx, AtomID *id) -{ - AtomBackend *backend; - AtomError *aerr; - backend = atom_backend (ctx); - if (backend && backend->is_feed) - { - return backend->is_feed (ctx, id); - } - /* Frontend may make the decision of whether the requested resource is - * a feed or not. If it is not able to do so and backend isn't either, - * it is an error. - */ - aerr = atom_error_new (); - atom_error_code_set (aerr, 404); - atom_error_message_set (aerr, "Not Found"); - atom_error_set (ctx, aerr); - return 0; -} - -AtomFeed * -atom_retrieve_feed (AtomCtx *ctx) -{ - AtomFeed *feed; - AtomEntry **entries; - size_t len; - atom_backend_enumerate_entries (ctx, NULL, &entries, &len); - if (atom_error_get (ctx) != NULL) - return NULL; - feed = atom_feed_new (); - atom_feed_entry_append_array (feed, entries, len); - g_free (entries); - return feed; -} - -AtomResource * -atom_retrieve_resource (AtomCtx *ctx, AtomID *id) -{ - AtomResource *res; - res = NULL; - if (atom_is_feed (ctx, id)) - { - AtomFeed *feed; - feed = atom_retrieve_feed (ctx); - if (feed == NULL) - return NULL; - res = atom_resource_new_from_feed (feed); - atom_feed_delete (feed); - } - else if (atom_error_get (ctx) == NULL) - { - AtomEntry *entry; - entry = atom_retrieve_entry (ctx, id); - if (entry == NULL) - return NULL; - res = atom_resource_new_from_entry (entry); - atom_entry_delete (entry); - } - return res; -} diff --git a/src/config.c b/src/config.c deleted file mode 100644 index 833ae18..0000000 --- a/src/config.c +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (C) 2007 Thadeu Lima de Souza Cascardo - * - * 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 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, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - - -#include - -extern void gkeyfile_atom_config_init (AtomCtx *); -extern char *gkeyfile_atom_config_get_str (AtomCtx *, char *, char *); - -void -atom_config_init (AtomCtx *ctx) -{ - gkeyfile_atom_config_init (ctx); -} - -char * -atom_config_get_str (AtomCtx *ctx, char *namespace, char *key) -{ - return gkeyfile_atom_config_get_str (ctx, namespace, key); -} diff --git a/src/ctx.c b/src/ctx.c deleted file mode 100644 index a472dae..0000000 --- a/src/ctx.c +++ /dev/null @@ -1,106 +0,0 @@ -/* - * Copyright (C) 2007 Thadeu Lima de Souza Cascardo - * - * 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 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, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - - -#include - -#include - -struct _atom_ctx -{ - AtomError *error; - gpointer config_data; - AtomBackend *backend; - GHashTable *bemap; -}; - -AtomCtx * -atom_ctx_new () -{ - AtomCtx *ctx; - ctx = g_slice_new (AtomCtx); - ctx->error = NULL; - ctx->config_data = NULL; - ctx->backend = NULL; - ctx->bemap = g_hash_table_new_full (g_str_hash, g_str_equal, - g_free, g_free); - return ctx; -} - -void -atom_ctx_delete (AtomCtx *ctx) -{ - if (ctx->error) - atom_error_delete (ctx->error); - if (ctx->backend) - atom_backend_delete (ctx->backend); - if (ctx->bemap) - g_hash_table_destroy (ctx->bemap); - g_slice_free (AtomCtx, ctx); -} - -void -atom_error_set (AtomCtx *ctx, AtomError *error) -{ - if (ctx->error) - atom_error_delete (ctx->error); - ctx->error = error; -} - -AtomError * -atom_error_get (AtomCtx *ctx) -{ - return ctx->error; -} - -void * -atom_config_data (AtomCtx *ctx) -{ - return ctx->config_data; -} - -void -atom_config_data_set (AtomCtx *ctx, void *data) -{ - ctx->config_data = data; -} - -AtomBackend * -atom_backend (AtomCtx *ctx) -{ - return ctx->backend; -} - -void -atom_backend_set (AtomCtx *ctx, AtomBackend *backend) -{ - ctx->backend = backend; -} - -void -atom_map_backend_requests (AtomCtx *ctx, char **reqs, - AtomEntry **entries, size_t len) -{ - int i; - for (i = 0; i < len; i++) - { - char *key = g_strdup (atom_entry_id (entries[i])); - char *val = g_strdup (reqs[i]); - g_hash_table_replace (ctx->bemap, key, val); - } -} diff --git a/src/error.c b/src/error.c deleted file mode 100644 index a8e08e4..0000000 --- a/src/error.c +++ /dev/null @@ -1,113 +0,0 @@ -/* - * Copyright (C) 2008 Thadeu Lima de Souza Cascardo - * - * 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 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, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - - -#include - -#include -#include - -struct _atom_error -{ - int code; - char * message; -}; - -AtomError * -atom_error_new () -{ - AtomError *error; - error = g_slice_new (AtomError); - error->code = 0; - error->message = NULL; - return error; -} - -void -atom_error_delete (AtomError *error) -{ - if (error->message) - g_free (error->message); - g_slice_free (AtomError, error); -} - -int -atom_error_code (AtomError *error) -{ - return error->code; -} - -void -atom_error_code_set (AtomError *error, int code) -{ - error->code = code; -} - -char * -atom_error_message (AtomError *error) -{ - return error->message; -} - -void -atom_error_message_set (AtomError *error, char *message) -{ - error->message = g_strdup (message); -} - -AtomError * -atom_error_new_from_gerror (GError *error) -{ - AtomError *aerr; - aerr = atom_error_new (); - if (error->domain == G_FILE_ERROR) - { - switch (error->code) - { - case G_FILE_ERROR_EXIST: - case G_FILE_ERROR_ACCES: - case G_FILE_ERROR_PERM: - aerr->code = 403; - break; - case G_FILE_ERROR_NOENT: - aerr->code = 404; - break; - default: - aerr->code = 500; - break; - } - } - else if (error->domain = G_IO_ERROR) - { - switch (error->code) - { - case G_IO_ERROR_NOT_FOUND: - aerr->code = 404; - break; - default: - aerr->code = 500; - break; - } - } - else - { - aerr->code = 500; - } - aerr->message = g_strdup (error->message); - return aerr; -}