Revision: 2269
          http://gtkpod.svn.sourceforge.net/gtkpod/?rev=2269&view=rev
Author:   jcsjcs
Date:     2009-03-14 11:31:41 +0000 (Sat, 14 Mar 2009)

Log Message:
-----------
        * src/itdb_itunesdb.c
          src/itdb_private.h

          Implement reading of big/little endian integers/floats using
          references to the respective functions rather than if/then
          statements. Thanks to Javier Kohen for the patch.

Modified Paths:
--------------
    libgpod/trunk/ChangeLog
    libgpod/trunk/src/itdb_itunesdb.c
    libgpod/trunk/src/itdb_private.h

Modified: libgpod/trunk/ChangeLog
===================================================================
--- libgpod/trunk/ChangeLog     2009-03-01 10:24:33 UTC (rev 2268)
+++ libgpod/trunk/ChangeLog     2009-03-14 11:31:41 UTC (rev 2269)
@@ -1,3 +1,12 @@
+2009-03-14  Jorg Schuler <jcsjcs at users.sourceforge.net>
+
+       * src/itdb_itunesdb.c
+         src/itdb_private.h
+
+         Implement reading of big/little endian integers/floats using
+         references to the respective functions rather than if/then
+         statements. Thanks to Javier Kohen for the patch.
+
 2009-03-01  Christophe Fergeau <[email protected]>
 
        * src/itdb_device.c: fix nano3g/ipod classic artwork information

Modified: libgpod/trunk/src/itdb_itunesdb.c
===================================================================
--- libgpod/trunk/src/itdb_itunesdb.c   2009-03-01 10:24:33 UTC (rev 2268)
+++ libgpod/trunk/src/itdb_itunesdb.c   2009-03-14 11:31:41 UTC (rev 2269)
@@ -306,6 +306,41 @@
 }
 
 
+static guint16 raw_get16lint (FContents *cts, glong seek);
+static guint32 raw_get24lint (FContents *cts, glong seek);
+static guint32 raw_get32lint (FContents *cts, glong seek);
+static guint64 raw_get64lint (FContents *cts, glong seek);
+static float raw_get32lfloat (FContents *cts, glong seek);
+static guint16 raw_get16bint (FContents *cts, glong seek);
+static guint32 raw_get24bint (FContents *cts, glong seek);
+static guint32 raw_get32bint (FContents *cts, glong seek);
+static guint64 raw_get64bint (FContents *cts, glong seek);
+static float raw_get32bfloat (FContents *cts, glong seek);
+
+static const ByteReader LITTLE_ENDIAN_READER =
+{
+    raw_get16lint, raw_get24lint, raw_get32lint, raw_get64lint, raw_get32lfloat
+};
+static const ByteReader BIG_ENDIAN_READER =
+{
+    raw_get16bint, raw_get24bint, raw_get32bint, raw_get64bint, raw_get32bfloat
+};
+
+static void fcontents_set_reversed(FContents *cts, gboolean reversed)
+{
+    cts->reversed = reversed;
+    if (!reversed)
+    {
+       memcpy(&cts->le_reader, &LITTLE_ENDIAN_READER, sizeof (ByteReader));
+       memcpy(&cts->be_reader, &BIG_ENDIAN_READER, sizeof (ByteReader));
+    }
+    else
+    {
+       memcpy(&cts->le_reader, &BIG_ENDIAN_READER, sizeof (ByteReader));
+       memcpy(&cts->be_reader, &LITTLE_ENDIAN_READER, sizeof (ByteReader));
+    }
+}
+
 /* Read the contents of @filename and return a FContents
    struct. Returns NULL in case of error and @error is set
    accordingly */
@@ -316,7 +351,7 @@
     g_return_val_if_fail (fname, NULL);
 
     cts = g_new0 (FContents, 1);
