[mafw-lastfm-devel] [PATCH mafw-lastfm 06/12] Generic cleanups

Felipe Contreras felipe.contreras at gmail.com
Thu Jan 14 18:33:52 EET 2010


'ptr != NULL' is redundant, 'ptr' does the trick.
Same for 'if (p) g_free(p)'; g_free checks for NULL already.

Signed-off-by: Felipe Contreras <felipe.contreras at gmail.com>
---
 mafw-lastfm/mafw-lastfm-scrobbler.c |   64 +++++++++++++----------------------
 mafw-lastfm/mafw-lastfm-scrobbler.h |    8 ++--
 mafw-lastfm/mafw-lastfm.c           |    8 ++--
 3 files changed, 32 insertions(+), 48 deletions(-)

diff --git a/mafw-lastfm/mafw-lastfm-scrobbler.c b/mafw-lastfm/mafw-lastfm-scrobbler.c
index 7b89f4e..c1ee469 100644
--- a/mafw-lastfm/mafw-lastfm-scrobbler.c
+++ b/mafw-lastfm/mafw-lastfm-scrobbler.c
@@ -39,7 +39,7 @@ typedef enum {
   MAFW_LASTFM_SCROBBLER_SUBMITTING
 } MafwLastfmScrobblerStatus;
 
