Moved code around to make possible to compile the C part alone.

The C code should be compilable without the need to compile the Gui part,
too. This is expecially good for unit testing as we can test all the
algorithms without a window appearing out of nowhere.

Signed-off-by: Tomaz Canabrava <tomaz.canabrava@intel.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Tomaz Canabrava 2014-04-17 12:21:39 -03:00 committed by Dirk Hohndel
parent 9598462830
commit 55d010bad8
6 changed files with 169 additions and 144 deletions

View file

@ -102,6 +102,8 @@ SET(SUBSURFACE_CORE_LIB_SRCS
linux.c
#gettextfrommoc should be added because we are using it on the c-code.
gettextfromc.cpp
#dirk ported some core functionality to c++.
qthelper.cpp
)
#the interface, in C++
@ -192,7 +194,7 @@ ADD_DEPENDENCIES(subsurface_corelib version)
ENABLE_TESTING()
ADD_EXECUTABLE( TestUnitConversion tests/testunitconversion.cpp )
TARGET_LINK_LIBRARIES( TestUnitConversion ${QT_LIBRARIES})
TARGET_LINK_LIBRARIES( TestUnitConversion ${QT_LIBRARIES} ${SUBSURFACE_LINK_LIBRARIES} -lzip -ldivecomputer subsurface_corelib)
ADD_TEST( NAME TestUnitConversion COMMAND TestUnitConversion)
ADD_EXECUTABLE( TestProfile tests/testprofile.cpp )

View file

