# HG changeset patch # User rosfran # Date 1164827218 0 # Node ID 92baff6356c7ebecb6c413a80784c68e7f5bc939 # Parent 1c777fffb66436b2d2a11c64d5661bb83ee0999f [svn r140] Add clean-up methods to GMythURI, and put it as a GObject. diff -r 1c777fffb664 -r 92baff6356c7 gmyth/src/gmyth_uri.c --- a/gmyth/src/gmyth_uri.c Wed Nov 29 17:20:00 2006 +0000 +++ b/gmyth/src/gmyth_uri.c Wed Nov 29 19:06:58 2006 +0000 @@ -1,11 +1,30 @@ /** + * GMyth Library * - * GMythURI utils - * - Extracts and parses a URI char string, in according with the RFC 2396 + * @file gmyth/gmyth_uri.c + * + * @brief

GMythURI utils + * - Extracts and parses a URI char string, in according with the RFC 2396 * [http://www.ietf.org/rfc/rfc2396.txt] + * + * Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia. + * @author Rosfran Borges * - * @author Rosfran Borges (rosfran.borges@indt.org.br) + *//* + * + * This program 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. * + * 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 Lesser 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 */ #include @@ -15,6 +34,120 @@ #include "gmyth_uri.h" #include "gmyth_debug.h" +static void gmyth_string_list_class_init (GMythURIClass *klass); +static void gmyth_uri_init (GMythURI *object); + +static void gmyth_uri_dispose (GObject *object); +static void gmyth_uri_finalize (GObject *object); + +G_DEFINE_TYPE(GMythURI, gmyth_uri, G_TYPE_OBJECT) + +static void +gmyth_uri_parser_setup_and_new( GMythURI *uri, const gchar *value ); + +static void +gmyth_uri_class_init (GMythURIClass *klass) +{ + GObjectClass *gobject_class; + + gobject_class = (GObjectClass *) klass; + + gobject_class->dispose = gmyth_uri_dispose; + gobject_class->finalize = gmyth_uri_finalize; +} + +static void +gmyth_uri_init (GMythURI *gmyth_uri) +{ + gmyth_uri->host = g_string_new(""); + gmyth_uri->fragment = g_string_new(""); + gmyth_uri->password = g_string_new(""); + gmyth_uri->path = g_string_new(""); + gmyth_uri->protocol = g_string_new(""); + gmyth_uri->query = g_string_new(""); + gmyth_uri->uri = g_string_new(""); + gmyth_uri->user = g_string_new(""); +} + +static void +gmyth_uri_dispose (GObject *object) +{ + GMythURI *gmyth_uri = GMYTH_URI(object); + + if ( gmyth_uri->host != NULL ) { + g_string_free( gmyth_uri->host, TRUE ); + gmyth_uri->host = NULL; + } + + if ( gmyth_uri->protocol != NULL ) { + g_string_free( gmyth_uri->protocol, TRUE ); + gmyth_uri->protocol = NULL; + } + + if ( gmyth_uri->path != NULL ) { + g_string_free( gmyth_uri->path, TRUE ); + gmyth_uri->path = NULL; + } + + if ( gmyth_uri->fragment != NULL ) { + g_string_free( gmyth_uri->fragment, TRUE ); + gmyth_uri->fragment = NULL; + } + + if ( gmyth_uri->user != NULL ) { + g_string_free( gmyth_uri->user, TRUE ); + gmyth_uri->user = NULL; + } + + if ( gmyth_uri->password != NULL ) { + g_string_free( gmyth_uri->password, TRUE ); + gmyth_uri->password = NULL; + } + + if ( gmyth_uri->query != NULL ) { + g_string_free( gmyth_uri->query, TRUE ); + gmyth_uri->query = NULL; + } + + G_OBJECT_CLASS (gmyth_uri_parent_class)->dispose (object); +} + +static void +gmyth_uri_finalize (GObject *object) +{ + //GMythURI *gmyth_uri = GMYTH_URI(object); + + g_signal_handlers_destroy (object); + + G_OBJECT_CLASS (gmyth_uri_parent_class)->finalize (object); +} + +/** Creates a new instance of GMythURI. + * + * @return a new instance of GMythURI. + */ +GMythURI * +gmyth_uri_new( ) +{ + GMythURI *gmyth_uri = GMYTH_URI (g_object_new (GMYTH_URI_TYPE, NULL)); + + return gmyth_uri; +} + +/** Creates a new instance of GMythURI. + * + * @return a new instance of GMythURI. + */ +GMythURI * +gmyth_uri_new_with_value( const gchar *value ) +{ + GMythURI *gmyth_uri = GMYTH_URI (g_object_new (GMYTH_URI_TYPE, NULL)); + + gmyth_uri_parser_setup_and_new( gmyth_uri, value ); + + return gmyth_uri; +} + static gint gmyth_strstr( const gchar *haystack, const gchar *needle ) { @@ -66,42 +199,25 @@ } -static GMythURI * -gmyth_uri_init( ) -{ - GMythURI *uri = g_new0( GMythURI, 1 ); - uri->host = g_string_new(""); - uri->fragment = g_string_new(""); - uri->password = g_string_new(""); - uri->path = g_string_new(""); - uri->protocol = g_string_new(""); - uri->query = g_string_new(""); - uri->uri = g_string_new(""); - uri->user = g_string_new(""); - return uri; -} - -const GMythURI * -gmyth_uri_new( const gchar *value ) +static void +gmyth_uri_parser_setup_and_new( GMythURI *uri, const gchar *value ) { - GMythURI *uri = gmyth_uri_init(); - - gchar *protocol; - gint uriLen; - gint currIdx; - gint protoIdx; - gint atIdx; - gint colonIdx; - gint shashIdx; - gchar *host; - gint eblacketIdx; + gchar *protocol; + gint uriLen; + gint currIdx; + gint protoIdx; + gint atIdx; + gint colonIdx; + gint shashIdx; + gchar *host; + gint eblacketIdx; GString *hostStr; GString *portStr; - gint hostLen; - gint sharpIdx; - gint questionIdx; - gint queryLen; + gint hostLen; + gint sharpIdx; + gint questionIdx; + gint queryLen; uriLen = strlen(value); uri->uri = g_string_new( value ); @@ -153,7 +269,7 @@ /**** port ****/ portStr = g_string_new(""); portStr = g_string_append_len( portStr, hostStr->str+colonIdx+1, hostLen-colonIdx-1 ); - uri->port = atoi( portStr->str ); + uri->port = (gint)g_ascii_strtoull( portStr->str, NULL, 10 ); g_string_free( portStr, TRUE ); g_string_free( hostStr, FALSE ); } @@ -204,12 +320,10 @@ queryLen -= uriLen - (currIdx+sharpIdx+1); uri->query = g_string_append_len( uri->query, value+currIdx+questionIdx+1, queryLen ); } - g_print( "[%s] host = %s, port = %d, path = %s, query = %s, fragment = %s, "\ + gmyth_debug( "[%s] GMythURI: host = %s, port = %d, path = %s, query = %s, fragment = %s, "\ "user = %s, password = %s.\n", __FUNCTION__, uri->host->str, uri->port, uri->path->str, uri->query->str, uri->fragment->str, uri->user->str, uri->password->str ); - - return uri; } diff -r 1c777fffb664 -r 92baff6356c7 gmyth/src/gmyth_uri.h --- a/gmyth/src/gmyth_uri.h Wed Nov 29 17:20:00 2006 +0000 +++ b/gmyth/src/gmyth_uri.h Wed Nov 29 19:06:58 2006 +0000 @@ -1,47 +1,94 @@ /** + * GMyth Library * - * GMythURI utils + * @file gmyth/gmyth_uri.h + * + * @brief

GMythURI utils * - Extracts and parses a URI char string, in according with the RFC 2396 * [http://www.ietf.org/rfc/rfc2396.txt] + * + * Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia. + * @author Rosfran Borges * - * @author Rosfran Borges (rosfran.borges@indt.org.br) + *//* * + * This program 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. + * + * 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 Lesser 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 */ #ifndef _GMYTH_URI_H_ #define _GMYTH_URI_H_ #include +#include + +#include +#include +#include + +G_BEGIN_DECLS + +#define GMYTH_URI_TYPE (gmyth_uri_get_type ()) +#define GMYTH_URI(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GMYTH_URI_TYPE, GMythURI)) +#define GMYTH_URI_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GMYTH_URI_TYPE, GMythURIClass)) +#define IS_GMYTH_URI(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GMYTH_URI_TYPE)) +#define IS_GMYTH_URI_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GMYTH_URI_TYPE)) +#define GMYTH_URI_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GMYTH_URI_TYPE, GMythURIClass)) + +typedef struct _GMythURI GMythURI; +typedef struct _GMythURIClass GMythURIClass; /**************************************** * Define ****************************************/ -#define GMYTH_URI_KNKOWN_PORT (-1) -#define GMYTH_URI_DEFAULT_HTTP_PORT 80 -#define GMYTH_URI_DEFAULT_FTP_PORT 21 -#define GMYTH_URI_DEFAULT_PATH "/" -#define GMYTH_URI_MAXLEN 256 +#define GMYTH_URI_KNKOWN_PORT (-1) +#define GMYTH_URI_DEFAULT_HTTP_PORT 80 +#define GMYTH_URI_DEFAULT_FTP_PORT 21 +#define GMYTH_URI_DEFAULT_PATH "/" +#define GMYTH_URI_MAXLEN 256 -#define GMYTH_URI_PROTOCOL_DELIM "://" -#define GMYTH_URI_USER_DELIM "@" -#define GMYTH_URI_COLON_DELIM ":" -#define GMYTH_URI_SLASH_DELIM "/" -#define GMYTH_URI_SBLACET_DELIM "[" -#define GMYTH_URI_EBLACET_DELIM "]" -#define GMYTH_URI_SHARP_DELIM "#" -#define GMYTH_URI_QUESTION_DELIM "?" -#define GMYTH_URI_ESCAPING_CHAR "%" +#define GMYTH_URI_PROTOCOL_DELIM "://" +#define GMYTH_URI_USER_DELIM "@" +#define GMYTH_URI_COLON_DELIM ":" +#define GMYTH_URI_SLASH_DELIM "/" +#define GMYTH_URI_SBLACET_DELIM "[" +#define GMYTH_URI_EBLACET_DELIM "]" +#define GMYTH_URI_SHARP_DELIM "#" +#define GMYTH_URI_QUESTION_DELIM "?" +#define GMYTH_URI_ESCAPING_CHAR "%" -#define GMYTH_URI_PROTOCOL_MYTH "myth" -#define GMYTH_URI_PROTOCOL_HTTP "http" -#define GMYTH_URI_PROTOCOL_FTP "ftp" +#define GMYTH_URI_PROTOCOL_MYTH "myth" +#define GMYTH_URI_PROTOCOL_HTTP "http" +#define GMYTH_URI_PROTOCOL_FTP "ftp" /**************************************** * Data Type ****************************************/ -typedef struct _GMythURI { +struct _GMythURIClass +{ + GObjectClass parent_class; + + /* callbacks */ + /* no one for now */ +}; + +struct _GMythURI { + + GObject parent; + GString *uri; GString *host; gint port; @@ -51,19 +98,26 @@ GString *user; GString *password; GString *query; -} GMythURI; + +}; -const GMythURI *gmyth_uri_new( const gchar *value ); +GType gmyth_uri_get_type (void); -gboolean gmyth_uri_is_equals( GMythURI* uri1, GMythURI* uri2 ); +GMythURI* gmyth_uri_new ( void ); -#define gmyth_uri_get_host(urip) (urip->host->str) -#define gmyth_uri_get_port(urip) (urip->port) -#define gmyth_uri_get_protocol(urip) (urip->protocol->str) -#define gmyth_uri_get_path(urip) (urip->path->str) -#define gmyth_uri_get_user(urip) (urip->user->str) -#define gmyth_uri_get_password(urip) (urip->password->str) -#define gmyth_uri_get_fragment(urip) (urip->fragment->str) -#define gmyth_uri_get_query(urip) (urip->query->str) +GMythURI* gmyth_uri_new_with_value ( const gchar *value ); -#endif +gboolean gmyth_uri_is_equals( GMythURI* uri1, GMythURI* uri2 ); + +#define gmyth_uri_get_host(urip) (urip->host->str) +#define gmyth_uri_get_port(urip) (urip->port) +#define gmyth_uri_get_protocol(urip) (urip->protocol->str) +#define gmyth_uri_get_path(urip) (urip->path->str) +#define gmyth_uri_get_user(urip) (urip->user->str) +#define gmyth_uri_get_password(urip) (urip->password->str) +#define gmyth_uri_get_fragment(urip) (urip->fragment->str) +#define gmyth_uri_get_query(urip) (urip->query->str) + +G_END_DECLS + +#endif /* _GMYTH_URI_H_ */