Permalink
Browse files

Start porting core to Boost/ZLib for compression

  • Loading branch information...
1 parent aac94ce commit 7187249b9034c01570849feed35e1cc6e664702b @Gnurou committed Aug 12, 2012
View
@@ -78,7 +78,7 @@ void ASyncQuery::process()
}
else {
_active = false;
- emit error(_query.lastError().message());
+ emit error(QString(_query.lastError().message()));
goto process_end;
}
}
@@ -93,13 +93,13 @@ void ASyncQuery::process()
QVariant value;
switch (_query.valueType(i)) {
case SQLite::Integer:
- value = _query.valueInt64(i);
+ value = (qint64) _query.valueInt64(i);
break;
case SQLite::Float:
value = _query.valueDouble(i);
break;
case SQLite::String:
- value = _query.valueString(i);
+ value = QString::fromUtf8(_query.valueString(i).c_str());
break;
case SQLite::Blob:
value = _query.valueBlob(i);
@@ -175,7 +175,7 @@ ThreadedDatabaseConnection::~ThreadedDatabaseConnection()
bool ThreadedDatabaseConnection::connect(const QString &dbFile)
{
if (!_connection.connect(dbFile)) {
- qWarning("Cannot open database: %s", _connection.lastError().message().toLatin1().data());
+ qWarning("Cannot open database: %s", _connection.lastError().message());
return false;
}
return true;
@@ -184,7 +184,7 @@ bool ThreadedDatabaseConnection::connect(const QString &dbFile)
bool ThreadedDatabaseConnection::attach(const QString &dbFile, const QString &alias)
{
if (!_connection.attach(dbFile, alias)) {
- qWarning("Failed to attach dictionary file %s: %s", dbFile.toLatin1().data(), _connection.lastError().message().toLatin1().data());
+ qWarning("Failed to attach dictionary file %s: %s", dbFile.toLatin1().data(), _connection.lastError().message());
return false;
}
return true;
@@ -193,7 +193,7 @@ bool ThreadedDatabaseConnection::attach(const QString &dbFile, const QString &al
bool ThreadedDatabaseConnection::detach(const QString &alias)
{
if (!_connection.detach(alias)) {
- qWarning("Failed to detach database %s: %s", alias.toLatin1().data(), _connection.lastError().message().toLatin1().data());
+ qWarning("Failed to detach database %s: %s", alias.toLatin1().data(), _connection.lastError().message());
return false;
}
return true;
@@ -263,12 +263,12 @@ void DatabaseThread::run()
_connection = new ThreadedDatabaseConnection();
// Connect to the main database
- connection()->connect(Database::instance()->userDBFile());
+ connection()->connect(Database::instance()->userDBFile().asQString());
// Attach all databases
- const QMap<QString, QString> &dbsToAttach(Database::attachedDBs());
- foreach (const QString &alias, dbsToAttach.keys()) {
- connection()->attach(dbsToAttach[alias], alias);
+ const QMap<TString, TString> &dbsToAttach(Database::attachedDBs());
+ foreach (const TString &alias, dbsToAttach.keys()) {
+ connection()->attach(dbsToAttach[alias].asQString(), alias.asQString());
}
// Add to instances list
View
@@ -0,0 +1,29 @@
+/*
+ * Copyright (C) 2012 Alexandre Courbot
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <exception>
+#include <iostream>
+#include <cstdlib>
+
+namespace boost
+{
+void throw_exception( std::exception const & e )
+{
+ std::cerr << "Boost threw an exception! This sucks!" << std::endl;
+ exit(1);
+}
+}
View
@@ -5,6 +5,8 @@
include(${QT_USE_FILE})
set(tagainijisho_core_SRCS
+BoostException.cc
+TString.cc
TextTools.cc
Paths.cc
Lang.cc
View
@@ -0,0 +1,23 @@
+/*
+ * Copyright (C) 2012 Alexandre Courbot
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "core/Compress.h"
+
+
+namespace Tagaini
+{
+}
View
@@ -0,0 +1,137 @@
+/*
+ * Copyright (C) 2012 Alexandre Courbot
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __CORE_COMPRESS_H
+#define __CORE_COMPRESS_H
+
+#include <stdint.h>
+#include <zlib.h>
+
+namespace Tagaini
+{
+
+template <class T>
+int Compress(const T &src, T &dst);
+
+template <class T>
+int Decompress(const T &src, T &dst);
+
+template <class T>
+int Compress(const T &src, T &dst)
+{
+ static const size_t BUFSIZE = 1024;
+ uint8_t temp_buffer[BUFSIZE];
+
+ z_stream strm;
+ strm.zalloc = 0;
+ strm.zfree = 0;
+ strm.next_in = (Bytef *)src.data();
+ strm.avail_in = src.size();
+ strm.next_out = temp_buffer;
+ strm.avail_out = BUFSIZE;
+
+ deflateInit(&strm, Z_BEST_COMPRESSION);
+
+ while (strm.avail_in != 0)
+ {
+ int err = deflate(&strm, Z_NO_FLUSH);
+ if (err != Z_OK)
+ return err;
+ if (strm.avail_out == 0)
+ {
+ dst.insert(dst.end(), temp_buffer, temp_buffer + BUFSIZE);
+ strm.next_out = temp_buffer;
+ strm.avail_out = BUFSIZE;
+ }
+ }
+
+ int err = Z_OK;
+ while (err == Z_OK)
+ {
+ if (strm.avail_out == 0)
+ {
+ dst.insert(dst.end(), temp_buffer, temp_buffer + BUFSIZE);
+ strm.next_out = temp_buffer;
+ strm.avail_out = BUFSIZE;
+ }
+ err = deflate(&strm, Z_FINISH);
+ }
+
+ if (!err == Z_STREAM_END)
+ return err;
+ dst.insert(dst.end(), temp_buffer, temp_buffer + BUFSIZE - strm.avail_out);
+ deflateEnd(&strm);
+
+ return 0;
+}
+
+template <class T>
+int Decompress(const T &src, T &dst)
+{
+ static const size_t BUFSIZE = 1024;
+ uint8_t temp_buffer[BUFSIZE];
+
+ z_stream strm;
+ strm.zalloc = 0;
+ strm.zfree = 0;
+ strm.next_in = (Bytef *)src.data();
+ strm.avail_in = src.size();
+ strm.next_out = temp_buffer;
+ strm.avail_out = BUFSIZE;
+
+ inflateInit(&strm);
+
+ while (strm.avail_in != 0)
+ {
+ int err = inflate(&strm, Z_NO_FLUSH);
+ if (err != Z_OK && err != Z_STREAM_END)
+ return err;
+
+ if (strm.avail_out == 0 || err == Z_STREAM_END)
+ {
+ dst.insert(dst.end(), temp_buffer, temp_buffer + BUFSIZE - strm.avail_out);
+ strm.next_out = temp_buffer;
+ strm.avail_out = BUFSIZE;
+ }
+ if (err == Z_STREAM_END) {
+ return 0;
+ }
+ }
+
+ int err = Z_OK;
+ while (err == Z_OK)
+ {
+ if (strm.avail_out == 0)
+ {
+ dst.insert(dst.end(), temp_buffer, temp_buffer + BUFSIZE);
+ strm.next_out = temp_buffer;
+ strm.avail_out = BUFSIZE;
+ }
+ err = inflate(&strm, Z_FINISH);
+ }
+
+ if (!err == Z_STREAM_END)
+ return err;
+
+ dst.insert(dst.end(), temp_buffer, temp_buffer + BUFSIZE - strm.avail_out);
+ inflateEnd(&strm);
+
+ return 0;
+}
+}
+
+#endif
View
@@ -23,6 +23,7 @@
#include <QStringList>
+#include <sstream>
/**
* Represents how entries lists are actually recorded inside the DB.
*/
@@ -73,7 +74,7 @@ struct DBListInfo
template <class T> class DBList
{
private:
- const QString _tableName;
+ const TString _tableName;
SQLite::Connection *_connection;
SQLite::Query getEntryQuery;
@@ -87,7 +88,7 @@ template <class T> class DBList
public:
DBList(const QString &tableName, SQLite::Connection *connection = 0);
- const QString &tableName() const { return _tableName; }
+ const TString &tableName() const { return _tableName; }
/**
* Prepare all the cached statements to be used with the given connection.
@@ -123,8 +124,8 @@ template <class T> DBList<T>::DBList(const QString &tableName, SQLite::Connectio
template <class T> bool DBList<T>::createTables(SQLite::Connection *connection)
{
- if (!connection->exec(QString("CREATE TABLE %1(rowid INTEGER PRIMARY KEY, leftSize INTEGER, red TINYINT, parent INTEGER, left INTEGER, right INTEGER, %2)").arg(_tableName).arg(DBListEntry<T>::tableDataMembers()))) return false;
- if (!connection->exec(QString("CREATE TABLE %1Roots(listId INTEGER PRIMARY KEY, rootId INTEGER, label TEXT)").arg(DBList<T>::tableName()))) return false;
+ if (!connection->exec(QString("CREATE TABLE %1(rowid INTEGER PRIMARY KEY, leftSize INTEGER, red TINYINT, parent INTEGER, left INTEGER, right INTEGER, %2)").arg(_tableName.asQString()).arg(DBListEntry<T>::tableDataMembers()))) return false;
+ if (!connection->exec(QString("CREATE TABLE %1Roots(listId INTEGER PRIMARY KEY, rootId INTEGER, label TEXT)").arg(DBList<T>::tableName().asQString()))) return false;
// Entry for root list (rowid = 0)
//if (!connection->exec(QString("insert into %1Root values(0, 0, \"\")").arg(DBList<T>::tableName()))) return false;
return true;
@@ -145,17 +146,31 @@ template <class T> bool DBList<T>::prepareForConnection(SQLite::Connection *conn
int nbDataMembers = DBListEntry<T>::tableDataMembers().count(',') + 1;
QStringList dataHoldersList;
while (nbDataMembers-- > 0) dataHoldersList << "?";
- QString dataHolders(dataHoldersList.join(", "));
+ TString dataHolders(dataHoldersList.join(", "));
if (connection) {
- if (!getEntryQuery.prepare(QString("select * from %1 where rowid = ?").arg(_tableName))) return false;
- if (!insertEntryQuery.prepare(QString("insert or replace into %1 values(?, ?, ?, ?, ?, ?, %2)").arg(_tableName).arg(dataHolders))) return false;
- if (!removeEntryQuery.prepare(QString("delete from %1 where rowid == ?").arg(_tableName))) return false;
-
- if (!newListQuery.prepare(QString("insert into %1Roots values(NULL, 0, \"\")").arg(_tableName))) return false;
- if (!getListQuery.prepare(QString("select * from %1Roots where listId = ?").arg(_tableName))) return false;
- if (!insertListQuery.prepare(QString("insert or replace into %1Roots values(?, ?, ?)").arg(DBList<T>::tableName()))) return false;
- if (!removeListQuery.prepare(QString("delete from %1Roots where listId = ?").arg(DBList<T>::tableName()))) return false;
+ std::ostringstream os;
+ os << "select * from " << _tableName << " where rowid = ?";
+ if (!getEntryQuery.prepare(os.str())) return false;
+ os.clear();
+ os << "insert or replace into " << _tableName << " values(?, ?, ?, ?, ?, ?, "<< dataHolders << ")";
+ if (!insertEntryQuery.prepare(os.str())) return false;
+ os.clear();
+ os << "delete from " << _tableName << " where rowid == ?";
+ if (!removeEntryQuery.prepare(os.str())) return false;
+ os.clear();
+
+ os << "insert into " << _tableName << "Roots values(NULL, 0, \"\")";
+ if (!newListQuery.prepare(os.str())) return false;
+ os.clear();
+ os << "select * from " << _tableName << "Roots where listId = ?";
+ if (!getListQuery.prepare(os.str())) return false;
+ os.clear();
+ os << "insert or replace into " << DBList<T>::tableName() << "Roots values(?, ?, ?)";
+ if (!insertListQuery.prepare(os.str())) return false;
+ os.clear();
+ os << "delete from " << DBList<T>::tableName() << "Roots where listId = ?";
+ if (!removeListQuery.prepare(os.str())) return false;
}
return true;
}
@@ -232,7 +247,7 @@ template <class T> DBListInfo DBList<T>::getList(quint32 listId)
}
ret.listId = getListQuery.valueUInt(0);
ret.rootId = getListQuery.valueUInt(1);
- ret.label = getListQuery.valueString(2);
+ ret.label = QString::fromUtf8(getListQuery.valueString(2).c_str());
getListQuery.reset();
return ret;
Oops, something went wrong.

0 comments on commit 7187249

Please sign in to comment.