Fixed compilation on CentOS 5.6.

ranzbak [2011-05-06 18:20]
Fixed compilation on CentOS 5.6.

git-svn-id: https://old.setv.nl/svn/swarmtv@311 88541080-9ad6-de11-9093-5254007c1f44
Filename
trunk/swarmtv/libswarmtv/CMakeLists.txt
trunk/swarmtv/libswarmtv/config.c
trunk/swarmtv/libswarmtv/database.c
trunk/swarmtv/libswarmtv/database.h
trunk/swarmtv/libswarmtv/runloop.c
trunk/swarmtv/libswarmtv/setup.c
trunk/swarmtv/libswarmtv/simplefilter.c
trunk/swarmtv/libswarmtv/torrentdb.c
trunk/swarmtv/libswarmtv/torrentdownload.c
trunk/swarmtv/shellfront/runloop.c
diff --git a/trunk/swarmtv/libswarmtv/CMakeLists.txt b/trunk/swarmtv/libswarmtv/CMakeLists.txt
index e0a2e5f..a3c38db 100644
--- a/trunk/swarmtv/libswarmtv/CMakeLists.txt
+++ b/trunk/swarmtv/libswarmtv/CMakeLists.txt
@@ -81,4 +81,4 @@ INSTALL(FILES swarmtv.h
 		DESTINATION include
 		PERMISSIONS OWNER_READ GROUP_READ WORLD_READ)