@ -9,8 +9,6 @@
#include <sys/time.h>
#include <ctype.h>
#include <libxslt/documents.h>
#include "dive.h"
#include "divelist.h"
#include "display.h"
@ -45,11 +43,6 @@
// this will create a warning when executing lupdate
#define translate(_context, arg) gettextFromC::instance()->tr(arg)
const char *default_dive_computer_vendor;
const char *default_dive_computer_product;
const char *default_dive_computer_device;
DiveComputerList dcList;
static QApplication *application = NULL;
static MainWindow *window = NULL;
@ -197,34 +190,6 @@ const QString get_dc_nickname(const char *model, uint32_t deviceid)
return model;
}
void set_dc_nickname(struct dive *dive)
{
if (!dive)
return;
struct divecomputer *dc = &dive->dc;
while (dc) {
if (dc->model && *dc->model && dc->deviceid &&
!dcList.getExact(dc->model, dc->deviceid)) {
// we don't have this one, yet
const DiveComputerNode *existNode = dcList.get(dc->model);
if (existNode) {
// we already have this model but a different deviceid
QString simpleNick(dc->model);
if (dc->deviceid == 0)
simpleNick.append(" (unknown deviceid)");
else
simpleNick.append(" (").append(QString::number(dc->deviceid, 16)).append(")");
dcList.addDC(dc->model, dc->deviceid, simpleNick);
} else {
dcList.addDC(dc->model, dc->deviceid);
}
}
dc = dc->next;
}
}
QString get_depth_string(int mm, bool showunit, bool showdecimal)
{
if (prefs.units.length == units::METERS) {
@ -358,55 +323,6 @@ double get_screen_dpi()
return mydesk->physicalDpiX();
}
int is_default_dive_computer(const char *vendor, const char *product)
{
return default_dive_computer_vendor && !strcmp(vendor, default_dive_computer_vendor) &&
default_dive_computer_product && !strcmp(product, default_dive_computer_product);
}
int is_default_dive_computer_device(const char *name)
{
return default_dive_computer_device && !strcmp(name, default_dive_computer_device);
}
void set_default_dive_computer(const char *vendor, const char *product)
{
QSettings s;
if (!vendor || !*vendor)
return;
if (!product || !*product)
return;
if (is_default_dive_computer(vendor, product))
return;
if (default_dive_computer_vendor)
free((void *)default_dive_computer_vendor);
if (default_dive_computer_product)
free((void *)default_dive_computer_product);
default_dive_computer_vendor = strdup(vendor);
default_dive_computer_product = strdup(product);
s.beginGroup("DiveComputer");
s.setValue("dive_computer_vendor", vendor);
s.setValue("dive_computer_product", product);
s.endGroup();
}
void set_default_dive_computer_device(const char *name)
{
QSettings s;
if (!name || !*name)
return;
if (is_default_dive_computer_device(name))
return;
if (default_dive_computer_device)
free((void *)default_dive_computer_device);
default_dive_computer_device = strdup(name);
s.beginGroup("DiveComputer");
s.setValue("dive_computer_device", name);
s.endGroup();
}
QString getSubsurfaceDataPath(QString folderToFind)
{
QString execdir;
@ -440,28 +356,6 @@ QString getSubsurfaceDataPath(QString folderToFind)
return QString("");
}
void create_device_node(const char *model, uint32_t deviceid, const char *serial, const char *firmware, const char *nickname)
{
dcList.addDC(model, deviceid, nickname, serial, firmware);
}
bool compareDC(const DiveComputerNode &a, const DiveComputerNode &b)
{
return a.deviceId < b.deviceId;
}
void call_for_each_dc(void *f, void (*callback)(void *, const char *, uint32_t,
const char *, const char *, const char *))
{
QList<DiveComputerNode> values = dcList.dcMap.values();
qSort(values.begin(), values.end(), compareDC);
for (int i = 0; i < values.size(); i++) {
const DiveComputerNode *node = &values.at(i);
callback(f, node->model.toUtf8().data(), node->deviceId, node->nickName.toUtf8().data(),
node->serialNumber.toUtf8().data(), node->firmware.toUtf8().data());
}
}
int gettimezoneoffset()
{
QDateTime dt1 = QDateTime::currentDateTime();
@ -530,36 +424,3 @@ QString get_trip_date_string(timestamp_t when, int nr)
.arg(monthname(tm.tm_mon))
.arg(tm.tm_year + 1900);
}
static xmlDocPtr get_stylesheet_doc(const xmlChar *uri, xmlDictPtr, int, void *, xsltLoadType)
{
QFile f(QLatin1String(":/xslt/") + (const char *)uri);
if (!f.open(QIODevice::ReadOnly))
return NULL;
/* Load and parse the data */
QByteArray source = f.readAll();
xmlDocPtr doc = xmlParseMemory(source, source.size());
return doc;
}
xsltStylesheetPtr get_stylesheet(const char *name)
{
// this needs to be done only once, but doesn't hurt to run every time
xsltSetLoaderFunc(get_stylesheet_doc);
// get main document:
xmlDocPtr doc = get_stylesheet_doc((const xmlChar *)name, NULL, 0, NULL, XSLT_LOAD_START);
if (!doc)
return NULL;
// xsltSetGenericErrorFunc(stderr, NULL);
xsltStylesheetPtr xslt = xsltParseStylesheetDoc(doc);
if (!xslt) {
xmlFreeDoc(doc);
return NULL;
}
return xslt;
}

View file

@ -1,12 +1,19 @@
#include "qthelper.h"
#include "qt-ui/mainwindow.h"
#include "qt-gui.h"
#include <QRegExp>
#include <QDir>
#include <QFileDialog>
#include <QDebug>
#define tr(_arg) MainWindow::instance()->information()->tr(_arg)
#include <QDebug>
#include <QSettings>
#include <libxslt/documents.h>
#define tr(_arg) QObject::tr(_arg)
const char *default_dive_computer_vendor;
const char *default_dive_computer_product;
const char *default_dive_computer_device;
DiveComputerList dcList;
DiveComputerList::DiveComputerList()
{
}
@ -265,3 +272,136 @@ int getUniqID(struct dive *d)
ids.insert(id);
return id;
}
extern "C" void create_device_node(const char *model, uint32_t deviceid, const char *serial, const char *firmware, const char *nickname)
{
dcList.addDC(model, deviceid, nickname, serial, firmware);
}
extern "C" bool compareDC(const DiveComputerNode &a, const DiveComputerNode &b)
{
return a.deviceId < b.deviceId;
}
extern "C" void call_for_each_dc(void *f, void (*callback)(void *, const char *, uint32_t,
const char *, const char *, const char *))
{
QList<DiveComputerNode> values = dcList.dcMap.values();
qSort(values.begin(), values.end(), compareDC);
for (int i = 0; i < values.size(); i++) {
const DiveComputerNode *node = &values.at(i);
callback(f, node->model.toUtf8().data(), node->deviceId, node->nickName.toUtf8().data(),
node->serialNumber.toUtf8().data(), node->firmware.toUtf8().data());
}
}
static xmlDocPtr get_stylesheet_doc(const xmlChar *uri, xmlDictPtr, int, void *, xsltLoadType)
{
QFile f(QLatin1String(":/xslt/") + (const char *)uri);
if (!f.open(QIODevice::ReadOnly))
return NULL;
/* Load and parse the data */
QByteArray source = f.readAll();
xmlDocPtr doc = xmlParseMemory(source, source.size());
return doc;
}
extern "C" xsltStylesheetPtr get_stylesheet(const char *name)
{
// this needs to be done only once, but doesn't hurt to run every time
xsltSetLoaderFunc(get_stylesheet_doc);
// get main document:
xmlDocPtr doc = get_stylesheet_doc((const xmlChar *)name, NULL, 0, NULL, XSLT_LOAD_START);
if (!doc)
return NULL;
// xsltSetGenericErrorFunc(stderr, NULL);
xsltStylesheetPtr xslt = xsltParseStylesheetDoc(doc);
if (!xslt) {
xmlFreeDoc(doc);
return NULL;
}
return xslt;
}
extern "C" int is_default_dive_computer(const char *vendor, const char *product)
{
return default_dive_computer_vendor && !strcmp(vendor, default_dive_computer_vendor) &&
default_dive_computer_product && !strcmp(product, default_dive_computer_product);
}
extern "C" int is_default_dive_computer_device(const char *name)
{
return default_dive_computer_device && !strcmp(name, default_dive_computer_device);
}
void set_default_dive_computer(const char *vendor, const char *product)
{
QSettings s;
if (!vendor || !*vendor)
return;
if (!product || !*product)
return;
if (is_default_dive_computer(vendor, product))
return;
if (default_dive_computer_vendor)
free((void *)default_dive_computer_vendor);
if (default_dive_computer_product)
free((void *)default_dive_computer_product);
default_dive_computer_vendor = strdup(vendor);
default_dive_computer_product = strdup(product);
s.beginGroup("DiveComputer");
s.setValue("dive_computer_vendor", vendor);
s.setValue("dive_computer_product", product);
s.endGroup();
}
void set_default_dive_computer_device(const char *name)
{
QSettings s;
if (!name || !*name)
return;
if (is_default_dive_computer_device(name))
return;
if (default_dive_computer_device)
free((void *)default_dive_computer_device);
default_dive_computer_device = strdup(name);
s.beginGroup("DiveComputer");
s.setValue("dive_computer_device", name);
s.endGroup();
}
extern "C" void set_dc_nickname(struct dive *dive)
{
if (!dive)
return;
struct divecomputer *dc = &dive->dc;
while (dc) {
if (dc->model && *dc->model && dc->deviceid &&
!dcList.getExact(dc->model, dc->deviceid)) {
// we don't have this one, yet
const DiveComputerNode *existNode = dcList.get(dc->model);
if (existNode) {
// we already have this model but a different deviceid
QString simpleNick(dc->model);
if (dc->deviceid == 0)
simpleNick.append(" (unknown deviceid)");
else
simpleNick.append(" (").append(QString::number(dc->deviceid, 16)).append(")");
dcList.addDC(dc->model, dc->deviceid, simpleNick);
} else {
dcList.addDC(dc->model, dc->deviceid);
}
}
dc = dc->next;
}
}

9
tests/testprofile.cpp Normal file
View file

@ -0,0 +1,9 @@
#include "testprofile.h"
#include "dive.h"
void TestProfile::testRedCeiling()
{
parse_file("../dives/deep.xml");
}
QTEST_MAIN(TestProfile)

12
tests/testprofile.h Normal file
View file

@ -0,0 +1,12 @@
#ifndef TESTPROFILE_H
#define TESTPROFILE_H
#include <QtTest>
class TestProfile : public QObject{
Q_OBJECT
private slots:
void testRedCeiling();
};
#endif

View file

@ -20,6 +20,7 @@ void TestUnitConversion::testUnitConversions()
QCOMPARE(IS_FP_SAME(bar_to_atm(1.013), 1), true);
QCOMPARE(IS_FP_SAME(mbar_to_atm(1013), 1), true);
QCOMPARE(mbar_to_PSI(1013), (int)15);
get_units();
}
QTEST_MAIN(TestUnitConversion)