diff options
Diffstat (limited to 'camel')
-rw-r--r-- | camel/camel-folder.c | 73 | ||||
-rw-r--r-- | camel/camel-folder.h | 17 | ||||
-rw-r--r-- | camel/camel-store.c | 43 | ||||
-rw-r--r-- | camel/camel-store.h | 8 | ||||
-rw-r--r-- | camel/providers/MH/camel-mh-folder.c | 38 | ||||
-rw-r--r-- | camel/providers/MH/camel-mh-store.c | 72 | ||||
-rw-r--r-- | camel/url-util.c | 161 | ||||
-rw-r--r-- | camel/url-util.h | 10 |
8 files changed, 272 insertions, 150 deletions
diff --git a/camel/camel-folder.c b/camel/camel-folder.c index f8c5f34ee3..bdd1a75a17 100644 --- a/camel/camel-folder.c +++ b/camel/camel-folder.c @@ -124,7 +124,7 @@ _init_with_store (CamelFolder *folder, CamelStore *parent_store) { g_assert(folder); g_assert(parent_store); - + folder->parent_store = parent_store; } @@ -166,21 +166,21 @@ _close (CamelFolder *folder, gboolean expunge) static void -_set_name(CamelFolder *folder, const gchar *name) +_set_name (CamelFolder *folder, const gchar *name) { gchar separator; gchar *full_name; const gchar *parent_full_name; - + g_assert (folder); g_assert (name); g_assert (folder->parent_store); - + if (folder->name) g_free(folder->name); if (folder->full_name) g_free (folder->full_name); separator = camel_store_get_separator (folder->parent_store); - + if (folder->parent_folder) { parent_full_name = camel_folder_get_full_name (folder->parent_folder); full_name = g_strdup_printf ("%s%d%s", parent_full_name, separator, name); @@ -188,10 +188,10 @@ _set_name(CamelFolder *folder, const gchar *name) } else { full_name = g_strdup (name); } - + folder->name = g_strdup (name); folder->full_name = full_name; - + } @@ -215,8 +215,8 @@ camel_folder_set_name (CamelFolder *folder, const gchar *name) static void _set_full_name (CamelFolder *folder, const gchar *name) { - if (folder->full_name) g_free(folder->full_name); - folder->full_name = g_strdup (name); + if (folder->full_name) g_free(folder->full_name); + folder->full_name = g_strdup (name); } @@ -292,7 +292,7 @@ camel_folder_get_full_name (CamelFolder *folder) static gboolean _can_hold_folders (CamelFolder *folder) { - return folder->can_hold_folders; + return folder->can_hold_folders; } @@ -311,7 +311,7 @@ _can_hold_folders (CamelFolder *folder) static gboolean _can_hold_messages (CamelFolder *folder) { - return folder->can_hold_messages; + return folder->can_hold_messages; } @@ -319,7 +319,7 @@ _can_hold_messages (CamelFolder *folder) static gboolean _exists (CamelFolder *folder) { - return FALSE; + return FALSE; } @@ -336,7 +336,7 @@ _exists (CamelFolder *folder) gboolean camel_folder_exists (CamelFolder *folder) { - return (CF_CLASS(folder)->exists (folder)); + return (CF_CLASS(folder)->exists (folder)); } @@ -353,7 +353,7 @@ camel_folder_exists (CamelFolder *folder) static gboolean _is_open (CamelFolder *folder) { - return (folder->open_state==FOLDER_OPEN); + return (folder->open_state == FOLDER_OPEN); } @@ -367,15 +367,15 @@ _get_folder (CamelFolder *folder, const gchar *folder_name) gchar *full_name; const gchar *current_folder_full_name; gchar separator; - + g_assert (folder); g_assert (folder_name); - + if (!folder->parent_store) return NULL; current_folder_full_name = camel_folder_get_full_name (folder); if (!current_folder_full_name) return NULL; - + separator = camel_store_get_separator (folder->parent_store); full_name = g_strdup_printf ("%s%d%s", current_folder_full_name, separator, folder_name); @@ -398,7 +398,7 @@ _get_folder (CamelFolder *folder, const gchar *folder_name) * could not be obtained **/ CamelFolder * -camel_folder_get_folder(CamelFolder *folder, gchar *folder_name) +camel_folder_get_folder (CamelFolder *folder, gchar *folder_name) { return (CF_CLASS(folder)->get_folder(folder,folder_name)); } @@ -427,22 +427,22 @@ _create(CamelFolder *folder) gchar dich_result; CamelFolder *parent; gchar sep; - + g_assert (folder); g_assert (folder->parent_store); g_assert (folder->name); - + if (CF_CLASS(folder)->exists (folder)) return TRUE; - + sep = camel_store_get_separator (folder->parent_store); if (folder->parent_folder) camel_folder_create (folder->parent_folder); else { if (folder->full_name) { dich_result = string_dichotomy ( - folder->full_name, sep, &prefix, NULL, - STRING_DICHOTOMY_STRIP_TRAILING | STRING_DICHOTOMY_RIGHT_DIR); + folder->full_name, sep, &prefix, NULL, + STRING_DICHOTOMY_STRIP_TRAILING | STRING_DICHOTOMY_RIGHT_DIR); if (dich_result!='o') { g_warning("I have to handle the case where the path is not OK\n"); return FALSE; @@ -456,7 +456,7 @@ _create(CamelFolder *folder) return TRUE; } - + /** * camel_folder_create: create the folder object on the physical store * @folder: folder object to create @@ -472,7 +472,7 @@ _create(CamelFolder *folder) * Return value: **/ gboolean -camel_folder_create(CamelFolder *folder) +camel_folder_create (CamelFolder *folder) { return (CF_CLASS(folder)->create(folder)); } @@ -512,13 +512,13 @@ _delete (CamelFolder *folder, gboolean recurse) gboolean ok; g_assert(folder); - + /* method valid only on closed folders */ if (folder->open_state != FOLDER_CLOSE) return FALSE; - + /* delete all messages in the folder */ CF_CLASS(folder)->delete_messages(folder); - + subfolders = CF_CLASS(folder)->list_subfolders(folder); if (recurse) { /* delete subfolders */ if (subfolders) { @@ -567,7 +567,7 @@ gboolean camel_folder_delete (CamelFolder *folder, gboolean recurse) * Return value: **/ static gboolean -_delete_messages(CamelFolder *folder) +_delete_messages (CamelFolder *folder) { return TRUE; } @@ -662,9 +662,9 @@ camel_folder_get_parent_store (CamelFolder *folder) * Return value: open mode of the folder **/ static CamelFolderOpenMode -_get_mode(CamelFolder *folder) +_get_mode (CamelFolder *folder) { - return folder->open_mode; + return folder->open_mode; } @@ -677,16 +677,16 @@ _get_mode(CamelFolder *folder) * Return value: open mode of the folder **/ CamelFolderOpenMode -camel_folder_get_mode(CamelFolder *folder) +camel_folder_get_mode (CamelFolder *folder) { - return CF_CLASS(folder)->get_mode(folder); + return CF_CLASS(folder)->get_mode(folder); } static GList * -_list_subfolders(CamelFolder *folder) +_list_subfolders (CamelFolder *folder) { return NULL; } @@ -703,7 +703,7 @@ _list_subfolders(CamelFolder *folder) GList * camel_folder_list_subfolders (CamelFolder *folder) { - return CF_CLASS(folder)->list_subfolders(folder); + return CF_CLASS(folder)->list_subfolders(folder); } @@ -728,7 +728,7 @@ _expunge (CamelFolder *folder) GList * camel_folder_expunge (CamelFolder *folder) { - return CF_CLASS (folder)->expunge (folder); + return CF_CLASS (folder)->expunge (folder); } @@ -757,3 +757,4 @@ camel_folder_get_message (CamelFolder *folder, gint number) { return CF_CLASS (folder)->get_message (folder, number); } + diff --git a/camel/camel-folder.h b/camel/camel-folder.h index fd9c94761e..6c17c331e2 100644 --- a/camel/camel-folder.h +++ b/camel/camel-folder.h @@ -60,7 +60,7 @@ typedef enum { struct _CamelFolder { GtkObject parent_object; - + gboolean can_hold_folders; gboolean can_hold_messages; CamelFolderOpenMode open_mode; @@ -76,13 +76,13 @@ struct _CamelFolder typedef struct { GtkObjectClass parent_class; - + /* Virtual methods */ void (*init_with_store) (CamelFolder *folder, CamelStore *parent_store); void (*open) (CamelFolder *object, CamelFolderOpenMode mode); void (*close) (CamelFolder *folder, gboolean expunge); void (*set_name) (CamelFolder *folder, const gchar *name); -/* void (*set_full_name) (CamelFolder *folder, const gchar *name); */ + /* void (*set_full_name) (CamelFolder *folder, const gchar *name); */ const gchar * (*get_name) (CamelFolder *folder); const gchar * (*get_full_name) (CamelFolder *folder); gboolean (*can_hold_folders) (CamelFolder *folder); @@ -108,15 +108,15 @@ GtkType camel_folder_get_type (void); /* public methods */ -CamelFolder *camel_folder_get_folder(CamelFolder *folder, gchar *folder_name); -gboolean camel_folder_create(CamelFolder *folder); +CamelFolder *camel_folder_get_folder (CamelFolder *folder, gchar *folder_name); +gboolean camel_folder_create (CamelFolder *folder); gboolean camel_folder_delete (CamelFolder *folder, gboolean recurse); gboolean camel_folder_delete_messages (CamelFolder *folder); CamelFolder *camel_folder_get_parent_folder (CamelFolder *folder); CamelStore *camel_folder_get_parent_store (CamelFolder *folder); -CamelFolderOpenMode camel_folder_get_mode(CamelFolder *folder); -GList *camel_folder_list_subfolders(CamelFolder *folder); -GList *camel_folder_expunge(CamelFolder *folder); +CamelFolderOpenMode camel_folder_get_mode (CamelFolder *folder); +GList *camel_folder_list_subfolders (CamelFolder *folder); +GList *camel_folder_expunge (CamelFolder *folder); void camel_folder_set_name (CamelFolder *folder, const gchar *name); const gchar *camel_folder_get_name (CamelFolder *folder); /* void camel_folder_set_full_name (CamelFolder *folder, const gchar *name); */ @@ -131,3 +131,4 @@ gboolean camel_folder_exists (CamelFolder *folder); #endif /* __cplusplus */ #endif /* CAMEL_FOLDER_H */ + diff --git a/camel/camel-store.c b/camel/camel-store.c index 78706b1703..56faca9a35 100644 --- a/camel/camel-store.c +++ b/camel/camel-store.c @@ -23,7 +23,7 @@ #include <config.h> #include "camel-store.h" -static GtkObjectClass *parent_class=NULL; +static CamelServiceClass *parent_class = NULL; /* Returns the class for a CamelStore */ #define CS_CLASS(so) CAMEL_STORE_CLASS (GTK_OBJECT(so)->klass) @@ -32,7 +32,8 @@ static void _set_separator(CamelStore *store, gchar sep); static CamelFolder *_get_root_folder(CamelStore *store); static CamelFolder *_get_default_folder(CamelStore *store); static void _init(CamelStore *store, CamelSession *session, gchar *url_name); - +static CamelFolder *_get_folder (CamelStore *store, const gchar *folder_name); +static gchar _get_separator (CamelStore *store); static void @@ -43,8 +44,8 @@ camel_store_class_init (CamelStoreClass *camel_store_class) /* virtual method definition */ camel_store_class->init = _init; camel_store_class->set_separator = _set_separator; - camel_store_class->get_separator = camel_store_get_separator; - camel_store_class->get_folder = camel_store_get_folder; + camel_store_class->get_separator = _get_separator; + camel_store_class->get_folder = _get_folder; camel_store_class->get_root_folder = _get_root_folder; camel_store_class->get_default_folder = _get_default_folder; /* virtual method overload */ @@ -95,7 +96,7 @@ camel_store_get_type (void) * **/ void -camel_store_init(CamelStore *store, CamelSession *session, gchar *url_name) +camel_store_init (CamelStore *store, CamelSession *session, gchar *url_name) { g_assert(store); CS_CLASS(store)->init (store, session, url_name); @@ -118,8 +119,8 @@ static void _init (CamelStore *store, CamelSession *session, gchar *url_name) { - - g_assert(session); +#warning re-enable assertion here. + /* g_assert(session); */ g_assert(url_name); store->session = session; @@ -144,6 +145,16 @@ _set_separator (CamelStore *store, gchar sep) + + +static gchar +_get_separator (CamelStore *store) +{ + g_assert(store); + return store->separator; +} + + /** * camel_store_get_separator: return the character which separates this folder * path from the folders names in a lower level of hierarchy. @@ -154,13 +165,22 @@ _set_separator (CamelStore *store, gchar sep) gchar camel_store_get_separator (CamelStore *store) { - g_assert(store); - return store->separator; + return CS_CLASS(store)->get_separator (store); } + + + +static CamelFolder * +_get_folder (CamelStore *store, const gchar *folder_name) +{ + return NULL; +} + + /** * camel_store_get_folder: return the folder corresponding to a path. * @@ -179,10 +199,9 @@ camel_store_get_separator (CamelStore *store) * Return value: the folder **/ CamelFolder * -camel_store_get_folder (CamelStore *store, gchar *folder_name) +camel_store_get_folder (CamelStore *store, const gchar *folder_name) { - - return NULL; + return CS_CLASS(store)->get_folder (store, folder_name); } diff --git a/camel/camel-store.h b/camel/camel-store.h index 38fb0f10f7..fea908ce99 100644 --- a/camel/camel-store.h +++ b/camel/camel-store.h @@ -62,7 +62,7 @@ typedef struct { void (*init) (CamelStore *store, CamelSession *session, gchar *url_name); void (*set_separator) (CamelStore *store, gchar sep); gchar (*get_separator) (CamelStore *store); - CamelFolder * (*get_folder) (CamelStore *store, gchar *folder_name); + CamelFolder * (*get_folder) (CamelStore *store, const gchar *folder_name); CamelFolder * (*get_root_folder) (CamelStore *store); CamelFolder * (*get_default_folder) (CamelStore *store); @@ -74,9 +74,9 @@ typedef struct { /* Standard Gtk function */ GtkType camel_store_get_type (void); -void camel_store_init(CamelStore *store, CamelSession *session, gchar *url_name); -CamelFolder *camel_store_get_folder(CamelStore *store, gchar *folder_name); -gchar camel_store_get_separator(CamelStore *store); +void camel_store_init (CamelStore *store, CamelSession *session, gchar *url_name); +CamelFolder *camel_store_get_folder (CamelStore *store, const gchar *folder_name); +gchar camel_store_get_separator (CamelStore *store); #ifdef __cplusplus } diff --git a/camel/providers/MH/camel-mh-folder.c b/camel/providers/MH/camel-mh-folder.c index 50c0ad34d4..e4bd6335c1 100644 --- a/camel/providers/MH/camel-mh-folder.c +++ b/camel/providers/MH/camel-mh-folder.c @@ -20,7 +20,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * USA */ - +#include <config.h> #include <sys/stat.h> #include <unistd.h> #include <sys/types.h> @@ -54,7 +54,7 @@ static CamelMimeMessage *_get_message (CamelFolder *folder, gint number); static void camel_mh_folder_class_init (CamelMhFolderClass *camel_mh_folder_class) { - CamelFolderClass *camel_folder_class = CAMEL_FOLDER_CLASS(camel_mh_folder_class); + CamelFolderClass *camel_folder_class = CAMEL_FOLDER_CLASS (camel_mh_folder_class); parent_class = gtk_type_class (camel_folder_get_type ()); @@ -126,12 +126,13 @@ _init_with_store (CamelFolder *folder, CamelStore *parent_store) static void _set_name (CamelFolder *folder, const gchar *name) { - CamelMhFolder *mh_folder = CAMEL_MH_FOLDER(folder); + CamelMhFolder *mh_folder = CAMEL_MH_FOLDER (folder); const gchar *root_dir_path; gchar *full_name; const gchar *parent_full_name; gchar separator; - + + CAMEL_LOG_FULL_DEBUG ("Entering CamelMhFolder::set_name\n"); g_assert(folder); g_assert(name); g_assert(folder->parent_store); @@ -143,9 +144,15 @@ _set_name (CamelFolder *folder, const gchar *name) separator = camel_store_get_separator (folder->parent_store); root_dir_path = camel_mh_store_get_toplevel_dir (CAMEL_MH_STORE(folder->parent_store)); - + + CAMEL_LOG_FULL_DEBUG ("CamelMhFolder::set_name full_name is %s\n", folder->full_name); + CAMEL_LOG_FULL_DEBUG ("CamelMhFolder::set_name root_dir_path is %s\n", root_dir_path); + CAMEL_LOG_FULL_DEBUG ("CamelMhFolder::separator is %c\n", separator); + mh_folder->directory_path = g_strdup_printf ("%s%c%s", root_dir_path, separator, folder->full_name); + CAMEL_LOG_FULL_DEBUG ("CamelMhFolder::set_name mh_folder->directory_path is %s\n", mh_folder->directory_path); + CAMEL_LOG_FULL_DEBUG ("Leaving CamelMhFolder::set_name\n"); } @@ -158,14 +165,21 @@ _exists (CamelFolder *folder) gint stat_error; gboolean exists; + CAMEL_LOG_FULL_DEBUG ("Entering CamelMhFolder::exists\n"); g_assert (folder); - + if (!mh_folder->directory_path) return FALSE; - + stat_error = stat (mh_folder->directory_path, &stat_buf); - if (stat_error == -1) return FALSE; + if (stat_error == -1) { + CAMEL_LOG_FULL_DEBUG ("CamelMhFolder::exists when executing stat on %s, stat_error = %d\n", + mh_folder->directory_path, stat_error); + CAMEL_LOG_FULL_DEBUG (" Full error text is : %s\n", strerror(errno)); + return FALSE; + } + exists = S_ISDIR (stat_buf.st_mode); - exists = S_ISDIR(stat_buf.st_mode); + CAMEL_LOG_FULL_DEBUG ("Leaving CamelMhFolder::exists\n"); return exists; } @@ -321,8 +335,10 @@ _list_subfolders(CamelFolder *folder) /* is it a directory ? */ if ((stat_error != -1) && S_ISDIR (stat_buf.st_mode)) { /* yes, add it to the list */ - CAMEL_LOG_FULL_DEBUG ("CamelMhFolder::list_subfolders adding %s\n", entry_name); - subfolder_name_list = g_list_append (subfolder_name_list, g_strdup (entry_name)); + if (entry_name[0] != '.') { + CAMEL_LOG_FULL_DEBUG ("CamelMhFolder::list_subfolders adding %s\n", entry_name); + subfolder_name_list = g_list_append (subfolder_name_list, g_strdup (entry_name)); + } } /* read next entry */ dir_entry = readdir (dir_handle); diff --git a/camel/providers/MH/camel-mh-store.c b/camel/providers/MH/camel-mh-store.c index efdb2b86e9..0e382fa913 100644 --- a/camel/providers/MH/camel-mh-store.c +++ b/camel/providers/MH/camel-mh-store.c @@ -23,25 +23,42 @@ #include "camel-mh-store.h" #include "camel-mh-folder.h" +#include "url-util.h" -static GtkObjectClass *parent_class=NULL; +static CamelStoreClass *parent_class=NULL; /* Returns the class for a CamelMhStore */ -#define CS_CLASS(so) CAMEL_MH_STORE_CLASS (GTK_OBJECT(so)->klass) +#define CMHS_CLASS(so) CAMEL_MH_STORE_CLASS (GTK_OBJECT(so)->klass) +#define CF_CLASS(so) CAMEL_FOLDER_CLASS (GTK_OBJECT(so)->klass) +#define CMHF_CLASS(so) CAMEL_MH_FOLDER_CLASS (GTK_OBJECT(so)->klass) +static void _init (CamelStore *store, CamelSession *session, gchar *url_name); +static CamelFolder *_get_folder (CamelStore *store, const gchar *folder_name); static void camel_mh_store_class_init (CamelMhStoreClass *camel_mh_store_class) { + CamelStoreClass *camel_store_class = CAMEL_STORE_CLASS (camel_mh_store_class); + parent_class = gtk_type_class (camel_store_get_type ()); /* virtual method definition */ /* virtual method overload */ + camel_store_class->init = _init; + camel_store_class->get_folder = _get_folder; } +static void +camel_mh_store_init (gpointer object, gpointer klass) +{ + CamelMhStore *mh_store = CAMEL_MH_STORE (object); + CamelStore *store = CAMEL_STORE (object); + + store->separator = '/'; +} @@ -58,13 +75,13 @@ camel_mh_store_get_type (void) sizeof (CamelMhStore), sizeof (CamelMhStoreClass), (GtkClassInitFunc) camel_mh_store_class_init, - (GtkObjectInitFunc) NULL, + (GtkObjectInitFunc) camel_mh_store_init, /* reserved_1 */ NULL, /* reserved_2 */ NULL, (GtkClassInitFunc) NULL, }; - camel_mh_store_type = gtk_type_unique (CAMEL_FOLDER_TYPE, &camel_mh_store_info); + camel_mh_store_type = gtk_type_unique (CAMEL_STORE_TYPE, &camel_mh_store_info); } return camel_mh_store_type; @@ -87,3 +104,50 @@ camel_mh_store_get_toplevel_dir (CamelMhStore *store) { return store->toplevel_dir; } + + + +static void +_init (CamelStore *store, CamelSession *session, gchar *url_name) +{ + CamelMhStore *mh_store = CAMEL_MH_STORE (store); + Gurl *store_url; + + g_assert (url_name); + /* call parent implementation */ + parent_class->init (store, session, url_name); + + + /* find the path in the URL*/ + store_url = g_url_new (url_name); + + g_return_if_fail (store_url); + g_return_if_fail (store_url->path); + + mh_store->toplevel_dir = g_strdup (store_url->path); + g_url_free (store_url); + + + +} + + +static CamelFolder * +_get_folder (CamelStore *store, const gchar *folder_name) +{ + CamelMhFolder *new_mh_folder; + CamelFolder *new_folder; + + /* check if folder has already been created */ + /* call the standard routine for that when */ + /* it is done ... */ + + new_mh_folder = gtk_type_new (CAMEL_MH_FOLDER_TYPE); + new_folder = CAMEL_FOLDER (new_mh_folder); + + CF_CLASS (new_folder)->init_with_store (new_folder, store); + CF_CLASS (new_folder)->set_name (new_folder, folder_name); + + + return new_folder; +} diff --git a/camel/url-util.c b/camel/url-util.c index cf2425fdc3..43f51fe365 100644 --- a/camel/url-util.c +++ b/camel/url-util.c @@ -32,7 +32,7 @@ Bertrand. */ /* - XXX TODO: recover the words between #'s or ?'s after the path */ + XXX TODO: recover the words between #'s or ?'s after the path */ #include <config.h> #include "url-util.h" @@ -40,7 +40,7 @@ /* general item finder */ /* it begins the search at position @position in @url, returns true when the item is found, amd set position after the item */ -typedef gboolean find_item_func(gchar *url, gchar **item, guint *position, gboolean *error); +typedef gboolean find_item_func(const gchar *url, gchar **item, guint *position, gboolean *error); /* used to find one item (protocol, then user .... */ typedef struct { @@ -49,12 +49,12 @@ typedef struct { find_item_func *find_func; /* item finder */ } FindStepStruct; -static gboolean find_protocol (gchar *url, gchar **item, guint *position, gboolean *error); -static gboolean find_user (gchar *url, gchar **item, guint *position, gboolean *error); -static gboolean find_passwd (gchar *url, gchar **item, guint *position, gboolean *error); -static gboolean find_host (gchar *url, gchar **item, guint *position, gboolean *error); -static gboolean find_port (gchar *url, gchar **item, guint *position, gboolean *error); -static gboolean find_path (gchar *url, gchar **item, guint *position, gboolean *error); +static gboolean _find_protocol (const gchar *url, gchar **item, guint *position, gboolean *error); +static gboolean _find_user (const gchar *url, gchar **item, guint *position, gboolean *error); +static gboolean _find_passwd (const gchar *url, gchar **item, guint *position, gboolean *error); +static gboolean _find_host (const gchar *url, gchar **item, guint *position, gboolean *error); +static gboolean _find_port (const gchar *url, gchar **item, guint *position, gboolean *error); +static gboolean _find_path (const gchar *url, gchar **item, guint *position, gboolean *error); @@ -77,7 +77,7 @@ static gboolean find_path (gchar *url, gchar **item, guint *position, gboolean * * * Return value: a Gurl structure containing the URL items. **/ -Gurl *g_url_new (gchar* url_string) +Gurl *g_url_new (const gchar* url_string) { Gurl *g_url; @@ -88,29 +88,29 @@ Gurl *g_url_new (gchar* url_string) gchar *port; gchar *path; - guint position=0; + guint position = 0; gboolean error; gboolean found; guint i; - g_url = g_new(Gurl,1); + g_url = g_new (Gurl,1); #define NB_STEP_URL 6 { FindStepStruct step[NB_STEP_URL] = { - { "protocol", &(g_url->protocol), find_protocol}, - { "user", &(g_url->user), find_user}, - { "password", &(g_url->passwd), find_passwd}, - { "host", &(g_url->host), find_host}, - { "port", &(g_url->port), find_port}, - { "path", &(g_url->path), find_path} + { "protocol", &(g_url->protocol), _find_protocol}, + { "user", &(g_url->user), _find_user}, + { "password", &(g_url->passwd), _find_passwd}, + { "host", &(g_url->host), _find_host}, + { "port", &(g_url->port), _find_port}, + { "path", &(g_url->path), _find_path} }; - for (i=0; i<NB_STEP_URL; i++) { - found = step[i].find_func(url_string, - step[i].item_value, - &position, - &error); + for (i = 0; i < NB_STEP_URL; i++) { + found = step[i].find_func (url_string, + step[i].item_value, + &position, + &error); } } @@ -119,14 +119,30 @@ Gurl *g_url_new (gchar* url_string) +void +g_url_free (Gurl *url) +{ + g_assert (url); + + if (url->protocol) g_free (url->protocol); + if (url->user) g_free (url->user); + if (url->passwd) g_free (url->passwd); + if (url->host) g_free (url->host); + if (url->port) g_free (url->port); + if (url->path) g_free (url->path); + g_free (url); + +} + +/**** PARSING FUNCTIONS ****/ /** So, yes, I must admit there would have been more elegant ways to do this, but it works, and quite well :) */ static gboolean -find_protocol(gchar *url, gchar **item, guint *position, gboolean *error) +_find_protocol (const gchar *url, gchar **item, guint *position, gboolean *error) { guint i; @@ -140,20 +156,20 @@ find_protocol(gchar *url, gchar **item, guint *position, gboolean *error) *item = NULL; *error = FALSE; - i=*position; + i = *position; /* find a ':' */ - while ( (i<len_url) && (url[i] != ':') ) i++; + while ((i < len_url) && (url[i] != ':')) i++; - if (i==len_url) return FALSE; + if (i == len_url) return FALSE; i++; /* check if it is followed by a "//" */ - if ((i<len_url) && (url[i++] == '/')) - if ((i<len_url) && (url[i++] == '/')) + if ((i < len_url) && (url[i++] == '/')) + if ((i < len_url) && (url[i++] == '/')) { *item = g_strndup (url, i-3); - *position=i; + *position = i; return TRUE; } @@ -164,7 +180,7 @@ find_protocol(gchar *url, gchar **item, guint *position, gboolean *error) static gboolean -find_user(gchar *url, gchar **item, guint *position, gboolean *error) +_find_user (const gchar *url, gchar **item, guint *position, gboolean *error) { guint i; guint at_pos; @@ -176,17 +192,17 @@ find_user(gchar *url, gchar **item, guint *position, gboolean *error) len_url = strlen (url); *item = NULL; - i=*position; + i = *position; /* find a '@' */ - while ((i<len_url) && (url[i] != '@')) i++; + while ((i < len_url) && (url[i] != '@')) i++; - if (i==len_url) return FALSE; + if (i == len_url) return FALSE; at_pos = i; i = *position; /* find a ':' */ - while ( (i<at_pos) && (url[i] != ':') ) i++; + while ((i < at_pos) && (url[i] != ':')) i++; /* now if i has not been incremented at all, there is no user */ if (i == *position) { @@ -194,15 +210,15 @@ find_user(gchar *url, gchar **item, guint *position, gboolean *error) return FALSE; } - *item = g_strndup(url+ *position, i - *position); - if (i<at_pos) *position=i+1; /* there was a ':', skip it */ - else *position=i; + *item = g_strndup (url+ *position, i - *position); + if (i < at_pos) *position = i + 1; /* there was a ':', skip it */ + else *position = i; return TRUE; } static gboolean -find_passwd(gchar *url, gchar **item, guint *position, gboolean *error) +_find_passwd (const gchar *url, gchar **item, guint *position, gboolean *error) { guint i; gint len_url; @@ -214,20 +230,20 @@ find_passwd(gchar *url, gchar **item, guint *position, gboolean *error) len_url = strlen (url); *item = NULL; - i=*position; + i = *position; /* find a '@' */ - while ((i<len_url) && (url[i] != '@')) i++; + while ((i < len_url) && (url[i] != '@')) i++; - if (i==len_url) return FALSE; + if (i == len_url) return FALSE; /*i has not been incremented at all, there is no passwd */ if (i == *position) { - *position = i+1; + *position = i + 1; return FALSE; } *item = g_strndup (url + *position, i - *position); - *position=i+1; /* skip it the '@' */ + *position = i + 1; /* skip it the '@' */ return TRUE; } @@ -235,7 +251,7 @@ find_passwd(gchar *url, gchar **item, guint *position, gboolean *error) static gboolean -find_host(gchar *url, gchar **item, guint *position, gboolean *error) +_find_host (const gchar *url, gchar **item, guint *position, gboolean *error) { guint i; guint slash_pos; @@ -247,26 +263,27 @@ find_host(gchar *url, gchar **item, guint *position, gboolean *error) len_url = strlen (url); *item = NULL; - i=*position; + i = *position; /* find a '/' */ - while ((i<len_url) && (url[i] != '/')) i++; + while ((i < len_url) && (url[i] != '/')) i++; slash_pos = i; i = *position; /* find a ':' */ - while ( (i<slash_pos) && (url[i] != ':') ) i++; + while ( (i < slash_pos) && (url[i] != ':') ) i++; /* at this point if i has not been incremented at all, there is no host */ if (i == *position) { - (*position)++; + /* if we have not met / or \0, we have : and must skip it */ + if (i < slash_pos) (*position)++; return FALSE; } *item = g_strndup (url + *position, i - *position); - if (i<slash_pos) *position=i+1; /* there was a ':', skip it */ + if (i < slash_pos) *position = i + 1; /* there was a ':', skip it */ else *position=i; return TRUE; @@ -274,7 +291,7 @@ find_host(gchar *url, gchar **item, guint *position, gboolean *error) static gboolean -find_port(gchar *url, gchar **item, guint *position, gboolean *error) +_find_port (const gchar *url, gchar **item, guint *position, gboolean *error) { guint i; guint slash_pos; @@ -289,26 +306,26 @@ find_port(gchar *url, gchar **item, guint *position, gboolean *error) i=*position; /* find a '/' */ - while ((i<len_url) && (url[i] != '/')) i++; + while ((i < len_url) && (url[i] != '/')) i++; slash_pos = i; i = *position; /* find a ':' */ - while ( (i<slash_pos) && (url[i] != ':') ) i++; + while ((i < slash_pos) && (url[i] != ':')) i++; /* at this point if i has not been incremented at all, */ /* there is no port */ if (i == *position) return FALSE; - *item = g_strndup(url+ *position, i - *position); + *item = g_strndup (url+ *position, i - *position); *position = i; return TRUE; } static gboolean -find_path(gchar *url, gchar **item, guint *position, gboolean *error) +_find_path (const gchar *url, gchar **item, guint *position, gboolean *error) { guint i; gint len_url; @@ -319,19 +336,18 @@ find_path(gchar *url, gchar **item, guint *position, gboolean *error) len_url = strlen (url); *item = NULL; - i=*position; + i = *position; /* find a '#' */ - while ((i<len_url) && (url[i] != '#') && (url[i] != '?')) i++; + while ((i < len_url) && (url[i] != '#') && (url[i] != '?')) i++; /*i has not been incremented at all, there is no path */ if (i == *position) return FALSE; - *item = g_strndup(url + *position, i - *position); + *item = g_strndup (url + *position, i - *position); *position=i; - return TRUE; } @@ -339,6 +355,7 @@ find_path(gchar *url, gchar **item, guint *position, gboolean *error) +/**** TEST ROUTINE - NOT COMPILED BY DEFAULT ****/ /* to tests this file : gcc -o test_url_util `glib-config --cflags` -I.. -DTEST_URL_UTIL url-util.c `glib-config --libs` @@ -367,12 +384,12 @@ main (int argc, char **argv) #define NB_STEP_TEST 6 FindStepStruct test_step[NB_STEP_TEST] = { - { "protocol", &protocol, find_protocol}, - { "user", &user, find_user}, - { "password", &passwd, find_passwd}, - { "host", &host, find_host}, - { "port", &port, find_port}, - { "path", &path, find_path} + { "protocol", &protocol, _find_protocol}, + { "user", &user, _find_user}, + { "password", &passwd, _find_passwd}, + { "host", &host, _find_host}, + { "port", &port, _find_port}, + { "path", &path, _find_path} }; url = argv[1]; printf("URL to test : %s\n\n", url); @@ -382,14 +399,14 @@ main (int argc, char **argv) &position, &error); if (found) { - printf("\t\t\t\t** %s found : %s\n", - test_step[i].item_name, - *(test_step[i].item_value)); - } else printf("** %s not found in URL\n", test_step[i].item_name); - printf("next item position:\n"); - printf("%s\n", url); - for(i_pos=0; i_pos<position; i_pos++) printf(" "); - printf("^\n"); + printf ("\t\t\t\t** %s found : %s\n", + test_step[i].item_name, + *(test_step[i].item_value)); + } else printf ("** %s not found in URL\n", test_step[i].item_name); + printf ("next item position:\n"); + printf ("%s\n", url); + for (i_pos = 0; i_pos < position; i_pos++) printf (" "); + printf ("^\n"); } diff --git a/camel/url-util.h b/camel/url-util.h index 20a8b91b39..19b4997e39 100644 --- a/camel/url-util.h +++ b/camel/url-util.h @@ -39,11 +39,15 @@ typedef struct { gchar *port; gchar *path; - /* used only as a cache */ - gchar *url_string; } Gurl; -Gurl *g_url_new (gchar *url_string); +/* the cache system has been disabled because it would + need the user to use accessors instead of modifying the + structure field. As the speed is not so important here, + I chose not to use it */ + +Gurl *g_url_new (const gchar *url_string); +void g_url_free (Gurl *url); #ifdef __cplusplus } |