-INSTALL(FILES libswarmtv.pc DESTINATION lib/pkgconfig/ )
+INSTALL(FILES ${CMAKE_SOURCE_DIR}/libswarmtv.pc DESTINATION lib/pkgconfig/ )
diff --git a/trunk/swarmtv/libswarmtv/config.c b/trunk/swarmtv/libswarmtv/config.c
index 9bf807c..27bd912 100644
--- a/trunk/swarmtv/libswarmtv/config.c
+++ b/trunk/swarmtv/libswarmtv/config.c
@@ -141,7 +141,7 @@ int rsstsetconfigitem(rsstor_handle *handle, const char *prop, const char *value
   /*
    * Prepare the Sqlite statement
    */
-  rc = sqlite3_prepare_v2(
+  rc = rsst_sqlite3_prepare(
       db,                 /* Database handle */
       query,            /* SQL statement, UTF-8 encoded */
       strlen(query),    /* Maximum length of SQL query in bytes. */
diff --git a/trunk/swarmtv/libswarmtv/database.c b/trunk/swarmtv/libswarmtv/database.c
index ea10c6a..e49e6dc 100644
--- a/trunk/swarmtv/libswarmtv/database.c
+++ b/trunk/swarmtv/libswarmtv/database.c
@@ -53,6 +53,39 @@
 #define SCRIPT_SYM "--"

 /*
+ * This is a fix for systems that use the old sqlite3 library without
+ * sqlite3_prepare_v2. Centos 5.6 is one of them.
+ */
+int rsst_sqlite3_prepare(
+    sqlite3 *db,            /* Database handle */
+    const char *zSql,       /* SQL statement, UTF-8 encoded */
+    int nByte,              /* Maximum length of zSql in bytes. */
+    sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
+    const char **pzTail     /* OUT: Pointer to unused portion of zSql */
+    )
+{
+  int rc=0;
+#if SQLITE_VERSION_NUMBER >= 3003009
+  rc = sqlite3_prepare_v2(
+      db,                 /* Database handle */
+      zSql,            /* SQL statement, UTF-8 encoded */
+      nByte,    /* Maximum length of zSql in bytes. */
+      ppStmt,             /* OUT: Statement handle */
+      pzTail              /* OUT: Pointer to unused portion of zSql */
+      );
+#else
+  rc = sqlite3_prepare(
+      db,                 /* Database handle */
+      zSql,            /* SQL statement, UTF-8 encoded */
+      nByte,    /* Maximum length of zSql in bytes. */
+      ppStmt,             /* OUT: Statement handle */
+      pzTail              /* OUT: Pointer to unused portion of zSql */
+      );
+#endif
+  return rc;
+}
+
+/*
  * Executing script
  * This function executes a script.
  * Each line should be separated by a '\n' char.
@@ -159,7 +192,7 @@ int rsstdosingletextquery(sqlite3 *db, const unsigned char **text, const char *q
   /*
    * Prepare the Sqlite statement
    */
-  rc = sqlite3_prepare_v2(
+  rc = rsst_sqlite3_prepare(
       db,                 /* Database handle */
       query,            /* SQL statement, UTF-8 encoded */
       strlen(query),    /* Maximum length of zSql in bytes. */
@@ -293,6 +326,8 @@ int rsstexecutequery(sqlite3 *db, const char *query, char *fmt, ...)
   double       	f=0.0;
   int          	count=0;
   int          	changes=0;
+  int           errno=0;
+  const char   *errstring=NULL;

   /*
    * fmt pointer to NULL is do not substitutes
@@ -304,7 +339,7 @@ int rsstexecutequery(sqlite3 *db, const char *query, char *fmt, ...)
   /*
    * Prepare the Sqlite statement
    */
-  rc = sqlite3_prepare_v2(
+  rc = rsst_sqlite3_prepare(
       db,                 /* Database handle */
       query,            /* SQL statement, UTF-8 encoded */
       strlen(query),    /* Maximum length of zSql in bytes. */
@@ -382,8 +417,18 @@ int rsstexecutequery(sqlite3 *db, const char *query, char *fmt, ...)
         retval=ROWS_CONSTRAINT;
         break;
       default:
-        rsstwritelog(LOG_ERROR, "sqlite3_step, %d %s:%d", step_rc, __FILE__, __LINE__);
+        errno = sqlite3_errcode(db);
+        errstring = sqlite3_errmsg(db);
+        /*
+         * Workaround for older sqlite3 libraries, I hope they will upgrade soon to v2 compatible versions of sqlite
+         */
+#ifndef sqlite_prepare_v2
+        rsstwritelog(LOG_DEBUG, "sqlite3_step, %d %s:%d", step_rc, __FILE__, __LINE__);
+        rsstwritelog(LOG_DEBUG, "in statement : \'%s\'", query);
+#else
+        rsstwritelog(LOG_ERROR, "sqlite3_step, %s %d %s:%d", errstring, step_rc, __FILE__, __LINE__);
         rsstwritelog(LOG_ERROR, "in statement : \'%s\'", query);
+#endif
         retval=ROWS_ERROR;
     }
   }
@@ -473,7 +518,7 @@ int rsstexecqueryresultva(sqlite3 *db, sqlite3_stmt **ppstmt, const char *query,
   /*
    * Prepare the Sqlite statement
    */
-  rc = sqlite3_prepare_v2(
+  rc = rsst_sqlite3_prepare(
       db,                 /* Database handle */
       query,            /* SQL statement, UTF-8 encoded */
       strlen(query),    /* Maximum length of zSql in bytes. */
diff --git a/trunk/swarmtv/libswarmtv/database.h b/trunk/swarmtv/libswarmtv/database.h
index 5d8a994..b1a2c02 100644
--- a/trunk/swarmtv/libswarmtv/database.h
+++ b/trunk/swarmtv/libswarmtv/database.h
@@ -24,6 +24,18 @@
 #define RSST_START_ELEMENTS 10

 /*
+ * This is a fix for systems that use the old sqlite3 library without
+ * sqlite3_prepare_v2. Centos 5.6 is one of them.
+ */
+int rsst_sqlite3_prepare(
+    sqlite3 *db,            /* Database handle */
+    const char *zSql,       /* SQL statement, UTF-8 encoded */
+    int nByte,              /* Maximum length of zSql in bytes. */
+    sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
+    const char **pzTail     /* OUT: Pointer to unused portion of zSql */
+    );
+
+/*
  * Do a query, and bind value's to the query in one step
  * This is a simplified version as it only returns 1 value.
  * that value is the first string of the first colomn.
diff --git a/trunk/swarmtv/libswarmtv/runloop.c b/trunk/swarmtv/libswarmtv/runloop.c
index ca669fc..3116dc1 100644
--- a/trunk/swarmtv/libswarmtv/runloop.c
+++ b/trunk/swarmtv/libswarmtv/runloop.c
@@ -34,6 +34,7 @@
 #include "logfile.h"
 #include "torrentdb.h"
 #include "callback.h"
+#include "database.h"

 #ifdef __MINGW32__
 #include <windows.h>
@@ -144,7 +145,7 @@ static void dowork(rsstor_handle *handle){
   /*
    * Prepare the Sqlite statement
    */
-  rc = sqlite3_prepare_v2(
+  rc = rsst_sqlite3_prepare(
       handle->db,                 /* Database handle */
       query,            /* SQL statement, UTF-8 encoded */
       strlen(query),    /* Maximum length of zSql in bytes. */
diff --git a/trunk/swarmtv/libswarmtv/setup.c b/trunk/swarmtv/libswarmtv/setup.c
index d6ba469..4c35300 100644
--- a/trunk/swarmtv/libswarmtv/setup.c
+++ b/trunk/swarmtv/libswarmtv/setup.c
@@ -150,12 +150,15 @@ void rsstfreecallbacks(struct_callbacks *callbacks)
  */
 static void rssfcheckthreadsafe()
 {
+#if SQLITE_VERSION_NUMBER >= 3003009
   int rc=0;
-
   rc = sqlite3_threadsafe();
   if(rc == 0){
     fprintf(stderr, "libSQLite3 was not built with thread support, make sure no instances of the library run at the same time.\n");
   }
+#else
+  #warning SQlite3 library too old can not detect threadsafety
+#endif
 }


diff --git a/trunk/swarmtv/libswarmtv/simplefilter.c b/trunk/swarmtv/libswarmtv/simplefilter.c
index daba41c..ea1fce6 100644
--- a/trunk/swarmtv/libswarmtv/simplefilter.c
+++ b/trunk/swarmtv/libswarmtv/simplefilter.c
@@ -467,7 +467,7 @@ int rsstdownloadsimple(rsstor_handle *handle, SIM simulate)
 	/*
 	 * Prepare the Sqlite statement
 	 */
-	rc = sqlite3_prepare_v2(
+	rc = rsst_sqlite3_prepare(
 			handle->db,         /* Database handle */
       query,            	/* SQL statement, UTF-8 encoded */
       strlen(query),    	/* Maximum length of zSql in bytes. */
diff --git a/trunk/swarmtv/libswarmtv/torrentdb.c b/trunk/swarmtv/libswarmtv/torrentdb.c
index 5e5b28b..e0bd897 100644
--- a/trunk/swarmtv/libswarmtv/torrentdb.c
+++ b/trunk/swarmtv/libswarmtv/torrentdb.c
@@ -95,6 +95,8 @@ int rsstaddnewtorrent(rsstor_handle *handle, newtorrents_struct *newtor)
 {
   int           rc=0;
 	sqlite3      *db=NULL;
+  const char   *error=NULL;
+  int           errno=0;

 	/*
 	 * Get db pointer;
@@ -131,6 +133,7 @@ int rsstaddnewtorrent(rsstor_handle *handle, newtorrents_struct *newtor)
   rc = rsstexecutequery(db, query, "ssdssddddfs",
 			newtor->title, newtor->link, newtor->pubdate, newtor->category, newtor->source, newtor->season,
 			newtor->episode, newtor->seeds, newtor->peers, (double)(newtor->size), newtor->metatype);
+
   switch(rc) {
     case ROWS_EMPTY:
     case ROWS_CHANGED:
@@ -140,8 +143,22 @@ int rsstaddnewtorrent(rsstor_handle *handle, newtorrents_struct *newtor)
       rsstwritelog(LOG_DEBUG, "Torrent already in DB");
       break;
     default:
-      rsstwritelog(LOG_ERROR, "SQL statement failed %d! %s:%d", rc, __FILE__, __LINE__);
+      errno = sqlite3_errcode(db);
+      error = sqlite3_errmsg(db);
+/*
+ * Workaround for older sqlite3 libraries.
+ */
+#if SQLITE_VERSION_NUMBER >= 3003009
+      rsstwritelog(LOG_ERROR, "SQL statement failed '%d'! %s %s:%d", errno, error, __FILE__, __LINE__);
       exit(1);
+#else
+      if(errno == SQLITE_CONSTRAINT) {
+        rsstwritelog(LOG_DEBUG, "Torrent already in DB");
+      } else {
+        rsstwritelog(LOG_ERROR, "SQL statement failed '%d'! %s %s:%d", errno, error, __FILE__, __LINE__);
+        exit(1);
+      }
+#endif
   }

   /*
@@ -161,9 +178,9 @@ void rsstadddownloaded(rsstor_handle *handle, downloaded_struct *downed, SIM  si
 {
   int        rc=0;
   time_t     now=0;
-	sqlite3 	*db=NULL;
+  sqlite3 	*db=NULL;

-	db = handle->db;
+  db = handle->db;

   char *query = "INSERT INTO downloaded (title, link, pubdate, category, season, episode, metatype, date) "
                 "VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7,  datetime(?8, 'unixepoch', 'localtime'))";
diff --git a/trunk/swarmtv/libswarmtv/torrentdownload.c b/trunk/swarmtv/libswarmtv/torrentdownload.c
index 0e8b8d5..c44642a 100644
--- a/trunk/swarmtv/libswarmtv/torrentdownload.c
+++ b/trunk/swarmtv/libswarmtv/torrentdownload.c
@@ -89,7 +89,7 @@ int rsstdownloadtorrents(rsstor_handle *handle)
   /*
    * Prepare the sqlite statement
    */
-  rc = sqlite3_prepare_v2(
+  rc = rsst_sqlite3_prepare(
       handle->db,        /* Database handle */
       query,            /* SQL statement, UTF-8 encoded */
       strlen(query),    /* Maximum length of zSql in bytes. */
@@ -160,7 +160,7 @@ static int testdouble(sqlite3 *db, char *nodouble, char *titleregexp, downloaded
   /*
    * prepare query
    */
-  rc = sqlite3_prepare_v2(
+  rc = rsst_sqlite3_prepare(
       db,                 /* Database handle */
       nodouble,            /* SQL statement, UTF-8 encoded */
       strlen(nodouble),    /* Maximum length of zSql in bytes. */
@@ -444,7 +444,7 @@ void rsstapplyfilter(rsstor_handle *handle, char *name, FILTER_TYPE type, char*
 	/*
 	 * Prepare the sqlite statement
 	 */
-	rc = sqlite3_prepare_v2(
+	rc = rsst_sqlite3_prepare(
 			handle->db,           /* Database handle */
 			filter,            		/* SQL statement, UTF-8 encoded */
 			strlen(filter),    		/* Maximum length of zSql in bytes. */
diff --git a/trunk/swarmtv/shellfront/runloop.c b/trunk/swarmtv/shellfront/runloop.c
index 57dcf9c..98ccd43 100644
--- a/trunk/swarmtv/shellfront/runloop.c
+++ b/trunk/swarmtv/shellfront/runloop.c
@@ -195,7 +195,12 @@ static gboolean rssfglibcycle(rsstor_handle *handle)
      * Reschedule for a new cycle
      */
     rsstwritelog(LOG_NORMAL, "Sleeping %ds, cycle took %ds.", timeleft, runtime);
+#ifdef g_timeout_add_seconds
     g_timeout_add_seconds(timeleft, (GSourceFunc)rssfglibcycle, handle);
+#else
+    g_timeout_add(timeleft * 1000, (GSourceFunc)rssfglibcycle, handle);
+#endif
+
   }
   return FALSE;
 }
@@ -256,7 +261,12 @@ int rssfrunloop(rsstor_handle *handle, LOOPMODE onetime)
     /*
      * Call the runcycle
      */
+
+#if (GLIB_MAJOR_VERSION >= 2 && GLIB_MINOR_VERSION >= 14) || GLIB_MAJOR_VERSION > 2
     g_timeout_add_seconds(1, (GSourceFunc)rssfglibcycle, handle);
+#else
+    g_timeout_add(1*1000, (GSourceFunc)rssfglibcycle, handle);
+#endif

     /*
      * Run main loop