-    cts->reversed = FALSE;
+    fcontents_set_reversed (cts, FALSE);
 
     if (g_file_get_contents (fname, &cts->contents, &cts->length, error))
     {
@@ -544,7 +579,7 @@
 
 /* Returns the 1-byte number stored at position @seek. On error the
  * GError in @cts is set. */
-static guint8 get8int (FContents *cts, glong seek)
+static inline guint8 get8int (FContents *cts, glong seek)
 {
     guint8 n=0;
 
@@ -568,7 +603,7 @@
 
     if (check_seek (cts, seek, 2))
     {
-       g_return_val_if_fail (cts->contents, 0);
+/*     g_return_val_if_fail (cts->contents, 0);*/
        memcpy (&n, &cts->contents[seek], 2);
        n = GUINT16_FROM_LE (n);
     }
@@ -583,7 +618,7 @@
 
     if (check_seek (cts, seek, 3))
     {
-       g_return_val_if_fail (cts->contents, 0);
+/*     g_return_val_if_fail (cts->contents, 0);*/
        n = ((guint32)get8int (cts, seek+0)) +
            (((guint32)get8int (cts, seek+1)) >> 8) +
            (((guint32)get8int (cts, seek+2)) >> 16);
@@ -599,7 +634,7 @@
 
     if (check_seek (cts, seek, 4))
     {
-       g_return_val_if_fail (cts->contents, 0);
+/*     g_return_val_if_fail (cts->contents, 0);*/
        memcpy (&n, &cts->contents[seek], 4);
        n = GUINT32_FROM_LE (n);
     }
@@ -631,7 +666,7 @@
 
     if (check_seek (cts, seek, 8))
     {
-       g_return_val_if_fail (cts->contents, 0);
+/*     g_return_val_if_fail (cts->contents, 0);*/
        memcpy (&n, &cts->contents[seek], 8);
        n = GUINT64_FROM_LE (n);
     }
@@ -651,7 +686,7 @@
 
     if (check_seek (cts, seek, 2))
     {
-       g_return_val_if_fail (cts->contents, 0);
+/*     g_return_val_if_fail (cts->contents, 0);*/
        memcpy (&n, &cts->contents[seek], 2);
        n = GUINT16_FROM_BE (n);
     }
@@ -666,7 +701,7 @@
 
     if (check_seek (cts, seek, 3))
     {
-       g_return_val_if_fail (cts->contents, 0);
+/*     g_return_val_if_fail (cts->contents, 0);*/
        n = ((guint32)get8int (cts, seek+2)) +
            (((guint32)get8int (cts, seek+1)) >> 8) +
            (((guint32)get8int (cts, seek+0)) >> 16);
@@ -682,7 +717,7 @@
 
     if (check_seek (cts, seek, 4))
     {
-       g_return_val_if_fail (cts->contents, 0);
+/*     g_return_val_if_fail (cts->contents, 0);*/
        memcpy (&n, &cts->contents[seek], 4);
        n = GUINT32_FROM_BE (n);
     }
@@ -713,7 +748,7 @@
 
     if (check_seek (cts, seek, 8))
     {
-       g_return_val_if_fail (cts->contents, 0);
+/*     g_return_val_if_fail (cts->contents, 0);*/
        memcpy (&n, &cts->contents[seek], 8);
        n = GUINT64_FROM_BE (n);
     }
@@ -722,107 +757,75 @@
 
 
 /* ------------------------------------------------------------
-   Reversed Endian Sensitive (little endian)
+   Little Endian
    ------------------------------------------------------------ */
 
-/* The following functions take into consideration the state of
- * cts->reversed and call either raw_getnnlint or raw_getnnbint */
-static guint16 get16lint (FContents *cts, glong seek)
+static inline guint16 get16lint (FContents *cts, glong seek)
 {
-    g_return_val_if_fail (cts, 0);
-    if (!cts->reversed)
-       return raw_get16lint (cts, seek);
-    else
-       return raw_get16bint (cts, seek);
+/*    g_return_val_if_fail (cts, 0);*/
+    return cts->le_reader.get16int (cts, seek);
 }
 
-static guint32 get24lint (FContents *cts, glong seek)
+static inline guint32 get24lint (FContents *cts, glong seek)
 {
-    g_return_val_if_fail (cts, 0);
-    if (!cts->reversed)
-       return raw_get24lint (cts, seek);
-    else
-       return raw_get24bint (cts, seek);
+/*    g_return_val_if_fail (cts, 0);*/
+    return cts->le_reader.get24int (cts, seek);
 }
 #if 0
-static guint32 get24bint (FContents *cts, glong seek)
+static inline guint32 get24bint (FContents *cts, glong seek)
 {
-    g_return_val_if_fail (cts, 0);
-    if (!cts->reversed)
-       return raw_get24bint (cts, seek);
-    else
-       return raw_get24lint (cts, seek);
+/*    g_return_val_if_fail (cts, 0);*/
+    return cts->be_reader.get24int (cts, seek);
 }
 #endif
-static guint32 get32lint (FContents *cts, glong seek)
+static inline guint32 get32lint (FContents *cts, glong seek)
 {
-    g_return_val_if_fail (cts, 0);
-    if (!cts->reversed)
-       return raw_get32lint (cts, seek);
-    else
-       return raw_get32bint (cts, seek);
+/*    g_return_val_if_fail (cts, 0);*/
+    return cts->le_reader.get32int (cts, seek);
 }
 
-static float get32lfloat (FContents *cts, glong seek)
+static inline float get32lfloat (FContents *cts, glong seek)
 {
-    g_return_val_if_fail (cts, 0);
-    if (!cts->reversed)
-       return raw_get32lfloat (cts, seek);
-    else
-       return raw_get32bfloat (cts, seek);
+/*    g_return_val_if_fail (cts, 0);*/
+    return cts->le_reader.get32float (cts, seek);
 }
 
-static guint64 get64lint (FContents *cts, glong seek)
+static inline guint64 get64lint (FContents *cts, glong seek)
 {
-    g_return_val_if_fail (cts, 0);
-    if (!cts->reversed)
-       return raw_get64lint (cts, seek);
-    else
-       return raw_get64bint (cts, seek);
+/*    g_return_val_if_fail (cts, 0);*/
+    return cts->le_reader.get64int (cts, seek);
 }
 
 
 
 /* ------------------------------------------------------------
-   Reversed Endian Sensitive (big endian)
+   Big Endian
    ------------------------------------------------------------ */
 
-static guint16 get16bint (FContents *cts, glong seek)
+static inline guint16 get16bint (FContents *cts, glong seek)
 {
-    g_return_val_if_fail (cts, 0);
-    if (!cts->reversed)
-       return raw_get16bint (cts, seek);
-    else
-       return raw_get16lint (cts, seek);
+/*    g_return_val_if_fail (cts, 0);*/
+    return cts->be_reader.get16int (cts, seek);
 }
 
-static guint32 get32bint (FContents *cts, glong seek)
+static inline guint32 get32bint (FContents *cts, glong seek)
 {
-    g_return_val_if_fail (cts, 0);
-    if (!cts->reversed)
-       return raw_get32bint (cts, seek);
-    else
-       return raw_get32lint (cts, seek);
+/*    g_return_val_if_fail (cts, 0);*/
+    return cts->be_reader.get32int (cts, seek);
 }
 
 #if 0
-static float get32bfloat (FContents *cts, glong seek)
+static inline float get32bfloat (FContents *cts, glong seek)
 {
-    g_return_val_if_fail (cts, 0);
-    if (!cts->reversed)
-       return raw_get32bfloat (cts, seek);
-    else
-       return raw_get32lfloat (cts, seek);
+/*    g_return_val_if_fail (cts, 0);*/
+    return cts->be_reader.get32float (cts, seek);
 }
 #endif
 
-static guint64 get64bint (FContents *cts, glong seek)
+static inline guint64 get64bint (FContents *cts, glong seek)
 {
-    g_return_val_if_fail (cts, 0);
-    if (!cts->reversed)
-       return raw_get64bint (cts, seek);
-    else
-       return raw_get64lint (cts, seek);
+/*    g_return_val_if_fail (cts, 0);*/
+    return cts->be_reader.get64int (cts, seek);
 }
 
 
@@ -908,7 +911,7 @@
            g_propagate_error (&fimp->error, cts->error);
            return FALSE;
        }
-       cts->reversed = TRUE;
+       fcontents_set_reversed (cts, TRUE);
        if (!check_header_seek (cts, "mhdp", 0))
        {
            if (cts->error)
@@ -1804,7 +1807,7 @@
 
     if (!check_header_seek (cts, "mhbd", 0))
     {
-       cts->reversed = TRUE;
+       fcontents_set_reversed (cts, TRUE);
        if (cts->error) return 0;
        if (!check_header_seek (cts, "mhbd", 0))
        {
@@ -2549,7 +2552,7 @@
            g_propagate_error (&fimp->error, cts->error);
            return FALSE;
        }
-       cts->reversed = TRUE;
+       fcontents_set_reversed (cts, TRUE);
        if (!check_header_seek (cts, "mhpo", 0))
        {
            /* cts->error can't be set as already checked above */

Modified: libgpod/trunk/src/itdb_private.h
===================================================================
--- libgpod/trunk/src/itdb_private.h    2009-03-01 10:24:33 UTC (rev 2268)
+++ libgpod/trunk/src/itdb_private.h    2009-03-14 11:31:41 UTC (rev 2269)
@@ -51,19 +51,31 @@
 };
 
 
-/* keeps the contents of one disk file (read) */
+struct FContents_;
+
 typedef struct
 {
+    guint16 (* get16int) (struct FContents_ *cts, glong seek);
+    guint32 (* get24int) (struct FContents_ *cts, glong seek);
+    guint32 (* get32int) (struct FContents_ *cts, glong seek);
+    guint64 (* get64int) (struct FContents_ *cts, glong seek);
+    float (* get32float) (struct FContents_ *cts, glong seek);
+} ByteReader;
+
+/* keeps the contents of one disk file (read) */
+typedef struct FContents_
+{
     gchar *filename;
     gchar *contents;
     /* indicate that endian order is reversed as in the case of the
        iTunesDBs for mobile phones */
     gboolean reversed;
+    ByteReader le_reader;
+    ByteReader be_reader;
     gsize length;
     GError *error;
 } FContents;
 
-
 /* struct used to hold all necessary information when importing a
    Itdb_iTunesDB */
 typedef struct


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
Apps built with the Adobe(R) Flex(R) framework and Flex Builder(TM) are
powering Web 2.0 with engaging, cross-platform capabilities. Quickly and
easily build your RIAs with Flex Builder, the Eclipse(TM)based development
software that enables intelligent coding and step-through debugging.
Download the free 60 day trial. http://p.sf.net/sfu/www-adobe-com
_______________________________________________
gtkpod-cvs2 mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gtkpod-cvs2

Reply via email to