[svn r140] Add clean-up methods to GMythURI, and put it as a GObject. trunk
authorrosfran
Wed Nov 29 19:06:58 2006 +0000 (2006-11-29)
branchtrunk
changeset 13992baff6356c7
parent 138 1c777fffb664
child 140 2d912dd5b845
[svn r140] Add clean-up methods to GMythURI, and put it as a GObject.
gmyth/src/gmyth_uri.c
gmyth/src/gmyth_uri.h
     1.1 --- a/gmyth/src/gmyth_uri.c	Wed Nov 29 17:20:00 2006 +0000
     1.2 +++ b/gmyth/src/gmyth_uri.c	Wed Nov 29 19:06:58 2006 +0000
     1.3 @@ -1,11 +1,30 @@
     1.4  /**
     1.5 + * GMyth Library
     1.6   *
     1.7 - *    GMythURI utils
     1.8 - *  - Extracts and parses a URI char string, in according with the RFC 2396
     1.9 + * @file gmyth/gmyth_uri.c
    1.10 + * 
    1.11 + * @brief <p> GMythURI utils
    1.12 + *  - Extracts and parses a URI char string, in according with the RFC 2396 
    1.13   *    [http://www.ietf.org/rfc/rfc2396.txt]
    1.14 + * 
    1.15 + * Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia.
    1.16 + * @author Rosfran Borges <rosfran.borges@indt.org.br>
    1.17   *
    1.18 - *   @author Rosfran Borges (rosfran.borges@indt.org.br)
    1.19 + *//*
    1.20 + * 
    1.21 + * This program is free software; you can redistribute it and/or modify
    1.22 + * it under the terms of the GNU Lesser General Public License as published by
    1.23 + * the Free Software Foundation; either version 2 of the License, or
    1.24 + * (at your option) any later version.
    1.25   *
    1.26 + * This program is distributed in the hope that it will be useful,
    1.27 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.28 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.29 + * GNU General Public License for more details.
    1.30 + *
    1.31 + * You should have received a copy of the GNU Lesser General Public License
    1.32 + * along with this program; if not, write to the Free Software
    1.33 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.34   */
    1.35  
    1.36  #include <glib.h>
    1.37 @@ -15,6 +34,120 @@
    1.38  #include "gmyth_uri.h"
    1.39  #include "gmyth_debug.h"
    1.40  
    1.41 +static void gmyth_string_list_class_init          (GMythURIClass *klass);
    1.42 +static void gmyth_uri_init                (GMythURI *object);
    1.43 +
    1.44 +static void gmyth_uri_dispose  (GObject *object);
    1.45 +static void gmyth_uri_finalize (GObject *object);
    1.46 +
    1.47 +G_DEFINE_TYPE(GMythURI, gmyth_uri, G_TYPE_OBJECT)
    1.48 +
    1.49 +static void
    1.50 +gmyth_uri_parser_setup_and_new( GMythURI *uri, const gchar *value );
    1.51 +    
    1.52 +static void
    1.53 +gmyth_uri_class_init (GMythURIClass *klass)
    1.54 +{
    1.55 +	GObjectClass *gobject_class;
    1.56 +
    1.57 +    gobject_class = (GObjectClass *) klass;
    1.58 +	
    1.59 +    gobject_class->dispose  = gmyth_uri_dispose;
    1.60 +    gobject_class->finalize = gmyth_uri_finalize;	
    1.61 +}
    1.62 +
    1.63 +static void
    1.64 +gmyth_uri_init (GMythURI *gmyth_uri)
    1.65 +{
    1.66 +	gmyth_uri->host = g_string_new("");
    1.67 +	gmyth_uri->fragment = g_string_new("");
    1.68 +	gmyth_uri->password = g_string_new("");
    1.69 +	gmyth_uri->path = g_string_new("");
    1.70 +	gmyth_uri->protocol = g_string_new("");
    1.71 +	gmyth_uri->query = g_string_new("");
    1.72 +	gmyth_uri->uri = g_string_new("");
    1.73 +	gmyth_uri->user = g_string_new("");
    1.74 +}
    1.75 +
    1.76 +static void
    1.77 +gmyth_uri_dispose  (GObject *object)
    1.78 +{
    1.79 +	GMythURI *gmyth_uri = GMYTH_URI(object);
    1.80 +
    1.81 +	if ( gmyth_uri->host != NULL ) {
    1.82 +		g_string_free( gmyth_uri->host, TRUE );
    1.83 +		gmyth_uri->host = NULL;
    1.84 +	}
    1.85 +	
    1.86 +	if ( gmyth_uri->protocol != NULL ) {
    1.87 +		g_string_free( gmyth_uri->protocol, TRUE );
    1.88 +		gmyth_uri->protocol = NULL;
    1.89 +	}
    1.90 +	
    1.91 +	if ( gmyth_uri->path != NULL ) {
    1.92 +		g_string_free( gmyth_uri->path, TRUE );
    1.93 +		gmyth_uri->path = NULL;
    1.94 +	}
    1.95 +	
    1.96 +	if ( gmyth_uri->fragment != NULL ) {
    1.97 +		g_string_free( gmyth_uri->fragment, TRUE );
    1.98 +		gmyth_uri->fragment = NULL;
    1.99 +	}
   1.100 +	
   1.101 +	if ( gmyth_uri->user != NULL ) {
   1.102 +		g_string_free( gmyth_uri->user, TRUE );
   1.103 +		gmyth_uri->user = NULL;
   1.104 +	}
   1.105 +	
   1.106 +	if ( gmyth_uri->password != NULL ) {
   1.107 +		g_string_free( gmyth_uri->password, TRUE );
   1.108 +		gmyth_uri->password = NULL;
   1.109 +	}
   1.110 +	
   1.111 +	if ( gmyth_uri->query != NULL ) {
   1.112 +		g_string_free( gmyth_uri->query, TRUE );
   1.113 +		gmyth_uri->query = NULL;
   1.114 +	}	
   1.115 +
   1.116 +	G_OBJECT_CLASS (gmyth_uri_parent_class)->dispose (object);
   1.117 +}
   1.118 +
   1.119 +static void
   1.120 +gmyth_uri_finalize (GObject *object)
   1.121 +{
   1.122 +	//GMythURI *gmyth_uri = GMYTH_URI(object);
   1.123 +
   1.124 +	g_signal_handlers_destroy (object);
   1.125 +
   1.126 +	G_OBJECT_CLASS (gmyth_uri_parent_class)->finalize (object);
   1.127 +}
   1.128 +
   1.129 +/** Creates a new instance of GMythURI.
   1.130 + * 
   1.131 + * @return a new instance of GMythURI.
   1.132 + */
   1.133 +GMythURI *
   1.134 +gmyth_uri_new( ) 
   1.135 +{
   1.136 +    GMythURI *gmyth_uri = GMYTH_URI (g_object_new (GMYTH_URI_TYPE, NULL));
   1.137 +    
   1.138 +    return gmyth_uri;
   1.139 +}
   1.140 +
   1.141 +/** Creates a new instance of GMythURI.
   1.142 + * 
   1.143 + * @return a new instance of GMythURI.
   1.144 + */
   1.145 +GMythURI *
   1.146 +gmyth_uri_new_with_value( const gchar *value ) 
   1.147 +{
   1.148 +    GMythURI *gmyth_uri = GMYTH_URI (g_object_new (GMYTH_URI_TYPE, NULL));
   1.149 +    
   1.150 +    gmyth_uri_parser_setup_and_new( gmyth_uri, value );
   1.151 +    
   1.152 +    return gmyth_uri;
   1.153 +}
   1.154 +
   1.155  static gint 
   1.156  gmyth_strstr( const gchar *haystack, const gchar *needle )
   1.157  {
   1.158 @@ -66,42 +199,25 @@
   1.159  
   1.160  }
   1.161  
   1.162 -static GMythURI *
   1.163 -gmyth_uri_init( )
   1.164 -{
   1.165 -	GMythURI *uri = g_new0( GMythURI, 1 );
   1.166 -	uri->host = g_string_new("");
   1.167 -	uri->fragment = g_string_new("");
   1.168 -	uri->password = g_string_new("");
   1.169 -	uri->path = g_string_new("");
   1.170 -	uri->protocol = g_string_new("");
   1.171 -	uri->query = g_string_new("");
   1.172 -	uri->uri = g_string_new("");
   1.173 -	uri->user = g_string_new("");
   1.174 -	return uri;
   1.175 -}
   1.176 -
   1.177 -const GMythURI *
   1.178 -gmyth_uri_new( const gchar *value )
   1.179 +static void
   1.180 +gmyth_uri_parser_setup_and_new( GMythURI *uri, const gchar *value )
   1.181  {
   1.182  	
   1.183 -	GMythURI *uri = gmyth_uri_init();
   1.184 -
   1.185 -	gchar *protocol;
   1.186 -	gint uriLen;
   1.187 -	gint currIdx;
   1.188 -	gint protoIdx;
   1.189 -	gint atIdx;
   1.190 -	gint colonIdx;
   1.191 -	gint shashIdx;
   1.192 -	gchar *host;
   1.193 -	gint eblacketIdx;
   1.194 +	gchar 	*protocol;
   1.195 +	gint 		uriLen;
   1.196 +	gint 		currIdx;
   1.197 +	gint 		protoIdx;
   1.198 +	gint 		atIdx;
   1.199 +	gint 		colonIdx;
   1.200 +	gint 		shashIdx;
   1.201 +	gchar 	*host;
   1.202 +	gint 		eblacketIdx;
   1.203  	GString *hostStr;
   1.204  	GString *portStr;
   1.205 -	gint hostLen;
   1.206 -	gint sharpIdx;
   1.207 -	gint questionIdx;
   1.208 -	gint queryLen;
   1.209 +	gint 		hostLen;
   1.210 +	gint 		sharpIdx;
   1.211 +	gint 		questionIdx;
   1.212 +	gint 		queryLen;
   1.213  	
   1.214  	uriLen = strlen(value);
   1.215  	uri->uri = g_string_new( value );
   1.216 @@ -153,7 +269,7 @@
   1.217  		/**** port ****/
   1.218  		portStr = g_string_new("");
   1.219  		portStr = g_string_append_len( portStr, hostStr->str+colonIdx+1, hostLen-colonIdx-1 );
   1.220 -		uri->port = atoi( portStr->str );
   1.221 +		uri->port = (gint)g_ascii_strtoull( portStr->str, NULL, 10 );
   1.222  		g_string_free( portStr, TRUE );
   1.223  		g_string_free( hostStr, FALSE );
   1.224  	}
   1.225 @@ -204,12 +320,10 @@
   1.226  			queryLen -= uriLen - (currIdx+sharpIdx+1);
   1.227  		uri->query = g_string_append_len( uri->query, value+currIdx+questionIdx+1, queryLen );
   1.228  	}
   1.229 -	g_print( "[%s] host = %s, port = %d, path = %s, query = %s, fragment = %s, "\
   1.230 +	gmyth_debug( "[%s] GMythURI: host = %s, port = %d, path = %s, query = %s, fragment = %s, "\
   1.231  								"user = %s, password = %s.\n", __FUNCTION__, uri->host->str, uri->port,
   1.232  													uri->path->str, uri->query->str, uri->fragment->str,
   1.233  													uri->user->str, uri->password->str );
   1.234 -	
   1.235 -	return uri;
   1.236  
   1.237  }
   1.238  
     2.1 --- a/gmyth/src/gmyth_uri.h	Wed Nov 29 17:20:00 2006 +0000
     2.2 +++ b/gmyth/src/gmyth_uri.h	Wed Nov 29 19:06:58 2006 +0000
     2.3 @@ -1,47 +1,94 @@
     2.4  /**
     2.5 + * GMyth Library
     2.6   *
     2.7 - *	GMythURI utils
     2.8 + * @file gmyth/gmyth_uri.h
     2.9 + * 
    2.10 + * @brief <p> GMythURI utils
    2.11   *  - Extracts and parses a URI char string, in according with the RFC 2396 
    2.12   *    [http://www.ietf.org/rfc/rfc2396.txt]
    2.13 + * 
    2.14 + * Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia.
    2.15 + * @author Rosfran Borges <rosfran.borges@indt.org.br>
    2.16   *
    2.17 - * @author Rosfran Borges (rosfran.borges@indt.org.br)
    2.18 + *//*
    2.19   * 
    2.20 + * This program is free software; you can redistribute it and/or modify
    2.21 + * it under the terms of the GNU Lesser General Public License as published by
    2.22 + * the Free Software Foundation; either version 2 of the License, or
    2.23 + * (at your option) any later version.
    2.24 + *
    2.25 + * This program is distributed in the hope that it will be useful,
    2.26 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.27 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    2.28 + * GNU General Public License for more details.
    2.29 + *
    2.30 + * You should have received a copy of the GNU Lesser General Public License
    2.31 + * along with this program; if not, write to the Free Software
    2.32 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    2.33   */
    2.34  
    2.35  #ifndef _GMYTH_URI_H_
    2.36  #define _GMYTH_URI_H_
    2.37  
    2.38  #include <glib.h>
    2.39 +#include <glib-object.h>
    2.40 +
    2.41 +#include <stdlib.h>
    2.42 +#include <stdio.h>
    2.43 +#include <string.h>
    2.44 +
    2.45 +G_BEGIN_DECLS
    2.46 +
    2.47 +#define GMYTH_URI_TYPE               (gmyth_uri_get_type ())
    2.48 +#define GMYTH_URI(obj)          		 (G_TYPE_CHECK_INSTANCE_CAST ((obj), GMYTH_URI_TYPE, GMythURI))
    2.49 +#define GMYTH_URI_CLASS(klass)       (G_TYPE_CHECK_CLASS_CAST ((klass), GMYTH_URI_TYPE, GMythURIClass))
    2.50 +#define IS_GMYTH_URI(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), GMYTH_URI_TYPE))
    2.51 +#define IS_GMYTH_URI_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), GMYTH_URI_TYPE))
    2.52 +#define GMYTH_URI_GET_CLASS(obj)     (G_TYPE_INSTANCE_GET_CLASS ((obj), GMYTH_URI_TYPE, GMythURIClass))
    2.53 +
    2.54 +typedef struct _GMythURI         GMythURI;
    2.55 +typedef struct _GMythURIClass    GMythURIClass;
    2.56  
    2.57  /****************************************
    2.58  * Define
    2.59  ****************************************/
    2.60  
    2.61 -#define GMYTH_URI_KNKOWN_PORT (-1)
    2.62 -#define GMYTH_URI_DEFAULT_HTTP_PORT 80
    2.63 -#define GMYTH_URI_DEFAULT_FTP_PORT 21
    2.64 -#define GMYTH_URI_DEFAULT_PATH "/"
    2.65 -#define GMYTH_URI_MAXLEN 256
    2.66 +#define GMYTH_URI_KNKOWN_PORT 				(-1)
    2.67 +#define GMYTH_URI_DEFAULT_HTTP_PORT 	80
    2.68 +#define GMYTH_URI_DEFAULT_FTP_PORT 		21
    2.69 +#define GMYTH_URI_DEFAULT_PATH 				"/"
    2.70 +#define GMYTH_URI_MAXLEN 							256
    2.71  
    2.72 -#define GMYTH_URI_PROTOCOL_DELIM "://"
    2.73 -#define GMYTH_URI_USER_DELIM "@"
    2.74 -#define GMYTH_URI_COLON_DELIM ":"
    2.75 -#define GMYTH_URI_SLASH_DELIM "/"
    2.76 -#define GMYTH_URI_SBLACET_DELIM "["
    2.77 -#define GMYTH_URI_EBLACET_DELIM "]"
    2.78 -#define GMYTH_URI_SHARP_DELIM "#"
    2.79 -#define GMYTH_URI_QUESTION_DELIM "?"
    2.80 -#define GMYTH_URI_ESCAPING_CHAR "%"
    2.81 +#define GMYTH_URI_PROTOCOL_DELIM 			"://"
    2.82 +#define GMYTH_URI_USER_DELIM 					"@"
    2.83 +#define GMYTH_URI_COLON_DELIM 				":"
    2.84 +#define GMYTH_URI_SLASH_DELIM 				"/"
    2.85 +#define GMYTH_URI_SBLACET_DELIM 			"["
    2.86 +#define GMYTH_URI_EBLACET_DELIM 			"]"
    2.87 +#define GMYTH_URI_SHARP_DELIM 				"#"
    2.88 +#define GMYTH_URI_QUESTION_DELIM 			"?"
    2.89 +#define GMYTH_URI_ESCAPING_CHAR 			"%"
    2.90  
    2.91 -#define GMYTH_URI_PROTOCOL_MYTH "myth"
    2.92 -#define GMYTH_URI_PROTOCOL_HTTP "http"
    2.93 -#define GMYTH_URI_PROTOCOL_FTP "ftp"
    2.94 +#define GMYTH_URI_PROTOCOL_MYTH 			"myth"
    2.95 +#define GMYTH_URI_PROTOCOL_HTTP 			"http"
    2.96 +#define GMYTH_URI_PROTOCOL_FTP 				"ftp"
    2.97  
    2.98  /****************************************
    2.99  * Data Type
   2.100  ****************************************/
   2.101  
   2.102 -typedef struct _GMythURI {
   2.103 +struct _GMythURIClass
   2.104 +{
   2.105 +  GObjectClass parent_class;
   2.106 +
   2.107 +  /* callbacks */
   2.108 +  /* no one for now */
   2.109 +};
   2.110 +
   2.111 +struct _GMythURI {
   2.112 +	
   2.113 +	GObject parent;
   2.114 +	
   2.115  	GString *uri;
   2.116  	GString *host;
   2.117  	gint port;
   2.118 @@ -51,19 +98,26 @@
   2.119  	GString *user;
   2.120  	GString *password;
   2.121  	GString *query;
   2.122 -} GMythURI;
   2.123 +	
   2.124 +};
   2.125  
   2.126 -const GMythURI *gmyth_uri_new( const gchar *value );
   2.127 +GType               gmyth_uri_get_type (void);
   2.128  
   2.129 -gboolean gmyth_uri_is_equals( GMythURI* uri1, GMythURI* uri2 );
   2.130 +GMythURI*   				gmyth_uri_new ( void );
   2.131  
   2.132 -#define gmyth_uri_get_host(urip) (urip->host->str)
   2.133 -#define gmyth_uri_get_port(urip) (urip->port)
   2.134 -#define gmyth_uri_get_protocol(urip) (urip->protocol->str)
   2.135 -#define gmyth_uri_get_path(urip) (urip->path->str)
   2.136 -#define gmyth_uri_get_user(urip) (urip->user->str)
   2.137 -#define gmyth_uri_get_password(urip) (urip->password->str)
   2.138 -#define gmyth_uri_get_fragment(urip) (urip->fragment->str)
   2.139 -#define gmyth_uri_get_query(urip) (urip->query->str)
   2.140 +GMythURI*   				gmyth_uri_new_with_value ( const gchar *value );
   2.141  
   2.142 -#endif
   2.143 +gboolean 						gmyth_uri_is_equals( GMythURI* uri1, GMythURI* uri2 );
   2.144 +
   2.145 +#define 						gmyth_uri_get_host(urip) 			(urip->host->str)
   2.146 +#define 						gmyth_uri_get_port(urip) 			(urip->port)
   2.147 +#define 						gmyth_uri_get_protocol(urip) 	(urip->protocol->str)
   2.148 +#define 						gmyth_uri_get_path(urip) 			(urip->path->str)
   2.149 +#define 						gmyth_uri_get_user(urip) 			(urip->user->str)
   2.150 +#define 						gmyth_uri_get_password(urip) 	(urip->password->str)
   2.151 +#define 						gmyth_uri_get_fragment(urip) 	(urip->fragment->str)
   2.152 +#define 						gmyth_uri_get_query(urip) 		(urip->query->str)
   2.153 +
   2.154 +G_END_DECLS
   2.155 +
   2.156 +#endif /* _GMYTH_URI_H_ */