-struct _MafwLastfmScrobblerPrivate {
+struct MafwLastfmScrobblerPrivate {
   SoupSession *session;
   gchar *session_id;
   gchar *np_url;
@@ -97,25 +97,18 @@ mafw_lastfm_scrobbler_dispose (GObject *object)
 {
   MafwLastfmScrobblerPrivate *priv = MAFW_LASTFM_SCROBBLER (object)->priv;
 
-  if (priv->session != NULL) {
+  if (priv->session) {
     soup_session_abort (priv->session);
     g_object_unref (priv->session);
     priv->session = NULL;
   }
 
-  if (priv->session_id != NULL) {
-    g_free (priv->session_id);
-    priv->session_id = NULL;
-  }
-  if (priv->np_url == NULL) {
-    g_free (priv->np_url);
-    priv->np_url = NULL;
-  }
-
-  if (priv->sub_url == NULL) {
-    g_free (priv->sub_url);
-    priv->sub_url = NULL;
-  }
+  g_free (priv->session_id);
+  priv->session_id = NULL;
+  g_free (priv->np_url);
+  priv->np_url = NULL;
+  g_free (priv->sub_url);
+  priv->sub_url = NULL;
 
   if (priv->scrobbling_queue) {
     g_queue_foreach (priv->scrobbling_queue, (GFunc) mafw_lastfm_track_free, NULL);
@@ -133,15 +126,10 @@ mafw_lastfm_scrobbler_dispose (GObject *object)
     priv->playing_now_track = NULL;
   }
 
-  if (priv->username) {
-    g_free (priv->username);
-    priv->username = NULL;
-  }
-
-  if (priv->md5password) {
-    g_free (priv->md5password);
-    priv->md5password = NULL;
-  }
+  g_free (priv->username);
+  priv->username = NULL;
+  g_free (priv->md5password);
+  priv->md5password = NULL;
 
   G_OBJECT_CLASS (mafw_lastfm_scrobbler_parent_class)->dispose (object);
 }
@@ -202,14 +190,10 @@ mafw_lastfm_scrobbler_set_credentials (MafwLastfmScrobbler *scrobbler,
                                        const gchar *username,
                                        const gchar *md5password)
 {
-  if (scrobbler->priv->username) {
-    g_free (scrobbler->priv->username);
-  }
+  g_free (scrobbler->priv->username);
   scrobbler->priv->username = g_strdup (username);
 
-  if (scrobbler->priv->md5password) {
-    g_free (scrobbler->priv->md5password);
-  }
+  g_free (scrobbler->priv->md5password);
   scrobbler->priv->md5password = g_strdup (md5password);
 
   scrobbler->priv->status = MAFW_LASTFM_SCROBBLER_NEED_HANDSHAKE;
@@ -244,7 +228,7 @@ mafw_lastfm_scrobbler_scrobbling_failed (MafwLastfmScrobbler *scrobbler)
   GList *iter;
 
   for (iter = g_list_last (scrobbler->priv->scrobble_list);
-       iter != NULL; iter = iter->prev) {
+       iter; iter = iter->prev) {
     g_queue_push_head (scrobbler->priv->scrobbling_queue,
                        iter->data);
   }
@@ -288,7 +272,7 @@ mafw_lastfm_scrobbler_scrobble_list (MafwLastfmScrobbler *scrobbler,
 
   post_data = g_strdup_printf ("s=%s", scrobbler->priv->session_id);
 
-  for (iter = list, i = 0; iter != NULL; iter = iter->next, i++) {
+  for (iter = list, i = 0; iter; iter = iter->next, i++) {
     track = (MafwLastfmTrack *) iter->data;
     track_data = g_strdup_printf ("&a[%i]=%s&t[%i]=%s&i[%i]=%li&o[%i]=%c&r[%i]=&l[%i]=%lld&b[%i]=%s&n[%i]=%i&m[%i]=",
                                   i, track->artist,
@@ -339,7 +323,7 @@ mafw_lastfm_scrobbler_set_playing_now (MafwLastfmScrobbler *scrobbler,
   SoupMessage *message;
 
   g_return_if_fail (MAFW_LASTFM_IS_SCROBBLER (scrobbler));
-  g_return_if_fail (encoded != NULL);
+  g_return_if_fail (encoded);
   g_return_if_fail (scrobbler->priv->status == MAFW_LASTFM_SCROBBLER_READY);
 
   post_data = g_strdup_printf ("s=%s&a=%s&t=%s&b=%s&l=%lli&n=%u&m=",
@@ -420,7 +404,7 @@ void
 mafw_lastfm_scrobbler_suspend (MafwLastfmScrobbler *scrobbler)
 {
   /* Nothing to suspend. */
-  if (scrobbler->priv->suspended_track != NULL)
+  if (scrobbler->priv->suspended_track)
     return;
 
   /* Remove the last track from the queue, since it is suspended. */
@@ -494,7 +478,7 @@ get_auth_string (const gchar *md5passwd,
   gchar *auth;
   gchar *md5;
 
-  g_return_val_if_fail (timestamp != NULL, NULL);
+  g_return_val_if_fail (timestamp, NULL);
 
   g_get_current_time (&time_val);
 
@@ -590,7 +574,7 @@ mafw_lastfm_scrobbler_handshake (MafwLastfmScrobbler *scrobbler)
   SoupMessage *message;
 
   g_return_if_fail (scrobbler->priv->status != MAFW_LASTFM_SCROBBLER_HANDSHAKING);
-  g_return_if_fail (scrobbler->priv->username != NULL || scrobbler->priv->md5password != NULL);
+  g_return_if_fail (scrobbler->priv->username || scrobbler->priv->md5password);
   if (scrobbler->priv->handshake_id) {
     g_source_remove (scrobbler->priv->handshake_id);
     scrobbler->priv->handshake_id = 0;
@@ -628,7 +612,7 @@ mafw_lastfm_track_new (void)
 void
 mafw_lastfm_track_free (MafwLastfmTrack *track)
 {
-  if (track == NULL)
+  if (!track)
     return;
 
   g_free (track->artist);
@@ -647,13 +631,13 @@ mafw_lastfm_track_encode (MafwLastfmTrack *track)
 
   encoded = mafw_lastfm_track_new ();
 
-  if (track->artist != NULL)
+  if (track->artist)
     encoded->artist = soup_uri_encode (track->artist, EXTRA_URI_ENCODE_CHARS);
 
-  if (track->title != NULL)
+  if (track->title)
     encoded->title = soup_uri_encode (track->title, EXTRA_URI_ENCODE_CHARS);
 
-  if (track->album != NULL)
+  if (track->album)
     encoded->album = soup_uri_encode (track->album, EXTRA_URI_ENCODE_CHARS);
 
   encoded->length = track->length;
diff --git a/mafw-lastfm/mafw-lastfm-scrobbler.h b/mafw-lastfm/mafw-lastfm-scrobbler.h
index dc1fe0f..f0cdaf4 100644
--- a/mafw-lastfm/mafw-lastfm-scrobbler.h
+++ b/mafw-lastfm/mafw-lastfm-scrobbler.h
@@ -18,8 +18,8 @@
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
-#ifndef _MAFW_LASTFM_SCROBBLER
-#define _MAFW_LASTFM_SCROBBLER
+#ifndef MAFW_LASTFM_SCROBBLER_H
+#define MAFW_LASTFM_SCROBBLER_H
 
 #include <glib-object.h>
 
@@ -42,7 +42,7 @@ G_BEGIN_DECLS
 #define MAFW_LASTFM_SCROBBLER_GET_CLASS(obj) \
   (G_TYPE_INSTANCE_GET_CLASS ((obj), MAFW_LASTFM_TYPE_SCROBBLER, MafwLastfmScrobblerClass))
 
-typedef struct _MafwLastfmScrobblerPrivate MafwLastfmScrobblerPrivate;
+typedef struct MafwLastfmScrobblerPrivate MafwLastfmScrobblerPrivate;
 
 typedef struct {
   GObject parent;
@@ -99,4 +99,4 @@ mafw_lastfm_track_free (MafwLastfmTrack *track);
 
 G_END_DECLS
 
-#endif /* _MAFW_LASTFM_SCROBBLER */
+#endif /* MAFW_LASTFM_SCROBBLER_H */
diff --git a/mafw-lastfm/mafw-lastfm.c b/mafw-lastfm/mafw-lastfm.c
index 2c1e162..98d76f4 100644
--- a/mafw-lastfm/mafw-lastfm.c
+++ b/mafw-lastfm/mafw-lastfm.c
@@ -68,7 +68,7 @@ metadata_callback (MafwRenderer *self,
   track->artist = mafw_metadata_lookup_string (metadata, MAFW_METADATA_KEY_ARTIST);
   track->title = mafw_metadata_lookup_string (metadata, MAFW_METADATA_KEY_TITLE);
 
-  if (track->artist == NULL || track->title == NULL)
+  if (!track->artist || !track->title)
     return;
 
   track->timestamp = time_val.tv_sec; /* This should probably be obtained in the
@@ -166,7 +166,7 @@ get_credentials (gchar *file,
   *pw_md5 = g_key_file_get_string (keyfile,
                                    "Credentials", "password", NULL);
 
-  if (*username == NULL || *pw_md5 == NULL) {
+  if (!*username || !*pw_md5) {
     g_warning ("Error loading username or password md5");
 
     g_free (*username);
@@ -241,13 +241,13 @@ int main ()
   scrobbler = mafw_lastfm_scrobbler_new ();
 
   registry = MAFW_REGISTRY (mafw_registry_get_instance ());
-  if (registry == NULL) {
+  if (!registry) {
     g_warning ("Failed to get register.\n");
     return 1;
   }
 
   mafw_shared_init (registry, &error);
-  if (error != NULL) {
+  if (error) {
     g_warning ("Failed to initialize the shared library.\n");
     return 1;
   }
-- 
1.6.6


More information about the mafw-lastfm-devel mailing list